Skip to main content

from_slice_multiple_with_options

Function from_slice_multiple_with_options 

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

Deserialize multiple YAML documents from a UTF-8 byte slice into a vector with configurable Options.

Like from_multiple_with_options, this supports owned and borrowed values and skips null documents unless application-tagged. Invalid UTF-8 returns Error::InvalidUtf8Input. Use from_bytes_multiple_with_options 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, options| from_slice_multiple_with_options::<String>(bytes, options).

Example: two Config documents with a custom budget from bytes.

use serde::Deserialize;
use serde_saphyr::DuplicateKeyPolicy;

#[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 options = serde_saphyr::options! {
    budget: serde_saphyr::budget! {
        max_anchors: 200,
    },
    duplicate_keys: DuplicateKeyPolicy::FirstWins,
};
let cfgs: Vec<Config> = serde_saphyr::from_slice_multiple_with_options(bytes, options).unwrap();
assert_eq!(cfgs.len(), 2);
assert!(!cfgs[1].enabled);