diff --git a/crates/ledger-config/src/lib.rs b/crates/ledger-config/src/lib.rs index 8d693918..56f70f5b 100644 --- a/crates/ledger-config/src/lib.rs +++ b/crates/ledger-config/src/lib.rs @@ -169,7 +169,11 @@ fn parse_share(name: &str, s: &str) -> Result { let s = s.trim(); let bad = || format!("member {name}: share {s:?} must be like \"50%\" or \"0.5\""); let share = match s.strip_suffix('%') { - Some(pct) => pct.trim().parse::().map_err(|_| bad())? / Decimal::from(100), + Some(pct) => ledger_core::kernel::Amount::checked_div( + pct.trim().parse::().map_err(|_| bad())?, + Decimal::from(100), + ) + .ok_or_else(bad)?, None => s.parse::().map_err(|_| bad())?, }; if share <= Decimal::ZERO || share > Decimal::ONE { @@ -359,7 +363,9 @@ pub fn parse(text: &str) -> Result { } } if !company.members.is_empty() { - let total: Decimal = company.members.iter().map(|m| m.share).sum(); + let total = ledger_core::kernel::total_of(company.members.iter(), |m| m.share).ok_or( + "member shares are too long to add exactly: write each with fewer decimal places", + )?; if total != Decimal::ONE { return Err(format!("member shares sum to {total}, not 100%")); } diff --git a/crates/ledger-store/src/audit.rs b/crates/ledger-store/src/audit.rs index a07baeb8..c15b890d 100644 --- a/crates/ledger-store/src/audit.rs +++ b/crates/ledger-store/src/audit.rs @@ -642,11 +642,13 @@ impl Store { "attachment {attachment_id} is not filed on or linked to entry {entry_id}" ))); } - let debits = entry - .lines() - .iter() - .filter(|l| l.side == ledger_core::Side::Debit) - .try_fold(Decimal::ZERO, |s, l| s.checked_add(l.amount.amount)); + let debits = ledger_core::kernel::total_of( + entry + .lines() + .iter() + .filter(|l| l.side == ledger_core::Side::Debit), + |l| l.amount.amount, + ); let amount_agrees = debits == Some(amount) || entry.lines().iter().any(|l| l.amount.amount == amount); let disagrees = !amount_agrees || date != entry.date(); diff --git a/crates/ledger-store/src/store.rs b/crates/ledger-store/src/store.rs index b8455551..02dd939d 100644 --- a/crates/ledger-store/src/store.rs +++ b/crates/ledger-store/src/store.rs @@ -2464,7 +2464,11 @@ impl Store { preview.excess_depreciation, ), (gain_loss_account, Side::Credit, preview.gain_loss), - (gain_loss_account, Side::Debit, -preview.gain_loss), + ( + gain_loss_account, + Side::Debit, + ledger_core::money::neg(preview.gain_loss), + ), ] .into_iter() .filter(|(_, _, a)| *a > Decimal::ZERO) @@ -2540,7 +2544,12 @@ impl Store { .enumerate() .filter_map(|(i, e)| { let d = self.ledger.get_entry(e)?.date(); - Some((e, d, if i % 2 == 0 { amount } else { -amount })) + let carried = if i % 2 == 0 { + amount + } else { + ledger_core::money::neg(amount) + }; + Some((e, d, carried)) }) .collect(); let what = format!("{period}'s depreciation run"); @@ -2661,10 +2670,8 @@ impl Store { let runs = self.live_runs(asset_id); let too_large = || StoreError::JournalCorrupt(format!("asset {asset_id}'s depreciation is too large")); - let posted = runs - .iter() - .try_fold(Decimal::ZERO, |t, r| t.checked_add(r.amount)) - .ok_or_else(too_large)?; + let posted = + ledger_core::kernel::total_of(runs.iter(), |r| r.amount).ok_or_else(too_large)?; let target = ledger_core::accumulated_scheduled_through(asset, date)?; let (up, down) = ledger_core::kernel::true_up(target, posted).ok_or_else(too_large)?; let in_month = runs.iter().any(|r| r.period == period); @@ -2673,13 +2680,9 @@ impl Store { } else { (up, Decimal::ZERO) }; - let accumulated = posted - .checked_add(final_depreciation) + let accumulated = ledger_core::kernel::Amount::checked_add(posted, final_depreciation) .ok_or_else(too_large)?; - let book_value = asset - .cost - .amount - .checked_sub(target) + let book_value = ledger_core::kernel::Amount::checked_sub(asset.cost.amount, target) .ok_or_else(too_large)?; Ok(DisposalPreview { asset_id, @@ -2690,9 +2693,7 @@ impl Store { excess_depreciation: down, catch_up_depreciation, book_value, - gain_loss: proceeds - .amount - .checked_sub(book_value) + gain_loss: ledger_core::kernel::Amount::checked_sub(proceeds.amount, book_value) .ok_or_else(too_large)?, period, }) @@ -4170,7 +4171,10 @@ impl Store { ledger_core::DocRef::Bill(id) => self.ledger.get_bill(id).map(|b| b.total()), }; if let Some(total) = total { - let older_owed = total?.checked_sub(self.ledger.payments_recorded(app.doc)?); + let older_owed = ledger_core::kernel::Amount::checked_sub( + total?, + self.ledger.payments_recorded(app.doc)?, + ); if older_owed.is_none_or(|owed| app.cash > owed) { self.ensure_feature(crate::format::DOCUMENT_STANDING)?; } diff --git a/docs/testing.md b/docs/testing.md index 277d16f9..c887233f 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -28,7 +28,7 @@ promise. | Fuzzing | crash, hang, broken books (4); for `fuzz_store_replay` (validly chained journals with fuzz-chosen events): books that open must verify, and the viewer must accept exactly what the writer does | `fuzz/fuzz_targets/`, corpora in `fuzz/corpus/` | `just fuzz ` | 10 min each | weekly | | Miri | undefined behavior in our use of dependencies (4) | the core's unit, bridge, invariant tests (the exhaustive calendar walk aside) | `just miri` | ~4 min | on demand | | Golden | `tests/golden/fixture_company.json` (5) | `invariants.rs` | `just check` | <1s | every change | -| Kernel only | no `Decimal` arithmetic outside `src/kernel/`: no raw operator, none of rust_decimal's own arithmetic or rounding methods, no `Iterator::sum` (AGENTS.md, bar item 1; the proofs are about the kernel only) | `scripts/raw-decimal-arith.sh`, which asks clippy twice and compares; the crates held at zero are the justfile's `kernel_only_crates` (ledger-core, ledger-cli, ledger-host, ledger-reader, ledger-vault; the other six join as K-U12 brings each to zero) | `just kernel-only` (in `just check`, the lint part) | ~1 min warm, a few minutes cold (two clippy runs of the crate) | every change. A waiver is an `#[allow]` with a `reason`; the last line counts them, and ledger-core has none | +| Kernel only | no `Decimal` arithmetic outside `src/kernel/`: no raw operator, none of rust_decimal's own arithmetic or rounding methods, no `Iterator::sum` (AGENTS.md, bar item 1; the proofs are about the kernel only) | `scripts/raw-decimal-arith.sh`, which asks clippy twice and compares; the crates held at zero are the justfile's `kernel_only_crates` (every crate that has reached zero; the rest join as K-U12 brings each there, and none leaves) | `just kernel-only` (in `just check`, the lint part) | ~1 min warm, a few minutes cold (two clippy runs of the crate) | every change. A waiver is an `#[allow]` with a `reason`; the last line counts them, and ledger-core has none | | Dependencies | no crate in the lock file with a known vulnerability (RustSec) or yanked; none under a licence that does not fit MIT OR Apache-2.0; none from a source `deny.toml` does not name; none of our own unmaintained (4) | `deny.toml`, each exception with its reason and date | `just deny` (in `just check`, the lint part) | seconds, needs the network | every change, and weekly (`scheduled.yml`), since an advisory is published when it is, not when we push. First run, 2026-11-09: 23 advisories; closed by upgrades (quick-xml, rusqlite, rust_xlsxwriter, rmcp, rustls, tokio, zstd, pulldown-cmark) but two, lived with in `deny.toml` | | Notices | every crate the program links and every font it embeds, with the licence text each asks to travel with (1, the licence itself) | `THIRD-PARTY-NOTICES.txt`, written by cargo-about from `ledger-cli`'s manifest, by `scripts/notices-packaged.py` (the crates' own licence files, where cargo-about could only give a licence's standard text with a placeholder for the copyright line) and from the licence files beside the fonts | `just notices`; `just notices-check` (in `just check`, the lint part) | ~2 min (12 CPU-minutes: it reads every crate's files for licence text) | every change that touches a dependency or a font. `just notices` writes the file; `just notices-check` fails when a dependency changed and the file was not regenerated, and the fix is `just notices` and committing the result | | Benchmarks | a baseline (6) | `ledger-core/benches/books.rs`, `ledger-store/benches/store.rs` | `just bench` | ~2 min | none (no budget, by decision) | diff --git a/justfile b/justfile index 3b01c422..564f60d7 100644 --- a/justfile +++ b/justfile @@ -61,7 +61,7 @@ check-shard shard: # The crates with no Decimal arithmetic outside the kernel. A crate joins # this list when its count reaches zero (K-U12) and never leaves it. -kernel_only_crates := "ledger-core ledger-cli ledger-host ledger-reader ledger-vault" +kernel_only_crates := "ledger-core ledger-cli ledger-host ledger-reader ledger-vault ledger-store ledger-config" # Money arithmetic lives in the kernel and nowhere else (AGENTS.md, bar # item 1). The compiler is asked (scripts/raw-decimal-arith.sh): a raw