liboxen/repositories/diffs/
utf8_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
49
50
51
use crate::error::OxenError;
use crate::model::diff::change_type::ChangeType;
use crate::model::diff::text_diff::LineDiff;
use crate::model::diff::text_diff::TextDiff;
use crate::util;

use difference::{Changeset, Difference};
use std::path::Path;

pub fn diff(
    version_file_1: impl AsRef<Path>,
    version_file_2: impl AsRef<Path>,
) -> Result<TextDiff, OxenError> {
    let version_file_1 = version_file_1.as_ref();
    let version_file_2 = version_file_2.as_ref();
    let original_data = util::fs::read_from_path(version_file_1)?;
    let compare_data = util::fs::read_from_path(version_file_2)?;
    let Changeset { diffs, .. } = Changeset::new(&original_data, &compare_data, "\n");

    let mut result = TextDiff { lines: vec![] };
    for diff in diffs {
        match diff {
            Difference::Same(ref x) => {
                for split in x.split('\n') {
                    result.lines.push(LineDiff {
                        modification: ChangeType::Unchanged,
                        text: split.to_string(),
                    });
                }
            }
            Difference::Add(ref x) => {
                for split in x.split('\n') {
                    result.lines.push(LineDiff {
                        modification: ChangeType::Added,
                        text: split.to_string(),
                    });
                }
            }
            Difference::Rem(ref x) => {
                for split in x.split('\n') {
                    result.lines.push(LineDiff {
                        modification: ChangeType::Removed,
                        text: split.to_string(),
                    });
                }
            }
        }
    }

    Ok(result)
}