Skip to main content

apply_yaml

Function apply_yaml 

Source
pub fn apply_yaml(text: &str, ops: &[Op]) -> Result<(String, usize), String>
Expand description

Apply every op to a YAML document via the pure-Rust, comment-preserving yaml-edit backend. Returns the new text and the number of ops that changed it. Supports --set (replace an existing key) and --delete; --add and --move-* (and array-index/predicate paths) error rather than risk malformed output.

ยงExamples

use coding_tools::patch::{apply_yaml, parse_path, normalize_value, Op};

let set = Op::Set {
    path: parse_path(".server.port").unwrap(),
    raw: ".server.port".into(),
    value: normalize_value("9090"),
};
let yaml = "# cfg\nserver:\n  host: localhost   # inline\n  port: 8080\n";
let (out, changes) = apply_yaml(yaml, &[set]).unwrap();
assert_eq!(changes, 1);
assert!(out.contains("# cfg"));       // leading comment preserved
assert!(out.contains("port: 9090"));  // a number, not a quoted string
assert!(out.contains("# inline"));    // inline comment preserved