deser-path
A layer for deser that tracks the
path of the current value (for instance servers[1].port) during
serialization and deserialization and attaches it to errors.
"invalid type: string, expected u16" is not a useful error message when the
input has hundreds of values. Add the PathLayer and errors tell you
which value is wrong. It works with every format and does not require any
changes to your types:
use Deserialize;
use ;
let json = r#"{"servers": [
{"host": "a.example.com", "port": 80},
{"host": "b.example.com", "port": "http"}
]}"#;
let err = from_str
.
.unwrap_err;
let path = err..unwrap;
assert_eq!;
assert_eq!;
Unlike wrapping the deserializer (as serde_path_to_error does) the path
is also correct for values that had to be buffered, such as the content of
internally tagged and untagged enums. During serialization and
deserialization the current Path is available in the state, so custom
Serialize and Deserialize implementations and other layers can use it
too (for instance to redact values at certain paths).