diff --git a/src/yaml_format_preserving.rs b/src/yaml_format_preserving.rs index 1abde0d..0b08c5f 100644 --- a/src/yaml_format_preserving.rs +++ b/src/yaml_format_preserving.rs @@ -16,7 +16,7 @@ pub fn write_yaml_preserving_format( // Check if there are unhandleable structural changes // If we're adding new nested structures, fall back to standard serialization - if has_unhandleable_nested_changes(&original_value, &updated_value) { + if has_unhandleable_nested_changes(&original_value, updated_value) { // For truly complex nested changes, use standard YAML serialization return serde_yaml::to_string(updated_value) .map_err(|e| format!("Failed to serialize YAML: {}", e)); @@ -24,10 +24,10 @@ pub fn write_yaml_preserving_format( // Collect keys that were removed (in original but not in updated) let mut removed_keys = Vec::new(); - collect_removed_keys(&original_value, &updated_value, "", &mut removed_keys); + collect_removed_keys(&original_value, updated_value, "", &mut removed_keys); // Build a map of all keys and their new values - let updates = collect_all_changes(original_content, &updated_value)?; + let updates = collect_all_changes(original_content, updated_value)?; if updates.is_empty() && removed_keys.is_empty() { // No changes, return original @@ -109,10 +109,11 @@ fn has_unhandleable_nested_changes(old: &Value, new: &Value) -> bool { if let Some(new_val) = new_map.get(key) { if old_val != new_val { // If both are mappings and contents changed, we need to be careful - if old_val.is_mapping() && new_val.is_mapping() { - if has_unhandleable_nested_changes(old_val, new_val) { - return true; - } + if old_val.is_mapping() + && new_val.is_mapping() + && has_unhandleable_nested_changes(old_val, new_val) + { + return true; } } } @@ -126,29 +127,26 @@ fn has_unhandleable_nested_changes(old: &Value, new: &Value) -> bool { /// Collects keys that were removed (in original but not in updated), including nested keys fn collect_removed_keys(old: &Value, new: &Value, prefix: &str, removed: &mut Vec) { - match (old, new) { - (Value::Mapping(old_map), Value::Mapping(new_map)) => { - for (key, old_val) in old_map { - if let Value::String(key_str) = key { - let full_key = if prefix.is_empty() { - key_str.clone() - } else { - format!("{}.{}", prefix, key_str) - }; - - if !new_map.contains_key(key) { - // Key was removed entirely - removed.push(full_key); - } else if let Some(new_val) = new_map.get(key) { - // Key exists in new, but might have removed nested keys - if old_val.is_mapping() && new_val.is_mapping() { - collect_removed_keys(old_val, new_val, &full_key, removed); - } + if let (Value::Mapping(old_map), Value::Mapping(new_map)) = (old, new) { + for (key, old_val) in old_map { + if let Value::String(key_str) = key { + let full_key = if prefix.is_empty() { + key_str.clone() + } else { + format!("{}.{}", prefix, key_str) + }; + + if !new_map.contains_key(key) { + // Key was removed entirely + removed.push(full_key); + } else if let Some(new_val) = new_map.get(key) { + // Key exists in new, but might have removed nested keys + if old_val.is_mapping() && new_val.is_mapping() { + collect_removed_keys(old_val, new_val, &full_key, removed); } } } } - _ => {} } } diff --git a/src/yaml_ops.rs b/src/yaml_ops.rs index 6dc3177..f6e16c9 100644 --- a/src/yaml_ops.rs +++ b/src/yaml_ops.rs @@ -114,7 +114,7 @@ pub fn get_value(value: &Value, path: &str) -> Result, String> { let mut current = value; for part in parts { if let Value::Mapping(map) = current { - match map.get(&Value::String(part.to_string())) { + match map.get(Value::String(part.to_string())) { Some(next) => current = next, None => return Ok(None), } @@ -263,14 +263,14 @@ fn unset_at_path(value: &mut Value, path: &str) -> Result<(), String> { if parts.len() == 1 { // Direct child: remove from root mapping if let Value::Mapping(ref mut map) = value { - map.remove(&Value::String(parts[0].to_string())); + map.remove(Value::String(parts[0].to_string())); } } else { // Navigate to parent, then remove the final key let mut current = value; for &part in parts[..parts.len() - 1].iter() { if let Value::Mapping(ref mut map) = current { - if let Some(next) = map.get_mut(&Value::String(part.to_string())) { + if let Some(next) = map.get_mut(Value::String(part.to_string())) { current = next; } else { // Path doesn't exist @@ -284,7 +284,7 @@ fn unset_at_path(value: &mut Value, path: &str) -> Result<(), String> { // Remove the final key if let Value::Mapping(ref mut map) = current { - map.remove(&Value::String(parts[parts.len() - 1].to_string())); + map.remove(Value::String(parts[parts.len() - 1].to_string())); } }