1use ansi_term::Colour::{Green, Red, White};
2use difference::{Changeset, Difference};
3
4pub use crate::errors::InspectError;
5
6pub fn diff(text1: String, text2: String) -> Result<String, InspectError> {
8 let Changeset { diffs, .. } = Changeset::new(&text1, &text2, "\n");
9
10 let mut t = String::new();
11
12 for i in 0..diffs.len() {
13 match diffs[i] {
14 Difference::Same(ref x) => {
15 t.push_str(&format!("\n{}", x));
16 }
17 Difference::Add(ref x) => {
18 match diffs[i - 1] {
19 Difference::Rem(ref y) => {
20 t.push_str(&Green.paint("\n+").to_string());
21 let Changeset { diffs, .. } = Changeset::new(y, x, " ");
22 for c in diffs {
23 match c {
24 Difference::Same(ref z) => {
25 t.push_str(&Green.paint(z).to_string());
26 t.push_str(&Green.paint(" ").to_string());
27 }
28 Difference::Add(ref z) => {
29 t.push_str(&White.on(Green).paint(z).to_string());
30 t.push_str(" ");
31 }
32 _ => (),
33 }
34 }
35 t.push_str("");
36 }
37 _ => {
38 t.push_str(&Green.paint(format!("\n+{}", x).to_string()));
39 }
40 };
41 }
42 Difference::Rem(ref x) => {
43 t.push_str(&Red.paint(format!("\n-{}", x)).to_string());
44 }
45 }
46 }
47 Ok(t)
48}