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

+26 -10
View File
@@ -12,6 +12,8 @@ use crate::widgets;
use egui::{CornerRadius, RichText, Sense, Ui, vec2};
use egui_phosphor::regular as icon;
use ledger_core::assets::accumulated_scheduled_through;
use ledger_core::kernel::Amount;
use ledger_core::money::sub_in_range;
use ledger_core::{AssetStatus, PeriodStatus};
use rust_decimal::Decimal;
@@ -247,7 +249,14 @@ pub fn show(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
ui.label(RichText::new(&m.name).font(theme::semibold(13.5)));
widgets::pill(
ui,
&format!("{}%", (m.share * Decimal::from(100)).normalize()),
&format!(
"{}%",
Amount::checked_mul_rounded(m.share, Decimal::from(100))
.expect(
"a member's share is at most one, so a hundred times it fits"
)
.normalize()
),
p.accent,
p.accent_soft,
);
@@ -384,7 +393,7 @@ fn assets(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
let later = b.ledger.asset_acquired_date(a.id).filter(|d| *d > b.today);
let acc = b.ledger.accumulated_posted(a.id, b.today);
let scheduled = accumulated_scheduled_through(a, b.today);
let dep = a.cost.amount - a.salvage_value.amount;
let dep = sub_in_range(a.cost.amount, a.salvage_value.amount);
ui.horizontal_wrapped(|ui| {
if widgets::link(
ui,
@@ -416,7 +425,10 @@ fn assets(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
),
(_, Some(_)) => format!("book value {}", fmt::dollars(Decimal::ZERO)),
(Ok(acc), None) => {
format!("book value {}", fmt::dollars(a.cost.amount - *acc))
format!(
"book value {}",
fmt::dollars(sub_in_range(a.cost.amount, *acc))
)
}
(Err(e), None) => format!("book value can't be computed: {e}"),
};
@@ -433,13 +445,15 @@ fn assets(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
let frac: f32 = if dep.is_zero() {
1.0
} else {
(*acc / dep).try_into().unwrap_or(0.0)
Amount::checked_div(*acc, dep)
.and_then(|x| x.try_into().ok())
.unwrap_or(0.0)
};
let note = match scheduled {
Ok(s) if s > *acc => {
format!(
" · {} more scheduled through today",
fmt::dollars(s - *acc)
fmt::dollars(sub_in_range(s, *acc))
)
}
Ok(_) => String::new(),
@@ -514,11 +528,10 @@ fn schedules(ui: &mut Ui, b: &Books) {
continue;
}
};
let recognized = total.checked_sub(remaining);
let recognized = Amount::checked_sub(total, remaining);
let frac: f32 = match recognized {
_ if total.is_zero() => 1.0,
Some(r) => r
.checked_div(total)
Some(r) => Amount::checked_div(r, total)
.and_then(|x| x.try_into().ok())
.unwrap_or(0.0),
None => 0.0,
@@ -580,7 +593,9 @@ fn recurring(ui: &mut Ui, b: &Books) {
.lines
.iter()
.filter(|(_, s, _)| *s == ledger_core::Side::Debit)
.try_fold(Decimal::ZERO, |t, (_, _, m)| t.checked_add(m.amount));
.try_fold(Decimal::ZERO, |t, (_, _, m)| {
Amount::checked_add(t, m.amount)
});
let next = (0..)
.map_while(|k| {
r.frequency
@@ -715,7 +730,8 @@ fn parties(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
.filter_map(|i| b.ledger.get_invoice(i))
.filter(|i| i.customer_id == c.id)
.try_fold(Decimal::ZERO, |t, i| {
t.checked_add(
Amount::checked_add(
t,
b.owed_today(ledger_core::DocRef::Invoice(i.id)).ok()?,
)
});