Skip to main content

Crate ayaml

Crate ayaml 

Source
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: adds ayaml::compat and its decoding entry points.
  • bench-internals: unstable hooks for ayaml’s benchmark and conformance harnesses. This module is hidden and outside SemVer.

§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§

compatlibyaml-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 (Tape and TapeDocuments).
value
The generic YAML value model.

Structs§

Deserializer
A document-bounded typed deserializer.
DocumentDeserializer
A source-backed single-document serde::Deserializer that preserves YAML source spans and borrows simple strings directly from the input.
EmitOptions
Options for YAML emitters.
Emitter
A reusable emitter configuration.
EmitterOptions
Emitter presentation options.
EmitterStyles
Per-node style overrides addressed by PathSegment paths.
EventStream
A pull traversal over spanned parser events.
Frontmatter
A leading YAML frontmatter block split from the remaining non-YAML body.
LineIndex
A line index over one input, for converting between byte offsets and positions.
NodeStyle
Per-node emitter presentation overrides.
ParseOptions
Options for default-profile YAML parsers and decoders.
ParsedFrontmatter
A decoded leading YAML frontmatter value paired with the remaining non-YAML body slice.
ParserBuf
Reusable opaque scratch buffer for high-throughput bulk YAML parsing.
SpannedMapIter
Iterator over (key, value) pairs in a SpannedNode mapping, in source order.
SpannedNode
One node in a SpannedTree.
SpannedTree
A parsed YAML stream with source spans.

Enums§

Chomping
Explicit block-scalar chomping preference.
ColumnEncoding
The unit that a LineIndex column counts. A later release may add a unit.
FrontmatterDelimiter
The closing delimiter that terminated a frontmatter block.
PathSegment
A path segment identifying a value in the emitted tree.
PositionError
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 input where 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 from buf.
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 input where possible.
from_str_with_in
Deserialize one YAML document into T with explicit parse options, reusing internal scratch buffers from buf.
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 from buf.
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 from buf.
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 Value under the default limits.
parse_frontmatter_value_with
Parse a leading YAML frontmatter block into a generic Value with 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 from buf.
parse_tape_documents_with
Parse every YAML document in a stream into TapeDocuments with explicit parse options.
parse_tape_documents_with_in
Parse every YAML document in a stream into TapeDocuments with explicit options, reusing internal scratch buffers from buf.
parse_tape_in
Parse one YAML document into a Tape, reusing internal scratch buffers from buf.
parse_tape_with
Parse one YAML document into a Tape with explicit parse options.
parse_tape_with_in
Parse one YAML document into a Tape with explicit options, reusing internal scratch buffers from buf.
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 from buf.
parse_value_with
Parse one YAML document with explicit parse options.
parse_value_with_in
Parse one YAML document into a generic Value with explicit parse options, reusing internal scratch buffers from buf.
split_frontmatter
Split a leading YAML frontmatter block (--- … --- or ...) from the remaining body.
split_frontmatter_with
Split a leading YAML frontmatter block from input with 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.