pub struct TextDiffRemapper<'x, T: ?Sized> { /* private fields */ }
Expand description

A remapper that can remap diff ops to the original slices.

The idea here is that when a TextDiff is created from two strings and the internal tokenization is used, this remapper can take a range in the tokenized sequences and remap it to the original string. This is particularly useful when you want to do things like character or grapheme level diffs but you want to not have to iterate over small sequences but large consequitive ones from the source.

use similar::{ChangeTag, TextDiff};
use similar::utils::TextDiffRemapper;

let old = "yo! foo bar baz";
let new = "yo! foo bor baz";
let diff = TextDiff::from_words(old, new);
let remapper = TextDiffRemapper::from_text_diff(&diff, old, new);
let changes: Vec<_> = diff.ops()
    .iter()
    .flat_map(move |x| remapper.iter_slices(x))
    .collect();

assert_eq!(changes, vec![
    (ChangeTag::Equal, "yo! foo "),
    (ChangeTag::Delete, "bar"),
    (ChangeTag::Insert, "bor"),
    (ChangeTag::Equal, " baz")
]);

Implementations§

source§

impl<'x, T: DiffableStr + ?Sized> TextDiffRemapper<'x, T>

source

pub fn new( old_slices: &[&'x T], new_slices: &[&'x T], old: &'x T, new: &'x T ) -> TextDiffRemapper<'x, T>

Creates a new remapper from strings and slices.

source

pub fn from_text_diff<'old, 'new, 'bufs>( diff: &TextDiff<'old, 'new, 'bufs, T>, old: &'x T, new: &'x T ) -> TextDiffRemapper<'x, T>
where 'old: 'x, 'new: 'x,

Creates a new remapper from a text diff and the original strings.

source

pub fn slice_old(&self, range: Range<usize>) -> Option<&'x T>

Slices into the old string.

source

pub fn slice_new(&self, range: Range<usize>) -> Option<&'x T>

Slices into the new string.

source

pub fn iter_slices( &self, op: &DiffOp ) -> impl Iterator<Item = (ChangeTag, &'x T)>

Given a diffop yields the changes it encodes against the original strings.

This is the same as the DiffOp::iter_slices method.

§Panics

This method can panic if the input strings passed to the constructor are incompatible with the input strings passed to the diffing algorithm.

Auto Trait Implementations§

§

impl<'x, T: ?Sized> Freeze for TextDiffRemapper<'x, T>

§

impl<'x, T: ?Sized> RefUnwindSafe for TextDiffRemapper<'x, T>
where T: RefUnwindSafe,

§

impl<'x, T: ?Sized> Send for TextDiffRemapper<'x, T>
where T: Sync,

§

impl<'x, T: ?Sized> Sync for TextDiffRemapper<'x, T>
where T: Sync,

§

impl<'x, T: ?Sized> Unpin for TextDiffRemapper<'x, T>

§

impl<'x, T: ?Sized> UnwindSafe for TextDiffRemapper<'x, T>
where T: RefUnwindSafe,

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