Skip to main content

parse_stream

Function parse_stream 

Source
pub fn parse_stream(input: &str) -> Result<Vec<Document>>
Expand description

Parse a YAML stream and return one Document per logical document.

Boundaries follow YAML 1.2.2 §9.1: an explicit ... end marker closes the current document, and a fresh --- opens the next. Trivia (comments, blank lines) between an explicit ... and the next document is treated as the next document’s prologue; trailing trivia at end-of-stream is attached to the last document so concatenating each document’s source reproduces the original input byte-for-byte.

§Errors

Same as parse_document.

§Examples

Single document:

use noyalib::cst::parse_stream;

let src = "---\nfoo: 1\n";
let docs = parse_stream(src).unwrap();
assert_eq!(docs.len(), 1);
assert_eq!(docs[0].to_string(), src);

Two documents — split on ---:

use noyalib::cst::{parse_stream, Document};

let src = "---\nfoo: 1\n---\nbar: 2\n";
let docs = parse_stream(src).unwrap();
assert_eq!(docs.len(), 2);
assert_eq!(docs[0].as_value()["foo"].as_i64(), Some(1));
assert_eq!(docs[1].as_value()["bar"].as_i64(), Some(2));
let joined: String = docs.iter().map(Document::source).collect();
assert_eq!(joined, src);