Remove operation - removes a value at the specified path
(value: &Value, path: &str)
| 224 | |
| 225 | /// Remove operation - removes a value at the specified path |
| 226 | fn remove_operation(value: &Value, path: &str) -> Result<Value, PatchError> { |
| 227 | let mut result = value.clone(); |
| 228 | let (parent_path, key) = split_path(path)?; |
| 229 | |
| 230 | let parent = get_mut_value(&mut result, parent_path)?; |
| 231 | |
| 232 | if let Some(obj) = parent.as_object_mut() { |
| 233 | obj.remove(key); |
| 234 | } else if let Some(arr) = parent.as_array_mut() { |
| 235 | let index: usize = key |
| 236 | .parse() |
| 237 | .map_err(|_| PatchError::InvalidPatch(format!("Invalid array index: {}", key)))?; |
| 238 | if index >= arr.len() { |
| 239 | return Err(PatchError::OperationFailed(format!( |
| 240 | "Array index out of bounds: {}", |
| 241 | index |
| 242 | ))); |
| 243 | } |
| 244 | arr.remove(index); |
| 245 | } else { |
| 246 | return Err(PatchError::OperationFailed(format!( |
| 247 | "Cannot remove from non-object/array at path: {}", |
| 248 | parent_path |
| 249 | ))); |
| 250 | } |
| 251 | |
| 252 | Ok(result) |
| 253 | } |
| 254 | |
| 255 | /// Replace operation - replaces a value at the specified path |
| 256 | fn replace_operation(value: &Value, path: &str, new_value: &Value) -> Result<Value, PatchError> { |
no test coverage detected