API reference and caveats
Complete Rustdoc signatures and trait implementations are published on docs.rs:
- Core crate: docs.rs/ayaml
- Source-preserving editor crate: docs.rs/ayaml-edit
Use the tables below to choose an entry point, check feature flags, and review
key caveats across ayaml and ayaml-edit.
Choosing the right entry point
Section titled “Choosing the right entry point”| 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. |
Feature flags
Section titled “Feature flags”| 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. |
Options and builder summary
Section titled “Options and builder summary”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);Flat-array Tape and bulk ParserBuf reuse
Section titled “Flat-array Tape and bulk ParserBuf reuse”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);}Important caveats
Section titled “Important caveats”- UTF-8 only:
ayamlaccepts valid UTF-8 (&stror 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-backedayamldeserializer so byte spans are guaranteed to lie within the source input. It is refused byfrom_value, foreign Serde deserializers (such asserde_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 nestedSpanned<U>fields inside the target struct report their definition spans.- Merge keys and
SpannedTree::entry_span: EnablingParseOptions::with_merge_keys(true)onSpannedTree::parse_withsuppressesentry_span()across the tree so deleting a merged entry cannot mutate the referenced anchor definition.ayaml-editsimilarly refusesmerge_keys. - Removed
0.xshims: Pre-1.0*_with_limits,*_with_options,*_with_stylesfunctions,Marker, and deadErrorKind::Aliashave been removed for1.0in favor of*_with(..., &ParseOptions),*_with(..., &EmitOptions), byteSpanwithLineIndex, andErrorKind::CompositionwithErrorCode::UnknownAnchor/ErrorCode::CyclicAlias(see Stability and versioning).Value::apply_merge_keys_with_limitsremains stable in1.0. Tape::deserializescalar typing:TapeandTapeRefdeserialize resolved YAML types strictly (rejecting non-string scalars such as42,true, or explicitnullforStringtargets, matchingfrom_value) while preserving source spans forSpanned<T>, zero-copy&'a strborrowing, and empty-scalar defaults.- Error text on untrusted inputs: Always log
err.masked()rather thanerr.to_string()when YAML inputs may contain secrets or PII (see Security and untrusted input and Error codes).