refactor(core): every report and book total goes through the kernel's checked sum
A raw Decimal + / += / .sum() panics at Decimal::MAX, and a replayed journal line is accepted of any size (rules.md B-28), so a total of restored figures must be exact or refused (B-21), never a crash. The reports (P&L, balance sheet, general ledger, members' equity, asset register), K-1 lines, tax-basis wholes, payouts, the 1099 split, the audit's articulation check, the members close split and arap's document_total now sum with kernel::total / total_of / total_try and add or subtract with checked arithmetic, refusing with TooLargeToCompute. money::sum_exact* and add_exact/sub_exact are the engine's thin wrappers over the kernel (no second copy of the sum). form1099::sum3 was a copy of the kernel's sum and is now kernel::total. periods::allocate and allocate_last_absorbs (frozen replay rules) keep their signatures and results to the cent; each gains a try_ twin that returns TooLargeToCompute where the raw amount * share used to panic, and the ledger's close split calls the twins. Behaviour for every ordinary input is unchanged: no figure moves, no test or golden edited. Infallible public signatures (trial_balance, ProfitAndLossStatement::net_income, tax book_net, project_entries, cash-flow kinds, the Regime) keep their raw sums; making them fallible would change ~100 callers and is a decision, not a refactor. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b3d504067f
commit
3376c3d549
10 files changed
+368
-162
No files matched your search
@@ -153,6 +153,51 @@ impl Money {
|
||||
}
|
||||
}
|
||||
|
||||
/// The exact sum of some figures ([`crate::kernel::total`]), or
|
||||
/// `TooLargeToCompute` naming `what` when a running sum cannot be held:
|
||||
/// the engine's way to total a column without a raw `.sum()` that would
|
||||
/// panic at `Decimal::MAX` (rules.md B-21, B-28).
|
||||
pub fn sum_exact(
|
||||
values: impl IntoIterator<Item = Decimal>,
|
||||
what: &str,
|
||||
) -> Result<Decimal, LedgerError> {
|
||||
crate::kernel::total(values).ok_or_else(|| LedgerError::TooLargeToCompute(what.to_string()))
|
||||
}
|
||||
|
||||
/// [`sum_exact`] over a figure drawn from each item
|
||||
/// ([`crate::kernel::total_of`]).
|
||||
pub fn sum_exact_of<I>(
|
||||
values: impl IntoIterator<Item = I>,
|
||||
f: impl FnMut(I) -> Decimal,
|
||||
what: &str,
|
||||
) -> Result<Decimal, LedgerError> {
|
||||
crate::kernel::total_of(values, f)
|
||||
.ok_or_else(|| LedgerError::TooLargeToCompute(what.to_string()))
|
||||
}
|
||||
|
||||
/// [`sum_exact`] over a derived figure that can itself be unrepresentable
|
||||
/// ([`crate::kernel::total_try`]).
|
||||
pub fn sum_exact_try<I>(
|
||||
values: impl IntoIterator<Item = I>,
|
||||
f: impl FnMut(I) -> Option<Decimal>,
|
||||
what: &str,
|
||||
) -> Result<Decimal, LedgerError> {
|
||||
crate::kernel::total_try(values, f)
|
||||
.ok_or_else(|| LedgerError::TooLargeToCompute(what.to_string()))
|
||||
}
|
||||
|
||||
/// `a + b` exactly, or `TooLargeToCompute` naming `what` (a checked `+`).
|
||||
pub fn add_exact(a: Decimal, b: Decimal, what: &str) -> Result<Decimal, LedgerError> {
|
||||
crate::kernel::Amount::checked_add(a, b)
|
||||
.ok_or_else(|| LedgerError::TooLargeToCompute(what.to_string()))
|
||||
}
|
||||
|
||||
/// `a - b` exactly, or `TooLargeToCompute` naming `what` (a checked `-`).
|
||||
pub fn sub_exact(a: Decimal, b: Decimal, what: &str) -> Result<Decimal, LedgerError> {
|
||||
crate::kernel::Amount::checked_sub(a, b)
|
||||
.ok_or_else(|| LedgerError::TooLargeToCompute(what.to_string()))
|
||||
}
|
||||
|
||||
impl fmt::Debug for Money {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{} {}", self.amount, self.currency)
|
||||
|
||||
Reference in new issue
Block a user