pub enum DiffOp {
Equal {
old_index: usize,
new_index: usize,
len: usize,
},
Delete {
old_index: usize,
old_len: usize,
new_index: usize,
},
Insert {
old_index: usize,
new_index: usize,
new_len: usize,
},
Replace {
old_index: usize,
old_len: usize,
new_index: usize,
new_len: usize,
},
}
Expand description
Utility enum to capture a diff operation.
This is used by Capture
.
Variants
Equal
Fields
old_index: usize
The starting index in the old sequence.
new_index: usize
The starting index in the new sequence.
len: usize
The length of the segment.
A segment is equal (see DiffHook::equal
)
Delete
Fields
old_index: usize
The starting index in the old sequence.
old_len: usize
The length of the old segment.
new_index: usize
The starting index in the new sequence.
A segment was deleted (see DiffHook::delete
)
Insert
Fields
old_index: usize
The starting index in the old sequence.
new_index: usize
The starting index in the new sequence.
new_len: usize
The length of the new segment.
A segment was inserted (see DiffHook::insert
)
Replace
Fields
old_index: usize
The starting index in the old sequence.
old_len: usize
The length of the old segment.
new_index: usize
The starting index in the new sequence.
new_len: usize
The length of the new segment.
A segment was replaced (see DiffHook::replace
)
Implementations
sourceimpl DiffOp
impl DiffOp
sourcepub fn as_tag_tuple(&self) -> (DiffTag, Range<usize>, Range<usize>)
pub fn as_tag_tuple(&self) -> (DiffTag, Range<usize>, Range<usize>)
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 byb[j1..j2]
Delete
:a[i1..i2]
should be deleted (j1 == j2
in this case).Insert
:b[j1..j2]
should be inserted ata[i1..i2]
(i1 == i2
in this case).Equal
:a[i1..i2]
is equal tob[j1..j2]
.
sourcepub fn apply_to_hook<D: DiffHook>(&self, d: &mut D) -> Result<(), D::Error>
pub fn apply_to_hook<D: DiffHook>(&self, d: &mut D) -> Result<(), D::Error>
Apply this operation to a diff hook.
sourcepub 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,
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,
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.
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"),
]);
sourcepub 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,
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,
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.
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.