oneyaml
serde and lossless, comment-preserving editing for YAML 1.2, in one
crate. Both run on one parser and one interpretation of the file, so a
typed read and an edit never disagree about what it means. Correct by
default, no unsafe, no panics on any input.
How many times fewer CPU instructions oneyaml executes than each
competitor (the fastest feature-complete serde library for serde reads
and writes, yaml-edit for Document parsing and a three-edit script),
on five workloads shaped like real configuration files (higher is
better; hatched bars are workloads where oneyaml is slower):
Full results, with wall-clock times and the method →
Why oneyaml
- One parser, one meaning. serde and the lossless
Documentread the same events through the same resolver, so a typed read and an edit never disagree about what a file means; a test checks that they agree on every corpus file (consistency.rs). - Edits that keep your file.
Document::parse(src)?.to_string() == src, byte for byte, on every valid file of the yaml-test-suite (round trip). An edit changes only what you touch: every comment stays with the entry it belongs to, by numbered rules, and new content is laid out like its neighbours. - Correct YAML 1.2.2 by default. The Core schema reads
yesas a string and0777as a decimal integer, as the spec says; the Failsafe and JSON schemas are built in. Every YAML 1.1 behaviour, merge keys included, and every leniency is a named setting, off by default (known deviations). yaml-test-suitedata-2022-01-17, 402 cases: 402/402 under the standard criterion (valid 308/308, error 94/94), 401/402 strict (exact events, error prefixes and codes), and 308/308 valid inputs round-trip byte for byte (evidence, other Rust parsers' scores and how to reproduce). - Safe on any input. Every published crate is
#![forbid(unsafe_code)]. Every input gives a value or an error, every fallible API returnsResultorOption, and parsing is iterative, so deep nesting can't overflow the stack. Every read enforces resource limits; for input from the network, useLimits::untrusted(). Parsing, serde, editing, comments and diagnostics are fuzzed (crates/fuzz) and tested againstserde_yaml_ng, saphyr, yaml-rust2, libyaml and yaml-edit. - Errors you can act on. Every error has a stable
Y####code, a span and a path, and common mistakes come with a suggested fix (diagnostics). - Fast, with the losses shown. oneyaml executes fewer instructions
than each competitor on most workloads in the chart above, and an
edit's cost doesn't grow with the file. It isn't faster everywhere: a
typed read of the OpenAPI workload and edits of the smallest file take
more instructions, and in wall-clock time its typed reads are slower
on most of these workloads,
Documentparsing is level or slightly slower on some, and yaml-edit edits small files faster (every loss).
The only runtime dependency by default is serde
(feature flags). How oneyaml compares with
serde_yaml_ng, serde_norway, serde-saphyr and yaml-edit, including
where they win:
docs/comparison.md.
Install
Quick start
Read and write typed data with serde, then edit a document in place: the edit changes only what you touched, and the comment stays.
use Document;
use ;
Diagnostics
An error says what went wrong, where, and how to fix it. Display is one
line for logs; Error::render
prints a rustc-style snippet of your source with a stable Y#### code,
labels, help, the path to the value, and a suggested fix you can apply:
use Deserialize;
A fix is marked machine-applicable only when applying it can't change
what the rest of the file means; any other fix, like the two above, is
labelled for review. Codes are never reused, and limit errors name the
setting to raise.
Document::parse_recovering
keeps going past the first error, so you see a file's errors together,
and the
miette feature plugs errors into miette's reports. More on
diagnostics on Error.
When not to use oneyaml
- You need the fastest typed reads in wall-clock time. oneyaml's
serde reads use fewer instructions on most workloads, but in
wall-clock time they are slower than
serde_yaml_ngorserde_norwayon most of the benchmark workloads (serde read wall-clock). - You make a few edits to many small files and edit speed matters. Below a few kilobytes, yaml-edit's edits cost fewer instructions (compose-merge in the chart above; see the benchmark tables).
- You need something oneyaml doesn't do:
no_std, async I/O, schema validation, templating, building objects from tags, or editing a file too large to hold in memory (Documentkeeps the whole tree; serde reads don't). - You want
serde_yaml's exact behaviour with no review.Compat::serde_yaml()gets close, but some differences are deliberate; the migration guide lists every one. - You need a long production track record.
serde_yaml's lineage has years of it; oneyaml is new.
Untrusted input
Every entry point enforces resource limits (depth, alias expansion,
document size, scalar length, document count). The default profile suits
configuration files; for input from the network, use
Limits::untrusted():
use ;
There are three profiles: Limits::default(), Limits::untrusted() and
Limits::unlimited(), the only one without alias budgets (for input you
trust completely). Exceeding a limit is an error whose help names the
setting to raise. The alias budgets count the nodes and bytes that alias
expansion produces beyond the input, and an alias whose whole expansion
would exceed a budget is refused before it is replayed, so "billion
laughs" fails fast. Document never expands aliases, and compatibility
presets never change limits. The values of each profile and how the
budgets are counted are on
Limits.
Merge keys (<<)
YAML 1.2 has no merge keys: by default << is an ordinary string key,
as the spec says. docker-compose files and many CI configurations rely on
the YAML 1.1 merge-key extension; turn it on with one setting:
use ;
Compat::yaml_1_1() turns on every YAML 1.1 behaviour, merge keys
included; Compat::serde_yaml() leaves them off, as serde_yaml's
deserializer does. Merges through aliases count toward the alias budget.
Document keeps << entries as written and edits them like any other
entry; examples/compose_merge.rs
edits a docker-compose file that uses them. Details:
Settings::merge_keys.
Going further
The API reference on docs.rs has a worked, tested example for each task, and the guides in the repository cover the rest:
- Read and edit a document.
Documentwalks the core path (parse, look up, edit, write) and lists what an edit refuses;NodeRefcovers typed reads, spans anddeserialize;NodeMutcovers edits in depth, andRemovedcovers moving an entry. Paths are strings ("spec.containers[0].image") orpath!values; the syntax, and how to quote a key that holds a., are onIntoPath. - Keep and edit comments. Each comment has an owner, set by the
comment attachment rulebook;
edits move comments with their owner, and
explain_commenttells you which rule decided. See comments onDocument. - Multi-document files, anchors and aliases. A file with several
documents is a
Stream, read and edited one document at a time (or iterated with serde). Editing an anchored node is seen through every alias; see anchors and aliases. - Diagnostics and recovering parses. Every error has a
Y####code (code), a span, a path and, for common mistakes, a fix you can apply; see diagnostics onErrorandRender.Document::parse_recoveringnever fails, and you can still read and edit everything outside the broken regions. - Serialize and configure.
to_stringandto_writerwrite anySerialize, quoting every string that some reader would read as something else. Every entry point has a*_withtwin that takes an explicitSettings(there are no globals): the schema, YAML 1.1 behaviours, duplicate-key handling, output layout (Emit) and resource limits (Limits). - Migrate from
serde_yaml. The entry points keepserde_yaml's names (from_str,from_slice,from_reader,to_string,to_writer,from_value,to_value,Value,Mapping,Deserializer,Serializer), so migrating starts with auseswap, andCompat::serde_yaml()reads and writes asserde_yamldoes where YAML 1.2 allows. The migration guide walks through it and lists every remaining difference with its reason. - Performance in detail. Results at a glance has the numbers behind the chart as tables (absolute instruction counts, both ratios, wall-clock time, every workload where oneyaml is slower and the machine they were measured on); the rest of docs/performance.md covers every workload, memory, scaling and how to reproduce it all.
- Real-world programs.
examples/starts withedit_config.rs(the quick start as a program), then edits a Kubernetes manifest, a GitHub Actions workflow and a docker-compose file with merge keys, checking every comment's owner before and after. Run one withcargo run -p oneyaml --example k8s_manifest.
Feature flags
All are off by default; without miette and serde_json, the only
third-party runtime dependency is serde.
miette:Errorimplementsmiette::Diagnostic(code, severity, labels, help with the suggested fixes, and a documentation URL per code);DiagnosticReportdoes the same for aDiagnostic. Addsmiette7 without its default features (it bringscfg-ifandunicode-width).serde_json:TryFromconversions betweenValueandserde_json::Valuein both directions, andValue::to_json_with. What has no JSON form (tags, non-string keys,.nan/.inf, integers beyond 64 bits, nesting past 128 levels) is an error with its path, never a silent coercion; the full table is onValue::to_json_with. Addsserde_jsonwith only itsstdfeature.__internal-checks(unstable, not covered by semver): test-only consistency checks used by this crate's own test suites and fuzzers. Never enable it in a library or application.
Minimum supported Rust version
Rust 1.85 (edition 2024), for oneyaml, oneyaml-core and
oneyaml-syntax. The MSRV is rolling: it may move to any stable release
at least 12 months old, only in a minor release, and every move is
announced in the
changelog.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in oneyaml by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. See CONTRIBUTING.md and SECURITY.md.