Skip to main content

DeserializeErrorKind

Enum DeserializeErrorKind 

Source
#[non_exhaustive]
pub enum DeserializeErrorKind {
Show 23 variants UnexpectedChar { ch: char, expected: &'static str, }, UnexpectedEof { expected: &'static str, }, InvalidUtf8 { context: [u8; 16], context_len: u8, }, UnexpectedToken { got: Cow<'static, str>, expected: &'static str, }, TypeMismatch { expected: &'static Shape, got: Cow<'static, str>, }, ShapeMismatch { expected: &'static Shape, got: &'static Shape, }, UnknownField { field: Cow<'static, str>, suggestion: Option<&'static str>, }, UnknownVariant { variant: Cow<'static, str>, enum_shape: &'static Shape, }, NoMatchingVariant { enum_shape: &'static Shape, input_kind: &'static str, }, MissingField { field: &'static str, container_shape: &'static Shape, }, DuplicateField { field: Cow<'static, str>, first_span: Option<Span>, }, NumberOutOfRange { value: Cow<'static, str>, target_type: &'static str, }, InvalidValue { message: Cow<'static, str>, }, CannotBorrow { reason: Cow<'static, str>, }, Reflect { kind: ReflectErrorKind, context: &'static str, }, Unsupported { message: Cow<'static, str>, }, Io { message: Cow<'static, str>, }, Solver { message: Cow<'static, str>, }, Validation { field: &'static str, message: Cow<'static, str>, }, Bug { error: Cow<'static, str>, context: &'static str, }, Alloc { shape: &'static Shape, operation: &'static str, }, Materialize { expected: &'static Shape, actual: &'static Shape, }, RawCaptureNotSupported { shape: &'static Shape, },
}
Expand description

Specific kinds of deserialization errors.

Uses Cow<'static, str> to avoid allocations when possible while still supporting owned strings when needed (e.g., field names from input).

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

UnexpectedChar

Unexpected character encountered by the parser.

Level: Parser (e.g., JsonParser)

This happens when the parser encounters a character that doesn’t fit the format’s grammar at the current position.

{"name": @invalid}
         ^
         unexpected character '@', expected value

Fields

§ch: char

The character that was found.

§expected: &'static str

What was expected instead (e.g., “value”, “digit”, “string”).

§

UnexpectedEof

Unexpected end of input.

Level: Parser (e.g., JsonParser)

The input ended before a complete value could be parsed.

{"name": "Alice
               ^
               unexpected EOF, expected closing quote

Fields

§expected: &'static str

What was expected before EOF.

§

InvalidUtf8

Invalid UTF-8 sequence in input.

Level: Parser (e.g., JsonParser)

The input contains bytes that don’t form valid UTF-8.

{"name": "hello\xff world"}
                ^^^^
                invalid UTF-8 sequence

Fields

§context: [u8; 16]

Up to 16 bytes of context around the invalid sequence.

§context_len: u8

Number of valid bytes in context (0-16).

§

UnexpectedToken

Unexpected token from parser.

Level: Deserializer (FormatDeserializer)

The parser produced a valid token, but it’s not what the deserializer expected at this point given the target Rust type.

// Deserializing into Vec<i32>
{"not": "an array"}
^
unexpected token: got object, expected array

Not to be confused with:

  • UnexpectedChar: parser-level, about invalid syntax
  • TypeMismatch: about shape expectations, not token types

Fields

§got: Cow<'static, str>

The token that was found (e.g., “object”, “string”, “null”).

§expected: &'static str

What was expected instead (e.g., “array”, “number”).

§

TypeMismatch

Type mismatch: expected a shape, got something else from the parser.

Level: Deserializer (FormatDeserializer)

We know the target Rust type (Shape), but the parser gave us something incompatible.

// Deserializing into struct User { age: u32 }
{"age": "not a number"}
        ^^^^^^^^^^^^^^
        type mismatch: expected u32, got string

Fields

§expected: &'static Shape

The expected shape/type we were trying to deserialize into.

§got: Cow<'static, str>

Description of what we got from the parser.

§

ShapeMismatch

Shape mismatch: expected one Rust type, but the code path requires another.

Level: Deserializer (FormatDeserializer)

This is an internal routing error - the deserializer was asked to deserialize into a type that doesn’t match what the current code path expects. For example, calling enum deserialization on a struct.

// Internal error: deserialize_enum called but shape is a struct
shape mismatch: expected enum, got struct User

Not to be confused with:

  • TypeMismatch: about parser output vs expected type
  • UnexpectedToken: about token types from parser

Fields

§expected: &'static Shape

The shape that was expected by this code path.

§got: &'static Shape

The actual shape that was provided.

§

UnknownField

Unknown field in struct.

Level: Deserializer (FormatDeserializer)

The input contains a field name that doesn’t exist in the target struct and the struct doesn’t allow unknown fields (no #[facet(deny_unknown_fields)] or similar).

// Deserializing into struct User { name: String }
{"name": "Alice", "age": 30}
                  ^^^^^
                  unknown field `age`

Fields

§field: Cow<'static, str>

The unknown field name.

§suggestion: Option<&'static str>

Optional suggestion for a similar field (typo correction).

§

UnknownVariant

Unknown enum variant.

Level: Deserializer (FormatDeserializer)

The input specifies a variant name that doesn’t exist in the target enum.

// Deserializing into enum Status { Active, Inactive }
"Pending"
^^^^^^^^^
unknown variant `Pending` for enum `Status`

Fields

§variant: Cow<'static, str>

The unknown variant name from the input.

§enum_shape: &'static Shape

The enum type.

§

NoMatchingVariant

No variant matched for untagged enum.

Level: Deserializer (FormatDeserializer)

For #[facet(untagged)] enums, we try each variant in order. This error means none of them matched the input.

// Deserializing into #[facet(untagged)] enum Value { Int(i32), Str(String) }
[1, 2, 3]
^^^^^^^^^
no matching variant for enum `Value` with array input

Fields

§enum_shape: &'static Shape

The enum type.

§input_kind: &'static str

What kind of input was provided (e.g., “array”, “object”, “string”).

§

MissingField

Missing required field.

Level: Deserializer (FormatDeserializer)

A struct field without a default value was not provided in the input.

// Deserializing into struct User { name: String, email: String }
{"name": "Alice"}
                ^
                missing field `email` in type `User`

Fields

§field: &'static str

The field that is missing.

§container_shape: &'static Shape

The type that contains the field.

§

DuplicateField

Duplicate field in input.

Level: Deserializer (FormatDeserializer)

The same field appears multiple times in the input.

{"name": "Alice", "name": "Bob"}
                  ^^^^^^
                  duplicate field `name` (first occurrence at offset 1)

Fields

§field: Cow<'static, str>

The field that appeared more than once.

§first_span: Option<Span>

Span of the first occurrence (for better diagnostics).

§

NumberOutOfRange

Number out of range for target type.

Level: Deserializer (FormatDeserializer)

The input contains a valid number, but it doesn’t fit in the target type.

// Deserializing into u8
256
^^^
number `256` out of range for u8

Fields

§value: Cow<'static, str>

The numeric value as a string.

§target_type: &'static str

The target type that couldn’t hold the value.

§

InvalidValue

Invalid value for the target type.

Level: Deserializer (FormatDeserializer)

The value is syntactically valid but semantically wrong for the target type. Used for things like invalid enum discriminants, malformed UUIDs, etc.

// Deserializing into Uuid
"not-a-valid-uuid"
^^^^^^^^^^^^^^^^^^
invalid value: expected UUID format

Fields

§message: Cow<'static, str>

Description of why the value is invalid.

§

CannotBorrow

Cannot borrow string from input.

Level: Deserializer (FormatDeserializer)

When deserializing into &str or Cow<str>, the string in the input required processing (e.g., escape sequences) and cannot be borrowed.

// Deserializing into &str
"hello\nworld"
^^^^^^^^^^^^^^
cannot borrow: string contains escape sequences

Fields

§reason: Cow<'static, str>

Description of why borrowing failed.

§

Reflect

Error from the reflection system.

Level: Deserializer (via facet-reflect)

These errors come from Partial operations like field access, variant selection, or type building.

Note: The path is stored at the DeserializeError level, not here. When converting from ReflectError, the path is extracted and stored in DeserializeError.path.

Fields

§kind: ReflectErrorKind

The specific kind of reflection error

§context: &'static str

What we were trying to do

§

Unsupported

Feature not implemented.

Level: Deserializer or Parser

The requested operation is not yet implemented. This is used for known gaps in functionality, not for invalid input.

// Trying to deserialize a type that's not yet supported
unsupported: multi-element tuple variants in flatten not yet supported

Fields

§message: Cow<'static, str>

Description of what is unsupported.

§

Io

I/O error during streaming deserialization.

Level: Parser

For parsers that read from streams, this wraps I/O errors.

Fields

§message: Cow<'static, str>

Description of the I/O error.

§

Solver

Error from the flatten solver.

Level: Deserializer (via facet-solver)

When deserializing types with #[facet(flatten)], the solver determines which fields go where. This error indicates solver failure.

Fields

§message: Cow<'static, str>

Description of the solver error.

§

Validation

Validation error.

Level: Deserializer (post-deserialization)

After successful deserialization, validation constraints failed.

// With #[facet(validate = "validate_age")]
{"age": -5}
        ^^
        validation failed for field `age`: must be non-negative

Fields

§field: &'static str

The field that failed validation.

§message: Cow<'static, str>

The validation error message.

§

Bug

Internal error indicating a logic bug in facet-format or one of the crates that relies on it (facet-json,e tc.)

Fields

§error: Cow<'static, str>

What happened?

§context: &'static str

What were we doing?

§

Alloc

Memory allocation failed.

Level: Deserializer (internal)

Failed to allocate memory for the partial value being built. This is rare but can happen with very large types or low memory.

Fields

§shape: &'static Shape

The shape we tried to allocate.

§operation: &'static str

What operation was being attempted.

§

Materialize

Shape mismatch when materializing a value.

Level: Deserializer (internal)

The shape of the built value doesn’t match the target type. This indicates a bug in the deserializer logic.

Fields

§expected: &'static Shape

The shape that was expected (the target type).

§actual: &'static Shape

The shape that was actually found.

§

RawCaptureNotSupported

Raw capture is not supported by the current parser.

Level: Deserializer (FormatDeserializer)

Types like RawJson require capturing the raw input without parsing it. This error occurs when attempting to deserialize such a type with a parser that doesn’t support raw capture (e.g., streaming parsers without buffering).

// Deserializing RawJson in streaming mode
raw capture not supported: type `RawJson` requires raw capture, but the
parser does not support it (e.g., streaming mode without buffering)

Fields

§shape: &'static Shape

The type that requires raw capture.

Implementations§

Source§

impl DeserializeErrorKind

Source

pub const fn with_span(self, span: Span) -> DeserializeError

Attach a span to this error kind, producing a full DeserializeError.

Trait Implementations§

Source§

impl Debug for DeserializeErrorKind

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for DeserializeErrorKind

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.