yaml_lib 0.1.8

YAML library implementation.
Documentation

YAML_lib ๐Ÿฆ€

Rust License: MIT

Tests YAML 1.2

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

โœจ Features

๐Ÿ… Core YAML Support

  • 90.5% YAML 1.2 specification compliance (364/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 (362+ internal tests, 364/402 YAML 1.2 official tests, 90.5% pass rate)
  • JSON Schema-style validation for YAML documents
  • Error codes and suggestions for programmatic error handling

๐Ÿ›ก๏ธ 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:

[dependencies]

yaml_lib = "0.1.8"

๐Ÿš€ Quick Start

Basic Parsing

use yaml_lib::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Parse YAML from string
    let yaml_str = r#"
    name: "YAML Library"
    version: 1.0
    features:
      - parsing
      - serialization
      - multi-format
    "#;
    
    let mut source = BufferSource::new(yaml_str.as_bytes());
    let document = parse(&mut source)?;
    
    println!("{:#?}", document);
    Ok(())
}

Working with Nodes

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

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

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
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() Helper macro for creating nodes
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 comprehensive examples:

Run examples:

cargo run --example yaml_parse_and_stringify

cargo run --example yaml_to_json

cargo run --example yaml_to_xml

๐Ÿงช Testing

The library includes an extensive test suite with 362+ 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
# 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)

# 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:

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

Run with the feature and env vars as needed:

$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

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 file for details.

๐Ÿ™ Acknowledgments

  • YAML 1.2 Specification - yaml.org
  • 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: ~10,000+
  • Test Coverage: 362+ tests
  • Documentation: Comprehensive inline docs
  • Examples: 6 comprehensive examples

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

For questions, issues, or contributions, please visit our GitHub repository.