Compatibility profile
ayaml has one default parsing profile and one optional compatibility mode.
Default profile
Section titled “Default profile”The default profile follows YAML 1.2.2 and is UTF-8 only.
Scalar resolution
Section titled “Scalar resolution”Untagged plain scalars resolve as follows:
| Input | Result |
|---|---|
empty, ~, null, Null, NULL |
null |
true, True, TRUE |
boolean true |
false, False, FALSE |
boolean false |
0b11, 0o17, 0x1f |
integer |
| decimal integers up to 128 bits | exact integer |
| wider decimal integers | finite float |
| wider radix integers | string |
1e3, 1.5, .inf, -.inf, .nan |
float |
yes, no, 0123, date-like text |
string |
Quoted and block scalars remain strings unless an explicit core tag requests
conversion. A custom tag preserves its payload and the tag; the non-specific
! tag makes a scalar a string.
Explicit core tags override implicit resolution:
!!str!!int!!float!!bool!!null
An invalid explicit core scalar is a resolution error.
A core-schema tag on a node of another kind is dropped in both modes: !!seq a reads the
string a, while !!str {}, a: !!int [1], and !!null [] read the collection without the
tag. In the value model, a core collection tag on a collection is dropped too, since it only
restates the kind. A custom tag is kept.
YAML 1.1 readers
Section titled “YAML 1.1 readers”The writers quote a string that looks like a decimal numeral with a leading zero, such as
0644, -0644, or 0_644, because YAML 1.2 keeps it a string but a YAML 1.1 reader may
read it as an octal integer. The writers leave the following plain strings as they are, so a
YAML 1.1 reader can still misread them:
| Text | YAML 1.1 reading |
|---|---|
yes, no, on, off |
boolean |
underscored numbers such as 1_000 |
integer |
sexagesimal values such as 1:20 or 1:20:30.5 |
integer or float |
dates such as 2001-12-15 |
timestamp |
= |
the special value key |
<< |
merge key |
A quoted or block scalar stays a string in both profiles. An explicit tag also settles the reading, so the event writer may leave a tagged scalar plain.
Collections and keys
Section titled “Collections and keys”- Mapping order is insertion order.
- Duplicate mapping pairs remain visible in the event stream.
- Generic value composition rejects duplicate semantic keys deterministically, including non-string keys that compare equal after resolution.
- Typed decoding follows the target Serde visitor. In compatibility mode,
duplicate mapping pairs reach it: generated structs reject duplicate fields,
and a map-like visitor keeps the last value of an exact duplicate. Default
mode rejects a duplicate semantic key before the visitor, including scalar
keys that differ only in type, as
1and"1". - In typed decoding, a custom tag, one outside YAML’s own
tag:yaml.org,2002:names, names an enum’s variant, and aValuekeeps it. So does any of those names on a collection but a collection tag such as!!map, which is dropped, as in!!C {x: 1}or!!str {x: 1}, while a scalar under one that the core schema doesn’t define for a scalar, as in!!C 5or!!seq a, is a string. A tag names the variant by its whole name: a local tag by all of it after its first!, asBfor!B, and any other tag by its full name in angle brackets, as<tag:x:B>for!<tag:x:B>, which!e!Bunder%TAG !e! tag:x:is too, or<tag:yaml.org,2002:C>for!!C. A reader stores the tag in a canonical full-name split, sohandle()andsuffix()agree across readers; the full name is what the tag means. Serialization writes a variant that holds a value under the local tag of its name, as!%3Ctag:x:B%3Efor one named<tag:x:B>, which selects it too, and a unit variant as its name. An enum also reads from a scalar that names a variant, and from a mapping of one entry whose key is a string naming it, as inB: 5or! B: 5, in both modes. A scalar that names a variant holds nothing: it fills a unit variant and one that holds an option, a unit, or any value, and not a variant that holds a sequence, a mapping, a struct, or a scalar (D87). Any other mapping is an error, such as one of two entries, or one whose key is1,true, or under a custom tag, as in!x B: 5(D80). A null, boolean, or number scalar does not name a variant, even when a variant is renamed to its spelling; use a quoted scalar or a one-entry mapping for such a name. A sequence does not fill a struct by position. A string target underfrom_valueaccepts only a string or a tagged string; the source-based readers still read a scalar’s text into a string. A null is not an empty sequence, map, or struct; an empty source value still supplies an empty collection or a struct’s defaults, but a composedValue::Nullis treated as an explicit null byfrom_value. A target that Serde reads by its type, such as a number, a sequence, a struct, or what an enum variant holds, reads the node under a custom tag, so!port 80reads into au16, while a collection under a YAML tag that it keeps, as in!!C {x: 1}, reaches the target as an enum. AnOptionreads a tagged node asSome, since what it holds may take the tag. A target that Serde reads throughdeserialize_any, such as an untagged or internally tagged enum or a flattened field, gets the tag as an enum. A tag with no value, as in!Point, fills a struct variant as an empty value fills a struct. - A float target reads a number, and from the input a number’s spelling with
a leading zero too, as in
01, which resolves to a string. Other text, such asinf, a spelling too large for a float, such as1e999, and a scalar tagged as a string, such as!!str 1.5, are errors read into anf64. - A plain or quoted
<<remains a literal key by default. Merge-key expansion is opt-in viaParseOptions::with_merge_keys(true)(for event, value,Tape,TapeDocuments,OwnedTape,OwnedTapeDocuments,SpannedTree, frontmatter, and typed readers, keeping quoted"<<"literal) orValue::apply_merge_keys(after composing aValue), not an implicit parser effect. - Tags, anchors, aliases, directives, and document markers are preserved in the event stream.
- A node has at most one tag and one anchor; a second one is an error, in both modes.
- An explicit key’s properties may be followed by an indentless sequence, in both modes.
- The non-specific tag
!isn’t changed by a%TAG !directive, and a tag whose handle expands to part of a core-schema name is that core tag, in both modes.
Unicode and line breaks
Section titled “Unicode and line breaks”Valid UTF-8 is required. LF, CR, and CRLF are accepted as line breaks. NEL, line separator, and paragraph separator are content under YAML 1.2.2. A UTF-8 byte-order mark is handled according to the specification: it may start any document prefix, and it takes up no column, so a document marker or directive right after one is still a marker or directive. Raw C0 control characters are rejected as content; a tab is accepted where YAML uses it as separation. Quoted scalars accept the printable characters that YAML permits, including DEL, C1 controls, U+FFFE, and U+FFFF.
Compatibility mode
Section titled “Compatibility mode”The additive libyaml-compat feature exposes ayaml::compat:
[dependencies]ayaml = { version = "1.0.0", features = ["libyaml-compat"] }let value: ayaml::Value = ayaml::compat::from_str("key: value\n")?;assert_eq!(value["key"].as_str(), Some("value"));The compatibility module reads YAML the way a libyaml-compatible stack does, for
callers that need it, apart from the differences listed below. Enabling the
feature does not change the root parsing functions; choose the compatibility
entry points explicitly. Every compatibility entry point has a _with form
(compat::from_str_with, compat::from_str_with_in,
compat::from_str_seed_with, compat::from_str_seed_with_in,
compat::deserialize_any_with, compat::parse_events_with,
compat::parse_events_with_in, compat::parse_events_eager_with,
compat::parse_tape_with, compat::parse_tape_with_in,
compat::parse_tape_documents_with, compat::parse_tape_documents_with_in,
compat::parse_owned_tape_with, compat::parse_owned_tape_with_in,
compat::parse_owned_tape_documents_with, compat::parse_owned_tape_documents_with_in,
compat::scan_comments_with,
DocumentDeserializer::from_str_compat_with,
SpannedTree::parse_compat_with, and SpannedTree::parse_compat_with_comments;
plus the matching *_in variants for ParserBuf reuse) taking
a compat::ParseOptions value for custom resource limits, global-tag
handling, and duplicate-key enforcement. compat::GlobalTags::Keep (the default) keeps tags
outside YAML’s core schema as Value::Tagged and Serde enums;
compat::GlobalTags::Drop drops non-core global tags such as !!timestamp,
!!binary, !<tag:example.com,2000:x>, and %TAG URI handles as a
libyaml-based reader does, reading a scalar beneath one as text for
deserialize_any and parsing a plain one as the requested type for a typed
target, reading a collection beneath one as untagged, and keeping local tags,
the non-specific ! tag, and core-schema tags unchanged.
Compatibility mode sets allow_duplicates: true by default to match libyaml
(which accepts repeated mapping keys with last-wins semantics in compat::parse_tape*,
compat::parse_owned_tape*, and arbitrary Serde targets such as BTreeMap or HashMap;
note that ayaml::Value’s Deserialize impl always rejects duplicate keys, while
compat::parse_events* and SpannedTree::parse_compat* preserve source syntax entries
as written).
Linters or config validators that want libyaml-style under-indented flow
continuations while still enforcing unique mapping keys across compatibility tape
and typed readers can opt into
compat::ParseOptions::new().with_allow_duplicates(false):
use std::collections::BTreeMap;
use ayaml::{ErrorCode, compat};
let opts = compat::ParseOptions::new().with_allow_duplicates(false);
// Under-indented flow continuation is accepted in compat mode...let flow: BTreeMap<String, BTreeMap<String, Vec<String>>> = compat::from_str_with("a:\n b: [c,\n d]\n", &opts)?;assert_eq!(flow["a"]["b"], vec!["c", "d"]);
// ...while duplicate mapping keys are rejected with ErrorCode::DuplicateKey.let err = compat::from_str_with::<BTreeMap<String, u64>>("k: 1\nk: 2\n", &opts).unwrap_err();assert_eq!(err.code(), ErrorCode::DuplicateKey);compat::parse_events, compat::parse_events_with, compat::parse_events_in,
and compat::parse_events_with_in return the same lazy EventStream as default
mode (with optional ParserBuf scratch reuse via EventStream::recycle_into);
compat::parse_events_eager_with keeps the whole-stream vector used by
the conformance runner. These event readers report aliases and syntax tags as
written and do not check aliases against earlier anchors; the compatibility
tape and typed value readers (compat::parse_tape*, compat::parse_tape_documents*,
compat::parse_owned_tape*, compat::parse_owned_tape_documents*, compat::from_str*,
compat::deserialize_any*) resolve aliases and honor limits, allow_duplicates,
and global_tags. The legacy 0.x *_with_limits shims were removed
for 1.0 (D109).
libyaml counts a column for a byte-order mark at the start of a line, so in
compatibility mode a --- right after one, or a ... followed by more text on
its line, is text, not a marker. A ... that ends its line is a marker, as in
default mode, so \u{feff}...\n...\u{2028} reads the second line’s text. On a
later line, the mark’s column also counts for block structure, so a block
collection after it is one column deeper. The stream’s first mark takes up no
column, as when libyaml detects the encoding itself, unless a marker follows it.
Then the marker is text, and the line reads like a later line. A run of marks at
the start of the stream is skipped as one mark would be; a later mark in the run
is never content, and a --- after the run is still text in compatibility mode.
A closing ], }, or quote may sit at any indentation in both modes, including
under a block collection, where the grammar asks for indentation, and after tabs alone.
libyaml accepts these too, but for a tab right after a plain scalar’s line, as in
k: [a\n\t], and files commonly close a flow collection this way:
key: [ a,]Only a quoted scalar’s own quote closes it there, so in k: "a\n\t'" the line is
indented by a tab alone, which is an error.
By default, any other line of a flow collection under a block collection is indented
past the innermost one, whether a plain scalar goes on there or an entry starts there,
so k: [a\nb] and a:\n b: [c,\n d] are errors. Compatibility mode reads such
lines, as libyaml does, except where an implicit flow key’s : sits on one, as in
k: {x\n: y}, or only a tab indents a line that a plain scalar goes on to, as in
k: [a\n\tb]. libyaml refuses those too. It reads an explicit key’s : there, as in
k: {? x\n: y}, and so does compatibility mode.
Compatibility mode isn’t an exact copy of libyaml. It follows YAML 1.2.2 instead in these places, on purpose:
- NEL, LS, and PS are content; libyaml reads them as line breaks. Comments run to a
physical line ending in both modes, including a comment after a flow
?. - A core-schema tag on a node of another kind is dropped, as libyaml drops it. Default mode
drops it too, so
!!str {}and!!seq aread as{}andain both modes. - A byte-order mark past the start of the stream behaves as described above.
- Anchor and alias names may hold any
ns-anchor-char, where libyaml takes only alphanumerics,_, and-, so&a: bdiffers. As in libyaml, a?ends a name when text follows it; a trailing?before white space, a line break, a flow indicator, or the end (or before NEL, LS, or PS outside a flow collection) stays in the name, as default mode reads it. Outside a flow collection, a:followed by text or a flow indicator ends a name, so&a:bis the string:bhere and&r:}is:}; a:before NEL, LS, or PS stays, as default mode reads it. - As in libyaml, an alias directly followed by
:is a mapping key, so*a: 1has the key thatastands for, in a flow collection too. Default mode readsa:as the name, as YAML 1.2.2 says, so there it’s an unknown anchor unless an anchor has that name. Compat keeps the colon in one case: an anchor earlier in the same document has that exact name, as in&a: xthen*a:.*a : 1means the same in both modes. In a flow collection a name runs on through a:with more text after it, so{*a:b}looks for an anchor nameda:b, where libyaml reads the key thatastands for and the valueb. Only a JSON-like key takes a value right after its:there, so{*a:[1]}is an error, where libyaml reads the value[1]. - Global, verbatim, and non-specific tags are kept by default, with their escapes as written;
compatibility callers can opt into dropping non-core global tags with
compat::ParseOptions::new().with_global_tags(compat::GlobalTags::Drop). Tag forms that YAML 1.2.2 allows and libyaml’s scanner refuses are accepted, such as!#,!<tag:a,1:b#c>, or!right before a flow indicator.!isn’t a tag character, so!.!and!!!are errors, except after an unseparated flow?, where compat keeps libyaml’s reading for non-word local tags and!!suffixes holding!(and!@!anywhere). A tag needs a name, so!<!>is an error too, where libyaml reads the non-specific tag. A verbatim tag names a local tag or a tag with a URI scheme, and each%in it starts a two-digit escape, so!<foo>,!<]>, and!<tag:x:50%>are errors. - In a flow collection,
[:a]holds a plain scalar starting with:,a:]holds a key, anda:?is a plain scalar.{?:{}}is an error because?:starts a plain scalar, and{?\n:{}}because only a JSON-like key allows an adjacent value. A flow mapping key may span lines or have a line break before its:.[-]is an error, and so is a?right before a flow indicator, as in{?}or[?[a]], where libyaml reads an empty key or the key[a]. A?followed directly by!is a plain scalar in default mode; compat reads the?as an explicit key and reads non-word local tags such as!@!or!)!and!!suffixes holding!such as!!!or!!a!bas libyaml does. A flow?followed by-and a flow indicator reads as an explicit key whose key is the scalar-, as in[?-]or{?-}; a comment may follow the?. - An implicit key in a flow mapping may run past 1024 characters, as YAML 1.2.2 allows, where
libyaml refuses it. Elsewhere an implicit key takes at most 1024, as in libyaml. The writers use
?followed by a space for a key past 128 characters, as libyaml’s emitter does. - An empty implicit key, such as
: a, is valid. - Tabs are separation wherever they aren’t indentation, as in
key:\t"a: b","a"\t: 1, or-\t[1]. Only spaces indent. A tab stands where indentation would before a block indicator or an implicit key, at a line’s start or after a-,?, or:indicator,---, or..., so\ta: 1and-\t- bare errors. A node that only tabs indent on its own line isn’t indented under the key or entry before it, sok:\n\tvis an error, as in libyaml. A flow collection’s or quoted scalar’s lines under a block collection need space indentation too, soa: [\n\tb]is an error. A tab on an empty line after a plain scalar is separation, as ink: a\n\t\nj: b, where libyaml refuses it. - A top-level block scalar may hold content at column 0. A
#line past the scalar’s parent and indented less than a blank line before it is that scalar’s first content, so the blank line is too long and it’s an error, where libyaml ends the scalar there. - A root block scalar’s explicit indentation indicator counts from the root’s -1, so
|1\n y\nkeeps its space in default mode, as productions 170 and 174 say. Compat counts from 0, as libyaml does, and readsy\n. - A block scalar or a quoted scalar’s continuation must be indented past its parent, so
a:\n|anda: "b\nc"are errors. A block scalar under a key with an anchor may sit at the parent’s column, as in&a::\n|, which libyaml reads as the key:with an empty value. That holds for a quoted line that the other kind of quote or''starts, as ink: "a\n'b". By default, after a compact key or entry the parent is the collection that it starts, so- k: 'a\n b'is an error. Compatibility mode measures those lines against the line’s indent, as libyaml reads files that align them with the key. - A byte-order mark at the start of a key’s line doesn’t tighten a quoted scalar below it: the mark’s column counts for block structure but not for the quoted scalar’s own continuation check, as libyaml has no such check.
- A comment must be separated from what precedes it, so
""#and|#are errors. - An unknown directive is ignored, and a stream may start with
.... - Integers are exact up to 128 bits.
- A quoted scalar may hold any
nb-jsoncharacter, such as DEL.
The maintained deviation list is
tests/data/yaml-test-suite-deviations.txt.
Each entry names a YAML Test Suite case, the affected mode and check, and the
reason. Compatibility-mode deviations are intentional; default-mode entries
are treated as bugs.
Error behavior
Section titled “Error behavior”Parsing errors use stable categories:
- invalid encoding;
- scanner/token error;
- grammar/parser error;
- unknown, cyclic, or over-budget alias;
- duplicate key;
- tag or scalar-resolution error;
- resource-limit violation;
- composition/value error;
- Serde type/data error; and
- emitter or I/O error.
When input provides a location, the error carries a span and Error::location()
returns the one-based line and column. Error::code() returns a stable ErrorCode
(yaml.<kebab-name>) preserved across compatibility kind remappings, and
Error::masked() formats kind, code, and location without document text; see
error-codes.md. Resource-limit failures expose the
configured ceiling and observed value through Error::limit(), with the exceptions
in limits.md.
A typed error’s path is written the same way in both modes, as Error::message
describes: a key that isn’t a short name is quoted in brackets, as in
ports["80"] or ["a.b"], where readers built on libyaml write ports.80 and
a.b.
Scope boundaries and ecosystem tradeoffs
Section titled “Scope boundaries and ecosystem tradeoffs”ayaml’s 1.0 design makes explicit tradeoffs to keep its core parser
zero-copy, predictable in memory, and strictly #![forbid(unsafe_code)] with
only serde and indexmap as runtime dependencies (see D113 in
decisions.md):
-
UTF-8-native inputs and byte spans: All parsers operate on valid UTF-8 (
&str,&[u8], orRead), so borrowed strings andSpan::slice(&str)stay zero-copy and O(1). Editor protocols that require UTF-16 column coordinates are served byLineIndex(ColumnEncoding::Utf16). Inputs encoded in UTF-16 or UTF-32 (YAML 1.2 section 5.2) should be decoded to UTF-8 at the I/O boundary before parsing rather than remapping byte offsets insideSpan. -
Comment-free and warning-free default hot paths with opt-in comment capture: Default parsing (
parse_events,parse_tape,parse_value,from_str, andSpannedTree::parse) skips#comments without allocating trivia nodes and does not collect advisory warning lists. Callers that need exact comment byte spans (for linters, formatters, or source-aware tools) can opt in withscan_comments/scan_comments_withorSpannedTree::parse_with_comments(SpannedTree::comments(&self) -> &[Span]), which enforceLimits::max_eventson the captured comment count while leaving default parse paths and node sizes unaffected. Callers migrating externally tagged enums or explicit YAML tags can likewise useayaml::with::singleton_mapandayaml::Tagged<T>without touching default hot paths:use ayaml::{ParseOptions, Spanned, SpannedTree, Tag, Tagged, from_str, scan_comments, to_string};use serde::{Deserialize, Serialize};#[derive(Debug, PartialEq, Serialize, Deserialize)]enum Endpoint {Local,Remote(String),}#[derive(Debug, PartialEq, Serialize, Deserialize)]struct Settings {#[serde(with = "ayaml::with::singleton_map")]endpoint: Endpoint,secret: Tagged<String>,}let yaml = "# service settings\nendpoint:\n Remote: https://example.com/#frag\nsecret: !Env API_TOKEN # inline\n";let comments = scan_comments(yaml)?;let tree = SpannedTree::parse_with_comments(yaml, &ParseOptions::new())?;assert_eq!(comments, tree.comments());let texts: Vec<&str> = comments.iter().filter_map(|s| s.slice(yaml)).collect();assert_eq!(texts, vec!["# service settings", "# inline"]);let cfg: Settings = from_str(yaml)?;assert_eq!(cfg.endpoint, Endpoint::Remote("https://example.com/#frag".into()));assert_eq!(cfg.secret, Tagged::new(Tag::local("Env"), "API_TOKEN".into()));let emitted = to_string(&cfg)?;assert_eq!(from_str::<Settings>(&emitted)?, cfg);let spanned_tag: Spanned<Tagged<u16>> = from_str("!Port 8080\n")?;assert_eq!(spanned_tag.tag, Tag::local("Port"));assert_eq!(spanned_tag.value, 8080); -
Comparison with newer pure-Rust YAML 1.2 stacks: Pure-Rust YAML 1.2 parsers make different architectural bets. For example,
astral-yaml(0.0.8at evaluation time) focuses on single-pass spanned arena parsing with inline comments, UTF-16/UTF-32 source decoding, flow-only Serde serialization, and cursor-based editing, whereasayaml 1.0prioritizes SemVer API stability, O(1) streamingparse_events, flat-arrayTape+ParserBufreuse, lower peak heap across DOM and spanned workloads, general block and flow emission, opt-in merge keys,libyaml-compat, Markdown frontmatter, and conservative double-verified editing inayaml-edit. Seebenchmarks.mdfor reproducible comparative speed, peak-heap, allocation, and emission data; a dedicated migration guide fromastral-yamlis deferred until its API stabilizes beyond0.0.x.