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
|
use std::str::FromStr;
use yaml_edit::YamlFile;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let yaml_content = r#"
# Base configuration
defaults: &defaults
timeout: 30
retries: 3
enabled: true
log_level: info
# Production configuration merges defaults
production:
<<: *defaults
host: prod.example.com
port: 443
log_level: warning # Override default
# Development configuration also merges defaults
development:
<<: *defaults
host: localhost
port: 3000
debug: true
# Multiple merge keys example
base_db: &base_db
driver: postgres
pool_size: 10
base_cache: &base_cache
provider: redis
ttl: 3600
services:
api:
<<: [*base_db, *base_cache]
name: api_service
port: 8080
"#;
println!("Original YAML with merge keys:");
println!("{}", yaml_content);
println!("\n{}\n", "=".repeat(50));
// Parse the YAML
let yaml = YamlFile::from_str(yaml_content)?;
println!("Parsed and preserved YAML:");
println!("{}", yaml);
println!("\n{}\n", "=".repeat(50));
// The output should preserve all merge keys and references
let output = yaml.to_string();
// Verify output is preserved exactly
assert_eq!(output, yaml_content);
Ok(())
}
|