diff --git a/crates/alknet-typedef/src/error.rs b/crates/alknet-typedef/src/error.rs index bfddeeb..41501cf 100644 --- a/crates/alknet-typedef/src/error.rs +++ b/crates/alknet-typedef/src/error.rs @@ -4,4 +4,48 @@ //! conditions across the engine's three phases (schema parsing, offset //! computation, read/write) plus validation. -// TODO: implement \ No newline at end of file +use std::fmt; + +/// Errors produced by the typedef engine across all phases. +#[derive(Debug)] +pub enum TypedefError { + /// Schema parsing errors — invalid JSON, missing required keywords, + /// unknown `TypeDef:*` kinds, malformed annotations. + Schema(String), + + /// Offset computation errors — field not found, type not supported + /// for offset computation, recursive depth exceeded. + Offset { + field_path: String, + reason: String, + }, + + /// Read/write errors — buffer too short, invalid UTF-8, value out + /// of range for the target type. + Access { + field_path: String, + reason: String, + }, + + /// Validation errors — delegated to the `jsonschema` crate. + /// The `'static` lifetime is correct: the validator owns its schema + /// reference and lives for the lifetime of the `TypedefEngine`. + Validation(jsonschema::ValidationError<'static>), +} + +impl fmt::Display for TypedefError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + TypedefError::Schema(msg) => write!(f, "schema error: {msg}"), + TypedefError::Offset { field_path, reason } => { + write!(f, "offset error at {field_path}: {reason}") + } + TypedefError::Access { field_path, reason } => { + write!(f, "access error at {field_path}: {reason}") + } + TypedefError::Validation(inner) => write!(f, "validation error: {inner}"), + } + } +} + +impl std::error::Error for TypedefError {} \ No newline at end of file diff --git a/crates/alknet-typedef/src/lib.rs b/crates/alknet-typedef/src/lib.rs index 333f5df..f5afdef 100644 --- a/crates/alknet-typedef/src/lib.rs +++ b/crates/alknet-typedef/src/lib.rs @@ -29,4 +29,6 @@ pub mod offset_map; pub mod schema; pub mod sequential_reader; pub mod tunion; -pub mod validation; \ No newline at end of file +pub mod validation; + +pub use error::TypedefError; \ No newline at end of file diff --git a/tasks/typedef/error-type.md b/tasks/typedef/error-type.md index 9dc0495..dad5eab 100644 --- a/tasks/typedef/error-type.md +++ b/tasks/typedef/error-type.md @@ -1,7 +1,7 @@ --- id: typedef/error-type name: Implement TypedefError enum with Schema, Offset, Access, and Validation variants -status: pending +status: completed depends_on: [typedef/crate-init] scope: narrow risk: low @@ -107,4 +107,4 @@ Since the `jsonschema` crate is a dependency, this is straightforward. ## Summary -> To be filled on completion +Implemented the `TypedefError` enum in `crates/alknet-typedef/src/error.rs` with four variants (`Schema`, `Offset`, `Access`, `Validation`) per ADR-098, plus `Display` (with field-path-aware messages for `Offset` and `Access`) and `std::error::Error` impls and a derived `Debug`. Re-exported `TypedefError` from `crates/alknet-typedef/src/lib.rs` so it is accessible as `alknet_typedef::TypedefError`. `cargo check`, `cargo clippy -D warnings`, and `cargo build --workspace` all pass with no warnings.