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
A segment is equal (see DiffHook::equal
)
Fields
Delete
A segment was deleted (see DiffHook::delete
)
Fields
Insert
A segment was inserted (see DiffHook::insert
)
Fields
Replace
A segment was replaced (see DiffHook::replace
)
Implementations§
source§impl 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> ⓘ
pub fn iter_changes<'lookup, Old, New, T>( &self, old: &'lookup Old, new: &'lookup New, ) -> ChangesIter<'lookup, Old, New, T> ⓘ
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)>
pub fn iter_slices<'lookup, Old, New, T>( &self, old: &'lookup Old, new: &'lookup New, ) -> impl Iterator<Item = (ChangeTag, &'lookup T)>
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.
Trait Implementations§
source§impl PartialEq for DiffOp
impl PartialEq for DiffOp
impl Copy for DiffOp
impl Eq for DiffOp
impl StructuralPartialEq for DiffOp
Auto Trait Implementations§
impl Freeze for DiffOp
impl RefUnwindSafe for DiffOp
impl Send for DiffOp
impl Sync for DiffOp
impl Unpin for DiffOp
impl UnwindSafe for DiffOp
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)