my fork of dmp
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

/**
 * The data structure representing a diff is an array of tuples:
 * [[DIFF_DELETE, 'Hello'], [DIFF_INSERT, 'Goodbye'], [DIFF_EQUAL, ' world.']]
 * which means: delete 'Hello', add 'Goodbye' and keep ' world.'
 */

/// Enum representing the different ops of diff
#[derive(Debug, PartialEq, Eq)]
pub enum Ops {
    Delete,
    Insert,
    Equal
}

/// A structure representing a diff
/// (Ops::Delete, String::new("Hello")) means delete `Hello`
/// (Ops::Insert, String::new("Goodbye")) means add `Goodbye`
/// (Ops::Equal, String::new("World")) means keep world
#[derive(Debug, PartialEq, Eq)]
pub struct Diff(Ops, String);

impl Diff {
    /// Create a new diff object
    pub fn new(op: Ops, text: &str) -> Self {
        Self(op, text.to_string())
    }
}

pub struct DiffMatchPatch {
    checklines: Option<bool>
}

impl DiffMatchPatch {
    fn checklines(&self) -> bool {
        self.checklines.map_or(true, |c| c)
    }
}

impl DiffMatchPatch {
    pub fn diff_main(&self, old: &str, new: &str) -> Vec<Diff> {
        // First, check if lhs and rhs are equal
        if old == new {
            if old.is_empty() {
                return Vec::new();
            }

            return vec![Diff::new(Ops::Equal, old)];
        }

        if old.is_empty() {
            return vec![Diff::new(Ops::Insert, new)]
        }

        if new.is_empty() {
            return vec![Diff::new(Ops::Delete, old)]
        }


        todo!()
    }
}