| Crates.io | yaml_lib |
| lib.rs | yaml_lib |
| version | 0.1.6 |
| created_at | 2025-11-07 17:29:43.368353+00 |
| updated_at | 2025-11-11 10:32:03.478058+00 |
| description | YAML library implementation. |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1921929 |
| size | 481,743 |
A comprehensive, high-performance YAML library for Rust that provides full YAML 1.2 specification compliance with excellent ergonomics and extensive format conversion capabilities.
!!str, !!int, !!float, !!bool, !!null!!seq, !!map, !!set!!binary with base64 validation!!omap for insertion-order preservation!!pairs with duplicate key support!!merge for YAML inheritance!!int:hex) and octal (!!int:oct)Add this to your Cargo.toml:
[dependencies]
yaml_lib = "0.1.6"
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(())
}
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(())
}
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(())
}
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(())
}
Node - The fundamental data structure representing any YAML valueNumeric - Enum for integer and floating-point numbersBufferSource/FileSource - Input sources for parsingBufferDestination/FileDestination - Output destinations for serialization| 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 |
| 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 |
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
The library includes an extensive test suite with 362+ tests covering:
# 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
YAML_lib is designed for performance:
rand for testing utilitiesContributions 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.
git clone https://github.com/clockworkengineer/yaml.git
cd yaml
cargo build
cargo test
cargo fmt)cargo test)This project is licensed under the MIT License - see the LICENSE file for details.
Made with โค๏ธ and ๐ฆ by the YAML_lib team
For questions, issues, or contributions, please visit our GitHub repository.