Apply a single JSON Patch operation
(value: &Value, op: &JsonPatchOperation)
| 160 | |
| 161 | /// Apply a single JSON Patch operation |
| 162 | fn apply_json_patch_operation(value: &Value, op: &JsonPatchOperation) -> Result<Value, PatchError> { |
| 163 | let require_value = |what: &str| -> Result<&Value, PatchError> { |
| 164 | op.value |
| 165 | .as_ref() |
| 166 | .ok_or_else(|| PatchError::InvalidPatch(format!("'value' required for {}", what))) |
| 167 | }; |
| 168 | match op.op { |
| 169 | JsonPatchOp::Add => add_operation(value, &op.path, require_value("add")?), |
| 170 | JsonPatchOp::Remove => remove_operation(value, &op.path), |
| 171 | JsonPatchOp::Replace => replace_operation(value, &op.path, require_value("replace")?), |
| 172 | JsonPatchOp::Move => { |
| 173 | let from = op |
| 174 | .from |
| 175 | .as_ref() |
| 176 | .ok_or_else(|| PatchError::InvalidPatch("'from' required for move".to_string()))?; |
| 177 | move_operation(value, from, &op.path) |
| 178 | } |
| 179 | JsonPatchOp::Copy => { |
| 180 | let from = op |
| 181 | .from |
| 182 | .as_ref() |
| 183 | .ok_or_else(|| PatchError::InvalidPatch("'from' required for copy".to_string()))?; |
| 184 | copy_operation(value, from, &op.path) |
| 185 | } |
| 186 | JsonPatchOp::Test => { |
| 187 | test_operation(value, &op.path, require_value("test")?)?; |
| 188 | Ok(value.clone()) |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /// Add operation - adds a value at the specified path |
| 194 | fn add_operation(value: &Value, path: &str, new_value: &Value) -> Result<Value, PatchError> { |
no test coverage detected