refactor: rule references cite bookspec ids

Every B-n citation in crates/, docs/, deploy/, scripts/ and .github/ now names its bookspec id BS-<area>-<n> (D-68), via scripts/rewrite-rule-refs.py: the map is parsed from docs/spec/rules.md's index, rules.md-prefixed and bare forms are rewritten, sub-references (rule 11, item 7, INVARIANT 2) survive, and plan/, docs/spec/rules.md and docs/spec/claims/ are untouched. Program-output string literals that cited a rule were reverted by hand so user-visible output and its goldens are unchanged; DatedRule.catalog, metadata no output prints, now holds BS-1099-1. One SOURCE line that cited only 'R-?' now also cites BS-statement-5.

Co-Authored-By: Muse Wonder 1.0
This commit is contained in:
swaitsandMuse Wonder 1.0 committed 2026-11-08 19:40:22 -07:00
1 parent 7a177a3cfc
commit 489360f598
221 files changed
+1706 -1580

No files matched your search

+47 -47
View File
@@ -27,7 +27,7 @@ pub trait Amount: Copy + Ord {
fn from_u32(n: u32) -> Option<Self>;
/// `self * rhs`, or `None` when it cannot be represented exactly.
///
/// A quantity times a unit price (a document line, B-39) is the one
/// A quantity times a unit price (a document line, BS-arap-3) is the one
/// use: it never rounds.
fn checked_mul(self, rhs: Self) -> Option<Self>;
/// `self / rhs` exactly, or `None` when `rhs` is zero or the quotient
@@ -38,8 +38,8 @@ pub trait Amount: Copy + Ord {
/// [`Decimal`] dollars), ties away from zero; never a negative zero.
/// `None` when `rhs` is zero or the quotient cannot be represented.
///
/// The one use is a tax category's deductible amount (rules.md B-32,
/// B-21), whose shares are added exactly and rounded once, never
/// The one use is a tax category's deductible amount (BS-ledger-11,
/// BS-entry-3), whose shares are added exactly and rounded once, never
/// share by share.
fn checked_div_round_cent(self, rhs: Self) -> Option<Self>;
/// `self` rounded to a whole multiple of `step`, in `mode`; `None`
@@ -330,7 +330,7 @@ pub fn net<T: Amount>(lines: impl IntoIterator<Item = (Side, T)>) -> Option<T> {
/// ```text
/// GOAL: an early-payment discount never takes more than its percentage
/// says, in exact money.
/// SOURCE: docs/spec/rules.md B-39 INVARIANT 2, B-42; plan/served.md
/// SOURCE: BS-arap-3 INVARIANT 2, BS-arap-6; plan/served.md
/// 8.3.13; CHOICE: the gross method, A-8 (the discount is
/// booked when taken, never estimated).
/// INVARIANTS:
@@ -360,14 +360,14 @@ pub struct Settlement<T> {
pub deposit: T,
}
/// The arithmetic of a settlement (rules.md B-42; plan P4b S6): each
/// The arithmetic of a settlement (BS-arap-6; plan P4b S6): each
/// document's cash and discount, and the processor's fee withheld from the
/// cash that reaches the bank.
///
/// ```text
/// GOAL: a settlement is exact: what the documents are settled by is the
/// cash plus the discounts, and the deposit is the cash less the fee.
/// SOURCE: docs/spec/rules.md B-42; plan/served.md 8.3.6, 8.3.13 (B-42);
/// SOURCE: BS-arap-6; plan/served.md 8.3.6, 8.3.13 (BS-arap-6);
/// CONVENTION; DECISION: the gross method (A-8) and the fee's
/// gross presentation (A-4).
/// INVARIANTS:
@@ -412,7 +412,7 @@ pub fn settle<T: Amount>(cash: &[T], discounts: &[T], fee: T) -> Option<Settleme
/// ```text
/// GOAL: a line's quantity times its unit price is the amount it posts,
/// to the last decimal place.
/// SOURCE: docs/spec/rules.md B-39; plan/served.md 8.3.4 ("kernel::
/// SOURCE: BS-arap-3; plan/served.md 8.3.4 ("kernel::
/// line_amount(q, u) == amount exactly").
/// INVARIANTS:
/// 1. Some(p) gives p = quantity x unit_price exactly.
@@ -431,8 +431,8 @@ pub fn line_amount<T: Amount>(quantity: T, unit_price: T) -> Option<T> {
/// ```text
/// GOAL: a category's deductible amount is its exact shares added and
/// rounded once, never a sum of rounded shares.
/// SOURCE: docs/spec/rules.md B-32 (CHOICE: owner, 2026-11-07, D-67) and
/// B-21 (Form 1065 instructions: "include cents when adding the
/// SOURCE: BS-ledger-11 (CHOICE: owner, 2026-11-07, D-67) and
/// BS-entry-3 (Form 1065 instructions: "include cents when adding the
/// amounts and round off only the total"); plan/served.md 10.1.
/// INVARIANTS:
/// 1. On Some(a), a is the exact sum S = Σ(book_i × percent_i ÷ 100)
@@ -579,7 +579,7 @@ pub fn credit_line<T: Amount>(credit: T) -> Option<(Side, T)> {
}
// ---------------------------------------------------------------------------
// 1099 payments: where each payment line goes (rules.md B-19)
// 1099 payments: where each payment line goes (BS-1099-1)
// ---------------------------------------------------------------------------
/// A line's role in a payment to a vendor, from its account.
@@ -646,7 +646,7 @@ impl Bucket {
/// GOAL: every payment line to a vendor counts toward its 1099, is a card
/// or payment-network payment (Form 1099-K), or is unknown; never
/// guessed.
/// SOURCE: docs/spec/rules.md B-19 (Treas. Reg. §1.6041-1(a)(1)(iv): a
/// SOURCE: BS-1099-1 (Treas. Reg. §1.6041-1(a)(1)(iv): a
/// transaction reportable under §6050W is not reported under
/// §6041; §1.6041-1(e)(1): a member writing a check at the
/// company's direction doesn't make the member the payor;
@@ -693,7 +693,7 @@ pub fn split_payment<T: Amount>(lines: &[(Option<Bucket>, Side, T)]) -> Option<[
/// ```text
/// GOAL: a card bill paid from the bank, a member reimbursed, or a loan
/// paid down never counts toward a vendor's 1099.
/// SOURCE: docs/spec/rules.md B-19 (26 U.S.C. §6041(a): payments made to
/// SOURCE: BS-1099-1 (26 U.S.C. §6041(a): payments made to
/// the payee; P3 review R1, 2026-10-20).
/// INVARIANTS:
/// 1. The buckets' sum P is kept exactly.
@@ -758,8 +758,8 @@ pub fn plug_last<T: Amount>(whole: T, parts: &mut [T]) -> Option<()> {
/// ```text
/// GOAL: split a whole into parts, each a whole number of units where
/// possible, summing to the whole exactly, without favoring anyone.
/// SOURCE: docs/spec/rules.md B-13 (shares: 26 U.S.C. §704(a); cent
/// rounding: DECISION, owner 2026-10-13), B-21 (exact totals).
/// SOURCE: BS-partner-1 (shares: 26 U.S.C. §704(a); cent
/// rounding: DECISION, owner 2026-10-13), BS-entry-3 (exact totals).
/// INVARIANTS:
/// 1. MUST make the parts sum to `whole` exactly.
/// 2. MUST change each part by zero or one unit (the first in order
@@ -835,7 +835,7 @@ pub fn apportion<T: Amount>(whole: T, parts: &mut [T], remainders: &[T], unit: T
/// ```text
/// GOAL: members' running totals that never drift a unit from their
/// exact shares, whatever earlier closes did.
/// SOURCE: docs/spec/rules.md B-13 (shares: 26 U.S.C. §704(a); capital
/// SOURCE: BS-partner-1 (shares: 26 U.S.C. §704(a); capital
/// accounts carry the allocations: Treas. Reg.
/// §1.704-1(b)(2)(iv)(b); cent rounding: DECISION, owner
/// 2026-10-13; counted from the first close: D-10, 2026-10-16).
@@ -880,7 +880,7 @@ pub fn apportion_cumulative<T: Amount>(
Some(())
}
/// Where a line falls on the statement of cash flows (rules.md B-14 rule
/// Where a line falls on the statement of cash flows (BS-statement-3 rule
/// 2): a cash account, or one of the three sections.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Flow {
@@ -895,8 +895,8 @@ pub enum Flow {
}
impl Flow {
/// The three sections, in the order cash is applied to them (rules.md
/// B-14 rule 3): operating, investing, financing.
/// The three sections, in the order cash is applied to them (
/// BS-statement-3 rule 3): operating, investing, financing.
pub const SECTIONS: [Flow; 3] = [Flow::Operating, Flow::Investing, Flow::Financing];
}
@@ -938,7 +938,7 @@ pub fn take<T: Amount>(remaining: T, movement: T) -> Option<T> {
/// ```text
/// GOAL: split an entry's cash among the parts it settled, without
/// inventing or losing any.
/// SOURCE: rules.md B-14 rule 3 (ASC 230-10-50-5: only the cash portion
/// SOURCE: BS-statement-3 rule 3 (ASC 230-10-50-5: only the cash portion
/// of a part-cash transaction is a cash flow; the order is a
/// CONVENTION, the ASC is silent).
/// INVARIANTS:
@@ -965,7 +965,7 @@ pub fn apply_cash<T: Amount>(cash: T, movements: &[T], applied: &mut [T]) -> Opt
(remaining == T::ZERO).then_some(())
}
/// An entry's cash flows (rules.md B-14 rule 3): the net movement of its
/// An entry's cash flows (BS-statement-3 rule 3): the net movement of its
/// cash lines, applied to the sections moving against it in the order
/// operating, investing, financing, each up to its own movement; what is
/// left of a section's movement is the part no cash settled. `None` when
@@ -1036,7 +1036,7 @@ fn credits_and_debits<T: Amount>(movements: &[T]) -> Option<(T, T)> {
}
/// Which way money a member or a lender handled for the company went
/// (rules.md B-14 rule 11, D-42).
/// (BS-statement-3 rule 11, D-42).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum OnBehalf {
/// Paid for the company: sources credited (borrowed, contributed)
@@ -1058,8 +1058,8 @@ impl OnBehalf {
}
/// What a member or a lender paid, or received, for the company with no
/// cash account moving: a constructive receipt and disbursement (rules.md
/// B-14 rule 11, D-42). `sources` are what is left unsettled of the
/// cash account moving: a constructive receipt and disbursement (
/// BS-statement-3 rule 11, D-42). `sources` are what is left unsettled of the
/// entry's kinds that can pay or receive for the company (a financing
/// liability, a member's equity), `uses` of the kinds they can pay or
/// receive for, each debit-positive. [`OnBehalf::Paid`]: `amount =
@@ -1075,7 +1075,7 @@ impl OnBehalf {
/// GOAL: show money a member or lender handled for the company as the
/// company's own, borrowed (or contributed) and spent, without
/// inventing or losing a cent.
/// SOURCE: rules.md B-14 rule 11 (D-42; ASC 230 is silent, PwC Viewpoint
/// SOURCE: BS-statement-3 rule 11 (D-42; ASC 230 is silent, PwC Viewpoint
/// FSP 6.9.3.1, Deloitte DART Roadmap 7.2, BDO Blueprint 3.4).
/// INVARIANTS:
/// 1. The parts net to zero: Σ from + Σ to = 0, so the entry's cash
@@ -1108,7 +1108,7 @@ pub fn constructive<T: Amount>(
}
/// How much of an entry is an exchange inside investing and financing
/// that no cash settled (rules.md B-14 rule 7, ASC 230-10-50-4: debt
/// that no cash settled (BS-statement-3 rule 7, ASC 230-10-50-4: debt
/// converted to equity, one asset traded for another). `debits` and
/// `credits` are the net debits and net credits of the entry's investing
/// and financing balance-sheet accounts (each account netted first; both
@@ -1123,7 +1123,7 @@ pub fn exchanged<T: Amount>(debits: T, credits: T, inflow: T, outflow: T) -> Opt
Some(d.min(c).max(T::ZERO))
}
/// Which way an entry moved value with its parties (rules.md B-35).
/// Which way an entry moved value with its parties (BS-partner-3).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Direction {
/// The company paid the party, or gave it value: a line on the
@@ -1172,7 +1172,7 @@ impl Direction {
}
/// What an entry's parties split, and what of it counts in their totals
/// (rules.md B-35).
/// (BS-partner-3).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PartyTarget<T> {
/// The whole, signed as a line on the parties' own account would be
@@ -1216,7 +1216,7 @@ fn least<T: Amount>(a: T, b: T) -> T {
if a < b { a } else { b }
}
/// What an entry's parties split and what of it counts (rules.md B-35
/// What an entry's parties split and what of it counts (BS-partner-3
/// item 3, every case but a close and a reversal, which the caller
/// handles: a close is its parties' own-account movement with direction
/// neither, a reversal is [`reverse_target`] of its original's).
@@ -1227,7 +1227,7 @@ fn least<T: Amount>(a: T, b: T) -> T {
/// money or as property, so a property contribution and its
/// reversal never read alike, and a promise is never counted as if
/// it were paid.
/// SOURCE: rules.md B-35 (B-2 signs; DECISION, owner, 2026-10-20: a
/// SOURCE: BS-partner-3 (BS-entry-2 signs; DECISION, owner, 2026-10-20: a
/// member's entry counts what moved their capital, money is cash,
/// cards, the members' financing and loans, and a liability credit
/// never counts as paying that liability's own creditor; Helvering
@@ -1321,7 +1321,7 @@ pub fn party_target<T: Amount>(
})
}
/// A reversal's target from its original's (rules.md B-35 item 3; B-9: a
/// A reversal's target from its original's (BS-partner-3 item 3; BS-ledger-5: a
/// correction is the opposite entry): every amount negated (never to a
/// negative zero), paid and received swapped. An involution. `None` past
/// the type's range.
@@ -1495,10 +1495,10 @@ fn round_to_unit<T: Units>(num: T, den: T, unit: T) -> Option<T> {
/// GOAL: how much of a total a span of days has used up by a given day,
/// each day of a month bearing the same share, the whole total
/// reached on the span's last day and never before.
/// SOURCE: docs/spec/rules.md B-15 (straight line, partial months by days;
/// SOURCE: BS-asset-1 (straight line, partial months by days;
/// ASC 360-10-35-4: "systematic and rational", as equitably as
/// possible to the periods served), B-16 (daily schedules, D-11),
/// B-21 (exact totals).
/// possible to the periods served), BS-prepaid-1 (daily schedules, D-11),
/// BS-entry-3 (exact totals).
/// INVARIANTS (W = months × MONTH_L, m = part.month_days, d = part.days):
/// 1. Not the last part: round(total × (before + k × MONTH_L / m) / W):
/// the monthly rate total/months times the days held over the days
@@ -1520,8 +1520,8 @@ fn round_to_unit<T: Units>(num: T, den: T, unit: T) -> Option<T> {
/// W), or any product overflows:
/// - SHOULD return None
/// - NEVER panic
/// BECAUSE a figure the books can't compute is refused (rules.md
/// B-28), and symbolic parts are not a calendar.
/// BECAUSE a figure the books can't compute is refused (
/// BS-entry-4), and symbolic parts are not a calendar.
/// 2. IF 12,000.00 over 12 months from Jan 1, through Dec 15 (the last
/// part, 31 days, before = 11 months):
/// - SHOULD be 11,000.00 + 1,000.00 × 15/31 = 11,483.87
@@ -1584,7 +1584,7 @@ pub fn prorated_through<T: Units>(
/// ```text
/// GOAL: one month's posting of a span spread by day (depreciation,
/// daily prepaid and deferred-revenue schedules).
/// SOURCE: docs/spec/rules.md B-15, B-16 (D-11), B-21; plan/served.md P3
/// SOURCE: BS-asset-1, BS-prepaid-1 (D-11), BS-entry-3; plan/served.md P3
/// defect 9 (one shared prorate_by_days, Kani: parts sum to the
/// total, each is days × rate within one unit, the last absorbs
/// the remainder).
@@ -1651,7 +1651,7 @@ pub fn prorate_by_days<T: Units>(
/// SOURCE: plan/served.md P3 "Settled in design" (2026-10-20): a month of
/// depreciation skipped is caught up in the month the run is made
/// (an immaterial miss; a material one is restated, ASC 250);
/// docs/spec/rules.md B-15.
/// BS-asset-1.
/// INVARIANTS:
/// 1. The result is >= 0.
/// 2. Whenever every posted[i] <= scheduled[i], posted[i] + result <=
@@ -1707,7 +1707,7 @@ pub fn catch_ups<T: Amount>(scheduled: &[T], posted: &[T], out: &mut [T]) -> Opt
/// ```text
/// GOAL: a disposal clears exactly what the schedule says was used up
/// through its day, whatever the runs posted.
/// SOURCE: docs/spec/rules.md B-15 (a disposed asset depreciates through
/// SOURCE: BS-asset-1 (a disposed asset depreciates through
/// its disposal day); Ledger::dispose_asset INVARIANT 1 (gain =
/// proceeds − book value at the disposal date).
/// ```
@@ -1737,7 +1737,7 @@ pub struct BasisFigures<T> {
/// ```text
/// GOAL: the three tax figures of an asset, each named for what it is,
/// from one function the register, the tools and the viewer share.
/// SOURCE: docs/spec/rules.md B-25: IRC §1012(a) (cost); IRC §723 (the
/// SOURCE: BS-asset-4: IRC §1012(a) (cost); IRC §723 (the
/// contributor's adjusted basis carries over); Treas. Reg.
/// §1.168(i)-4(b)(1) (property converted from personal use: "the
/// lesser of its fair market value or its adjusted depreciable
@@ -1782,12 +1782,12 @@ pub fn basis_figures<T: Amount>(
}
/// A project's unrecovered cost from its tagged revenue and expenses to
/// date: what it would take to be repaid in full (rules.md B-37 item 4).
/// date: what it would take to be repaid in full (BS-statement-5 item 4).
///
/// ```text
/// GOAL: the "if repaid" figure of work done on spec, from one function
/// the tools, the viewer and bookspec share.
/// SOURCE: docs/spec/rules.md B-37 item 4 (ASC 450-30-25-1: a hoped-for
/// SOURCE: BS-statement-5 item 4 (ASC 450-30-25-1: a hoped-for
/// payment is a gain contingency, never recorded before it is
/// received; so this is a figure, never a receivable).
/// INVARIANTS:
@@ -1826,7 +1826,7 @@ pub enum Bounds {
/// ```text
/// GOAL: one definition of "a document is never overpaid and never owes
/// more than it is for", at every date, not only at the end.
/// SOURCE: docs/spec/rules.md B-38 INVARIANT 2 (0 <= outstanding(d, D)
/// SOURCE: BS-arap-2 INVARIANT 2 (0 <= outstanding(d, D)
/// <= total(d) at every D); plan/served.md P4b.3 (Bounds).
/// INVARIANTS:
/// 1. Within exactly when every prefix sum of `deltas` is within
@@ -1873,7 +1873,7 @@ fn out_of_bounds<T: Amount>(total: T, running: T) -> Option<(T, bool)> {
/// GOAL: a command never puts a document out of bounds, yet books an
/// earlier version left out of bounds (a void under a payment that
/// stands) take the step that mends them.
/// SOURCE: docs/spec/rules.md B-38 INVARIANT 2, B-28 (replay accepts what
/// SOURCE: BS-arap-2 INVARIANT 2, BS-entry-4 (replay accepts what
/// was written; the bound is the commands'); plan/served.md
/// P4b.14 item 3 (the remedy: reverse the payment).
/// INVARIANTS:
@@ -1918,7 +1918,7 @@ pub fn bounds_kept<T: Amount>(total: T, before: &[T], after: &[T]) -> Bounds {
mod tests {
use super::*;
/// rules.md B-14 rule 3, by hand. Equipment 1,000 for 300 cash and a
/// BS-statement-3 rule 3, by hand. Equipment 1,000 for 300 cash and a
/// 700 note: investing takes the 300, the note's 700 and the rest of
/// the equipment's are the noncash part.
#[test]
@@ -1964,7 +1964,7 @@ mod tests {
assert_eq!(apply_cash(10, &[-3], &mut out), None);
}
/// The exchange measure (rules.md B-14 rule 7) on the review's
/// The exchange measure (BS-statement-3 rule 7) on the review's
/// hand-checked cases.
#[test]
fn exchanged_matches_the_hand_checked_cases() {
@@ -1986,7 +1986,7 @@ mod tests {
assert_eq!(exchanged(1000i64, 1000, 1000, 1000), Some(0));
}
/// rules.md B-14 rule 11 (D-42), by hand.
/// BS-statement-3 rule 11 (D-42), by hand.
#[test]
fn constructive_pays_and_receives_for_the_company() {
use OnBehalf::{Paid, Received};
@@ -2339,7 +2339,7 @@ mod tests {
assert!(!i64::MIN.in_line_range());
}
/// B-9: a reversal flips every side and keeps accounts and amounts, so
/// BS-ledger-5: a reversal flips every side and keeps accounts and amounts, so
/// with the original it nets every account to zero.
#[test]
fn a_reversal_flips_every_line_and_nets_to_zero() {
+2 -2
View File
@@ -54,7 +54,7 @@ use rust_decimal::Decimal;
/// from ISO 4217), and its CHOICE BS-ledger-14
/// "book-in-the-smallest-unit" (the owner, 2026-10-13: book in
/// the smallest unit, half away from zero); wanderbooks
/// docs/spec/claims/ledger.md B-3 states the same pick. The
/// docs/spec/claims/ledger.md BS-ledger-1 states the same pick. The
/// rounding itself is kernel::round_step (BS-ledger-1,
/// BS-entry-3); owner decision D-69 (the laws live once, in
/// bookspec's Lean).
@@ -102,7 +102,7 @@ pub fn fx_booked<T: Amount>(foreign: T, rate: T, step: T, mode: Rounding) -> Opt
/// [`fx_booked`] at a currency's minor unit, on `Decimal`: the spelling
/// `Ledger::add_line_fx` books through. A currency ISO 4217 gives no
/// minor unit (gold, SDRs) books the exact product, as `round_minor`
/// has always done (B-3). The result's scale is the pre-kernel
/// has always done (BS-ledger-1). The result's scale is the pre-kernel
/// computation's: `round_places` keeps an amount already at or under
/// the minor places unchanged, and otherwise carries exactly the minor
/// places (a booked 1.50 prints "1.50", never "1.5000").
+19 -19
View File
@@ -12,54 +12,54 @@
//! `docs/spec/rules.md`):
//!
//! * a line's signed amount is `+amount` for a debit, `-amount` for a credit
//! (B-2);
//! (BS-entry-2);
//! * an entry is acceptable when it has two or more lines, every amount is
//! positive and in range, and its debits equal its credits exactly (B-1,
//! B-2, B-21);
//! * an acceptable entry nets to zero, account by account summed (B-1);
//! positive and in range, and its debits equal its credits exactly (BS-entry-1,
//! BS-entry-2, BS-entry-3);
//! * an acceptable entry nets to zero, account by account summed (BS-entry-1);
//! * a reversal flips every side, so it is acceptable exactly when the
//! original is, and the two together net every account to zero (B-9);
//! original is, and the two together net every account to zero (BS-ledger-5);
//! * an account's balance is its movement read on its normal side, so the
//! balances satisfy the accounting equation whenever movements net to
//! zero (B-5, B-7);
//! zero (BS-ledger-3, BS-statement-2);
//! * a trial balance puts each movement in exactly one column, so its
//! columns differ by the net of all movements (B-6);
//! * a closing line zeroes the account it closes (B-11);
//! columns differ by the net of all movements (BS-statement-1);
//! * a closing line zeroes the account it closes (BS-period-1);
//! * a payment to a vendor splits, line by line, into what counts toward
//! its 1099, what a card or payment network paid, and what is unknown,
//! and money moved between the company's own sources nets out (B-19);
//! and money moved between the company's own sources nets out (BS-1099-1);
//! * an early-payment discount never exceeds its stated percentage of the
//! total, and a document line's amount is exactly its quantity times its
//! unit price, neither rounded (B-39);
//! unit price, neither rounded (BS-arap-3);
//! * an allocation's parts sum to the whole, to the last unit, by largest
//! remainder (B-13);
//! remainder (BS-partner-1);
//! * a cumulative allocation hands out exactly the new amount, and leaves
//! each running total one allocation of everything allocated, so it never
//! drifts a unit from its exact share (B-13);
//! drifts a unit from its exact share (BS-partner-1);
//! * an entry's cash is applied to the sections of the statement of cash
//! flows moving against it, in a fixed order, each up to its own
//! movement: the parts sum to the entry's cash, what no cash settled sums
//! to zero, and a reversal's parts are the original's negated (B-14);
//! to zero, and a reversal's parts are the original's negated (BS-statement-3);
//! * what a member or lender paid or received for the company, with no
//! cash account moving, is split among the kinds it settled, netting to
//! zero, and mirrored by a reversal (B-14 rule 11);
//! zero, and mirrored by a reversal (BS-statement-3 rule 11);
//! * a party's part of an entry is signed as a line on the party's own
//! account would be, a reversal's is its original's negated, and what of
//! it counts as money or as property never exceeds it (B-35);
//! * a span prorated by days (depreciation, B-15; daily schedules, B-16)
//! it counts as money or as property never exceeds it (BS-partner-3);
//! * a span prorated by days (depreciation, BS-asset-1; daily schedules, BS-prepaid-1)
//! splits into parts that sum to its total, each but the last its days
//! times the monthly rate within one unit, the last the remainder;
//! * a catch-up run brings the books to the schedule without ever carrying
//! more than it says at a month end already posted (B-15), and a
//! more than it says at a month end already posted (BS-asset-1), and a
//! disposal trues what the books carry up or down to the schedule;
//! * an asset's basis figures follow from its basis rule and facts: the
//! §723 basis is the contributor's adjusted basis, the depreciable basis
//! the cost, that basis, or the lesser of it and fair market value, and
//! the §704(c) built-in gain is fair market value less the §723 basis
//! (B-25);
//! (BS-asset-4);
//! * a tax category's deductible amount adds its accounts' exact shares and
//! rounds once to the cent, half away from zero, never share by share
//! (B-32, B-21).
//! (BS-ledger-11, BS-entry-3).
//!
//! The kernel is a module directory so each kernel-sweep unit adds its
//! arithmetic in a file of its own (`fx`, `round`, `allocate`, `total`,
+6 -6
View File
@@ -14,7 +14,7 @@ use rust_decimal::Decimal;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Rounding {
/// Nearest multiple; an exact half rounds away from zero
/// (2.5 -> 3, -2.5 -> -3). The FX booking rule (B-3).
/// (2.5 -> 3, -2.5 -> -3). The FX booking rule (BS-ledger-1).
HalfAwayFromZero,
/// Nearest multiple; an exact half rounds to the even multiple
/// (2.5 -> 2, 3.5 -> 4). Rust's `Decimal::round_dp`.
@@ -30,12 +30,12 @@ pub enum Rounding {
/// ```text
/// GOAL: one rounding primitive: money is rounded to a whole multiple of a
/// unit, once, with the mode named at the call.
/// SOURCE: docs/spec/claims/ledger.md B-3 (a ledger in one money holds no
/// SOURCE: docs/spec/claims/ledger.md BS-ledger-1 (a ledger in one money holds no
/// fractions of its smallest unit; FX books the base currency's
/// minor unit, half away from zero), claims/entry.md B-21 (exact
/// minor unit, half away from zero), claims/entry.md BS-entry-3 (exact
/// amounts in the books; round only where a rule says to), and
/// claims/ledger.md B-32; the call sites' own rules (B-13, B-14,
/// B-15, B-16, B-19, B-35, B-36, B-37); owner decision D-69 (the
/// claims/ledger.md BS-ledger-11; the call sites' own rules (BS-partner-1, BS-statement-3,
/// BS-asset-1, BS-prepaid-1, BS-1099-1, BS-partner-3, BS-partner-4, BS-statement-5); owner decision D-69 (the
/// laws live once, in bookspec's Lean).
/// INVARIANTS:
/// 1. Some(r) is a whole multiple of step: r = n x step for an integer
@@ -96,7 +96,7 @@ pub fn round_places(amount: Decimal, places: u32, mode: Rounding) -> Option<Deci
/// `amount` rounded to whole `currency` minor units in `mode`: cents for
/// USD, whole yen for JPY, thousandths for KWD. A currency ISO 4217 gives
/// no minor unit (gold, SDRs) is returned exactly, as the FX booking rule
/// has always done (B-3).
/// has always done (BS-ledger-1).
pub fn round_minor(amount: Decimal, currency: Currency, mode: Rounding) -> Option<Decimal> {
match currency.minor_units() {
Some(places) => round_places(amount, places, mode),
+5 -5
View File
@@ -3,7 +3,7 @@
//! The sum of a collection of money figures, in one place. A raw `Decimal`
//! `+`, `+=` or `.sum()` panics at `Decimal::MAX`; these return `None`
//! instead, so the caller can refuse with `TooLargeToCompute` and never
//! crash a book (rules.md B-28: a replayed journal line is accepted "of
//! crash a book (BS-entry-4: a replayed journal line is accepted "of
//! any size", so a restored figure is the realistic path to the edge).
//! The signed sum of `(Side, amount)` lines is [`super::net`] and the pair
//! of columns [`super::totals`]; they were here first and stay the one
@@ -17,8 +17,8 @@ use super::Amount;
/// ```text
/// GOAL: a book's or report's total is the exact sum of its figures, or
/// refused; no total is ever rounded, wrapped or a crash.
/// SOURCE: docs/spec/rules.md B-21 (amounts are exact; rounding is
/// confined to returns), B-28 (replayed lines are accepted "of any
/// SOURCE: BS-entry-3 (amounts are exact; rounding is
/// confined to returns), BS-entry-4 (replayed lines are accepted "of any
/// size"; no input may reach a panic); bookspec BS-entry-3,
/// BS-entry-4; CONVENTION.
/// INVARIANTS:
@@ -49,7 +49,7 @@ pub fn total<T: Amount>(values: impl IntoIterator<Item = T>) -> Option<T> {
/// ```text
/// GOAL: the sum of a mapped field (a row's amount, a member's share) is
/// the exact sum of the mapping, or refused.
/// SOURCE: docs/spec/rules.md B-21, B-28; bookspec BS-entry-3,
/// SOURCE: BS-entry-3, BS-entry-4; bookspec BS-entry-3,
/// BS-entry-4; CONVENTION.
/// INVARIANTS:
/// 1. total_of(items, f) = total(f(item) for item in items), exactly.
@@ -69,7 +69,7 @@ pub fn total_of<T: Amount, I>(
/// ```text
/// GOAL: the sum of a derived figure (a column's contributions less its
/// distributions) is exact, or refused.
/// SOURCE: docs/spec/rules.md B-21, B-28; bookspec BS-entry-3,
/// SOURCE: BS-entry-3, BS-entry-4; bookspec BS-entry-3,
/// BS-entry-4; CONVENTION.
/// INVARIANTS:
/// 1. total_try(items, f) = Some(Σ f(item)) exactly when every f(item)