pub struct IgnoredFields { /* private fields */ }Expand description
Collects the keys ignored with UnknownFields::Collect.
Every ignored key is reported as an Error which carries the same
information as if the key was rejected: the location (if the format
provides it) and the context registered in the state (for instance the
path with deser-path). The line and column are only available if the
format provides the Source (see TrackLocations).
The collector is shared between its clones, the clone in the state
reports to the one that was set up. This makes it an output of the
deserialization rather than configuration: set the policy in the state
of the deserialization (for instance in the setup callback of
Deserializer::deserialize_with)
rather than in a Context that is shared. In a
shared context the keys of all deserializations that use it (which
might run on other threads) are reported to the same collector:
use deser::de::{Deserializer, IgnoredFields, UnknownFields};
use deser::Deserialize;
#[derive(Deserialize)]
struct Config {
name: String,
}
let ignored = IgnoredFields::new();
let config: Config = deser_json::Deserializer::from_str(r#"{"name": "demo", "nmae": "x"}"#)
.deserialize_with(|driver| {
UnknownFields::Collect(ignored.clone()).set(driver.state_mut())
})
.unwrap();
assert_eq!(config.name, "demo");
let ignored = ignored.take();
assert_eq!(ignored.len(), 1);
assert_eq!(ignored[0].message(), "unknown field `nmae`, expected `name`");Implementations§
Source§impl IgnoredFields
impl IgnoredFields
Sourcepub fn new() -> IgnoredFields
pub fn new() -> IgnoredFields
Creates an empty collector.