Skip to main content

Module diff

Module diff 

Source
Expand description

#656: one parse of a unified diff, in core, for three surfaces to draw.

patch already returns a small unified diff (tools_patch::render_diff), and every surface threw that shape away — the TUI by drawing one line per call, the REPL by collapsing whitespace with clip, the dashboard by putting it in a <pre>. Each of those is a rendering decision, and none of them can be checked by a test that lives in a page.

So the parse lives here, where cargo test reaches it, and the renderers only choose colours, a cap and how many rows to show. Three surfaces that agree by parsing the same rows once is a weaker guarantee than three that each re-read the text, and it is the difference between a change that reads at a glance and one that has to be scanned for + and - by eye.

Nothing in this module knows what a terminal, a browser or a prompt is. It has no caller until the renderers land (#656b), which is deliberate: a parse with tests and no consumer is inert, and it can land beside the audit batch without touching a line of it.

Two rules are worth stating because they are the ones a page gets wrong:

  • A file preamble is not a change. --- a/x / +++ b/x before the first @@ are the two file names, not a removed and an added line. Read as changes they inflate every count by one in each direction, which is the sort of error nobody notices until they trust the number.
  • A partial line is shown as it is. render_diff deliberately emits a -/+ pair whose text is a fragment of a line when the match began mid-line. The fragment is what a person needs to see; pretending it is a whole line would be a different, wrong edit.

Structs§

Row
One row, with the line numbers it has on each side.

Enums§

Kind
What one row of a diff is.

Functions§

change
How many lines the edit adds and removes: (added, removed).
parse
Read a unified diff into rows.