yaml_lib 0.2.0

YAML library implementation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# YAML_lib ๐Ÿฆ€


[![Rust](https://img.shields.io/badge/rust-1.88.0+-orange.svg)](https://www.rust-lang.org)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

![Tests](https://img.shields.io/badge/internal_tests-1014%20passing-green.svg)
![YAML 1.2](https://img.shields.io/badge/YAML_1.2-100%25%20(402%2F402)-brightgreen.svg)

A comprehensive, high-performance YAML library for Rust with full YAML 1.2 specification compliance (100%, 402/402 tests passing as of Mar 2026), excellent ergonomics, advanced error handling, validation, and extensive format conversion capabilities.

## โœจ Features



### ๐Ÿ… **Core YAML Support**

- **100% YAML 1.2 specification compliance** (402/402 tests passing)
- **Unicode-aware parsing** with BOM detection
- **Multi-document streams** support
- **Anchors and aliases** including on mapping keys, with circular reference detection
- **Block and flow syntax** parsing with empty key support
- **Cross-platform line endings** (Unix LF, Windows CRLF)
- **Comments preservation** throughout parsing
- **All scalar types**: strings, integers, floats, booleans, null
- **All collection types**: sequences, mappings, sets


### ๐Ÿท๏ธ **Advanced Tag Support**

- **Standard YAML tags**: `!!str`, `!!int`, `!!float`, `!!bool`, `!!null`
- **Collection tags**: `!!seq`, `!!map`, `!!set`
- **Binary data**: `!!binary` with base64 validation
- **Ordered mappings**: `!!omap` for insertion-order preservation
- **Key-value pairs**: `!!pairs` with duplicate key support
- **Merge keys**: `!!merge` for YAML inheritance
- **Numeric bases**: hexadecimal (`!!int:hex`) and octal (`!!int:oct`)
- **Custom tags**: preservation and round-trip support


### ๐Ÿ”„ **Multi-Format Conversion**

- **YAML** โ†” Native format with pretty-printing
- **JSON** โ†” With pretty-printing support
- **XML** โ†” With configurable formatting
- **TOML** โ†” With table structure preservation
- **Bencode** โ†” For BitTorrent applications


### ๐Ÿš€ **Performance, Safety & Validation**

- **Zero-copy parsing** where possible
- **Memory-efficient** node representation
- **Thread-safe** operations
- **Error recovery** and detailed diagnostics
- **Comprehensive test suite** (1014+ internal tests, 402/402 YAML 1.2 official tests, 100% pass rate)
- **JSON Schema-style validation** for YAML documents
- **Error codes and suggestions** for programmatic error handling


### ๐Ÿ”จ **Fluent Builder API**

- **`ArrayBuilder`** - Chainable API for constructing sequences
- **`MappingBuilder`** - Chainable API for constructing mappings
- **`SetBuilder`** - Chainable API for constructing sets
- **`make_node!` macro** - Concise literal syntax for node construction


### ๐Ÿ” **Developer Tools**

- **Node inspection** (`NodeDebugger`, `node_type`, `node_depth`, `print_tree`)
- **Node diffing** (`diff_nodes`, `DiffResult`) for comparing document trees
- **Execution tracing** (`Tracer`, `TraceGuard`) for debugging
- **Debug assertions** (`DebugAssert`, `DebugContext`) with configurable levels


### ๐Ÿงช **Testing Infrastructure**

- **Fuzz testing** (`YamlFuzzer`, `fuzz_parse`, `fuzz_roundtrip`) for robustness
- **Property-based testing** (`PropertySuite`) for invariant checking
- **Memory safety auditing** (`SafetyAudit`, `audit_node`) for correctness


### โšก **Optimization Utilities**

- **`CapacityHints`** for pre-sizing allocations
- **`FastPathDetector`** for common YAML pattern shortcuts
- **`NodeBuilder`** with allocation reuse
- **`StringInterner` / `SimpleInterner`** for string deduplication
- **`InternedString`** / `CommonStrings`** for zero-cost key sharing

## ๐Ÿ›ก๏ธ Error Handling & Validation


### Centralized Error Handling (Contributor Note)


All parser and lexer error messages must use the centralized helpers in `parser/document/error_builder.rs` (e.g., `syntax_error`, `structure_error`, `limit_error`, `forbidden_error`).

**Do not return raw error strings.**

This ensures all errors are consistent, include context, and are easy to maintain. See the module-level docs in `error_builder.rs` for usage examples and extension guidelines.


### Error Handling

- **Error codes (E001-E015)** for programmatic handling
- **Intelligent suggestions** for fixing common mistakes
- **Error recovery strategies** to continue parsing after errors
- **Enhanced error context** with source spans and snippets
- **Multi-error collection** for batch reporting

### Validation

- **JSON Schema-style validation** for YAML documents
- **Type checking, constraint validation, and schema enforcement**
- **Built-in validators**: type, range, length, pattern, enum, required, custom
- **Comprehensive validation examples** in `examples/yaml_validation/`

## ๐Ÿ“ฆ Installation


Add this to your `Cargo.toml`:

```toml
[dependencies]
yaml_lib = "0.2.0"
```

## ๐Ÿš€ Quick Start


### Basic Parsing


```rust
use yaml_lib::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Parse YAML from a string (convenience function)
    let yaml_str = r#"
    name: "YAML Library"
    version: 1.0
    features:
      - parsing
      - serialization
      - multi-format
    "#;
    
    let document = parse_string(yaml_str)?;
    println!("{:#?}", document);
    
    // Or parse from a file directly
    // let document = parse_file("config.yaml")?;
    
    // Or use the lower-level source-based API
    let mut source = BufferSource::new(yaml_str.as_bytes());
    let document = parse(&mut source)?;
    println!("{:#?}", document);
    Ok(())
}
```

### Working with Nodes


```rust
use yaml_lib::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create nodes programmatically
    let config = make_node! {
        "database" => {
            "host" => "localhost",
            "port" => 5432,
            "ssl" => true
        },
        "servers" => ["web1", "web2", "web3"]
    };
    
    // Convert to different formats
    let mut buffer = BufferDestination::new();
    
    // To YAML
    stringify(&config, &mut buffer)?;
    println!("YAML:\n{}", buffer.to_string());
    buffer.clear();
    
    // To JSON
    to_json_pretty(&config, &mut buffer)?;
    println!("JSON:\n{}", buffer.to_string());
    buffer.clear();
    
    // To XML
    to_xml_pretty(&config, &mut buffer)?;
    println!("XML:\n{}", buffer.to_string());
    
    Ok(())
}
```

### File Operations


```rust
use yaml_lib::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Read from file with automatic encoding detection
    let content = read_file_to_string("config.yaml")?;
    let mut source = BufferSource::new(content.as_bytes());
    let document = parse(&mut source)?;
    
    // Write to different formats
    let mut json_dest = FileDestination::new("output.json")?;
    to_json_pretty(&document, &mut json_dest)?;
    
    let mut xml_dest = FileDestination::new("output.xml")?;
    to_xml_pretty(&document, &mut xml_dest)?;
    
    Ok(())
}
```

### Advanced Features


```rust
use yaml_lib::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Working with tags and anchors
    let yaml_with_tags = r#"
    binary_data: !!binary |
      R0lGODlhDAAMAIQAAP//9/X17unp5WZmZgAAAOfn515eXvPz7Y6OjuDg4J+fn5
      OTk6enp56enmlpaWNjY6Ojo4SEhP/++f/++f/++f/++f/++f/++f/++f/++f/++Q==
    
    ordered_map: !!omap
      - first: 1
      - second: 2
      - third: 3
    
    config: &default_config
      timeout: 30
      retries: 3
    
    development:
      <<: *default_config
      debug: true
    
    production:
      <<: *default_config
      debug: false
    "#;
    
    let mut source = BufferSource::new(yaml_with_tags.as_bytes());
    let document = parse(&mut source)?;
    
    // Access specific documents in multi-doc streams
    let base_doc = get_document(&document, 0)?;
    
    Ok(())
}
```

## ๐Ÿ“š API Reference


### Core Types


- **`Node`** - The fundamental data structure representing any YAML value
- **`Numeric`** - Enum for integer and floating-point numbers
- **`BufferSource`/`FileSource`** - Input sources for parsing
- **`BufferDestination`/`FileDestination`** - Output destinations for serialization

### Key Functions


| Function | Description |
|----------|-------------|
| `parse()` | Parse YAML from any source into a Node tree |
| `parse_string()` | Parse YAML from an in-memory `&str` (default config) |
| `parse_file()` | Parse YAML from a file path (default config) |
| `parse_with_config()` | Parse YAML from a string with a `ParserConfig` |
| `parse_string_with_recovery()` | Parse with error recovery, returns `(Node, Vec<YamlError>)` |
| `stringify()` | Convert Node tree back to YAML format |
| `to_json()` / `to_json_pretty()` | Convert to JSON format |
| `to_xml()` / `to_xml_pretty()` | Convert to XML format |
| `to_toml()` / `to_toml_pretty()` | Convert to TOML format |
| `to_bencode()` | Convert to Bencode format |
| `make_node!` | Macro for creating nodes with literal syntax |
| `make_set()` | Create set nodes with duplicate removal |

### File Utilities


| Function | Description |
|----------|-------------|
| `read_file_to_string()` | Read file with automatic encoding detection |
| `write_file_from_string()` | Write file with specified encoding |
| `detect_format()` | Detect Unicode format from BOM |

## ๐ŸŽฏ Examples


The repository includes 23 comprehensive examples:

| Example | Description |
|---------|-------------|
| [yaml_parse_and_stringify]../examples/yaml_parse_and_stringify/ | Basic parsing and serialization |
| [yaml_to_json]../examples/yaml_to_json/ | YAML โ†’ JSON conversion |
| [yaml_to_xml]../examples/yaml_to_xml/ | YAML โ†’ XML conversion |
| [yaml_to_toml]../examples/yaml_to_toml/ | YAML โ†’ TOML conversion |
| [yaml_to_bencode]../examples/yaml_to_bencode/ | YAML โ†’ Bencode conversion |
| [yaml_anchors_aliases]../examples/yaml_anchors_aliases/ | Anchors, aliases, and merge keys |
| [yaml_advanced_tags]../examples/yaml_advanced_tags/ | Custom and standard tag handling |
| [yaml_multi_document]../examples/yaml_multi_document/ | Multi-document YAML streams |
| [yaml_node_manipulation]../examples/yaml_node_manipulation/ | Programmatic tree building |
| [yaml_fluent_api]../examples/yaml_fluent_api/ | Fluent builder API |
| [yaml_error_handling]../examples/yaml_error_handling/ | Error codes and recovery |
| [yaml_validation]../examples/yaml_validation/ | JSON Schema-style validation |
| [yaml_safe_access]../examples/yaml_safe_access/ | Nil-safe deep path access |
| [yaml_tree_traversal]../examples/yaml_tree_traversal/ | Iterators and traversal orders |
| [yaml_streaming]../examples/yaml_streaming/ | Streaming serialization |
| [yaml_string_interning]../examples/yaml_string_interning/ | String deduplication |
| [yaml_performance]../examples/yaml_performance/ | Benchmark helpers |
| [yaml_performance_opts]../examples/yaml_performance_opts/ | Optimization strategies |
| [yaml_embedded_systems]../examples/yaml_embedded_systems/ | `no_std` / embedded target |
| [yaml_embedded_safe]../examples/yaml_embedded_safe/ | Safe embedded parsing |
| [yaml_fibonacci]../examples/yaml_fibonacci/ | Algorithmic YAML generation |
| [yaml_comprehensive]../examples/yaml_comprehensive/ | End-to-end showcase |
| [yaml_utility_lib]../examples/yaml_utility_lib/ | Library usage patterns |

Run any example:
```bash
cargo run -p yaml_parse_and_stringify
cargo run -p yaml_to_json
cargo run -p yaml_validation
```

## ๐Ÿงช Testing


The library includes an extensive test suite with 1014+ tests covering:

- **Basic parsing** - All YAML constructs and edge cases
- **Document structure** - Multi-document streams, markers, directives
- **Tag coercion** - All standard and custom tags
- **Error handling** - Malformed YAML and recovery
- **File parsing** - Different encodings and formats
- **Nested structures** - Deep nesting and complex documents
- **Flow syntax** - Inline sequences and mappings
- **Set operations** - Unique collections and operations
- **Anchor/alias resolution** - Including circular reference detection
- **Directive handling** - `%YAML` and `%TAG` directives
- **Official YAML test suite** - 402/402 tests (100% compliance)

```bash
# Run all tests

cargo test

# Run specific test categories

cargo test basic_parsing
cargo test tag_coercion
cargo test error_handling

# Run with output

cargo test -- --nocapture
```

## ๐Ÿชต Debug Logging


Debug logging is opt-in and disabled by default to avoid overhead. Enable it with the `debug-trace` feature and a logger (e.g., `env_logger`).

### Enable in tests (Windows PowerShell)


```powershell
# Enable logging for this session

$env:RUST_LOG = 'yaml_lib=debug'

# Promote token-stream internals to debug without global trace

$env:YAML_TRACE_TOKENS = '1'

# Run tests with logging enabled

cargo test -p yaml_lib --features debug-trace -- --nocapture

# For very verbose internals, use full trace instead of YAML_TRACE_TOKENS

# $env:RUST_LOG = 'yaml_lib=trace'

```

### Enable in binaries/examples


Add a logger init (once) in your `main`:

```rust
fn main() {
  // Initialize any `log` compatible logger
  let _ = env_logger::try_init();
  // ... your code
}
```

Run with the feature and env vars as needed:

```powershell
$env:RUST_LOG = 'yaml_lib=debug'; $env:YAML_TRACE_TOKENS = '1'
cargo run --features debug-trace
```

Notes:
- `yaml_lib=debug` shows high-level parser decisions; token-stream stays quiet unless `YAML_TRACE_TOKENS` is set.
- Set `yaml_lib=trace` for maximum verbosity (may be very chatty).
- Feature-gating ensures zero overhead when `debug-trace` is not enabled.

## ๐Ÿ”ง Performance


YAML_lib is designed for performance:

- **Lazy parsing** - Only parse what you need
- **Memory efficiency** - Minimal allocations and copying
- **Zero-copy strings** - Where possible, reference original data
- **Optimized algorithms** - Efficient parsing and serialization
- **Minimal dependencies** - Only `rand` for testing utilities

## ๐Ÿค Contributing


Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

### Development Setup


```bash
git clone https://github.com/clockworkengineer/yaml.git
cd yaml
cargo build
cargo test
```

### Code Guidelines


- Follow standard Rust formatting (`cargo fmt`)
- Ensure all tests pass (`cargo test`)
- Add tests for new functionality
- Update documentation as needed

## ๐Ÿ“„ License


This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿ™ Acknowledgments


- **YAML 1.2 Specification** - [yaml.org]https://yaml.org/spec/1.2.2/
- **Rust Community** - For excellent tooling and ecosystem
- **Contributors** - Everyone who helped improve this library

## ๐Ÿ“Š Project Stats


- **Language**: Rust ๐Ÿฆ€
- **Minimum Rust Version**: 1.88.0
- **Lines of Code**: ~15,000+
- **Internal Tests**: 1014+ passing
- **Official YAML 1.2 Tests**: 402/402 (100%)
- **Documentation**: Comprehensive inline docs and doc-tests
- **Examples**: 23 comprehensive examples

---

**Made with โค๏ธ and ๐Ÿฆ€ by the YAML_lib team**

*For questions, issues, or contributions, please visit our [GitHub repository](https://github.com/clockworkengineer/yaml).*