Enum similar::DiffOp

source ·
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

§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

§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

§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

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

source§

impl DiffOp

source

pub fn tag(self) -> DiffTag

Returns the tag of the operation.

source

pub fn old_range(&self) -> Range<usize>

Returns the old range.

source

pub fn new_range(&self) -> Range<usize>

Returns the new range.

source

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

pub fn apply_to_hook<D: DiffHook>(&self, d: &mut D) -> Result<(), D::Error>

Apply this operation to a diff hook.

source

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"),
]);
source

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.

Trait Implementations§

source§

impl Clone for DiffOp

source§

fn clone(&self) -> DiffOp

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for DiffOp

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for DiffOp

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Hash for DiffOp

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for DiffOp

source§

fn eq(&self, other: &DiffOp) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for DiffOp

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Copy for DiffOp

source§

impl Eq for DiffOp

source§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,