refactor(core): foreign currency conversion, fx_booked, in the kernel

The one FX conversion that books money (add_line_fx: foreign x rate,
rounded once to the base currency's minor unit, half away from zero,
B-3 / BS-ledger-1 and its CHOICE BS-ledger-14) was inline in ledger.rs.
It now lives in kernel/fx.rs as fx_booked<T: Amount> (exact checked
product, one round_step; None on a non-positive rate or overflow) with
fx_booked_minor as its Decimal/currency spelling, which also keeps the
pre-kernel scale and books the exact product for a currency with no
minor unit. add_line_fx keeps its own error variants and refusals, so
behaviour is identical for every input; every test and golden passes
unchanged. Grep found no other FX conversion site: set/get_fx_rate
only record a rate, the store delegates to the core, and replay books
a recorded amount verbatim.

Co-Authored-By: Muse Wonder 1.0
This commit is contained in:
swaitsandMuse Wonder 1.0 committed 2026-11-08 15:11:36 -07:00
1 parent 8874b603b4
commit f9affd0e1f
2 files changed
+110 -11

No files matched your search

+7 -6
View File
@@ -1864,15 +1864,16 @@ impl Ledger {
found: foreign_currency.code().to_string(),
});
}
let exact = foreign_amount
.checked_mul(fx_rate)
.ok_or(LedgerError::AmountOutOfRange)?;
// Booked in the base currency's smallest unit (cents for USD), half
// away from zero: a ledger in one money holds no fractions of its
// smallest unit (rules.md B-3; DECISION, owner 2026-10-13, after
// the GnuCash cross-check found the difference).
let base_amount = crate::kernel::round_minor(
exact,
// the GnuCash cross-check found the difference). The kernel's
// fx_booked_minor is that computation (foreign x rate exactly,
// rounded once); the rate and zero-result refusals above and
// below keep their error variants.
let base_amount = crate::kernel::fx_booked_minor(
foreign_amount,
fx_rate,
self.base_currency,
crate::kernel::Rounding::HalfAwayFromZero,
)