config, mcp, gui: specs from sources, spec-blind suites, and the bugs they found
ledger-config: parse gets a sourced spec. A fiscal year starts on the first of a month (IRS Pub. 538); tax_classification is read by whole words (Treas. Reg. 301.7701-3, Schedule C instructions; "business corp" no longer reads as an S corp, "corporation or S corporation" is refused); duplicate or blank members and shared capital accounts are refused; a negative or non-grammatical receipt threshold is refused; codes trimmed. tests/spec_config.rs (65 spec-blind tests, 14 properties), fuzz target fuzz_config_parse (weekly). Mutation: 55 of 55 viable caught (from 25/38). The receipt policy moves to ledger-config, with one loader (effective_receipt_policy) and one decimal grammar, so the server and the viewer enforce and show the same rule (the viewer had its own: only expense-like debits, a silent $25 default, and no config.json). ledger-mcp: a module spec (MCP 2025-06-18 tools, JSON-RPC 2.0, the operating contract). Amounts in are exactly -?digits[.digits]; a number that is one account's code and another's id (vendors, classes alike) is refused, not guessed; the threshold is validated at startup. Fixed: add_line with a bad class journaled the line before refusing; profit_and_loss_by_class ignored `class`; aging buckets said "0". A wire in front of rmcp answers malformed JSON with -32700 and invalid requests with -32600 and their id (rmcp dropped the first, omitted the id). tests/spec_mcp.rs (26 spec-blind tests: every tool's result against its own outputSchema, a random-call state machine checking the journal is untouched by every refusal, amount grammar, previews, the receipt policy). ledger-gui: money, accounting, and dollars never round (they rounded to cents, banker's style); export file names avoid Windows device names, trailing dots and spaces, empty names, and 255-byte overflow; specs on attention, history, fuzzy search, and axis ticks. ledger-core: the lifetime profit_and_loss is renamed unclosed_earnings (docs/testing.md #3). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GWVo7x3UHw5kwCUaWxiejX
This commit is contained in:
1 parent
09efa17670
commit
57c4cc0ab6
42 files changed
+4636
-363
No files matched your search
@@ -1,36 +1,56 @@
|
||||
//! Formatting money, dates, and counts. Amounts are exact decimals in and
|
||||
//! exact strings out: nothing here rounds except `compact`, which says so.
|
||||
//!
|
||||
//! ```text
|
||||
//! GOAL: show every amount exactly as the books hold it.
|
||||
//! SOURCE: DECISION (the MCP contract: "money is exact or it is wrong");
|
||||
//! CONVENTION (US accounting display: thousands commas,
|
||||
//! negatives in parentheses or with a minus sign).
|
||||
//! INVARIANTS:
|
||||
//! 1. money, accounting, and dollars NEVER round: every decimal place
|
||||
//! the amount has (at least two) is shown, thousands grouped by
|
||||
//! commas in threes, so the text reads back to the same value.
|
||||
//! 2. Zero (and negative zero) shows without a sign; a negative shows
|
||||
//! U+2212 (money, dollars) or parentheses (accounting), never both
|
||||
//! and never "-0".
|
||||
//! 3. compact rounds (chart axes, tight tiles): $950, $1.2k, $25k,
|
||||
//! $3.4M; it never shows a sign on what rounds to zero.
|
||||
//! ```
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use rust_decimal::Decimal;
|
||||
|
||||
/// Thousands separators, two decimals, a true minus sign: `−1,234.56`.
|
||||
/// Thousands separators, every decimal (at least two), a true minus sign:
|
||||
/// `−1,234.56`.
|
||||
pub fn money(d: Decimal) -> String {
|
||||
let neg = d.is_sign_negative() && !d.is_zero();
|
||||
let s = group(d.abs().round_dp(2));
|
||||
if neg { format!("\u{2212}{s}") } else { s }
|
||||
let s = group(d.abs());
|
||||
if is_negative(d) {
|
||||
format!("\u{2212}{s}")
|
||||
} else {
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
/// Accounting style: negatives in parentheses, `(1,234.56)`.
|
||||
pub fn accounting(d: Decimal) -> String {
|
||||
if d.is_sign_negative() && !d.is_zero() {
|
||||
format!("({})", group(d.abs().round_dp(2)))
|
||||
} else {
|
||||
group(d.round_dp(2))
|
||||
}
|
||||
let s = group(d.abs());
|
||||
if is_negative(d) { format!("({s})") } else { s }
|
||||
}
|
||||
|
||||
/// With the currency sign: `$1,234.56`, `−$12.00`.
|
||||
pub fn dollars(d: Decimal) -> String {
|
||||
let neg = d.is_sign_negative() && !d.is_zero();
|
||||
let s = format!("${}", group(d.abs().round_dp(2)));
|
||||
if neg { format!("\u{2212}{s}") } else { s }
|
||||
let s = format!("${}", group(d.abs()));
|
||||
if is_negative(d) {
|
||||
format!("\u{2212}{s}")
|
||||
} else {
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
/// Rounded for chart axes and tight tiles: `$1.2k`, `$3.4M`, `$950`.
|
||||
pub fn compact(d: Decimal) -> String {
|
||||
let f: f64 = d.try_into().unwrap_or(0.0);
|
||||
let (neg, a) = (f < 0.0, f.abs());
|
||||
let a = f.abs();
|
||||
let s = if a >= 1_000_000.0 {
|
||||
format!("${:.1}M", a / 1_000_000.0)
|
||||
} else if a >= 10_000.0 {
|
||||
@@ -40,11 +60,22 @@ pub fn compact(d: Decimal) -> String {
|
||||
} else {
|
||||
format!("${a:.0}")
|
||||
};
|
||||
if neg { format!("\u{2212}{s}") } else { s }
|
||||
// No sign on what rounds to zero ("$0", not "−$0").
|
||||
if f < 0.0 && s != "$0" {
|
||||
format!("\u{2212}{s}")
|
||||
} else {
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
fn is_negative(d: Decimal) -> bool {
|
||||
d.is_sign_negative() && !d.is_zero()
|
||||
}
|
||||
|
||||
/// A non-negative amount, every decimal it has (at least two), grouped.
|
||||
fn group(d: Decimal) -> String {
|
||||
let s = format!("{:.2}", d);
|
||||
let places = d.normalize().scale().max(2) as usize;
|
||||
let s = format!("{:.*}", places, d);
|
||||
let (int, frac) = s.split_once('.').unwrap_or((&s, "00"));
|
||||
let mut out = String::new();
|
||||
for (i, c) in int.chars().enumerate() {
|
||||
@@ -172,7 +203,9 @@ mod tests {
|
||||
assert_eq!(money(dec("0")), "0.00");
|
||||
assert_eq!(money(dec("-0.00")), "0.00");
|
||||
assert_eq!(accounting(dec("-207.68")), "(207.68)");
|
||||
assert_eq!(dollars(dec("999.999")), "$1,000.00");
|
||||
assert_eq!(dollars(dec("999.999")), "$999.999");
|
||||
assert_eq!(money(dec("0.125")), "0.125");
|
||||
assert_eq!(compact(dec("-0.4")), "$0");
|
||||
assert_eq!(compact(dec("1234")), "$1.2k");
|
||||
assert_eq!(compact(dec("-25000")), "\u{2212}$25k");
|
||||
assert_eq!(compact(dec("950.4")), "$950");
|
||||
|
||||
Reference in new issue
Block a user