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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/// A trait for reacting to an edit script from the "old" version to
/// the "new" version.
pub trait DiffHook: Sized {
    /// The error produced from the hook methods.
    type Error;

    /// Called when lines with indices `old_index` (in the old version) and
    /// `new_index` (in the new version) start an section equal in both
    /// versions, of length `len`.
    fn equal(&mut self, old_index: usize, new_index: usize, len: usize) -> Result<(), Self::Error> {
        let _ = old_index;
        let _ = new_index;
        let _ = len;
        Ok(())
    }

    /// Called when a section of length `old_len`, starting at `old_index`,
    /// needs to be deleted from the old version.
    fn delete(
        &mut self,
        old_index: usize,
        old_len: usize,
        new_index: usize,
    ) -> Result<(), Self::Error> {
        let _ = old_index;
        let _ = old_len;
        let _ = new_index;
        Ok(())
    }

    /// Called when a section of the new version, of length `new_len`
    /// and starting at `new_index`, needs to be inserted at position `old_index'.
    fn insert(
        &mut self,
        old_index: usize,
        new_index: usize,
        new_len: usize,
    ) -> Result<(), Self::Error> {
        let _ = old_index;
        let _ = new_index;
        let _ = new_len;
        Ok(())
    }

    /// Called when a section of the old version, starting at index
    /// `old_index` and of length `old_len`, needs to be replaced with a
    /// section of length `new_len`, starting at `new_index`, of the new
    /// version.
    ///
    /// The default implementations invokes `delete` and `insert`.
    ///
    /// You can use the [`Replace`](crate::algorithms::Replace) hook to
    /// automatically generate these.
    #[inline(always)]
    fn replace(
        &mut self,
        old_index: usize,
        old_len: usize,
        new_index: usize,
        new_len: usize,
    ) -> Result<(), Self::Error> {
        self.delete(old_index, old_len, new_index)?;
        self.insert(old_index, new_index, new_len)
    }

    /// Always called at the end of the algorithm.
    #[inline(always)]
    fn finish(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }
}

impl<'a, D: DiffHook + 'a> DiffHook for &'a mut D {
    type Error = D::Error;

    #[inline(always)]
    fn equal(&mut self, old_index: usize, new_index: usize, len: usize) -> Result<(), Self::Error> {
        (*self).equal(old_index, new_index, len)
    }

    #[inline(always)]
    fn delete(
        &mut self,
        old_index: usize,
        old_len: usize,
        new_index: usize,
    ) -> Result<(), Self::Error> {
        (*self).delete(old_index, old_len, new_index)
    }

    #[inline(always)]
    fn insert(
        &mut self,
        old_index: usize,
        new_index: usize,
        new_len: usize,
    ) -> Result<(), Self::Error> {
        (*self).insert(old_index, new_index, new_len)
    }

    #[inline(always)]
    fn replace(
        &mut self,
        old: usize,
        old_len: usize,
        new: usize,
        new_len: usize,
    ) -> Result<(), Self::Error> {
        (*self).replace(old, old_len, new, new_len)
    }

    #[inline(always)]
    fn finish(&mut self) -> Result<(), Self::Error> {
        (*self).finish()
    }
}

/// Wrapper [`DiffHook`] that prevents calls to [`DiffHook::finish`].
///
/// This hook is useful in situations where diff hooks are composed but you
/// want to prevent that the finish hook method is called.
pub struct NoFinishHook<D: DiffHook>(D);

impl<D: DiffHook> NoFinishHook<D> {
    /// Wraps another hook.
    pub fn new(d: D) -> NoFinishHook<D> {
        NoFinishHook(d)
    }

    /// Extracts the inner hook.
    pub fn into_inner(self) -> D {
        self.0
    }
}

impl<D: DiffHook> DiffHook for NoFinishHook<D> {
    type Error = D::Error;

    #[inline(always)]
    fn equal(&mut self, old_index: usize, new_index: usize, len: usize) -> Result<(), Self::Error> {
        self.0.equal(old_index, new_index, len)
    }

    #[inline(always)]
    fn delete(
        &mut self,
        old_index: usize,
        old_len: usize,
        new_index: usize,
    ) -> Result<(), Self::Error> {
        self.0.delete(old_index, old_len, new_index)
    }

    #[inline(always)]
    fn insert(
        &mut self,
        old_index: usize,
        new_index: usize,
        new_len: usize,
    ) -> Result<(), Self::Error> {
        self.0.insert(old_index, new_index, new_len)
    }

    #[inline(always)]
    fn replace(
        &mut self,
        old_index: usize,
        old_len: usize,
        new_index: usize,
        new_len: usize,
    ) -> Result<(), Self::Error> {
        self.0.replace(old_index, old_len, new_index, new_len)
    }

    fn finish(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }
}