Skip to main content

from_slice_multiple

Function from_slice_multiple 

Source
pub fn from_slice_multiple<'de, T>(bytes: &'de [u8]) -> Result<Vec<T>, Error>
where T: Deserialize<'de>,
Expand description

Deserialize multiple YAML documents from a UTF-8 byte slice into a vector of T. Empty, untagged null-like, and explicit !!null documents are skipped, as with from_multiple. Application-tagged roots are deserialized, even with null payloads.

Supports both owned values and values borrowing from the input byte slice. Borrowed values cannot outlive the input, and strings requiring transformation cannot be borrowed as &str; see from_str_multiple for details. Invalid UTF-8 returns Error::InvalidUtf8Input. Use from_bytes_multiple to collect into other collection types.

Since 2.0, function pointers or callbacks accepting any input lifetime may need a forwarding closure such as |bytes| from_slice_multiple::<String>(bytes).

Example: read two Config documents separated by --- from bytes.

use serde::Deserialize;

#[derive(Debug, Deserialize, PartialEq)]
struct Config {
    name: String,
    enabled: bool,
    retries: i32,
}

let yaml = r#"
name: First
enabled: true
retries: 1
---
name: Second
enabled: false
retries: 2
"#;
let bytes = yaml.as_bytes();
let cfgs: Vec<Config> = serde_saphyr::from_slice_multiple(bytes).unwrap();
assert_eq!(cfgs.len(), 2);
assert_eq!(cfgs[0].name, "First");