Function serde_yml::de::from_reader

source ·
pub fn from_reader<R, T>(rdr: R) -> Result<T, Error>
where R: Read, T: DeserializeOwned,
Expand description

Deserialize an instance of type T from an IO stream of YAML.

This function reads YAML data from an IO stream and attempts to parse and deserialize it into an instance of the type T. The type must implement the DeserializeOwned trait from Serde, which means it must be able to be deserialized without any borrowed data.

§Errors

This conversion can fail if the structure of the YAML does not match the structure expected by T, for example if T is a struct type but the YAML contains something other than a mapping. It can also fail if the structure is correct but T’s implementation of Deserialize decides that something is wrong with the data, for example required struct fields are missing from the YAML mapping or some number is too big to fit in the expected primitive type.

§Examples

use serde::Deserialize;
use std::io::Cursor;

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

let yaml_data = br#"
debug: true
port: 8080
"#;

let reader = Cursor::new(yaml_data);
let config: Config = serde_yml::from_reader(reader).unwrap();
println!("{:?}", config);