fast-yaml-core
Core YAML 1.2.2 parser and emitter for the fast-yaml ecosystem.
[!NOTE] This crate provides three distinct components: Parser (YAML → data), Emitter (data → YAML), and Streaming Formatter (events → YAML, no DOM).
Components
Parser
Purpose: Deserialize YAML text into Rust data structures (DOM).
When to use:
- Need to manipulate YAML data programmatically
- Building APIs that consume YAML config
- Validating YAML structure
Data flow: YAML text → Value (DOM)
Emitter
Purpose: Serialize Rust data structures back to YAML text.
When to use:
- Generating YAML from code
- Config file generation
- Data export
Data flow: Value (DOM) → YAML text
Configuration: EmitterConfig allows customizing indent, line width, flow style, etc.
Streaming Formatter (feature: streaming)
Purpose: Format YAML directly from parser events without building DOM.
When to use:
- Formatting large files (faster, less memory)
- CLI tools (convert, format, validate)
- Processing YAML streams
Data flow: Parser events → YAML text (zero-copy)
Advantages:
- ⚡ 2-3x faster than parse + emit
- 📉 O(1) memory vs O(n) for DOM
- 🎯 Ideal for batch operations
[!TIP] Use Streaming Formatter for CLI batch mode formatting. Use Parser + Emitter when you need to modify YAML data.
Installation
Add to your Cargo.toml:
[]
= "0.3"
Or with cargo-add:
[!IMPORTANT] Requires Rust 1.88 or later.
Usage
Parser: YAML → Data Structures
use ;
// Parse single document
let yaml = "name: test\nvalue: 123";
let doc = parse_str?;
// Parse multiple documents
let yaml = "---\nfoo: 1\n---\nbar: 2";
let docs = parse_all_str?;
# Ok::
Emitter: Data Structures → YAML
use ;
// Basic emission
let value = Value;
let yaml = emit_str?;
// Custom configuration
let config = new
.with_indent
.with_width
.with_explicit_start;
let emitter = new;
let yaml = emitter.emit_str?;
# Ok::
Streaming Formatter: Events → YAML (no DOM)
use ;
# Ok::
[!TIP] Enable the
streamingfeature for formatter:fast-yaml-core = { version = "0.4", features = ["streaming"] }
YAML 1.2.2 Compliance
This library implements the YAML 1.2.2 specification with the Core Schema:
| Type | Supported Values |
|---|---|
| Null | ~, null, empty |
| Boolean | true/false (lowercase only per YAML 1.2 Core Schema) |
| Integer | Decimal, 0o octal, 0x hex |
| Float | Standard, .inf, -.inf, .nan |
| String | Plain, single/double-quoted, literal (|), folded (>) |
[!NOTE] YAML 1.1 booleans (
yes/no/on/off) are treated as strings per YAML 1.2.2 spec.
Features
| Feature | Description | Use Case |
|---|---|---|
streaming |
Event-based formatting without DOM | CLI tools, large file processing |
arena |
Arena-based memory allocation | High-performance parsing |
# Enable streaming formatter
= { = "0.4", = ["streaming"] }
# Enable arena allocation
= { = "0.4", = ["arena"] }
# Enable both
= { = "0.4", = ["streaming", "arena"] }
[!TIP] The
arenafeature provides 10-15% faster parsing for large documents by reducing allocator overhead.
Related Crates
This crate is part of the fast-yaml workspace:
fast-yaml-linter— YAML linting with rich diagnosticsfast-yaml-parallel— Multi-threaded YAML processing
License
Licensed under either of Apache License, Version 2.0 or MIT License at your option.