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 <noreply@anthropic.com>
This commit is contained in:
1 parent
8ad55e82a8
commit
e0c78a783b
1 file changed
+88
-1
@@ -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, LedgerEr
|
||||
.ok_or_else(|| LedgerError::TooLargeToCompute(what.to_string()))
|
||||
}
|
||||
|
||||
/// `-value`, through the kernel. A `Decimal`'s negation is always exact,
|
||||
/// so this never fails (a zero comes back as a negative zero, bit for bit
|
||||
/// what a raw `-` gives).
|
||||
pub(crate) fn neg(value: Decimal) -> 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!(
|
||||
|
||||
Reference in new issue
Block a user