Apply JSON Patch (RFC 6902)
(original: &Value, patch: &Value)
| 146 | |
| 147 | /// Apply JSON Patch (RFC 6902) |
| 148 | fn apply_json_patch(original: &Value, patch: &Value) -> Result<Value, PatchError> { |
| 149 | let operations: Vec<JsonPatchOperation> = serde_json::from_value(patch.clone()) |
| 150 | .map_err(|e| PatchError::InvalidPatch(e.to_string()))?; |
| 151 | |
| 152 | let mut current = original.clone(); |
| 153 | |
| 154 | for op in operations { |
| 155 | current = apply_json_patch_operation(¤t, &op)?; |
| 156 | } |
| 157 | |
| 158 | Ok(current) |
| 159 | } |
| 160 | |
| 161 | /// Apply a single JSON Patch operation |
| 162 | fn apply_json_patch_operation(value: &Value, op: &JsonPatchOperation) -> Result<Value, PatchError> { |