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

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

Returns the tag of the operation.

Returns the old range.

Returns the new range.

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

Apply this operation to a diff hook.

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

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

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.