Get a mutable reference to a value at the specified path
(value: &'a mut Value, path: &str)
| 568 | |
| 569 | /// Get a mutable reference to a value at the specified path |
| 570 | fn get_mut_value<'a>(value: &'a mut Value, path: &str) -> Result<&'a mut Value, PatchError> { |
| 571 | if path.is_empty() || path == "/" { |
| 572 | return Ok(value); |
| 573 | } |
| 574 | |
| 575 | let parts = parse_path(path)?; |
| 576 | let mut current = value; |
| 577 | |
| 578 | for part in parts { |
| 579 | if current.is_object() { |
| 580 | current = current |
| 581 | .as_object_mut() |
| 582 | .unwrap() |
| 583 | .entry(part.clone()) |
| 584 | .or_insert(json!({})); |
| 585 | } else if current.is_array() { |
| 586 | let index: usize = part |
| 587 | .parse() |
| 588 | .map_err(|_| PatchError::InvalidPatch(format!("Invalid array index: {}", part)))?; |
| 589 | current = current |
| 590 | .as_array_mut() |
| 591 | .unwrap() |
| 592 | .get_mut(index) |
| 593 | .ok_or_else(|| { |
| 594 | PatchError::OperationFailed(format!("Array index out of bounds: {}", index)) |
| 595 | })?; |
| 596 | } else { |
| 597 | return Err(PatchError::OperationFailed(format!( |
| 598 | "Cannot traverse non-object/array in path: {}", |
| 599 | path |
| 600 | ))); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | Ok(current) |
| 605 | } |
| 606 | |
| 607 | /// Parse a JSON pointer path into parts |
| 608 | fn parse_path(path: &str) -> Result<Vec<String>, PatchError> { |
no test coverage detected