Enum similar::DiffOp [−][src]
Utility enum to capture a diff operation.
This is used by Capture
.
Variants
A segment is equal (see DiffHook::equal
)
Fields of Equal
A segment was deleted (see DiffHook::delete
)
Fields of Delete
A segment was inserted (see DiffHook::insert
)
Fields of Insert
A segment was replaced (see DiffHook::replace
)
Fields of Replace
Implementations
impl DiffOp
[src]
pub fn tag(self) -> DiffTag
[src]
Returns the tag of the operation.
pub fn old_range(&self) -> Range<usize>
[src]
Returns the old range.
pub fn new_range(&self) -> Range<usize>
[src]
Returns the new range.
pub fn as_tag_tuple(&self) -> (DiffTag, Range<usize>, Range<usize>)
[src]
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]
.
pub fn apply_to_hook<D: DiffHook>(&self, d: &mut D) -> Result<(), D::Error>
[src]
Apply this operation to a diff hook.
pub fn iter_changes<'x, 'lookup, Old: ?Sized, New: ?Sized, T: ?Sized>(
&self,
old: &'lookup Old,
new: &'lookup New
) -> ChangesIter<'lookup, 'x, Old, New, T>ⓘNotable traits for ChangesIter<'lookup, 'data, Old, New, T>
impl<'lookup, 'data, Old: ?Sized, New: ?Sized, T: ?Sized> Iterator for ChangesIter<'lookup, 'data, Old, New, T> where
Old: Index<usize, Output = &'data T>,
New: Index<usize, Output = &'data T>,
T: 'data,
'data: 'lookup, type Item = Change<'data, T>;
where
Old: Index<usize, Output = &'x T>,
New: Index<usize, Output = &'x T>,
T: 'x,
'x: 'lookup,
[src]
&self,
old: &'lookup Old,
new: &'lookup New
) -> ChangesIter<'lookup, 'x, Old, New, T>ⓘ
Notable traits for ChangesIter<'lookup, 'data, Old, New, T>
impl<'lookup, 'data, Old: ?Sized, New: ?Sized, T: ?Sized> Iterator for ChangesIter<'lookup, 'data, Old, New, T> where
Old: Index<usize, Output = &'data T>,
New: Index<usize, Output = &'data T>,
T: 'data,
'data: 'lookup, type Item = Change<'data, T>;
Old: Index<usize, Output = &'x T>,
New: Index<usize, Output = &'x T>,
T: 'x,
'x: 'lookup,
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"), ]);
pub fn iter_slices<'lookup, Old: ?Sized, New: ?Sized, T: ?Sized>(
&self,
old: &'lookup Old,
new: &'lookup New
) -> impl Iterator<Item = (ChangeTag, &'lookup T)> where
T: 'lookup,
Old: Index<Range<usize>, Output = T>,
New: Index<Range<usize>, Output = T>,
[src]
&self,
old: &'lookup Old,
new: &'lookup New
) -> impl Iterator<Item = (ChangeTag, &'lookup T)> where
T: 'lookup,
Old: Index<Range<usize>, Output = T>,
New: Index<Range<usize>, Output = 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
impl Clone for DiffOp
[src]
impl Copy for DiffOp
[src]
impl Debug for DiffOp
[src]
impl Eq for DiffOp
[src]
impl Hash for DiffOp
[src]
fn hash<__H: Hasher>(&self, state: &mut __H)
[src]
pub fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
H: Hasher,
impl PartialEq<DiffOp> for DiffOp
[src]
impl StructuralEq for DiffOp
[src]
impl StructuralPartialEq for DiffOp
[src]
Auto Trait Implementations
impl RefUnwindSafe for DiffOp
impl Send for DiffOp
impl Sync for DiffOp
impl Unpin for DiffOp
impl UnwindSafe for DiffOp
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,