Expand description
Side-table CST (concrete syntax tree) for lossless round-tripping.
This module is the implementation of the design described in
docs/design/green-tree.md. It exposes a Document type that
parses YAML byte-faithfully — every byte of the input is retained
as a green-tree leaf — so that
parse_document(s).unwrap().to_string() is byte-identical to s
for any input the parser accepts.
The Document API depends on the parser’s SpanTree, which lives
under the std feature.
The Value API (from_str, to_string, StreamingDeserializer)
is unchanged. Trivia capture is enabled only on this path; the
fast path pays no extra cost.
§Current scope
-
Read access.
Document::as_valuefor a typed view,Document::span_at/Document::getfor byte-range lookups bypath, andDocument::syntaxfor the green tree itself. -
Mutation.
Document::replace_span(primitive byte replacement) andDocument::set(path-targeted, the wrapper most callers want). Both re-parse on edit and reject the change if the spliced source is invalid YAML, leaving the document untouched.Document::editcreates anEditSessionthat batches independent byte-range replacements and validates the assembled result once. -
Comments.
Document::comments_atclassifies the comments decorating a node into aCommentBundle, andDocument::set_comment/Document::remove_commentwrite them back, addressed byCommentPosition. A leading block is written at the node’s own indentation. Both go throughreplace_span, so they inherit its guard. -
Auto-formatting.
Emitturns a typed value into the YAML spelling that re-parses to exactly that value at a given site, soDocument::insert_entry_value,Document::push_back_valueandDocument::insert_after_valuequote and escape what the fragment-taking mutators splice verbatim.
The green tree itself is still a flat sequence of leaves under a
single Document parent — sufficient for byte-faithful
round-tripping and for the span-based edit primitive. Hierarchical
nesting (per-mapping / per-sequence parent nodes) is tracked as a
follow-up in docs/design/green-tree.md.
§Examples
use noyalib::cst::parse_document;
let src = "name: noyalib # the project\nversion: 0.0.1\n";
let doc = parse_document(src).unwrap();
assert_eq!(doc.to_string(), src);§Parser configuration
parse_document and
parse_stream run under the default
ParserConfig, the same limits as
from_str.
parse_document_with_config
and parse_stream_with_config
take one, mirroring from_str_with_config;
the returned Document keeps it for every
later re-parse of its own source, so an edit never falls back to
the defaults.
§Multi-document streams
Use parse_stream for inputs
containing --- / ... separators — one
Document per logical YAML document,
with each slice covering the exact bytes of that document so
concatenation reproduces the input verbatim:
use noyalib::cst::{parse_stream, Document};
let src = "---\nfoo: 1\n...\n---\nbar: 2\n";
let docs = parse_stream(src).unwrap();
assert_eq!(docs.len(), 2);
assert_eq!(docs[0].as_value()["foo"].as_i64(), Some(1));
assert_eq!(docs[1].as_value()["bar"].as_i64(), Some(2));
let joined: String = docs.iter().map(Document::source).collect();
assert_eq!(joined, src);Structs§
- Alias
Info - A
*namealias reference discovered in the document source. - Anchor
Info - An
&nameanchor declaration discovered in the document source. - Comment
Bundle - Comments that decorate a single node, organised by their position relative to the node.
- Document
- A YAML document with byte-faithful source preservation, typed data access, and path-targeted edits.
- Edit
Session - A deferred, atomic batch of byte-range edits to a
Document. - EmitCtx
- The site an emitted fragment will be spliced into.
- Entry
- A path-shaped mutable handle to a node in a
Document. - Format
Config - Configuration for the formatter.
- Green
Node - An immutable, byte-faithful syntax-tree node.
Enums§
- Comment
Position - Where a comment sits relative to the node it decorates.
- Green
Child - A leaf-or-node child of a
GreenNode. - Repair
Scope - The scope at which the most recent edit was repaired.
- Syntax
Kind - Kind of a green-tree node or token.
Traits§
- Emit
- A value that can be spliced into a document by the auto-formatting mutators.
Functions§
- coerce_
to_ schema - Apply schema-driven type coercions to
doclosslessly: only the bytes of the coerced scalars are rewritten; comments, indentation, and sibling entries survive byte-faithfully. - format
- Auto-formats a messy YAML file into a canonical style based on the CST.
- format_
with_ config - Auto-formats a messy YAML file into a canonical style based on the CST, using the provided configuration.
- format_
with_ parser_ config - Auto-formats a YAML stream under the given formatter and parser configuration.
- parse_
document - Parse a YAML stream into an editable
Document. - parse_
document_ with_ config - Parse one YAML document into an editable
Documentunderconfig, mirroringcrate::from_str_with_config. - parse_
stream - Parse a YAML stream and return one
Documentper logical document. - parse_
stream_ with_ config - Parse a YAML stream into one
Documentper logical document underconfig, mirroringcrate::load_all_with_config.