Repository navigation
feat: formatter honors per-currency display precision (#1766) - #1807
Conversation
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.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
📊 Benchmark Results
vs main branch (2026-07-18)
What do these numbers mean?
|
There was a problem hiding this comment.
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
FormatConfigwithnumber_display: Option<DisplayContext>and introducerender_number()as the central number-rendering path. - Thread
FormatConfigthrough 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.
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
- 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.
|
Deep review (24 agents, adversarial verification): 10 findings, all addressed in 6a3dfe1 + 86909fa. The one real correctness bug: untracked currencies lost trailing zeros. 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 Also fixed: a doc splice that had cut Workspace green (3938 tests), clippy/doc clean. |
What
The remaining #1766 blocker: the rendering pipeline had no precision machinery anywhere (CLI included), which is why
format-loadedcouldn't honordisplay-precisionandsession.formatwas deliberately not shipped in #1805. This PR grows it at the canonical layer:FormatConfiggainsnumber_display: Option<DisplayContext>— reusing the canonical precision machinery the query/report renderers already use (per-currency fixed precisions fromoption \"display_precision\"+ observed distributions), rather than duplicating it.render_numberchokepoint and config-threaded formatters, so the two behaviors (context-padded vs historical own-scale) cannot drift per call site.Noneis byte-identical to the historical rendering.render_commas— canonical form carries no separators (the CST canonicalizer strips them by definition, matchingbean-format), andrender_numberstrips 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 emitted1,234.50, pass 2 canonicalized it away — the test now pins the resolved semantics.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.formatbuilding theDisplayContextfrom held options — completing #1766's format surface.Part of #1766.
🤖 Generated with Claude Code
https://claude.ai/code/session_013gJH7uBBF9Xw9et9QFRcsU