From e0c78a783b3947d9940000d70809e76a85a853e2 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 9 Nov 2026 17:22:16 -0700 Subject: [PATCH] refactor(core): a line's sign and an in-range sum, said once over the kernel Twenty modules each wrote 'debit plus, credit minus' and 'a + b' for themselves, on raw Decimal operators. Five helpers in money.rs say them once, over the kernel's signed, flip and Amount: neg, signed_amount (a line's amount as a movement), on_side (plus on an account's normal side, minus on the other), and add_in_range / sub_in_range for sums of posted lines, which the line cap keeps far inside Decimal's range. Two tests hold them to the raw operators bit for bit, the scale and the sign of a zero included. The second fails today, and it is right to: the kernel's default checked_sub is an add of the negation, and rust_decimal answers 0.00 + (-0.00) with -0.00 where 0.00 - 0.00 is 0.00. K-U10a gives Decimal its own exact_sub (rust_decimal's own difference, checked for exactness); the test passes once it is merged under this branch. Co-Authored-By: Claude --- crates/ledger-core/src/money.rs | 89 ++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/crates/ledger-core/src/money.rs b/crates/ledger-core/src/money.rs index c2d133da..d2b074e5 100644 --- a/crates/ledger-core/src/money.rs +++ b/crates/ledger-core/src/money.rs @@ -4,7 +4,7 @@ //! [`Money`] arithmetic is checked: adding different currencies is an error, //! not a guess. -use crate::LedgerError; +use crate::{LedgerError, Side}; use rust_decimal::Decimal; use serde::{Deserialize, Serialize}; use std::fmt; @@ -198,6 +198,43 @@ pub fn sub_exact(a: Decimal, b: Decimal, what: &str) -> Result Decimal { + crate::kernel::Amount::checked_neg(value).expect("Decimal negation is exact") +} + +/// A line's amount signed by its side ([`crate::kernel::signed`]): +/// a debit as it is, a credit negated. +pub(crate) fn signed_amount(side: Side, amount: Decimal) -> Decimal { + crate::kernel::signed(side, amount).expect("Decimal negation is exact") +} + +/// A line's amount counted from one side of an account: as it is when +/// the line is on `normal`, negated when it is on the other side. With +/// `normal` the account's normal side this is the line's effect on the +/// balance as a reader sees it. +pub(crate) fn on_side(normal: Side, side: Side, amount: Decimal) -> Decimal { + match normal { + Side::Debit => signed_amount(side, amount), + Side::Credit => signed_amount(crate::kernel::flip(side), amount), + } +} + +/// `a + b` for figures built from posted lines, where the sum is always +/// exact (BS-entry-3, BS-entry-4 bound every line). A sum that could not +/// be held exactly is a broken ledger, so it panics rather than round. +/// Anything a caller can make too large uses [`add_exact`] instead. +pub(crate) fn add_in_range(a: Decimal, b: Decimal) -> Decimal { + crate::kernel::Amount::checked_add(a, b).expect("posted lines are in range, so their sums are") +} + +/// `a - b` for figures built from posted lines: see [`add_in_range`]. +pub(crate) fn sub_in_range(a: Decimal, b: Decimal) -> Decimal { + crate::kernel::Amount::checked_sub(a, b).expect("posted lines are in range, so their sums are") +} + impl fmt::Debug for Money { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{} {}", self.amount, self.currency) @@ -286,6 +323,56 @@ mod tests { Decimal::new(cents, 2) } + /// The sign helpers against the plain rule they stand for, on both + /// sides and both signs: a line counts as it is on the named side and + /// negated on the other, and a debit is the plus side of `signed`. + #[test] + fn sign_helpers_follow_the_side() { + for amount in [d(0), d(1), d(-1), d(123_456), d(-999)] { + assert_eq!(neg(amount), -amount); + assert_eq!(signed_amount(Side::Debit, amount), amount); + assert_eq!(signed_amount(Side::Credit, amount), -amount); + for normal in [Side::Debit, Side::Credit] { + for side in [Side::Debit, Side::Credit] { + let plain = if side == normal { amount } else { -amount }; + let got = on_side(normal, side, amount); + assert_eq!(got, plain); + // Bit for bit, the sign of a zero included. + assert_eq!(got.is_sign_negative(), plain.is_sign_negative()); + assert_eq!(got.scale(), plain.scale()); + } + } + } + } + + /// The in-range sum and difference are the raw operators' figures, + /// scale and the sign of a zero included. + #[test] + fn in_range_arithmetic_is_the_raw_operators() { + let figures = [ + d(0), + -d(0), + d(1), + d(-1), + Decimal::new(5, 1), + d(123_456), + Decimal::new(-7, 4), + ]; + for a in figures { + for b in figures { + for (got, plain) in [(add_in_range(a, b), a + b), (sub_in_range(a, b), a - b)] { + assert_eq!(got, plain); + assert_eq!( + got.is_sign_negative(), + plain.is_sign_negative(), + "{a} and {b}" + ); + assert_eq!(got.scale(), plain.scale(), "{a} and {b}"); + } + } + } + } + #[test] fn checked_arithmetic() { assert_eq!(