Struct similar::TextDiffConfig[][src]

pub struct TextDiffConfig { /* fields omitted */ }

A builder type config for more complex uses of TextDiff.

Requires the text feature.

Implementations

impl TextDiffConfig[src]

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

Changes the algorithm.

The default algorithm is Algorithm::Myers.

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

impl Clone for TextDiffConfig[src]

impl Debug for TextDiffConfig[src]

impl Default for TextDiffConfig[src]

Auto Trait Implementations

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.