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§
- Field
Error - Error captured for a malformed field value.
Enums§
- Borrowed
Json Field - JSON field-level result that borrows the raw value when decoding fails.
- Field
- Field-level result of deserializing an upstream value.
- Json
Field - JSON field-level result that owns the raw value when decoding fails.
Traits§
- Field
Decode - A type that can decode itself as a recoverable
Field. - Scalar
Field Decode - 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.