Skip to main content

Crate serde_field_result

Crate serde_field_result 

Source
Expand description

Field-level Serde recovery for schema-drift-tolerant API clients.

Field<T> is for values where this crate knows how to consume both valid and invalid representations without aborting the parent struct. A malformed field becomes Field::Invalid, while the rest of the response can keep deserializing.

use serde::Deserialize;
use serde_field_result::Field;

#[derive(Deserialize)]
struct Quote {
    #[serde(default)] // required if absent fields should become Field::Missing
    price: Field<f64>,
}

let quote: Quote = serde_json::from_str(r#"{"price":{"raw":"bad"}}"#).unwrap();
assert!(quote.price.is_invalid());

Serde resolves struct field presence before Field’s deserializer runs, so #[serde(default)] is required for absent fields to become Field::Missing. Without it, the parent struct fails first:

use serde::Deserialize;
use serde_field_result::Field;

#[derive(Deserialize)]
struct Quote {
    price: Field<f64>,
}

let error = match serde_json::from_str::<Quote>("{}") {
    Ok(_) => panic!("missing field unexpectedly decoded"),
    Err(error) => error,
};
assert_eq!(error.to_string(), "missing field `price` at line 1 column 2");

The core type is not a generic buffering layer over arbitrary T: Deserialize. Serde does not expose that abstraction in a format-agnostic way. Implement ScalarFieldDecode for custom scalar-like types, implement FieldDecode for custom non-scalar recovery, or enable the json feature and use JsonField or BorrowedJsonField for arbitrary JSON values with raw-value capture.

For ordinary Field<T> values, null and Serde unit decode as Field::Missing. Use Field<Option<T>> when explicit null or unit should decode as Field::Valid(None) while absent fields still require #[serde(default)] to become Field::Missing.

The built-in scalar decoders use Deserializer::deserialize_any, so they are meant for self-describing formats such as JSON.

Structs§

FieldError
Error captured for a malformed field value.

Enums§

BorrowedJsonField
JSON field-level result that borrows the raw value when decoding fails.
Field
Field-level result of deserializing an upstream value.
JsonField
JSON field-level result that owns the raw value when decoding fails.

Traits§

FieldDecode
A type that can decode itself as a recoverable Field.
ScalarFieldDecode
A scalar-like type that can decode itself from primitive Serde values.

Functions§

drain_map
Drains every remaining entry from a Serde map.
drain_seq
Drains every remaining element from a Serde sequence.
invalid_map
Drains a Serde map and returns Field::Invalid.
invalid_seq
Drains a Serde sequence and returns Field::Invalid.