summaryrefslogtreecommitdiffstats
path: root/vendor/similar/src/types.rs
blob: 89676761a1fdbdff43122939f2091261681bed17 (plain)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use std::fmt;
use std::ops::{Index, Range};

use crate::algorithms::utils::is_empty_range;
use crate::algorithms::DiffHook;
use crate::iter::ChangesIter;

/// An enum representing a diffing algorithm.
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "snake_case")
)]
pub enum Algorithm {
    /// Picks the myers algorithm from [`crate::algorithms::myers`]
    Myers,
    /// Picks the patience algorithm from [`crate::algorithms::patience`]
    Patience,
    /// Picks the LCS algorithm from [`crate::algorithms::lcs`]
    Lcs,
}

impl Default for Algorithm {
    /// Returns the default algorithm ([`Algorithm::Myers`]).
    fn default() -> Algorithm {
        Algorithm::Myers
    }
}

/// The tag of a change.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Ord, PartialOrd)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "snake_case")
)]
pub enum ChangeTag {
    /// The change indicates equality (not a change)
    Equal,
    /// The change indicates deleted text.
    Delete,
    /// The change indicates inserted text.
    Insert,
}

impl fmt::Display for ChangeTag {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}",
            match &self {
                ChangeTag::Equal => ' ',
                ChangeTag::Delete => '-',
                ChangeTag::Insert => '+',
            }
        )
    }
}

/// Represents the expanded [`DiffOp`] change.
///
/// This type is returned from [`DiffOp::iter_changes`] and
/// [`TextDiff::iter_changes`](crate::text::TextDiff::iter_changes).
///
/// It exists so that it's more convenient to work with textual differences as
/// the underlying [`DiffOp`] encodes a group of changes.
///
/// This type has additional methods that are only available for types
/// implementing [`DiffableStr`](crate::text::DiffableStr).
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Change<T> {
    pub(crate) tag: ChangeTag,
    pub(crate) old_index: Option<usize>,
    pub(crate) new_index: Option<usize>,
    pub(crate) value: T,
}

/// These methods are available for all change types.
impl<T: Clone> Change<T> {
    /// Returns the change tag.
    pub fn tag(&self) -> ChangeTag {
        self.tag
    }

    /// Returns the old index if available.
    pub fn old_index(&self) -> Option<usize> {
        self.old_index
    }

    /// Returns the new index if available.
    pub fn new_index(&self) -> Option<usize> {
        self.new_index
    }

    /// Returns the underlying changed value.
    ///
    /// Depending on the type of the underlying [`crate::text::DiffableStr`]
    /// this value is more or less useful.  If you always want to have a utf-8
    /// string it's best to use the [`Change::as_str`] and
    /// [`Change::to_string_lossy`] methods.
    pub fn value(&self) -> T {
        self.value.clone()
    }
}

/// Utility enum to capture a diff operation.
///
/// This is used by [`Capture`](crate::algorithms::Capture).
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "snake_case", tag = "op")
)]
pub enum DiffOp {
    /// A segment is equal (see [`DiffHook::equal`])
    Equal {
        /// The starting index in the old sequence.
        old_index: usize,
        /// The starting index in the new sequence.
        new_index: usize,
        /// The length of the segment.
        len: usize,
    },
    /// A segment was deleted (see [`DiffHook::delete`])
    Delete {
        /// The starting index in the old sequence.
        old_index: usize,
        /// The length of the old segment.
        old_len: usize,
        /// The starting index in the new sequence.
        new_index: usize,
    },
    /// A segment was inserted (see [`DiffHook::insert`])
    Insert {
        /// The starting index in the old sequence.
        old_index: usize,
        /// The starting index in the new sequence.
        new_index: usize,
        /// The length of the new segment.
        new_len: usize,
    },
    /// A segment was replaced (see [`DiffHook::replace`])
    Replace {
        /// The starting index in the old sequence.
        old_index: usize,
        /// The length of the old segment.
        old_len: usize,
        /// The starting index in the new sequence.
        new_index: usize,
        /// The length of the new segment.
        new_len: usize,
    },
}

/// The tag of a diff operation.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, Ord, PartialOrd)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(rename_all = "snake_case")
)]
pub enum DiffTag {
    /// The diff op encodes an equal segment.
    Equal,
    /// The diff op encodes a deleted segment.
    Delete,
    /// The diff op encodes an inserted segment.
    Insert,
    /// The diff op encodes a replaced segment.
    Replace,
}

impl DiffOp {
    /// Returns the tag of the operation.
    pub fn tag(self) -> DiffTag {
        self.as_tag_tuple().0
    }

    /// Returns the old range.
    pub fn old_range(&self) -> Range<usize> {
        self.as_tag_tuple().1
    }

    /// Returns the new range.
    pub fn new_range(&self) -> Range<usize> {
        self.as_tag_tuple().2
    }

    /// Transform the op into a tuple of diff tag and ranges.
    ///
    /// This is useful when operating on slices.  The returned format is
    /// `(tag, i1..i2, j1..j2)`:
    ///
    /// * `Replace`: `a[i1..i2]` should be replaced by `b[j1..j2]`
    /// * `Delete`: `a[i1..i2]` should be deleted (`j1 == j2` in this case).
    /// * `Insert`: `b[j1..j2]` should be inserted at `a[i1..i2]` (`i1 == i2` in this case).
    /// * `Equal`: `a[i1..i2]` is equal to `b[j1..j2]`.
    pub fn as_tag_tuple(&self) -> (DiffTag, Range<usize>, Range<usize>) {
        match *self {
            DiffOp::Equal {
                old_index,
                new_index,
                len,
            } => (
                DiffTag::Equal,
                old_index..old_index + len,
                new_index..new_index + len,
            ),
            DiffOp::Delete {
                old_index,
                new_index,
                old_len,
            } => (
                DiffTag::Delete,
                old_index..old_index + old_len,
                new_index..new_index,
            ),
            DiffOp::Insert {
                old_index,
                new_index,
                new_len,
            } => (
                DiffTag::Insert,
                old_index..old_index,
                new_index..new_index + new_len,
            ),
            DiffOp::Replace {
                old_index,
                old_len,
                new_index,
                new_len,
            } => (
                DiffTag::Replace,
                old_index..old_index + old_len,
                new_index..new_index + new_len,
            ),
        }
    }

    /// Apply this operation to a diff hook.
    pub fn apply_to_hook<D: DiffHook>(&self, d: &mut D) -> Result<(), D::Error> {
        match *self {
            DiffOp::Equal {
                old_index,
                new_index,
                len,
            } => d.equal(old_index, new_index, len),
            DiffOp::Delete {
                old_index,
                old_len,
                new_index,
            } => d.delete(old_index, old_len, new_index),
            DiffOp::Insert {
                old_index,
                new_index,
                new_len,
            } => d.insert(old_index, new_index, new_len),
            DiffOp::Replace {
                old_index,
                old_len,
                new_index,
                new_len,
            } => d.replace(old_index, old_len, new_index, new_len),
        }
    }

    /// Iterates over all changes encoded in the diff op against old and new
    /// sequences.
    ///
    /// `old` and `new` are two indexable objects like the types you pass to
    /// the diffing algorithm functions.
    ///
    /// ```rust
    /// use similar::{ChangeTag, Algorithm};
    /// use similar::capture_diff_slices;
    /// let old = vec!["foo", "bar", "baz"];
    /// let new = vec!["foo", "bar", "blah"];
    /// let ops = capture_diff_slices(Algorithm::Myers, &old, &new);
    /// let changes: Vec<_> = ops
    ///     .iter()
    ///     .flat_map(|x| x.iter_changes(&old, &new))
    ///     .map(|x| (x.tag(), x.value()))
    ///     .collect();
    /// assert_eq!(changes, vec![
    ///     (ChangeTag::Equal, "foo"),
    ///     (ChangeTag::Equal, "bar"),
    ///     (ChangeTag::Delete, "baz"),
    ///     (ChangeTag::Insert, "blah"),
    /// ]);
    /// ```
    pub fn iter_changes<'lookup, Old, New, T>(
        &self,
        old: &'lookup Old,
        new: &'lookup New,
    ) -> ChangesIter<'lookup, Old, New, T>
    where
        Old: Index<usize, Output = T> + ?Sized,
        New: Index<usize, Output = T> + ?Sized,
    {
        ChangesIter::new(old, new, *self)
    }

    /// Given a diffop yields the changes it encodes against the given slices.
    ///
    /// This is similar to [`DiffOp::iter_changes`] but instead of yielding the
    /// individual changes it yields consequitive changed slices.
    ///
    /// This will only ever yield a single tuple or two tuples in case a
    /// [`DiffOp::Replace`] operation is passed.
    ///
    /// ```rust
    /// use similar::{ChangeTag, Algorithm};
    /// use similar::capture_diff_slices;
    /// let old = vec!["foo", "bar", "baz"];
    /// let new = vec!["foo", "bar", "blah"];
    /// let ops = capture_diff_slices(Algorithm::Myers, &old, &new);
    /// let changes: Vec<_> = ops.iter().flat_map(|x| x.iter_slices(&old, &new)).collect();
    /// assert_eq!(changes, vec![
    ///     (ChangeTag::Equal, &["foo", "bar"][..]),
    ///     (ChangeTag::Delete, &["baz"][..]),
    ///     (ChangeTag::Insert, &["blah"][..]),
    /// ]);
    /// ```
    ///
    /// Due to lifetime restrictions it's currently impossible for the
    /// returned slices to outlive the lookup.
    pub fn iter_slices<'lookup, Old, New, T>(
        &self,
        old: &'lookup Old,
        new: &'lookup New,
    ) -> impl Iterator<Item = (ChangeTag, &'lookup T)>
    where
        T: 'lookup + ?Sized,
        Old: Index<Range<usize>, Output = T> + ?Sized,
        New: Index<Range<usize>, Output = T> + ?Sized,
    {
        match *self {
            DiffOp::Equal { old_index, len, .. } => {
                Some((ChangeTag::Equal, &old[old_index..old_index + len]))
                    .into_iter()
                    .chain(None.into_iter())
            }
            DiffOp::Insert {
                new_index, new_len, ..
            } => Some((ChangeTag::Insert, &new[new_index..new_index + new_len]))
                .into_iter()
                .chain(None.into_iter()),
            DiffOp::Delete {
                old_index, old_len, ..
            } => Some((ChangeTag::Delete, &old[old_index..old_index + old_len]))
                .into_iter()
                .chain(None.into_iter()),
            DiffOp::Replace {
                old_index,
                old_len,
                new_index,
                new_len,
            } => Some((ChangeTag::Delete, &old[old_index..old_index + old_len]))
                .into_iter()
                .chain(Some((ChangeTag::Insert, &new[new_index..new_index + new_len])).into_iter()),
        }
    }

    pub(crate) fn is_empty(&self) -> bool {
        let (_, old, new) = self.as_tag_tuple();
        is_empty_range(&old) && is_empty_range(&new)
    }

    pub(crate) fn shift_left(&mut self, adjust: usize) {
        self.adjust((adjust, true), (0, false));
    }

    pub(crate) fn shift_right(&mut self, adjust: usize) {
        self.adjust((adjust, false), (0, false));
    }

    pub(crate) fn grow_left(&mut self, adjust: usize) {
        self.adjust((adjust, true), (adjust, false));
    }

    pub(crate) fn grow_right(&mut self, adjust: usize) {
        self.adjust((0, false), (adjust, false));
    }

    pub(crate) fn shrink_left(&mut self, adjust: usize) {
        self.adjust((0, false), (adjust, true));
    }

    pub(crate) fn shrink_right(&mut self, adjust: usize) {
        self.adjust((adjust, false), (adjust, true));
    }

    fn adjust(&mut self, adjust_offset: (usize, bool), adjust_len: (usize, bool)) {
        #[inline(always)]
        fn modify(val: &mut usize, adj: (usize, bool)) {
            if adj.1 {
                *val -= adj.0;
            } else {
                *val += adj.0;
            }
        }

        match self {
            DiffOp::Equal {
                old_index,
                new_index,
                len,
            } => {
                modify(old_index, adjust_offset);
                modify(new_index, adjust_offset);
                modify(len, adjust_len);
            }
            DiffOp::Delete {
                old_index,
                old_len,
                new_index,
            } => {
                modify(old_index, adjust_offset);
                modify(old_len, adjust_len);
                modify(new_index, adjust_offset);
            }
            DiffOp::Insert {
                old_index,
                new_index,
                new_len,
            } => {
                modify(old_index, adjust_offset);
                modify(new_index, adjust_offset);
                modify(new_len, adjust_len);
            }
            DiffOp::Replace {
                old_index,
                old_len,
                new_index,
                new_len,
            } => {
                modify(old_index, adjust_offset);
                modify(old_len, adjust_len);
                modify(new_index, adjust_offset);
                modify(new_len, adjust_len);
            }
        }
    }
}

#[cfg(feature = "text")]
mod text_additions {
    use super::*;
    use crate::text::DiffableStr;
    use std::borrow::Cow;

    /// The text interface can produce changes over [`DiffableStr`] implementing
    /// values.  As those are generic interfaces for different types of strings
    /// utility methods to make working with standard rust strings more enjoyable.
    impl<'s, T: DiffableStr + ?Sized> Change<&'s T> {
        /// Returns the value as string if it is utf-8.
        pub fn as_str(&self) -> Option<&'s str> {
            T::as_str(self.value)
        }

        /// Returns the value (lossy) decoded as utf-8 string.
        pub fn to_string_lossy(&self) -> Cow<'s, str> {
            T::to_string_lossy(self.value)
        }

        /// Returns `true` if this change does not end in a newline and must be
        /// followed up by one if line based diffs are used.
        ///
        /// The [`std::fmt::Display`] implementation of [`Change`] will automatically
        /// insert a newline after the value if this is true.
        pub fn missing_newline(&self) -> bool {
            !T::ends_with_newline(self.value)
        }
    }

    impl<'s, T: DiffableStr + ?Sized> fmt::Display for Change<&'s T> {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(
                f,
                "{}{}",
                self.to_string_lossy(),
                if self.missing_newline() { "\n" } else { "" }
            )
        }
    }
}