Skip to main content

apply_doc

Function apply_doc 

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

Apply every op to a whole-document text (JSON/JSONC), in order. Returns the new text and the number of ops that changed it.

Edits are byte-range splices, so untouched bytes — including comments — are preserved exactly.

§Examples

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

let set = Op::Set {
    path: parse_path(".a").unwrap(),
    raw: ".a".into(),
    value: normalize_value("42"),
};
let (out, changes) =
    apply_doc("{\n  \"a\": 1, // keep me\n  \"b\": 2\n}\n", &[set]).unwrap();
assert_eq!(changes, 1);
// Only the value changed; the comment and layout are preserved.
assert_eq!(out, "{\n  \"a\": 42, // keep me\n  \"b\": 2\n}\n");