Struct similar::TextDiffConfig

source ·
pub struct TextDiffConfig { /* private fields */ }
Expand description

A builder type config for more complex uses of TextDiff.

Requires the text feature.

Implementations§

source§

impl TextDiffConfig

source

pub fn algorithm(&mut self, alg: Algorithm) -> &mut Self

Changes the algorithm.

The default algorithm is Algorithm::Myers.

source

pub fn deadline(&mut self, deadline: Instant) -> &mut Self

Sets a deadline for the diff operation.

By default a diff will take as long as it takes. For certain diff algorithms like Myer’s and Patience a maximum running time can be defined after which the algorithm gives up and approximates.

source

pub fn timeout(&mut self, timeout: Duration) -> &mut Self

Sets a timeout for thediff operation.

This is like deadline but accepts a duration.

source

pub fn newline_terminated(&mut self, yes: bool) -> &mut Self

Changes the newline termination flag.

The default is automatic based on input. This flag controls the behavior of TextDiff::iter_changes and unified diff generation with regards to newlines. When the flag is set to false (which is the default) then newlines are added. Otherwise the newlines from the source sequences are reused.

source

pub fn diff_lines<'old, 'new, 'bufs, T: DiffableStrRef + ?Sized>( &self, old: &'old T, new: &'new T, ) -> TextDiff<'old, 'new, 'bufs, T::Output>

Creates a diff of lines.

This splits the text old and new into lines preserving newlines in the input. Line diffs are very common and because of that enjoy special handling in similar. When a line diff is created with this method the newline_terminated flag is flipped to true and will influence the behavior of unified diff generation.

use similar::{TextDiff, ChangeTag};

let diff = TextDiff::configure().diff_lines("a\nb\nc", "a\nb\nC");
let changes: Vec<_> = diff
    .iter_all_changes()
    .map(|x| (x.tag(), x.value()))
    .collect();

assert_eq!(changes, vec![
   (ChangeTag::Equal, "a\n"),
   (ChangeTag::Equal, "b\n"),
   (ChangeTag::Delete, "c"),
   (ChangeTag::Insert, "C"),
]);
source

pub fn diff_words<'old, 'new, 'bufs, T: DiffableStrRef + ?Sized>( &self, old: &'old T, new: &'new T, ) -> TextDiff<'old, 'new, 'bufs, T::Output>

Creates a diff of words.

This splits the text into words and whitespace.

Note on word diffs: because the text differ will tokenize the strings into small segments it can be inconvenient to work with the results depending on the use case. You might also want to combine word level diffs with the TextDiffRemapper which lets you remap the diffs back to the original input strings.

use similar::{TextDiff, ChangeTag};

let diff = TextDiff::configure().diff_words("foo bar baz", "foo BAR baz");
let changes: Vec<_> = diff
    .iter_all_changes()
    .map(|x| (x.tag(), x.value()))
    .collect();

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

pub fn diff_chars<'old, 'new, 'bufs, T: DiffableStrRef + ?Sized>( &self, old: &'old T, new: &'new T, ) -> TextDiff<'old, 'new, 'bufs, T::Output>

Creates a diff of characters.

Note on character diffs: because the text differ will tokenize the strings into small segments it can be inconvenient to work with the results depending on the use case. You might also want to combine word level diffs with the TextDiffRemapper which lets you remap the diffs back to the original input strings.

use similar::{TextDiff, ChangeTag};

let diff = TextDiff::configure().diff_chars("abcdef", "abcDDf");
let changes: Vec<_> = diff
    .iter_all_changes()
    .map(|x| (x.tag(), x.value()))
    .collect();

assert_eq!(changes, vec![
   (ChangeTag::Equal, "a"),
   (ChangeTag::Equal, "b"),
   (ChangeTag::Equal, "c"),
   (ChangeTag::Delete, "d"),
   (ChangeTag::Delete, "e"),
   (ChangeTag::Insert, "D"),
   (ChangeTag::Insert, "D"),
   (ChangeTag::Equal, "f"),
]);
source

pub fn diff_unicode_words<'old, 'new, 'bufs, T: DiffableStrRef + ?Sized>( &self, old: &'old T, new: &'new T, ) -> TextDiff<'old, 'new, 'bufs, T::Output>

Creates a diff of unicode words.

This splits the text into words according to unicode rules. This is generally recommended over TextDiffConfig::diff_words but requires a dependency.

This requires the unicode feature.

Note on word diffs: because the text differ will tokenize the strings into small segments it can be inconvenient to work with the results depending on the use case. You might also want to combine word level diffs with the TextDiffRemapper which lets you remap the diffs back to the original input strings.

use similar::{TextDiff, ChangeTag};

let diff = TextDiff::configure().diff_unicode_words("ah(be)ce", "ah(ah)ce");
let changes: Vec<_> = diff
    .iter_all_changes()
    .map(|x| (x.tag(), x.value()))
    .collect();

assert_eq!(changes, vec![
   (ChangeTag::Equal, "ah"),
   (ChangeTag::Equal, "("),
   (ChangeTag::Delete, "be"),
   (ChangeTag::Insert, "ah"),
   (ChangeTag::Equal, ")"),
   (ChangeTag::Equal, "ce"),
]);
source

pub fn diff_graphemes<'old, 'new, 'bufs, T: DiffableStrRef + ?Sized>( &self, old: &'old T, new: &'new T, ) -> TextDiff<'old, 'new, 'bufs, T::Output>

Creates a diff of graphemes.

This requires the unicode feature.

Note on grapheme diffs: because the text differ will tokenize the strings into small segments it can be inconvenient to work with the results depending on the use case. You might also want to combine word level diffs with the TextDiffRemapper which lets you remap the diffs back to the original input strings.

use similar::{TextDiff, ChangeTag};

let diff = TextDiff::configure().diff_graphemes("💩🇦🇹🦠", "💩🇦🇱❄️");
let changes: Vec<_> = diff
    .iter_all_changes()
    .map(|x| (x.tag(), x.value()))
    .collect();

assert_eq!(changes, vec![
   (ChangeTag::Equal, "💩"),
   (ChangeTag::Delete, "🇦🇹"),
   (ChangeTag::Delete, "🦠"),
   (ChangeTag::Insert, "🇦🇱"),
   (ChangeTag::Insert, "❄️"),
]);
source

pub fn diff_slices<'old, 'new, 'bufs, T: DiffableStr + ?Sized>( &self, old: &'bufs [&'old T], new: &'bufs [&'new T], ) -> TextDiff<'old, 'new, 'bufs, T>

Creates a diff of arbitrary slices.

use similar::{TextDiff, ChangeTag};

let old = &["foo", "bar", "baz"];
let new = &["foo", "BAR", "baz"];
let diff = TextDiff::configure().diff_slices(old, new);
let changes: Vec<_> = diff
    .iter_all_changes()
    .map(|x| (x.tag(), x.value()))
    .collect();

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

Trait Implementations§

source§

impl Clone for TextDiffConfig

source§

fn clone(&self) -> TextDiffConfig

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 TextDiffConfig

source§

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

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

impl Default for TextDiffConfig

source§

fn default() -> TextDiffConfig

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.