diffguard-diff
Unified diff parser used by diffguard.
This crate parses git-style unified diffs and extracts scoped lines in diff order. It is pure parsing logic with no I/O.
What It Returns
Vec<DiffLine>withpath,line,content, andChangeKindDiffStatswith file/line totals
Scope behavior:
Scope::Added- all added (+) linesScope::Changed/Scope::Modified- added lines that replace removed linesScope::Deleted- removed (-) lines
Main API
use ;
use Scope;
let diff = r#"
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,1 +1,2 @@
fn existing() {}
+fn added() {}
"#;
let = parse_unified_diff?;
assert_eq!;
assert_eq!;
Special Cases Handled
- binary file markers
- submodule diffs
- deleted files (included only for
Scope::Deleted) - mode-only changes
- renames (
rename from/rename to) - quoted/escaped git paths
- malformed hunks (skip bad hunk and continue parsing later content)
Robustness Contract
- No panics for malformed diff content
- Deterministic output ordering
- Fuzz-tested (
fuzz_targets/unified_diff_parser)
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.