Skip to main content

parse_patch

Function parse_patch 

Source
pub fn parse_patch(text: &str) -> Result<ChangedRanges, DiffError>
Expand description

Parse a unified diff/patch and extract changed (added) line ranges.

This function parses the standard unified diff format as produced by git diff or patch files. It extracts only added lines (lines starting with + that aren’t header lines).

§Arguments

  • text - The unified diff text to parse

§Returns

A ChangedRanges map where keys are normalized file paths and values are sorted, non-overlapping line ranges of added lines.

§Errors

Returns DiffError::InvalidFormat if the diff is malformed.

§Examples

use covguard_adapters_diff::parse_patch;

let diff = r#"diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..1111111
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,3 @@
+pub fn add(a: i32, b: i32) -> i32 {
+    a + b
+}
"#;

let ranges = parse_patch(diff).unwrap();
assert_eq!(ranges.get("src/lib.rs"), Some(&vec![1..=3]));