Getting started
Parse YAML into Value trees or typed Serde structs, stream multiple documents,
inspect spans, emit YAML, and enforce resource limits with masked errors.
Adding ayaml
Section titled “Adding ayaml”[dependencies]ayaml = "1.0.0"serde = { version = "1", features = ["derive"] }Enable the optional libyaml-compat feature only when you need ayaml::compat; see the
compatibility profile.
Parsing values and document streams
Section titled “Parsing values and document streams”Use parse_value for a single YAML document and parse_documents for a multi-document stream:
use ayaml::{parse_documents, parse_value};
let value = parse_value("host: localhost\nport: 5432\n")?;assert_eq!(value["host"].as_str(), Some("localhost"));assert_eq!(value["port"].as_u64(), Some(5432));
let docs = parse_documents("first: 1\n---\nsecond: 2\n")?;assert_eq!(docs.len(), 2);assert_eq!(docs[1]["second"].as_u64(), Some(2));Single-document readers reject a second document with ErrorCode::TrailingDocuments.
Zero-copy read-only Tape and buffer reuse
Section titled “Zero-copy read-only Tape and buffer reuse”For read-only inspection or high-throughput bulk loops, parse_tape stores the document in a
compact flat array borrowing unmodified scalars directly from the input slice, and ParserBuf
reuses internal scratch allocations across parses:
use ayaml::{ParserBuf, parse_tape_in};
let mut buf = ParserBuf::new();let tape = parse_tape_in("host: localhost\nports: [80, 443]\n", &mut buf)?;let root = tape.root();assert_eq!(root.get("host").and_then(|n| n.as_str()), Some("localhost"));assert_eq!( root.get("ports").and_then(|n| n.get(1)).and_then(|n| n.as_u64()), Some(443),);tape.recycle_into(&mut buf);Typed Serde decoding
Section titled “Typed Serde decoding”Use from_str, from_slice, from_str_borrowed, or from_value for a single document,
and Deserializer to read a multi-document stream lazily:
use serde::Deserialize;
#[derive(Debug, PartialEq, Deserialize)]struct Entry<'a> { key: &'a str, count: u32,}
let borrowed: Entry<'_> = ayaml::from_str_borrowed("key: alpha\ncount: 3\n")?;assert_eq!(borrowed, Entry { key: "alpha", count: 3 });
let mut stream = ayaml::Deserializer::from_str("10\n---\n20\n")?;assert_eq!(stream.next::<u32>()?, Some(10));assert_eq!(stream.next::<u32>()?, Some(20));assert_eq!(stream.next::<u32>()?, None);Spanned events, positions, and SpannedTree
Section titled “Spanned events, positions, and SpannedTree”When source locations matter (for policy checks, diagnostics, or locating keys and values),
use parse_events, LineIndex, and SpannedTree:
use ayaml::{ColumnEncoding, Event, LineIndex, Location, PathSegment, SpannedTree, parse_events};
let input = "service:\n port: 8080\n";let events: Vec<Event<'_>> = parse_events(input)?.collect::<Result<_, _>>()?;assert!(!events.is_empty());
let tree = SpannedTree::parse(input)?;let port_span = tree .span_at(&[PathSegment::key("service"), PathSegment::key("port")]) .expect("port span");assert_eq!(&input[port_span.range()], "8080");
let index = LineIndex::new(input);let pos = index.to_position(port_span.start(), ColumnEncoding::Scalar).expect("position");let loc = Location::from(pos);assert_eq!((loc.line(), loc.column()), (2, 9));Emitting YAML
Section titled “Emitting YAML”Use to_string, to_string_value, to_writer, or their _with forms taking &EmitOptions:
use ayaml::{EmitOptions, parse_value, to_string_value_with};
let value = parse_value("service:\n port: 8080\n")?;
let flow = to_string_value_with(&value, &EmitOptions::flow())?;assert_eq!(flow, "{service: {port: 8080}}\n");
let mut buf = Vec::new();ayaml::to_writer_with(&mut buf, &value, &EmitOptions::new().with_indent(4))?;assert_eq!( String::from_utf8(buf).expect("utf-8"), "service:\n port: 8080\n");Options, limits, and masked errors
Section titled “Options, limits, and masked errors”Pass ParseOptions to any *_with reader to tighten Limits, match on Error::code()
rather than message text, and format error.masked() whenever logs or responses must not
contain YAML document text:
use std::collections::HashMap;use ayaml::{ErrorCode, Limits, ParseOptions, from_str_with};
const OPTIONS: ParseOptions = ParseOptions::new().with_limits( Limits::new() .with_max_input_bytes(64 * 1024) .with_max_depth(32),);
let err = from_str_with::<HashMap<String, u32>>("token: top-secret\n", &OPTIONS).unwrap_err();assert_eq!(err.code(), ErrorCode::InvalidType);
let masked = err.masked().to_string();assert!(!masked.contains("top-secret"));assert_eq!(masked, "serde error (yaml.invalid-type) at line 1 column 8");See Error codes, Security and untrusted input, and
Resource limits for the full contracts. For streaming I/O (from_reader),
Markdown frontmatter (parse_frontmatter), opt-in merge keys (with_merge_keys), and
Spanned<T>, see the Cookbook; for verified comment-preserving edits, see
Source-preserving editing (ayaml-edit); for how Tape, Value, and pull
events compare in memory, see Parsing modes explained.