Enum similar::DiffOp[][src]

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,
    },
}

Utility enum to capture a diff operation.

This is used by Capture.

Variants

Equal

A segment is equal (see DiffHook::equal)

Fields of Equal

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.

Delete

A segment was deleted (see DiffHook::delete)

Fields of Delete

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.

Insert

A segment was inserted (see DiffHook::insert)

Fields of Insert

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.

Replace

A segment was replaced (see DiffHook::replace)

Fields of Replace

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.

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 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 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]

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]

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]

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]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.