Skip to content

API reference and caveats

Complete Rustdoc signatures and trait implementations are published on docs.rs:

Use the tables below to choose an entry point, check feature flags, and review key caveats across ayaml and ayaml-edit.

Task Recommended API Key behavior
Decode one document into a Rust struct from_str, from_str_with, from_slice, from_slice_with, from_str_seed, from_str_seed_with Streams directly from events at every input size; rejects trailing documents and duplicate keys.
Zero-copy &str struct decode from_str_borrowed, from_slice_borrowed, from_str_with, from_slice_with Borrows unescaped scalars straight from the input slice; use Cow<'a, str> or String when escapes or folding may occur.
Bounded I/O stream decode from_reader, from_reader_with Stops reading after max_input_bytes + 1 bytes before UTF-8 validation.
In-memory Value <-> Serde conversion from_value, from_value_seed, to_value Converts between ayaml::Value and typed Rust structs without re-parsing text (from_value rejects Spanned<T> and treats Value::Null as explicit null).
Decode with field-level byte spans Spanned<T> with from_str*, Tape / TapeRef, DocumentDeserializer, Deserializer, or parse_frontmatter* Attaches exact byte Span to each wrapped field; refuses untrusted or buffered deserializers.
Singleton-map enums and explicit typed tags ayaml::with::singleton_map, ayaml::Tagged<T> Serializes externally tagged enum variants as single-entry mappings (Variant: payload) and captures explicit YAML tags alongside typed payloads T (composable with Spanned<T>).
Multi-document typed stream Deserializer::from_str, Deserializer::from_str_with + next / next_borrowed / next_seed / next_seed_borrowed Lazy one-document-at-a-time streaming with a shared alias expansion budget across documents.
Serde middleware integration DocumentDeserializer::from_str, from_str_with, from_str_compat, from_str_compat_with Implements serde::Deserializer<'de> by value and &mut for wrappers like serde_path_to_error or serde_ignored.
Generic dynamic DOM parse_value, parse_value_with, parse_documents, parse_documents_with Insertion-ordered Mapping, exact i64/u64/i128/u128 + IEEE-754 f64 Number, Value::Tagged.
Read-only zero-copy flat-array DOM parse_tape, parse_tape_with, parse_tape_documents, parse_tape_documents_with, parse_owned_tape*, parse_owned_tape_documents*, Tape, TapeDocuments, OwnedTape, OwnedTapeDocuments, TapeRef Compact flat-array DOM borrowing unmodified strings directly from &str (or a 'static Arc<str> with OwnedTape / OwnedTapeDocuments) with source spans, streaming merge_keys, serde::Deserializer<'de> on Tape, &Tape, &OwnedTape, and TapeRef, and to_value() conversion.
Bulk high-throughput buffer reuse ParserBuf, parse_events_in, parse_events_with_in, compat::parse_events*_in, EventStream::recycle_into, parse_tape*_in, parse_owned_tape*_in, compat::parse_tape*_in, compat::parse_owned_tape*_in, parse_value*_in, parse_documents*_in, from_str*_in, compat::from_str*_in, Tape::recycle_into, OwnedTape::recycle_into, TapeDocuments::recycle_into, OwnedTapeDocuments::recycle_into Reuses scanner, line-window, continuation, event-queue, duplicate-key, and recycled tape buffers across repeated parses; automatically releases outlier capacities and stays clean across errors or unwinds.
Pull-based spanned event stream parse_events, parse_events_with, parse_events_in, parse_events_with_in, EventStream, EventStream::recycle_into Preserves raw duplicate mapping pairs, anchors, aliases, and spans; supports ParserBuf scratch reuse across repeated event-streaming loops.
Spanned AST, path lookup, and opt-in comment spans SpannedTree::parse, SpannedTree::parse_with, SpannedTree::parse_with_comments, SpannedNode, scan_comments, scan_comments_with, LineIndex Traverse nodes (as_mapping, as_sequence, get, lookup, scalar_value, scalar_text, scalar_style, scalar_tag), query value (span_at), entry (entry_span_at), and opt-in comment (comments) byte spans, and convert offsets to UTF-8/scalar/UTF-16 Position and Location.
Markdown / static-site frontmatter split_frontmatter, split_frontmatter_with, parse_frontmatter, parse_frontmatter_with, parse_frontmatter_value, parse_frontmatter_value_with Bounded closing delimiter scan; preserves original file byte offsets for spans and body.
Deterministic YAML emission to_string, to_string_with, to_writer, to_writer_with, to_string_value_with, to_string_documents_with, to_string_events_with, Emitter YAML 1.1-safe leading-zero numeral quoting (0644), EmitOptions (indent, flow, EmitterStyles, Limits).
Comment-preserving edits ayaml_edit::replace_scalar*, remove_entry*, insert_mapping_entry*, upsert_mapping_entry*, insert_sequence_item*, insert_comment*, apply_batch*, plan_batch*, apply_batch_validated*, apply_batch_checked*, Editable<T>, EditOptions::with_compat_parse_options (libyaml-compat), apply_edits Applies double-verified (Value + canonical Event) byte edits, plan-only TextEdit generation, and transactional typed validation across block/flow/empty mappings while preserving comments, untouched sibling block scalars/anchors/tags, BOMs, and CRLF/LF formatting.
Crate Feature Default Stability Purpose
ayaml libyaml-compat off Stable Enables additive ayaml::compat module (compat::from_str*, compat::parse_events*, compat::parse_tape*, compat::parse_owned_tape*), DocumentDeserializer::from_str_compat*, and SpannedTree::parse_compat*; never alters root ayaml::* APIs.
ayaml bench-internals off Unstable (outside SemVer) Internal harness hooks for workspace benchmarks and conformance tests.
ayaml-edit libyaml-compat off Stable Enables EditOptions::with_compat_parse_options and EditOptions::compat_parse_options for editing documents under libyaml-compatible scanning and duplicate-key rules.
use ayaml::{EmitOptions, Limits, ParseOptions, from_str_with, to_string_with};
use serde::{Deserialize, Serialize};
#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct Job {
name: String,
retries: u8,
}
const PARSE: ParseOptions = ParseOptions::new()
.with_merge_keys(true)
.with_limits(Limits::new().with_max_depth(32).with_max_input_bytes(64 * 1024));
const EMIT: EmitOptions = EmitOptions::new()
.with_indent(2)
.with_limits(Limits::new().with_max_output_bytes(64 * 1024));
let job: Job = from_str_with("base: &b\n retries: 2\nname: test\n<<: *b\n", &PARSE)?;
let rendered = to_string_with(&job, &EMIT)?;
assert_eq!(from_str_with::<Job>(&rendered, &PARSE)?, job);

For read-only inspection, query indexing, or bulk document loops, pair ParserBuf with Tape and Tape::recycle_into. Unmodified scalars are borrowed zero-copy from the input slice, and sub-nodes (TapeRef<'t, 'a>) can deserialize borrowed &'a str values via Serde that outlive the Tape:

use ayaml::{ParserBuf, TapeKind};
use serde::Deserialize;
#[derive(Debug, PartialEq, Deserialize)]
struct Record<'a> {
name: &'a str,
port: u16,
}
let mut buf = ParserBuf::new();
let docs = [
"service:\n name: api\n port: 8080\n",
"service:\n name: worker\n port: 9090\n",
];
for input in docs {
let record: Record<'_> = {
let tape = buf.parse_tape(input)?;
let svc = tape.get("service").expect("service mapping");
assert_eq!(svc.kind(), TapeKind::Mapping);
assert!(svc.get("name").and_then(|n| n.as_borrowed_str()).is_some());
let decoded = Record::deserialize(svc)?;
tape.recycle_into(&mut buf);
decoded
};
assert!(record.port >= 8080);
}
  • UTF-8 only: ayaml accepts valid UTF-8 (&str or UTF-8 &[u8] / Read). UTF-16 and UTF-32 inputs must be transcoded before parsing.
  • Spanned<T> deserializer and alias semantics: Spanned<T> requires a direct source-backed ayaml deserializer so byte spans are guaranteed to lie within the source input. It is refused by from_value, foreign Serde deserializers (such as serde_json), and buffered Serde attributes (#[serde(flatten)], #[serde(untagged)], #[serde(tag = "...")]). When applied to an alias node (copy: *anchor), Spanned<T> reports the alias use-site span (*anchor), while nested Spanned<U> fields inside the target struct report their definition spans.
  • Merge keys and SpannedTree::entry_span: Enabling ParseOptions::with_merge_keys(true) on SpannedTree::parse_with suppresses entry_span() across the tree so deleting a merged entry cannot mutate the referenced anchor definition. ayaml-edit similarly refuses merge_keys.
  • Removed 0.x shims: Pre-1.0 *_with_limits, *_with_options, *_with_styles functions, Marker, and dead ErrorKind::Alias have been removed for 1.0 in favor of *_with(..., &ParseOptions), *_with(..., &EmitOptions), byte Span with LineIndex, and ErrorKind::Composition with ErrorCode::UnknownAnchor / ErrorCode::CyclicAlias (see Stability and versioning). Value::apply_merge_keys_with_limits remains stable in 1.0.
  • Tape::deserialize scalar typing: Tape and TapeRef deserialize resolved YAML types strictly (rejecting non-string scalars such as 42, true, or explicit null for String targets, matching from_value) while preserving source spans for Spanned<T>, zero-copy &'a str borrowing, and empty-scalar defaults.
  • Error text on untrusted inputs: Always log err.masked() rather than err.to_string() when YAML inputs may contain secrets or PII (see Security and untrusted input and Error codes).