Struct similar::udiff::UnifiedDiff

source ·
pub struct UnifiedDiff<'diff, 'old, 'new, 'bufs, T: DiffableStr + ?Sized> { /* private fields */ }
Expand description

Unified diff formatter.

use similar::TextDiff;
let text_diff = TextDiff::from_lines(old_text, new_text);
print!("{}", text_diff
    .unified_diff()
    .context_radius(10)
    .header("old_file", "new_file"));

§Unicode vs Bytes

The UnifiedDiff type supports both unicode and byte diffs for all types compatible with DiffableStr. You can pick between the two versions by using [UnifiedDiff.to_string] or [UnifiedDiff.to_writer]. The former uses DiffableStr::to_string_lossy, the latter uses DiffableStr::as_bytes for each line.

Implementations§

source§

impl<'diff, 'old, 'new, 'bufs, T: DiffableStr + ?Sized> UnifiedDiff<'diff, 'old, 'new, 'bufs, T>

source

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

Creates a formatter from a text diff object.

source

pub fn context_radius(&mut self, n: usize) -> &mut Self

Changes the context radius.

The context radius is the number of lines between changes that should be emitted. This defaults to 3.

source

pub fn header(&mut self, a: &str, b: &str) -> &mut Self

Sets a header to the diff.

a and b are the file names that are added to the top of the unified file format. The names are accepted verbatim which lets you encode a timestamp into it when separated by a tab (\t). For more information, see the unified diff format specification.

source

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

Controls the missing newline hint.

By default a special \ No newline at end of file marker is added to the output when a file is not terminated with a final newline. This can be disabled with this flag.

source

pub fn iter_hunks( &self, ) -> impl Iterator<Item = UnifiedDiffHunk<'diff, 'old, 'new, 'bufs, T>>

Iterates over all hunks as configured.

source

pub fn to_writer<W: Write>(&self, w: W) -> Result<(), Error>
where 'diff: 'old + 'new + 'bufs,

Write the unified diff as bytes to the output stream.

Trait Implementations§

source§

impl<'diff, 'old, 'new, 'bufs, T: DiffableStr + ?Sized> Display for UnifiedDiff<'diff, 'old, 'new, 'bufs, T>
where 'diff: 'old + 'new + 'bufs,

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'diff, 'old, 'new, 'bufs, T> Freeze for UnifiedDiff<'diff, 'old, 'new, 'bufs, T>
where T: ?Sized,

§

impl<'diff, 'old, 'new, 'bufs, T> RefUnwindSafe for UnifiedDiff<'diff, 'old, 'new, 'bufs, T>
where T: RefUnwindSafe + ?Sized,

§

impl<'diff, 'old, 'new, 'bufs, T> Send for UnifiedDiff<'diff, 'old, 'new, 'bufs, T>
where T: Sync + ?Sized,

§

impl<'diff, 'old, 'new, 'bufs, T> Sync for UnifiedDiff<'diff, 'old, 'new, 'bufs, T>
where T: Sync + ?Sized,

§

impl<'diff, 'old, 'new, 'bufs, T> Unpin for UnifiedDiff<'diff, 'old, 'new, 'bufs, T>
where T: ?Sized,

§

impl<'diff, 'old, 'new, 'bufs, T> UnwindSafe for UnifiedDiff<'diff, 'old, 'new, 'bufs, T>
where T: RefUnwindSafe + ?Sized,

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.