#[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
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: PathQuotingQuoting for DiffLabel::Path labels.
context_lines: usizeUnchanged lines kept around each change (Git -U, default 3).
inter_hunk_context: usizeExtra 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: DiffAlgorithmLine alignment algorithm. Ignored when anchors is non-empty, because
anchored diffs always use patience alignment, as in Git.
indent_heuristic: IndentHeuristicPlacement of changes whose boundaries are ambiguous.
extent: HunkExtentHow 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: WhitespaceModeWhitespace rules for comparing lines (-w, -b, --ignore-space-at-eol,
--ignore-blank-lines). Patch lines always show the original text.
no_newline_marker: NoNewlineMarkerWhether \ No newline at end of file markers are written.
Implementations§
Source§impl<'a> UnifiedDiffOptions<'a>
impl<'a> UnifiedDiffOptions<'a>
Sourcepub fn new() -> Self
pub fn new() -> Self
Same as Default::default: Git’s defaults with no header labels.
Sourcepub fn with_paths(self, old_path: &'a str, new_path: &'a str) -> Self
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.
Sourcepub fn with_labels(self, old: DiffLabel<'a>, new: DiffLabel<'a>) -> Self
pub fn with_labels(self, old: DiffLabel<'a>, new: DiffLabel<'a>) -> Self
Set both --- / +++ labels.
Sourcepub fn with_path_quoting(self, path_quoting: PathQuoting) -> Self
pub fn with_path_quoting(self, path_quoting: PathQuoting) -> Self
Set how path labels quote unusual bytes.
Sourcepub fn with_context_lines(self, context_lines: usize) -> Self
pub fn with_context_lines(self, context_lines: usize) -> Self
Set the context line count (-U).
Sourcepub fn with_inter_hunk_context(self, inter_hunk_context: usize) -> Self
pub fn with_inter_hunk_context(self, inter_hunk_context: usize) -> Self
Set the inter-hunk context (--inter-hunk-context).
Sourcepub fn with_funcname(self, matcher: &'a FuncnameMatcher) -> Self
pub fn with_funcname(self, matcher: &'a FuncnameMatcher) -> Self
Use matcher for hunk-header function names and function context.
Sourcepub fn with_algorithm(self, algorithm: DiffAlgorithm) -> Self
pub fn with_algorithm(self, algorithm: DiffAlgorithm) -> Self
Set the line alignment algorithm.
Sourcepub fn with_indent_heuristic(self, indent_heuristic: IndentHeuristic) -> Self
pub fn with_indent_heuristic(self, indent_heuristic: IndentHeuristic) -> Self
Set the indent heuristic.
Sourcepub fn with_extent(self, extent: HunkExtent) -> Self
pub fn with_extent(self, extent: HunkExtent) -> Self
Set how far hunks reach (HunkExtent::Function is Git’s -W).
Sourcepub fn with_anchors<I, S>(self, anchors: I) -> Self
pub fn with_anchors<I, S>(self, anchors: I) -> Self
Replace the --anchored prefixes.
Sourcepub fn with_whitespace(self, whitespace: WhitespaceMode) -> Self
pub fn with_whitespace(self, whitespace: WhitespaceMode) -> Self
Set the whitespace comparison rules.
Sourcepub fn with_no_newline_marker(self, marker: NoNewlineMarker) -> Self
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>
impl<'a> Clone for UnifiedDiffOptions<'a>
Source§impl<'a> Debug for UnifiedDiffOptions<'a>
impl<'a> Debug for UnifiedDiffOptions<'a>
Source§impl Default for UnifiedDiffOptions<'_>
impl Default for UnifiedDiffOptions<'_>
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.
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.