-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdiff.rs
48 lines (44 loc) · 1.8 KB
/
diff.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use ansi_term::Colour::{Green, Red, White};
use difference::{Changeset, Difference};
pub use crate::errors::InspectError;
/// Compare two strings and return a Github-style diff output
pub fn diff(text1: String, text2: String) -> Result<String, InspectError> {
let Changeset { diffs, .. } = Changeset::new(&text1, &text2, "\n");
let mut t = String::new();
for i in 0..diffs.len() {
match diffs[i] {
Difference::Same(ref x) => {
t.push_str(&format!("\n{}", x));
}
Difference::Add(ref x) => {
match diffs[i - 1] {
Difference::Rem(ref y) => {
t.push_str(&Green.paint("\n+").to_string());
let Changeset { diffs, .. } = Changeset::new(y, x, " ");
for c in diffs {
match c {
Difference::Same(ref z) => {
t.push_str(&Green.paint(z).to_string());
t.push_str(&Green.paint(" ").to_string());
}
Difference::Add(ref z) => {
t.push_str(&White.on(Green).paint(z).to_string());
t.push_str(" ");
}
_ => (),
}
}
t.push_str("");
}
_ => {
t.push_str(&Green.paint(format!("\n+{}", x).to_string()));
}
};
}
Difference::Rem(ref x) => {
t.push_str(&Red.paint(format!("\n-{}", x)).to_string());
}
}
}
Ok(t)
}