diffus/diffable_impls/
string.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use crate::{
    edit::{self, string},
    lcs, Diffable,
};

impl<'a> Diffable<'a> for str {
    type Diff = Vec<string::Edit>;

    fn diff(&'a self, other: &'a Self) -> edit::Edit<Self> {
        let s = lcs::lcs(
            || self.chars(),
            || other.chars(),
            self.chars().count(),
            other.chars().count(),
        )
        .map(Into::into)
        .collect::<Vec<_>>();

        if s.iter().all(string::Edit::is_copy) {
            edit::Edit::Copy(self)
        } else {
            edit::Edit::Change(s)
        }
    }
}

impl<'a> Diffable<'a> for String {
    type Diff = <str as Diffable<'a>>::Diff;

    fn diff(&'a self, other: &'a Self) -> edit::Edit<Self> {
        match self.as_str().diff(other.as_str()) {
            edit::Edit::Change(diff) => edit::Edit::Change(diff),
            edit::Edit::Copy(_) => edit::Edit::Copy(self),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::edit::{self, string};

    #[test]
    fn string() {
        use super::Diffable;

        let left = "XMJYAUZ".to_owned();
        let right = "MZJAWXU".to_owned();

        let diff = left.diff(&right);
        if let edit::Edit::Change(diff) = diff {
            assert_eq!(
                diff.into_iter().collect::<Vec<_>>(),
                vec![
                    string::Edit::Remove('X'),
                    string::Edit::Copy('M'),
                    string::Edit::Insert('Z'),
                    string::Edit::Copy('J'),
                    string::Edit::Remove('Y'),
                    string::Edit::Copy('A'),
                    string::Edit::Insert('W'),
                    string::Edit::Insert('X'),
                    string::Edit::Copy('U'),
                    string::Edit::Remove('Z')
                ]
            );
        } else {
            unreachable!()
        }
    }

    #[test]
    fn str() {
        use super::Diffable;

        let left = "XMJYAUZ";
        let right = "MZJAWXU";

        let diff = left.diff(&right);
        if let edit::Edit::Change(diff) = diff {
            assert_eq!(
                diff.into_iter().collect::<Vec<_>>(),
                vec![
                    string::Edit::Remove('X'),
                    string::Edit::Copy('M'),
                    string::Edit::Insert('Z'),
                    string::Edit::Copy('J'),
                    string::Edit::Remove('Y'),
                    string::Edit::Copy('A'),
                    string::Edit::Insert('W'),
                    string::Edit::Insert('X'),
                    string::Edit::Copy('U'),
                    string::Edit::Remove('Z')
                ]
            );
        } else {
            unreachable!()
        }
    }
}