refactor(gui): the viewer's Decimal arithmetic goes through the kernel (K-U12)

The detector (scripts/raw-decimal-arith.sh ledger-gui) counted 123 places
in the viewer that did Decimal arithmetic outside the kernel; it counts 0
now. No figure the viewer shows changes: a raw operator panicked when the
exact answer did not fit, and add_in_range / sub_in_range / neg panic in
exactly that case; rust_decimal's checked_* methods, which round past 96
bits, became the kernel's Amount::checked_* (exact or None), with the
existing None handling untouched.

Places that are not plain sums or differences, and what each became:
- Sums of balances and posted lines (receivables, due to members, a
  disposal's true-up, a statement row across columns, cleared total):
  kernel::total / total_of; a refused sum is an expect where the figure
  is drawn from the books, and a blank cell in the cash-flow keyed rows
  (which already blank a refused statement).
- The share split (books.rs): share_of(v, share, net), the same multiply
  then divide; the scaled parts use checked_mul_rounded because the
  fraction is a quotient, truncated to cents on the next line.
- Percents of a share (capital, company, reports): checked_mul_rounded
  by 100, exactly what * did.
- Averages and runway (monthly burn, cash runway, net per month, a
  third of an amount, build-in pct): Amount::checked_div, a None joins
  the None or "can't compute" the code already has.
- The 125% watch threshold: checked_mul_rounded and is_some_and.
- Rescale to one place (pct): rescale_exact, keeping the value when it
  would round.
- The demo's rounding to a step ((x / 50).round() * 50): to_step, the
  same half-to-even rounding through round_places.
- f32 widths and usize counts: plain folds, no allow.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude committed 2026-11-10 01:07:35 -07:00
1 parent 0de906e9ed
commit e4c41ccb3f
16 files changed
+244 -151

No files matched your search

+6 -5
View File
@@ -21,6 +21,7 @@ use crate::widgets;
use chrono::{Datelike, NaiveDate};
use egui::{Color32, CornerRadius, Margin, RichText, Sense, Ui, vec2};
use egui_phosphor::regular as icon;
use ledger_core::money::{neg, sub_in_range};
use ledger_core::{
Asset, AssetId, AssetRegister, AssetRegisterRow, AssetStatus, BasisRule, Sec704cMethod,
};
@@ -91,7 +92,7 @@ pub fn notices(b: &Books, r: &AssetRegister) -> Vec<Notice> {
),
});
}
let accruing = unposted - due_amount;
let accruing = sub_in_range(unposted, due_amount);
if accruing > Decimal::ZERO {
out.push(Notice {
problem: false,
@@ -108,7 +109,7 @@ pub fn notices(b: &Books, r: &AssetRegister) -> Vec<Notice> {
problem: true,
text: format!(
"The books carry {} more accumulated depreciation than the schedules through {} — ask the bookkeeper to check the depreciation entries.",
fmt::dollars(-unposted),
fmt::dollars(neg(unposted)),
fmt::date(r.as_of)
),
});
@@ -127,7 +128,7 @@ pub fn notices(b: &Books, r: &AssetRegister) -> Vec<Notice> {
problem: true,
text: format!(
"{} of registered cost isn't in the books' asset cost accounts — ask the bookkeeper to check the purchase entries.",
fmt::dollars(-unregistered)
fmt::dollars(neg(unregistered))
),
});
}
@@ -324,7 +325,7 @@ impl TaxFacts {
/// figure, for a built-in gain (+) or loss (−).
pub fn gain_or_loss(g: Decimal) -> (&'static str, Decimal) {
if g < Decimal::ZERO {
("Built-in loss", -g)
("Built-in loss", neg(g))
} else {
("Built-in gain", g)
}
@@ -399,7 +400,7 @@ pub(crate) fn row(
ui.painter().rect_filled(r, CornerRadius::same(4), p.hover);
ui.ctx().set_cursor_icon(egui::CursorIcon::PointingHand);
}
let fixed: f32 = cells.iter().map(|c| c.1).sum();
let fixed: f32 = cells.iter().map(|c| c.1).fold(0.0_f32, |a, b| a + b);
let flex = (w - fixed).max(40.0);
let painter = ui.painter_at(r);
let font = if strong {