Skip to main content

read

Function read 

Source
pub fn read<R, T>(reader: R) -> Result<DocumentReadIterator<T>>
where R: Read, T: for<'de> Deserialize<'de> + 'static,
Available on crate feature std only.
Expand description

Stream-decode every YAML document from a reader into typed values, yielding one Result<T> per document.

The reader is drained eagerly (see DocumentReadIterator for the memory caveat); document-by-document deserialisation is then produced lazily on demand. Per-document deserialisation errors surface as Err values inside the iterator so callers can recover and continue. A syntax error in the underlying YAML is returned synchronously from this function before any iteration happens.

§Errors

Returns an error if the reader fails, the YAML cannot be parsed, or any document exceeds the default security limits. Per-document deserialisation errors are not surfaced here; they appear inside the iterator.

§Examples

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

#[derive(Debug, Deserialize, PartialEq)]
struct Doc { id: u32 }

let yaml = "id: 1\n---\nid: 2\n---\nid: 3\n";
let docs: Vec<Doc> = noyalib::read::<_, Doc>(Cursor::new(yaml))
    .unwrap()
    .filter_map(Result::ok)
    .collect();
assert_eq!(docs, vec![Doc { id: 1 }, Doc { id: 2 }, Doc { id: 3 }]);