Skip to main content

from_str_strict

Function from_str_strict 

Source
pub fn from_str_strict<T>(s: &str) -> Result<T>
where T: for<'de> Deserialize<'de> + 'static,
Expand description

Strict deserialise: like from_str but errors if s contains any keys that the target type T does not declare.

Solves the typo-in-config-key problem — by default, both serde_yaml and noyalib silently ignore keys the struct does not know about, so a misspelled replicass: 3 (with the typo) deserialises into a struct whose replicas field stays at its Default. from_str_strict surfaces those extra keys as a typed Error::UnknownField listing every offending path.

§Errors

  • Any key in the YAML document is not declared on T.
  • Any of the regular from_str error paths.

§Examples

#[derive(Debug, serde::Deserialize)]
struct Config {
    port: u16,
}

// The typo "porrt" is silently ignored by `from_str`. With
// `from_str_strict` it surfaces as a typed error.
let yaml = "port: 8080\nporrt: 9090\n";
assert!(noyalib::from_str::<Config>(yaml).is_ok());
assert!(noyalib::from_str_strict::<Config>(yaml).is_err());