Skip to main content

UnifiedDiffOptions

Struct UnifiedDiffOptions 

Source
#[non_exhaustive]
pub struct UnifiedDiffOptions<'a> { pub old_label: Option<DiffLabel<'a>>, pub new_label: Option<DiffLabel<'a>>, pub path_quoting: PathQuoting, pub context_lines: usize, pub inter_hunk_context: usize, pub funcname: Option<&'a FuncnameMatcher>, pub algorithm: DiffAlgorithm, pub indent_heuristic: IndentHeuristic, pub extent: HunkExtent, pub anchors: Vec<String>, pub whitespace: WhitespaceMode, pub no_newline_marker: NoNewlineMarker, }
Expand description

Everything that shapes a unified diff of two text sides.

Default matches git diff --no-index with no options, except that no --- / +++ labels are set, so the output starts at the first @@ hunk header. Set labels with with_paths or with_labels.

§Examples

use grit_lib::diff::{unified_diff, DiffAlgorithm, UnifiedDiffOptions};

let options = UnifiedDiffOptions::new()
    .with_paths("greeting.txt", "greeting.txt")
    .with_algorithm(DiffAlgorithm::Histogram)
    .with_context_lines(1);
let patch = unified_diff("hello\nworld\n", "hello\nthere\n", &options);
assert_eq!(
    patch,
    "--- a/greeting.txt\n+++ b/greeting.txt\n@@ -1,2 +1,2 @@\n hello\n-world\n+there\n"
);

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§old_label: Option<DiffLabel<'a>>

Label for the --- line; None omits the line.

§new_label: Option<DiffLabel<'a>>

Label for the +++ line; None omits the line.

§path_quoting: PathQuoting

Quoting for DiffLabel::Path labels.

§context_lines: usize

Unchanged lines kept around each change (Git -U, default 3).

§inter_hunk_context: usize

Extra unchanged lines allowed between two changes before they split into separate hunks (Git --inter-hunk-context, default 0).

§funcname: Option<&'a FuncnameMatcher>

Function-name rules for the text after each @@ … @@ header and for HunkExtent::Function. None uses Git’s default rule: the nearest earlier line that starts with a letter, _ or $.

§algorithm: DiffAlgorithm

Line alignment algorithm. Ignored when anchors is non-empty, because anchored diffs always use patience alignment, as in Git.

§indent_heuristic: IndentHeuristic

Placement of changes whose boundaries are ambiguous.

§extent: HunkExtent

How far hunks reach: context lines, or the whole enclosing function (-W).

§anchors: Vec<String>

--anchored prefixes: a line that starts with one of them and occurs exactly once on each side is kept as unchanged context instead of appearing as a move.

§whitespace: WhitespaceMode

Whitespace rules for comparing lines (-w, -b, --ignore-space-at-eol, --ignore-blank-lines). Patch lines always show the original text.

§no_newline_marker: NoNewlineMarker

Whether \ No newline at end of file markers are written.

Implementations§

Source§

impl<'a> UnifiedDiffOptions<'a>

Source

pub fn new() -> Self

Same as Default::default: Git’s defaults with no header labels.

Source

pub fn with_paths(self, old_path: &'a str, new_path: &'a str) -> Self

Label the sides a/<old_path> and b/<new_path>, as git diff does.

Source

pub fn with_labels(self, old: DiffLabel<'a>, new: DiffLabel<'a>) -> Self

Set both --- / +++ labels.

Source

pub fn with_path_quoting(self, path_quoting: PathQuoting) -> Self

Set how path labels quote unusual bytes.

Source

pub fn with_context_lines(self, context_lines: usize) -> Self

Set the context line count (-U).

Source

pub fn with_inter_hunk_context(self, inter_hunk_context: usize) -> Self

Set the inter-hunk context (--inter-hunk-context).

Source

pub fn with_funcname(self, matcher: &'a FuncnameMatcher) -> Self

Use matcher for hunk-header function names and function context.

Source

pub fn with_algorithm(self, algorithm: DiffAlgorithm) -> Self

Set the line alignment algorithm.

Source

pub fn with_indent_heuristic(self, indent_heuristic: IndentHeuristic) -> Self

Set the indent heuristic.

Source

pub fn with_extent(self, extent: HunkExtent) -> Self

Set how far hunks reach (HunkExtent::Function is Git’s -W).

Source

pub fn with_anchors<I, S>(self, anchors: I) -> Self
where I: IntoIterator<Item = S>, S: Into<String>,

Replace the --anchored prefixes.

Source

pub fn with_whitespace(self, whitespace: WhitespaceMode) -> Self

Set the whitespace comparison rules.

Source

pub fn with_no_newline_marker(self, marker: NoNewlineMarker) -> Self

Choose whether \ No newline at end of file markers are written.

Trait Implementations§

Source§

impl<'a> Clone for UnifiedDiffOptions<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for UnifiedDiffOptions<'a>

Source§

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

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

impl Default for UnifiedDiffOptions<'_>

Source§

fn default() -> Self

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

impl From<&DiffOptions> for UnifiedDiffOptions<'_>

Carries the line settings of a repository diff (context, inter-hunk context, algorithm, indent heuristic, whitespace rules and -W) over to a text diff. Labels, function-name rules and anchors stay at their defaults.

Source§

fn from(options: &DiffOptions) -> Self

Converts to this type from the input type.

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§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

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

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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.