Files
wanderbooks/crates/ledger-core/src/money.rs
T
swaitsandClaude 2843278138 refactor(core): the sign and in-range helpers are public (K-U12 prelude)
The crates around the core (the store, the tools, the viewer, the
exports) negate amounts, sign a line by its side and add figures drawn
from posted lines, each with a raw Decimal operator of its own: 183
places the kernel's proofs say nothing about. K-U12 routes them through
the kernel, and they need the same five helpers ledger-core uses for
that: neg, signed_amount, on_side, add_in_range, sub_in_range. They were
crate-private; they are public now, so a sign or a sum of posted figures
is still said once.

No behaviour changes.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-11-09 23:42:15 -07:00

502 lines
19 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Exact money: [`Decimal`] amounts paired with an ISO 4217 currency code.
//!
//! There is no float anywhere in this module, and there never will be.
//! [`Money`] arithmetic is checked: adding different currencies is an error,
//! not a guess.
use crate::{LedgerError, Side};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::fmt;
/// An ISO 4217 alpha-3 currency code, stored as three uppercase ASCII bytes.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Currency([u8; 3]);
impl Currency {
/// US dollar.
pub const USD: Currency = Currency(*b"USD");
/// Euro.
pub const EUR: Currency = Currency(*b"EUR");
/// British pound.
pub const GBP: Currency = Currency(*b"GBP");
/// Swiss franc.
pub const CHF: Currency = Currency(*b"CHF");
/// Japanese yen.
pub const JPY: Currency = Currency(*b"JPY");
/// Parse and validate a three-letter code. Input is uppercased, so
/// `"usd"` and `"USD"` both work; `"US"` or `"USDD"` fail.
pub fn new(code: &str) -> Result<Self, LedgerError> {
let bytes = code.as_bytes();
if bytes.len() != 3 || !bytes.iter().all(|b| b.is_ascii_alphabetic()) {
return Err(LedgerError::InvalidCurrencyCode(code.to_string()));
}
let mut upper = [0u8; 3];
for (i, b) in bytes.iter().enumerate() {
upper[i] = b.to_ascii_uppercase();
}
Ok(Currency(upper))
}
/// The currency's minor units: how many decimal places its smallest
/// unit has (2 for USD's cents, 0 for JPY, 3 for KWD), or `None` for
/// codes ISO 4217 gives none (gold, SDRs). Unlisted codes are 2.
///
/// SOURCE: ISO 4217 list one, published 2026-09-17
/// (docs/spec/sources/iso-4217.md); generated from it, not typed in.
pub fn minor_units(&self) -> Option<u32> {
match self.code() {
"BIF" | "CLP" | "DJF" | "GNF" | "ISK" | "JPY" | "KMF" | "KRW" | "PYG" | "RWF"
| "UGX" | "UYI" | "VND" | "VUV" | "XAF" | "XOF" | "XPF" => Some(0),
"BHD" | "IQD" | "JOD" | "KWD" | "LYD" | "OMR" | "TND" => Some(3),
"CLF" | "UYW" => Some(4),
"XAG" | "XAU" | "XBA" | "XBB" | "XBC" | "XBD" | "XDR" | "XPD" | "XPT" | "XSU"
| "XTS" | "XUA" | "XXX" => None,
_ => Some(2),
}
}
/// The decimal places a share of an amount in this currency is split
/// to (members' shares of earnings, periods::allocate_cumulative): its
/// minor units (cents, whole yen, thousandths of a dinar), or, for a
/// code ISO 4217 gives none (gold, SDRs), the most a line may carry
/// (kernel::MAX_LINE_SCALE). Every posted amount is then a whole number
/// of the unit split to, so no member's running share is ever a unit
/// from exact (BS-partner-1); split to cents, a finer amount's
/// leftover fraction would all go to one member.
pub fn allocation_places(&self) -> u32 {
self.minor_units().unwrap_or(crate::kernel::MAX_LINE_SCALE)
}
/// One allocation unit as an amount: `1` at [`Self::allocation_places`]
/// (a cent, a whole yen, a thousandth of a dinar, the finest a line
/// carries for a code with no minor unit). This is the one place that
/// turns the places into an amount, so a split and a check of that
/// split take their unit from the same rule and cannot drift apart
/// (BS-partner-1 statement 4: the allocation is the split of the
/// amount to the base currency's smallest unit).
pub fn allocation_unit(&self) -> Decimal {
Decimal::new(1, self.allocation_places())
}
/// The three-letter code, e.g. `"USD"`.
pub fn code(&self) -> &str {
std::str::from_utf8(&self.0).expect("currency codes are ASCII by construction")
}
}
impl fmt::Debug for Currency {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Currency({})", self.code())
}
}
impl fmt::Display for Currency {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.code())
}
}
/// An exact decimal amount in a specific currency.
///
/// `Money` is `Copy` and cheap: pass it by value. All arithmetic is checked
/// for currency agreement and returns [`LedgerError::CurrencyMismatch`]
/// rather than silently converting.
#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Money {
/// The exact amount. Never a float.
pub amount: Decimal,
/// The currency this amount is denominated in.
pub currency: Currency,
}
impl Money {
/// Build money from an exact decimal amount.
pub fn new(amount: Decimal, currency: Currency) -> Self {
Self { amount, currency }
}
/// Zero in the given currency.
pub fn zero(currency: Currency) -> Self {
Self {
amount: Decimal::ZERO,
currency,
}
}
/// True when the amount is exactly zero.
pub fn is_zero(&self) -> bool {
self.amount.is_zero()
}
fn check_currency(&self, other: &Money) -> Result<(), LedgerError> {
if self.currency != other.currency {
return Err(LedgerError::CurrencyMismatch {
expected: self.currency.code().to_string(),
found: other.currency.code().to_string(),
});
}
Ok(())
}
/// Add two money values. Fails on currency mismatch, and with
/// `TooLargeToCompute` when the sum is past exact arithmetic (never a
/// panic, never rounded).
pub fn checked_add(self, rhs: Money) -> Result<Money, LedgerError> {
self.check_currency(&rhs)?;
let amount = crate::kernel::Amount::checked_add(self.amount, rhs.amount)
.ok_or_else(|| LedgerError::TooLargeToCompute(format!("{} plus {}", self, rhs)))?;
Ok(Money::new(amount, self.currency))
}
/// Subtract two money values. Fails on currency mismatch, and with
/// `TooLargeToCompute` when the difference is past exact arithmetic.
pub fn checked_sub(self, rhs: Money) -> Result<Money, LedgerError> {
self.check_currency(&rhs)?;
let amount = crate::kernel::Amount::checked_sub(self.amount, rhs.amount)
.ok_or_else(|| LedgerError::TooLargeToCompute(format!("{} minus {}", self, rhs)))?;
Ok(Money::new(amount, self.currency))
}
}
/// 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` (BS-entry-3, BS-entry-4).
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()))
}
/// `-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).
///
/// This and the four helpers below are public for the crates around the
/// core (the store, the tools, the viewer): they too may hold no raw
/// `Decimal` operator (`just kernel-only`), and a sign or a sum of posted
/// figures is said here once rather than in each of them.
pub 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 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 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 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 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)
}
}
/// A non-negative amount as a whole number of steps, for exact proration
/// by days (kernel::prorate_by_days): `units` steps of 10^-`scale` each,
/// where `scale` is the amount's own places or its currency's minor unit,
/// whichever is more, and `unit` is one minor unit in steps.
///
/// ```text
/// GOAL: depreciation and daily schedules round to the currency's own
/// smallest unit (cents, whole yen, thousandths of a dinar), never a
/// fixed two places.
/// SOURCE: BS-ledger-1 (no fraction of the minor unit; ISO 4217,
/// docs/spec/sources/iso-4217.md).
/// INVARIANTS:
/// 1. amount = units × 10^-scale exactly; unit = 10^(scale − minor).
/// 2. A currency with no minor unit (gold, SDRs) counts as two places.
/// 3. NEVER rounds: an amount that doesn't fit is None.
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct Steps {
/// The amount, in steps.
pub units: i128,
/// One minor unit, in steps.
pub unit: i128,
/// Decimal places of one step.
pub scale: u32,
/// The currency's minor unit (two when it has none).
pub minor: u32,
}
impl Steps {
/// `amount` (not negative) in `currency`'s steps.
pub fn of(amount: Decimal, currency: Currency) -> Option<Steps> {
if amount.is_sign_negative() && !amount.is_zero() {
return None;
}
let minor = currency.minor_units().unwrap_or(2);
let n = amount.normalize();
let scale = n.scale().max(minor);
let units = n
.mantissa()
.checked_mul(10i128.checked_pow(scale - n.scale())?)?;
Some(Steps {
units,
unit: 10i128.checked_pow(scale - minor)?,
scale,
minor,
})
}
/// `n` steps as a decimal, with the currency's minor places at least
/// (12,000 dollars prints 12000.00, 8,333 yen 8333). `None` past
/// Decimal's range.
pub fn decimal(&self, n: i128) -> Option<Decimal> {
let d = Decimal::try_from_i128_with_scale(n, self.scale).ok()?;
if self.scale == self.minor {
return Some(d);
}
let d = d.normalize();
if d.scale() < self.minor {
// Trailing zeros only, so never refused.
return Some(crate::kernel::rescale_exact(d, self.minor).unwrap_or(d));
}
Some(d)
}
}
impl fmt::Display for Money {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {}", self.amount, self.currency)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn usd(amount: Decimal) -> Money {
Money::new(amount, Currency::USD)
}
fn d(cents: i64) -> Decimal {
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!(
usd(d(1050)).checked_add(usd(d(425))).unwrap().amount,
d(1475)
);
assert_eq!(
usd(d(1050)).checked_sub(usd(d(425))).unwrap().amount,
d(625)
);
// Subtraction is not division, and not addition.
assert_ne!(
usd(d(1000)).checked_sub(usd(d(200))).unwrap().amount,
d(500)
);
}
#[test]
fn checked_arithmetic_refuses_overflow_instead_of_panicking() {
let max = usd(Decimal::MAX);
let min = usd(Decimal::MIN);
assert!(matches!(
max.checked_add(max),
Err(LedgerError::TooLargeToCompute(_))
));
assert!(matches!(
min.checked_add(min),
Err(LedgerError::TooLargeToCompute(_))
));
assert!(matches!(
min.checked_sub(max),
Err(LedgerError::TooLargeToCompute(_))
));
assert!(matches!(
max.checked_sub(min),
Err(LedgerError::TooLargeToCompute(_))
));
// At the edge, exact results still work.
assert_eq!(max.checked_add(min).unwrap().amount, Decimal::ZERO);
assert_eq!(max.checked_sub(max).unwrap().amount, Decimal::ZERO);
// Currency mismatch still reports as a mismatch.
let eur = Money::new(Decimal::ONE, Currency::new("EUR").unwrap());
assert!(matches!(
max.checked_add(eur),
Err(LedgerError::CurrencyMismatch { .. })
));
}
/// ISO 4217 list one (docs/spec/sources/iso-4217.md): one code from
/// each exponent class, and the codes with no minor unit.
#[test]
fn minor_units_follow_iso_4217() {
let units = |c: &str| Currency::new(c).unwrap().minor_units();
assert_eq!(units("USD"), Some(2));
assert_eq!(units("JPY"), Some(0));
assert_eq!(units("BHD"), Some(3));
assert_eq!(units("KWD"), Some(3));
assert_eq!(units("CLF"), Some(4));
assert_eq!(units("UYW"), Some(4));
assert_eq!(units("XAU"), None);
assert_eq!(units("XDR"), None);
}
#[test]
fn currency_and_money_print_their_code_and_amount() {
assert_eq!(Currency::new("usd").unwrap().to_string(), "USD");
assert_eq!(format!("{:?}", Currency::EUR), "Currency(EUR)");
assert_eq!(usd(d(1250)).to_string(), "12.50 USD");
assert_eq!(format!("{:?}", usd(d(-5))), "-0.05 USD");
}
#[test]
fn is_zero_means_exactly_zero() {
assert!(Money::zero(Currency::USD).is_zero());
assert!(usd(d(0)).is_zero());
assert!(!usd(d(1)).is_zero());
assert!(!usd(d(-1)).is_zero());
}
/// Steps INVARIANTS 1-3: cents for dollars, whole yen, thousandths of
/// a dinar, two places for gold; an amount finer than its currency
/// keeps its own places, with the unit a minor unit.
#[test]
fn steps_are_the_currencys_minor_unit() {
let s =
|a: &str, c: &str| Steps::of(a.parse().unwrap(), Currency::new(c).unwrap()).unwrap();
let usd = s("12000", "USD");
assert_eq!((usd.units, usd.unit, usd.scale), (1_200_000, 1, 2));
assert_eq!(usd.decimal(1_148_387).unwrap().to_string(), "11483.87");
assert_eq!(usd.decimal(1_200_000).unwrap().to_string(), "12000.00");
let jpy = s("100000", "JPY");
assert_eq!((jpy.units, jpy.unit, jpy.scale), (100_000, 1, 0));
assert_eq!(jpy.decimal(8_333).unwrap().to_string(), "8333");
let kwd = s("1000.000", "KWD");
assert_eq!((kwd.units, kwd.unit, kwd.scale), (1_000_000, 1, 3));
let gold = s("5", "XAU");
assert_eq!((gold.units, gold.unit, gold.scale), (500, 1, 2));
let fine = s("100.006", "USD");
assert_eq!((fine.units, fine.unit, fine.scale), (100_006, 10, 3));
assert_eq!(fine.decimal(100_010).unwrap().to_string(), "100.01");
assert_eq!(fine.decimal(100_006).unwrap().to_string(), "100.006");
assert_eq!(fine.decimal(0).unwrap().to_string(), "0.00");
assert_eq!(Steps::of(d(-1), Currency::USD), None);
assert_eq!(usd.decimal(i128::MAX), None);
}
#[test]
fn checked_arithmetic_rejects_currency_mismatch() {
let eur = Money::new(d(100), Currency::EUR);
assert!(usd(d(100)).checked_add(eur).is_err());
assert!(usd(d(100)).checked_sub(eur).is_err());
}
}