refactor(package): Decimal arithmetic goes through the kernel (K-U12)

The raw-decimal-arith detector counted 13 places in ledger-package
before and counts 0 after. No figure changes.

Negations go through money::neg; the two differences of books figures
(attention, k1_tax) through sub_in_range; the checked sums in the
attention checks through Amount::checked_add / checked_sub with their
"cannot compute" handling untouched.

Not a plain sum or sign: text.rs padded a number to two places with
rescale (which rounds when 96 bits do not hold the padded digits); it
now uses rescale_exact and keeps the number as it was if padding would
round, so the printed text never rounds.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude committed 2026-11-09 23:57:22 -07:00
1 parent 0fb19b4b25
commit 5ef5f47ebf
5 files changed
+17 -13

No files matched your search

+11 -7
View File
@@ -100,6 +100,7 @@
use std::collections::{BTreeSet, HashMap, HashSet}; use std::collections::{BTreeSet, HashMap, HashSet};
use chrono::{Datelike, NaiveDate}; use chrono::{Datelike, NaiveDate};
use ledger_core::kernel::Amount;
use ledger_core::parties::MoneyAccounts; use ledger_core::parties::MoneyAccounts;
use ledger_core::{ use ledger_core::{
AccountId, AccountType, AssetId, DocRef, DocStatus, EntryId, EntryStatus, Ledger, LedgerError, AccountId, AccountType, AssetId, DocRef, DocStatus, EntryId, EntryStatus, Ledger, LedgerError,
@@ -344,7 +345,7 @@ pub fn find_with(
if missing.len() > 3 { if missing.len() > 3 {
let total = missing let total = missing
.iter() .iter()
.try_fold(Decimal::ZERO, |t, e| t.checked_add(e.amount?)); .try_fold(Decimal::ZERO, |t, e| Amount::checked_add(t, e.amount?));
let list: Vec<String> = missing let list: Vec<String> = missing
.iter() .iter()
.map(|e| format!("{} ({}, {})", e.id, e.description, e.date)) .map(|e| format!("{} ({}, {})", e.id, e.description, e.date))
@@ -428,7 +429,7 @@ pub fn find_with(
if !due.is_empty() { if !due.is_empty() {
let Some(total) = due let Some(total) = due
.iter() .iter()
.try_fold(Decimal::ZERO, |t, (_, a)| t.checked_add(*a)) .try_fold(Decimal::ZERO, |t, (_, a)| Amount::checked_add(t, *a))
else { else {
out.push(cant_compute( out.push(cant_compute(
format!("Amortization schedule \"{}\"", s.description), format!("Amortization schedule \"{}\"", s.description),
@@ -704,7 +705,10 @@ pub fn find_with(
.map(|r| { .map(|r| {
[r.days_60, r.days_90, r.days_90_plus] [r.days_60, r.days_90, r.days_90_plus]
.into_iter() .into_iter()
.try_fold(r.days_30, Decimal::checked_add) .try_fold(
r.days_30,
<Decimal as ledger_core::kernel::Amount>::checked_add,
)
.ok_or_else(too_large) .ok_or_else(too_large)
}) })
.collect::<Result<Vec<_>, _>>()? .collect::<Result<Vec<_>, _>>()?
@@ -713,7 +717,7 @@ pub fn find_with(
.collect(); .collect();
let total = late let total = late
.iter() .iter()
.try_fold(Decimal::ZERO, |t, d| t.checked_add(*d)) .try_fold(Decimal::ZERO, |t, d| Amount::checked_add(t, *d))
.ok_or_else(too_large)?; .ok_or_else(too_large)?;
Ok::<_, LedgerError>((late.len(), total)) Ok::<_, LedgerError>((late.len(), total))
}; };
@@ -870,7 +874,7 @@ pub fn find_with(
}; };
match lines match lines
.iter() .iter()
.try_fold(Decimal::ZERO, |t, l| t.checked_sub(l.amount)) .try_fold(Decimal::ZERO, |t, l| Amount::checked_sub(t, l.amount))
{ {
Some(amount) => written_off.push((inv.id, inv.number.clone(), last.date, amount)), Some(amount) => written_off.push((inv.id, inv.number.clone(), last.date, amount)),
None => out.push(cant_compute( None => out.push(cant_compute(
@@ -1133,7 +1137,7 @@ pub fn find_with(
( (
format!( format!(
"{} of registered assets not in the books", "{} of registered assets not in the books",
dollars(-unregistered) dollars(ledger_core::money::neg(unregistered))
), ),
"Registered assets cost more than their cost accounts hold: a purchase entry may be missing.", "Registered assets cost more than their cost accounts hold: a purchase entry may be missing.",
) )
@@ -1864,7 +1868,7 @@ fn paid_this_year(
.max(today); .max(today);
let end = last.min(NaiveDate::from_ymd_opt(today.year(), 12, 31).expect("valid")); let end = last.min(NaiveDate::from_ymd_opt(today.year(), 12, 31).expect("valid"));
let a = ledger_core::parties::activity_with(ledger, party, (start, end), money, members); let a = ledger_core::parties::activity_with(ledger, party, (start, end), money, members);
a.paid - a.received ledger_core::money::sub_in_range(a.paid, a.received)
} }
/// A figure the books can't compute (BS-entry-4: books an earlier /// A figure the books can't compute (BS-entry-4: books an earlier
+1 -1
View File
@@ -269,7 +269,7 @@ pub fn invoice_document(
{ {
label.push_str(&format!(" · ref {r}")); label.push_str(&format!(" · ref {r}"));
} }
settled.push((label, -l.amount)); settled.push((label, ledger_core::money::neg(l.amount)));
last_paid = Some(l.date); last_paid = Some(l.date);
} }
for (label, amount) in [ for (label, amount) in [
+2 -2
View File
@@ -135,7 +135,7 @@ pub fn tax_basis_csv(ledger: &Ledger, names: &[String], tc: &TaxCapital) -> Stri
Some(t) => [ Some(t) => [
money(book), money(book),
money(t), money(t),
money(book - t), money(ledger_core::money::sub_in_range(book, t)),
"tax basis".into(), "tax basis".into(),
note, note,
], ],
@@ -244,7 +244,7 @@ pub fn tax_basis_csv(ledger: &Ledger, names: &[String], tc: &TaxCapital) -> Stri
name, name,
"Reconciliation: less property distributed, at book", "Reconciliation: less property distributed, at book",
recon( recon(
Some(-r.property_distributed), Some(ledger_core::money::neg(r.property_distributed)),
"the company's adjusted tax basis in it is the CPA's", "the company's adjusted tax basis in it is the CPA's",
), ),
); );
+2 -2
View File
@@ -27,7 +27,7 @@ pub fn money(n: Decimal) -> String {
// is what drops a negative zero. // is what drops a negative zero.
let mut n = ledger_import::round_display(n, n.scale().max(2)); let mut n = ledger_import::round_display(n, n.scale().max(2));
if n.scale() < 2 { if n.scale() < 2 {
n.rescale(2); n = ledger_core::kernel::rescale_exact(n, 2).unwrap_or(n);
} }
n.to_string() n.to_string()
} }
@@ -47,7 +47,7 @@ pub fn share_text(n: Decimal) -> String {
let mut n = n.normalize(); let mut n = n.normalize();
n = ledger_import::round_display(n, n.scale().max(2)); n = ledger_import::round_display(n, n.scale().max(2));
if n.scale() < 2 { if n.scale() < 2 {
n.rescale(2); n = ledger_core::kernel::rescale_exact(n, 2).unwrap_or(n);
} }
n.to_string() n.to_string()
} }
+1 -1
View File
@@ -268,7 +268,7 @@ fn notes_for(ledger: &Ledger, r: &ledger_core::Vendor1099Report) -> Vec<String>
for p in &r.other_receipts { for p in &r.other_receipts {
// Money in is negative; its reversal gives it back. // Money in is negative; its reversal gives it back.
let what = if p.amount < Decimal::ZERO { let what = if p.amount < Decimal::ZERO {
format!("received {} from", money(-p.amount)) format!("received {} from", money(ledger_core::money::neg(p.amount)))
} else { } else {
format!("reversed {} received from", money(p.amount)) format!("reversed {} received from", money(p.amount))
}; };