| Crates.io | yamp |
| lib.rs | yamp |
| version | 0.1.0 |
| created_at | 2025-09-17 06:20:51.046503+00 |
| updated_at | 2025-09-17 06:20:51.046503+00 |
| description | Yet Another Minimal Parser - A safe, predictable YAML parser that treats all values as strings |
| homepage | |
| repository | https://github.com/sanjeevprasad/yamp |
| max_upload_size | |
| id | 1842807 |
| size | 137,546 |
A lightweight, efficient YAML parser written in Rust that prioritizes comment preservation, simplicity, and avoiding common YAML pitfalls.
YAMP takes a radically simplified approach to YAML parsing with two core principles:
Unlike most YAML parsers that discard comments, YAMP treats comments as first-class citizens. Every comment in your YAML file is preserved during parsing and re-emitted exactly where it belongs. This makes YAMP ideal for configuration files that need human-readable documentation.
NO, 3.10, 0755 are always strings| and folded >)|) and folded (>) styles-, clip default, keep +)YAMP's killer feature is preserving comments through parse/emit cycles:
use yamp::{parse, emit};
fn main() {
let yaml = r#"# Application configuration
name: MyApp # Application name
version: 1.2.3 # Semantic version
# Server settings
server:
host: localhost # Bind address
port: 8080 # Listen port
"#;
// Parse the YAML - all comments are preserved
let parsed = parse(yaml).expect("Failed to parse");
// Emit it back - comments remain intact!
let output = emit(&parsed);
println!("{}", output);
// Output includes all the original comments
}
All values are treated as strings, avoiding YAML's type confusion:
use yamp::{parse, emit, YamlValue};
use std::borrow::Cow;
fn main() {
let yaml = r#"
name: John Doe
age: 30
active: true
version: 3.10
country: NO
permissions: 0755
"#;
// Parse YAML
let parsed = parse(yaml).expect("Failed to parse YAML");
// Easy access with helper methods
assert_eq!(parsed.get("age").and_then(|n| n.as_str()), Some("30"));
assert_eq!(parsed.get("active").and_then(|n| n.as_str()), Some("true"));
assert_eq!(parsed.get("version").and_then(|n| n.as_str()), Some("3.10")); // Not 3.1!
assert_eq!(parsed.get("country").and_then(|n| n.as_str()), Some("NO")); // Not false!
assert_eq!(parsed.get("permissions").and_then(|n| n.as_str()), Some("0755")); // Not 493!
// Or using traditional approach for more control
if let YamlValue::Object(map) = &parsed.value {
let age = &map.get(&Cow::Borrowed("age")).unwrap().value;
assert_eq!(age, &YamlValue::String(Cow::Borrowed("30")));
}
// Emit back to YAML
let output = emit(&parsed);
println!("{}", output);
}
YAMP supports YAML multiline strings while maintaining the all-strings philosophy:
use yamp::{parse, YamlValue};
use std::borrow::Cow;
fn main() {
let yaml = r#"
description: |
This is a multiline string
that preserves line breaks.
summary: >
This is a folded multiline string
that joins lines with spaces.
"#;
let parsed = parse(yaml).expect("Failed to parse YAML");
if let YamlValue::Object(map) = &parsed.value {
// Literal style (|) preserves line breaks
let desc = &map.get(&Cow::Borrowed("description")).unwrap().value;
if let YamlValue::String(s) = desc {
assert!(s.contains("\n"));
}
// Folded style (>) joins lines with spaces
let summary = &map.get(&Cow::Borrowed("summary")).unwrap().value;
if let YamlValue::String(s) = summary {
assert!(!s.contains("\n"));
assert!(s.contains("string that joins"));
}
}
}
YAML's implicit typing leads to countless surprising behaviors and security issues:
NO becomes false3.10 becomes 3.10755 becomes 4932e5a8d9 partially parsed as 200000yes, on, y, true, True, TRUE all mean true~ in paths like ~/.ssh/config becomes null12:34:56 becomes 45296 (base-60).inf, .nan cause portability issuesEverything is a string. Period.
If you need typed data, parse the strings in your application where you have full control over the conversion rules. This approach is:
YAMP is perfect when you:
Don't use YAMP if you:
Add to your Cargo.toml:
[dependencies]
yamp = "0.1.0"
Contributions are welcome! However, please note that we will NOT accept PRs that:
MIT
This parser is inspired by the StrictYAML project and the numerous articles documenting YAML's problematic "features".
YAMP was created to solve a specific problem: the need for a YAML parser that preserves comments while avoiding YAML's type system complexities. Most YAML parsers treat comments as throwaway metadata, but in configuration files, comments are often as important as the data itself.