Skip to content

feat: formatter honors per-currency display precision (#1766) - #1807

Merged
robcohen merged 3 commits into
mainfrom
feat/format-display-precision
Jul 18, 2026
Merged

robcohen merged 3 commits into
mainfrom
feat/format-display-precision

Conversation

@robcohen

Copy link
Copy Markdown
Member

What

The remaining #1766 blocker: the rendering pipeline had no precision machinery anywhere (CLI included), which is why format-loaded couldn't honor display-precision and session.format was deliberately not shipped in #1805. This PR grows it at the canonical layer:

  • FormatConfig gains number_display: Option<DisplayContext> — reusing the canonical precision machinery the query/report renderers already use (per-currency fixed precisions from option \"display_precision\" + observed distributions), rather than duplicating it.
  • Every amount/cost/price number emission flows through one render_number chokepoint and config-threaded formatters, so the two behaviors (context-padded vs historical own-scale) cannot drift per call site. None is byte-identical to the historical rendering.
  • Deliberate decision: thousands separators are NOT emitted in ledger text even when the context has render_commas — canonical form carries no separators (the CST canonicalizer strips them by definition, matching bean-format), and render_number strips any grouping so direct emitters agree byte-for-byte with the canonicalize shim. Commas remain a report/query display concern, honored where they always were. This surfaced from a failing test: pass 1 emitted 1,234.50, pass 2 canonicalized it away — the test now pins the resolved semantics.
  • Bare interpolation-target numbers (no currency) keep their own scale.

Tests

Core render_number + directive-threading tests; a parser-level test pins precision padding through the two-pass canonicalize shim and byte-fidelity under the default config. Full workspace green (3937 tests).

Next

The follow-up PR adds WIT session.format building the DisplayContext from held options — completing #1766's format surface.

Part of #1766.

🤖 Generated with Claude Code

https://claude.ai/code/session_013gJH7uBBF9Xw9et9QFRcsU

The rendering pipeline had no precision machinery anywhere - the CLI
included - which blocked options-aware rendering for format-loaded and
a future session.format. This grows it at the canonical layer:

- FormatConfig gains number_display: Option<DisplayContext> (the
  canonical precision machinery the query/report renderers already
  use). When set, every amount/cost/price number renders through
  DisplayContext::format - per-currency precision padding from
  option "display_precision" and observed distributions. None keeps
  the historical own-scale byte-exact rendering.
- All number emission flows through one render_number chokepoint plus
  config-threaded formatters (amount_split, posting_amount_split,
  format_amount_with, format_cost_spec, format_price_annotation,
  format_incomplete_amount) so the two behaviors cannot drift per
  call site.
- Thousands separators are deliberately NOT emitted in ledger text
  even when the context has render_commas: canonical form carries no
  separators (the CST canonicalizer strips them by definition,
  matching bean-format), and render_number strips any grouping so
  direct emitters agree byte-for-byte with the canonicalize shim.
  Commas remain a report/query display concern, honored where they
  always were. Documented at the field and pinned by tests.
- Bare interpolation-target numbers (no currency) keep their own
  scale - there is no currency to look precision up under.
- Tests: core render_number + directive threading; a parser-level
  test pins precision padding through the two-pass canonicalize shim
  and byte-fidelity under the default config.

Next (#1766): a WIT session.format consuming held options builds the
DisplayContext from display-precision, completing the format surface.

Part of #1766.
@robcohen
robcohen requested a review from a team as a code owner July 18, 2026 17:12
Copilot AI review requested due to automatic review settings July 18, 2026 17:12
@codecov

codecov Bot commented Jul 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 39.7ms ± 4.7ms 23.8 MB
beancount 956ms ± 12.2ms 50.7 MB
Comparison 24.1x faster 2.1x less

vs main branch (2026-07-18)

Metric main this PR Change
Time 35.6ms 39.7ms +11.5% 🔴
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds per-currency display-precision support to the canonical ledger-text rendering pipeline by threading an optional DisplayContext through FormatConfig and routing amount/cost/price number emission through a single render_number chokepoint (while deliberately suppressing thousands separators in ledger text).

Changes:

  • Extend FormatConfig with number_display: Option<DisplayContext> and introduce render_number() as the central number-rendering path.
  • Thread FormatConfig through directive/posting/amount formatting so balance/price/posting units/cost/price can honor per-currency precision padding when configured.
  • Add tests (core + parser canonicalize shim) to pin padding behavior and the “no thousands separators in ledger text” decision.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
crates/rustledger-parser/src/cst/format.rs Adds a parser-level regression test ensuring the two-pass canonicalize shim preserves precision padding (and never emits separators).
crates/rustledger-core/src/format/mod.rs Introduces FormatConfig.number_display and render_number(); updates formatting tests to pass config through.
crates/rustledger-core/src/format/transaction.rs Threads FormatConfig into posting amount splitting and incomplete amount formatting so posting numbers/cost/price use render_number().
crates/rustledger-core/src/format/directives.rs Uses render_number() for directive amounts (balance/price) during alignment-friendly splitting.
crates/rustledger-core/src/format/amount.rs Adds config-aware amount formatting and updates cost/price annotation rendering to honor number_display.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 811
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

robcohen added 2 commits July 18, 2026 21:23
- render_number now uses a new DisplayContext::format_plain: a TRACKED
  currency pads to its precision (never rounding), an UNTRACKED
  currency stays byte-faithful to its own scale - the previous path
  went through format(), whose untracked branch normalize()d away
  trailing zeros, silently widening balance-assertion implicit
  tolerances 10x for any currency the context did not track. This also
  deletes the comma add-then-strip: format_plain never emits
  separators.
- the chokepoint now genuinely covers every number: balance tolerances
  render with the amount currency precision, and amount-typed values
  in metadata and custom directives thread through format_amount_with
  (format_meta_value/metadata_lines take the config; posting metadata
  keeps its deeper indent).
- repaired the doc splice that had cut format_directives rustdoc in
  half and attached its head to render_number; both docs restored and
  the pad-never-round / untracked-faithful semantics documented at the
  field, the chokepoint, and format_plain.
- format_incomplete_amount Complete arm calls the canonical
  format_amount_with instead of re-deriving it; FormatConfig
  convenience constructors use ..Self::default().
- tests: exact-equality asserts replace substring proxies; a new
  untracked-currency fidelity assert; a parser-level drift guard pins
  padded COST and PRICE numbers surviving the shim pass 2 (proving the
  CST re-canonicalization preserves trailing zeros) and pass-1/shim
  agreement on rendered numbers.
@robcohen

Copy link
Copy Markdown
Member Author

Deep review (24 agents, adversarial verification): 10 findings, all addressed in 6a3dfe1 + 86909fa.

The one real correctness bug: untracked currencies lost trailing zeros. render_number went through DisplayContext::format, whose untracked branch calls normalize() — so with a context tracking only USD, a 100.50 EUR posting reformatted to 100.5 EUR, silently widening the balance assertion's implicit tolerance 10×. Fixed with a new DisplayContext::format_plain: tracked currencies pad (never round), untracked currencies stay byte-faithful, and separators are never emitted — which also deletes the comma add-then-strip inefficiency outright.

The chokepoint now genuinely covers every number: balance tolerances render with the amount's currency precision (mixed precision on one line read as a typo), and amount-typed metadata and custom-directive values thread through format_amount_with (the config now flows through format_meta_value/metadata_lines, with posting metadata keeping its deeper indent).

Also fixed: a doc splice that had cut format_directives' rustdoc in half and attached its head to render_number (both restored, and the pad-never-round / untracked-faithful semantics documented at the field, the chokepoint, and format_plain); format_incomplete_amount calls the canonical instead of re-deriving it; dead format_amount removed; constructors use ..Self::default(); exact-equality asserts replace substring proxies; and a new parser-level drift guard pins padded cost and price numbers surviving the canonicalize shim's pass 2 (proving the CST re-canonicalization preserves trailing zeros) plus pass-1/shim agreement on rendered numbers.

Workspace green (3938 tests), clippy/doc clean.

@robcohen
robcohen merged commit b76020d into main Jul 18, 2026
54 checks passed
@robcohen
robcohen deleted the feat/format-display-precision branch July 18, 2026 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants