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:
1 parent
0de906e9ed
commit
e4c41ccb3f
16 files changed
+244
-151
No files matched your search
@@ -13,6 +13,8 @@ use chrono::{Datelike, Days, NaiveDate};
|
||||
use egui::{CornerRadius, RichText, Sense, Ui, vec2};
|
||||
use egui_phosphor::regular as icon;
|
||||
use ledger_core::analysis::{Interval, balance_history, flows_by_interval, interval_start};
|
||||
use ledger_core::kernel::Amount;
|
||||
use ledger_core::money::{add_in_range, neg, sub_in_range};
|
||||
use rust_decimal::Decimal;
|
||||
|
||||
pub fn show(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
|
||||
@@ -29,7 +31,7 @@ pub fn show(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
|
||||
);
|
||||
let (cin, cout): (Decimal, Decimal) =
|
||||
flows.iter().fold((Decimal::ZERO, Decimal::ZERO), |a, f| {
|
||||
(a.0 + f.2, a.1 + f.3)
|
||||
(add_in_range(a.0, f.2), add_in_range(a.1, f.3))
|
||||
});
|
||||
widgets::page_header(
|
||||
ui,
|
||||
@@ -74,7 +76,7 @@ pub fn show(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
|
||||
.collect();
|
||||
let bal = b.balance_on(*id, b.today);
|
||||
let ago = b.balance_on(*id, since);
|
||||
let delta = bal - ago;
|
||||
let delta = sub_in_range(bal, ago);
|
||||
let r = widgets::kpi(
|
||||
ui,
|
||||
widgets::Kpi {
|
||||
@@ -186,7 +188,7 @@ fn runway(ui: &mut Ui, b: &Books) {
|
||||
let Some(net) = ledger_core::kernel::total_try(&months, |&(_, _, i, o)| {
|
||||
ledger_core::kernel::Amount::checked_sub(i, o)
|
||||
})
|
||||
.and_then(|sum| sum.checked_div(Decimal::from(months.len() as u64))) else {
|
||||
.and_then(|sum| Amount::checked_div(sum, Decimal::from(months.len() as u64))) else {
|
||||
ui.label(RichText::new("The figures are too large to compute.").color(p.muted));
|
||||
return;
|
||||
};
|
||||
@@ -224,13 +226,10 @@ fn runway(ui: &mut Ui, b: &Books) {
|
||||
if net < Decimal::ZERO {
|
||||
// Checked: a sub-cent average (lines an earlier version
|
||||
// booked) makes a runway past any calendar.
|
||||
let months_left = b
|
||||
.kpis
|
||||
.cash
|
||||
.checked_div(-net)
|
||||
let months_left = Amount::checked_div(b.kpis.cash, neg(net))
|
||||
.map(|m| fmt::round_display(m, 1));
|
||||
let out = months_left
|
||||
.and_then(|m| m.checked_mul(Decimal::from(30)))
|
||||
.and_then(|m| Amount::checked_mul(m, Decimal::from(30)))
|
||||
.and_then(|d| u64::try_from(d).ok())
|
||||
.and_then(|d| b.today.checked_add_days(Days::new(d)));
|
||||
big(
|
||||
@@ -319,9 +318,9 @@ fn coming_up(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
|
||||
.filter(|(a, _, _)| b.cash_accounts.contains(a))
|
||||
.try_fold(Decimal::ZERO, |sum, (_, s, m)| {
|
||||
if *s == ledger_core::Side::Debit {
|
||||
sum.checked_add(m.amount)
|
||||
Amount::checked_add(sum, m.amount)
|
||||
} else {
|
||||
sum.checked_sub(m.amount)
|
||||
Amount::checked_sub(sum, m.amount)
|
||||
}
|
||||
});
|
||||
for d in occurrences_from(r, b.today, horizon) {
|
||||
@@ -378,7 +377,7 @@ fn coming_up(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
|
||||
items.push(Upcoming {
|
||||
date: bill.due_date,
|
||||
label: format!("Bill {id} to {who}"),
|
||||
amount: balance.map(|x| -x),
|
||||
amount: balance.map(neg),
|
||||
route: Some(Route::Entry(bill.entry_id)),
|
||||
});
|
||||
}
|
||||
@@ -409,9 +408,9 @@ fn coming_up(ui: &mut Ui, b: &Books, shell: &mut Shell<'_>) {
|
||||
(Some(Decimal::ZERO), Some(Decimal::ZERO)),
|
||||
|(inflow, outflow), i| match i.amount {
|
||||
Some(a) if a > Decimal::ZERO => {
|
||||
(inflow.and_then(|x| x.checked_add(a)), outflow)
|
||||
(inflow.and_then(|x| Amount::checked_add(x, a)), outflow)
|
||||
}
|
||||
Some(a) => (inflow, outflow.and_then(|x| x.checked_sub(a))),
|
||||
Some(a) => (inflow, outflow.and_then(|x| Amount::checked_sub(x, a))),
|
||||
None => (None, None),
|
||||
},
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user