1use similar::{ChangeTag, TextDiff};
2
3pub struct LineDiff {
4 pub added_lines: usize,
5 pub removed_lines: usize,
6 pub unified: String,
7}
8
9pub fn build_unified_line_diff(before: &str, after: &str, path: &str) -> LineDiff {
10 let diff = TextDiff::from_lines(before, after);
11 let mut added_lines = 0;
12 let mut removed_lines = 0;
13
14 for change in diff.iter_all_changes() {
15 match change.tag() {
16 ChangeTag::Insert => added_lines += 1,
17 ChangeTag::Delete => removed_lines += 1,
18 ChangeTag::Equal => {}
19 }
20 }
21
22 let unified = diff
23 .unified_diff()
24 .context_radius(3)
25 .header(&format!("a/{path}"), &format!("b/{path}"))
26 .to_string();
27
28 LineDiff {
29 added_lines,
30 removed_lines,
31 unified,
32 }
33}