apply_patch

Function apply_patch 

Source
pub fn apply_patch(doc: &Value, patch: &Patch) -> TireaResult<Value>
Expand description

Apply a patch to a JSON document (pure function).

This function is deterministic: given the same document and patch, it will always produce the same result.

§Arguments

  • doc - The original document (not modified)
  • patch - The patch to apply

§Returns

A new document with all operations applied, or an error if any operation fails.

§Examples

use tirea_state::{apply_patch, Patch, Op, path};
use serde_json::json;

let doc = json!({"count": 0});
let patch = Patch::new()
    .with_op(Op::set(path!("count"), json!(10)))
    .with_op(Op::set(path!("name"), json!("test")));

let new_doc = apply_patch(&doc, &patch).unwrap();
assert_eq!(new_doc["count"], 10);
assert_eq!(new_doc["name"], "test");

// Original is unchanged (pure function)
assert_eq!(doc["count"], 0);