Expand description
ayaml is a safe, pure-Rust YAML 1.2.2 parser, event stream, value model, Serde implementation, and emitter.
One scanner and grammar back every surface, so the spanned event stream, generic values, typed Serde decoding, and emitted output use the same syntax, resolution, and resource limits.
§Quick start
Parse a document into the generic value model:
use ayaml::{parse_value, to_string};
let value = parse_value("name: example\nitems:\n - one\n - two\n")?;
assert_eq!(value["name"].as_str(), Some("example"));
let output = to_string(&value)?;
assert!(output.contains("name: example"));Decode into a typed value:
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct Config {
name: String,
retries: u32,
}
let config: Config = ayaml::from_str("name: example\nretries: 3\n")?;
assert_eq!(config.retries, 3);Walk the event stream when source spans matter:
use ayaml::Event;
let events: Vec<Event<'_>> = ayaml::parse_events("a: 1\n")?
.collect::<Result<_, _>>()?;
assert!(events.iter().any(|event| matches!(event, Event::MappingStart { .. })));Configure options and limits with ParseOptions and EmitOptions:
use ayaml::{EmitOptions, Limits, ParseOptions, parse_value_with, to_string_value_with};
const PARSE: ParseOptions =
ParseOptions::new().with_limits(Limits::new().with_max_depth(64));
let value = parse_value_with("a: [1, 2, 3]\n", &PARSE)?;
assert_eq!(value["a"][1].as_u64(), Some(2));
let yaml = to_string_value_with(&value, &EmitOptions::flow())?;
assert_eq!(yaml, "{a: [1, 2, 3]}\n");§Compatibility
The default parser follows YAML 1.2.2. The additive libyaml-compat
feature exposes ayaml::compat for callers that need libyaml-compatible
behavior; it never changes the root functions. The deliberate deviations
are listed in the workspace’s
tests/data/yaml-test-suite-deviations.txt file.
§Features
libyaml-compat: addsayaml::compatand its decoding entry points.bench-internals: unstable hooks for ayaml’s benchmark and conformance harnesses. This module is hidden and outsideSemVer.
§Resource limits
Every parse, decode, and emit operation has a _with form taking one
options value (ParseOptions, compat::ParseOptions, or
EmitOptions), which carries its Limits; Value::apply_merge_keys
and Value::apply_merge_keys_with_limits bound merge depth and merged
mapping sizes; from_value,
from_value_seed, and to_value operate on an in-memory Value and do
not take limits. Exceeding a limit
returns ErrorKind::Limit; Error::limit exposes the structured
LimitExceeded value. See the docs/limits.md file for the defaults and
the alias-budget formula.
§Errors and spans
Error implements Clone, PartialEq, and Eq, and carries a
coarse category (Error::kind -> ErrorKind), a stable
machine-readable code (Error::code -> ErrorCode, with
ErrorCode::as_str returning yaml.<kebab-name>), an optional byte
span, an optional source error, and a human-readable message. Input-related
errors carry a span and Error::location returns the one-based line and
column. Normal Display and Debug
output on Error may include text from the document (such as scalar
values, mapping keys, paths, tags, or directives); use Error::masked
(MaskedError) when logging or returning errors from sensitive or
untrusted documents. See the documentation site
(https://asamarts.github.io/ayaml/) — including docs/error-codes.md,
docs/security.md, and docs/stability.md — for the full code reference,
security and untrusted-input guidance, stability guarantees, and masked
error formatting. Events carry their own spans for source-aware tooling.
§Minimum supported Rust version
Rust 1.88. The crate is dual-licensed under MIT or Apache-2.0.
Re-exports§
pub use error::Error;pub use error::ErrorCode;pub use error::ErrorKind;pub use error::MaskedError;pub use event::Anchor;pub use event::CollectionStyle;pub use event::Event;pub use event::ScalarStyle;pub use limits::ALIAS_BYTES_PER_INPUT_BYTE;pub use limits::COMPAT_MAX_DOCUMENTS;pub use limits::DEFAULT_MAX_DOCUMENTS;pub use limits::LimitExceeded;pub use limits::LimitKind;pub use limits::Limits;pub use limits::MAX_ALIAS_EXPANSION_BYTES;pub use limits::MAX_DEPTH;pub use limits::MAX_DEPTH_CAP;pub use limits::MAX_INPUT_BYTES;pub use limits::MAX_TAG_EXPANSION_BYTES;pub use limits::NODE_COST_BYTES;pub use mapping::Entry;pub use mapping::Mapping;pub use number::Number;pub use span::Location;pub use span::Position;pub use span::Span;pub use span::Spanned;pub use tag::Tag;pub use tag::TaggedValue;pub use tape::Tape;pub use tape::TapeDocuments;pub use tape::TapeDocumentsIter;pub use tape::TapeKind;pub use tape::TapeMapIter;pub use tape::TapeRef;pub use tape::TapeSeqIter;pub use value::Value;
Modules§
- compat
libyaml-compat - Libyaml-compatible parsing and decoding.
- error
- Structured errors with a stable kind, a stable machine-readable code, and, when the source provides one, a span.
- event
- Spanned parser events.
- limits
- Resource limits and options for hostile YAML.
- mapping
- An insertion-ordered YAML mapping.
- number
- A YAML number with compatibility semantics.
- span
- Byte offsets, zero-based positions, and one-based locations.
- tag
- YAML tags and tagged values.
- tape
- Read-only flat-array YAML DOM (
TapeandTapeDocuments). - value
- The generic YAML value model.
Structs§
- Deserializer
- A document-bounded typed deserializer.
- Document
Deserializer - A source-backed single-document
serde::Deserializerthat preserves YAML source spans and borrows simple strings directly from the input. - Emit
Options - Options for YAML emitters.
- Emitter
- A reusable emitter configuration.
- Emitter
Options - Emitter presentation options.
- Emitter
Styles - Per-node style overrides addressed by
PathSegmentpaths. - Event
Stream - A pull traversal over spanned parser events.
- Frontmatter
- A leading YAML frontmatter block split from the remaining non-YAML body.
- Line
Index - A line index over one input, for converting between byte offsets and positions.
- Node
Style - Per-node emitter presentation overrides.
- Parse
Options - Options for default-profile YAML parsers and decoders.
- Parsed
Frontmatter - A decoded leading YAML frontmatter value paired with the remaining non-YAML body slice.
- Parser
Buf - Reusable opaque scratch buffer for high-throughput bulk YAML parsing.
- Spanned
MapIter - Iterator over
(key, value)pairs in aSpannedNodemapping, in source order. - Spanned
Node - One node in a
SpannedTree. - Spanned
Tree - A parsed YAML stream with source spans.
Enums§
- Chomping
- Explicit block-scalar chomping preference.
- Column
Encoding - The unit that a
LineIndexcolumn counts. A later release may add a unit. - Frontmatter
Delimiter - The closing delimiter that terminated a frontmatter block.
- Path
Segment - A path segment identifying a value in the emitted tree.
- Position
Error - Why a byte offset or a position cannot be converted.
Constants§
- VERSION
- The crate version.
Functions§
- from_
reader - Deserialize one UTF-8 YAML document from an I/O reader under the default limits.
- from_
reader_ with - Deserialize one UTF-8 YAML document from an I/O reader with explicit parse options.
- from_
slice - Deserialize one UTF-8 YAML document from bytes.
- from_
slice_ borrowed - Deserialize one UTF-8 YAML document from bytes, borrowing simple strings.
- from_
slice_ with - Deserialize one UTF-8 YAML document from bytes with explicit parse options,
borrowing simple strings from
inputwhere possible. - from_
str - Deserialize one YAML document into a typed Serde value.
- from_
str_ borrowed - Deserialize one YAML document while borrowing simple strings from
input. - from_
str_ in - Deserialize one YAML document into
T, reusing internal scratch buffers frombuf. - from_
str_ seed - Deserialize one YAML document with a caller-supplied seed, borrowing simple strings from
input. - from_
str_ seed_ in - Deserialize one YAML document with a caller-supplied seed, reusing internal scratch
buffers from
buf. - from_
str_ seed_ with - Deserialize one YAML document with a caller-supplied seed and explicit
parse options, borrowing simple strings from
input. - from_
str_ seed_ with_ in - Deserialize one YAML document with a caller-supplied seed and explicit parse options,
reusing internal scratch buffers from
buf. - from_
str_ with - Deserialize one YAML document into a typed Serde value with explicit parse
options, borrowing simple strings from
inputwhere possible. - from_
str_ with_ in - Deserialize one YAML document into
Twith explicit parse options, reusing internal scratch buffers frombuf. - from_
value - Deserialize a value from an already-composed generic value.
- from_
value_ seed - Deserialize using a caller-supplied Serde seed.
- parse_
documents - Parse every YAML document in the stream into generic values.
- parse_
documents_ in - Parse every YAML document in a stream into
Vec<Value>, reusing internal scratch buffers frombuf. - parse_
documents_ with - Parse every YAML document in the stream with explicit parse options.
- parse_
documents_ with_ in - Parse every YAML document in a stream into
Vec<Value>with explicit parse options, reusing internal scratch buffers frombuf. - parse_
events - Parse a YAML input into a pull traversal of spanned events.
- parse_
events_ with - Parse a YAML input into a pull traversal with explicit parse options.
- parse_
frontmatter - Parse a leading YAML frontmatter block into a typed Serde value under the default limits, returning the decoded value alongside the remaining non-YAML body slice and its byte offset.
- parse_
frontmatter_ value - Parse a leading YAML frontmatter block into a generic
Valueunder the default limits. - parse_
frontmatter_ value_ with - Parse a leading YAML frontmatter block into a generic
Valuewith explicit parse options. - parse_
frontmatter_ with - Parse a leading YAML frontmatter block into a typed Serde value with explicit parse options.
- parse_
tape - Parse one YAML document into a zero-copy flat-array
Tape. - parse_
tape_ documents - Parse every YAML document in a stream into a zero-copy flat-array
TapeDocuments. - parse_
tape_ documents_ in - Parse every YAML document in a stream into
TapeDocuments, reusing internal scratch buffers frombuf. - parse_
tape_ documents_ with - Parse every YAML document in a stream into
TapeDocumentswith explicit parse options. - parse_
tape_ documents_ with_ in - Parse every YAML document in a stream into
TapeDocumentswith explicit options, reusing internal scratch buffers frombuf. - parse_
tape_ in - Parse one YAML document into a
Tape, reusing internal scratch buffers frombuf. - parse_
tape_ with - Parse one YAML document into a
Tapewith explicit parse options. - parse_
tape_ with_ in - Parse one YAML document into a
Tapewith explicit options, reusing internal scratch buffers frombuf. - parse_
value - Parse one YAML document into the generic value model.
- parse_
value_ in - Parse one YAML document into a generic
Value, reusing internal scratch buffers frombuf. - parse_
value_ with - Parse one YAML document with explicit parse options.
- parse_
value_ with_ in - Parse one YAML document into a generic
Valuewith explicit parse options, reusing internal scratch buffers frombuf. - split_
frontmatter - Split a leading YAML frontmatter block (
---…---or...) from the remaining body. - split_
frontmatter_ with - Split a leading YAML frontmatter block from
inputwith explicit parse options. - to_
string - Serialize a value into YAML text with default options and limits.
- to_
string_ documents - Serialize a sequence of generic values as a multi-document stream.
- to_
string_ documents_ with - Serialize a sequence of generic values as a multi-document stream with explicit emitter options and limits.
- to_
string_ events - Emit a collected event stream as YAML text.
- to_
string_ events_ with - Emit a collected event stream as YAML text with explicit emitter options and limits.
- to_
string_ value - Serialize an existing generic value as YAML, preserving its tags.
- to_
string_ value_ with - Serialize an existing generic value as YAML with explicit emitter options and limits, preserving its tags.
- to_
string_ with - Serialize a value into YAML text with explicit emitter options and limits.
- to_
value - Serialize a Rust value into the generic YAML value model.
- to_
writer - Serialize a value as YAML to a writer.
- to_
writer_ documents - Write a multi-document value stream one document at a time.
- to_
writer_ documents_ with - Write a multi-document value stream one document at a time with explicit emitter options and limits.
- to_
writer_ event_ stream - Write a lazy event stream as it is parsed.
- to_
writer_ event_ stream_ with - Write a lazy event stream with explicit emitter options and limits.
- to_
writer_ events - Write a collected event stream to a writer.
- to_
writer_ events_ with - Write a collected event stream to a writer with explicit emitter options and limits.
- to_
writer_ value - Serialize an existing tagged value as YAML to a writer.
- to_
writer_ value_ with - Serialize an existing tagged value as YAML to a writer with explicit emitter options and limits.
- to_
writer_ with - Serialize a value as YAML to a writer with explicit emitter options and limits.