metamon/diff

Structural diff for failure reports. Compares two values element-by-element and produces a tree that the formatter can render compactly.

The implementation is type-erased: it inspects the dynamic shape of each side via string.inspect. This is sufficient for the failure-report use case (we only need to display the diff, not reason about it programmatically), and avoids requiring users to supply per-type formatters.

Types

One node in a diff tree.

pub type Diff {
  Same(rendered: String)
  Differ(left: String, right: String)
  ListDiff(items: List(IndexedDiff))
  TupleDiff(items: List(IndexedDiff))
  StringDiff(
    left: String,
    right: String,
    segments: List(Segment),
  )
}

Constructors

  • Same(rendered: String)

    Both sides identical at this position.

  • Differ(left: String, right: String)

    Sides differ — show both rendered values.

  • ListDiff(items: List(IndexedDiff))

    Element-wise diff for lists.

  • TupleDiff(items: List(IndexedDiff))

    Element-wise diff for tuples.

  • StringDiff(left: String, right: String, segments: List(Segment))

    Multi-line diff for strings (line-by-line LCS).

A list element together with its position.

pub type IndexedDiff {
  IndexedDiff(index: Int, diff: Diff)
}

Constructors

  • IndexedDiff(index: Int, diff: Diff)

One section of a multi-line string diff.

pub type Segment {
  Common(text: String)
  Removed(text: String)
  Added(text: String)
}

Constructors

  • Common(text: String)
  • Removed(text: String)
  • Added(text: String)

Values

pub fn diff(left: a, right: a) -> Diff

Compute a structural diff between two values. Equal inputs collapse to Same; otherwise the result depends on whether string.inspect exposes list-shaped or string-shaped output.

pub fn diff_string(left: String, right: String) -> Diff

Multi-line string diff using a longest-common-subsequence (LCS) on lines. Lines unique to the left side are shown as Removed, lines unique to the right are shown as Added, lines present in both are shown as Common.

pub fn render(d: Diff) -> String

Render a Diff as a human-readable multi-line string.

Search Document