Skip to content

fix(extract): validate currency_column cells and --currency for every importer - #2531

Merged
robcohen merged 6 commits into
mainfrom
fix/extract-validate-currencies
Oct 9, 2026
Merged

robcohen merged 6 commits into
mainfrom
fix/extract-validate-currencies

Conversation

@robcohen

@robcohen robcohen commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

What changed

#2516: currency_column cells are validated. Each non-blank cell goes through rustledger_parser::is_valid_currency, the lexer's own commodity rule. rustledger-importer now depends on rustledger-parser; there is no cycle, and it is a workspace crate already in the lock. A cell that is not a commodity is now a row error naming the row, the column and the value. The other rows still import:

warning: Row 2: currency_column "Currency" is "€", which is not a valid commodity (commodities are upper-case, like `USD` or `EUR`)

Before, the value went into the posting as is and failed much later as canonical formatter failed to re-parse, naming nothing.

Decision: a lower- or mixed-case cell (usd, Eur) is upper-cased, not refused. Only the ASCII case changes, and the value is validated after that change: full-width usd (not ASCII), U SD and €, US$, 1USD are refused. Surrounding whitespace is trimmed first (usd imports as USD). The message is worded exactly like #2498's configured-currency error. The reasoning:

  • the cell comes from the bank's file, which the user cannot fix, whereas a configured currency = "usd" is the user's own and fixed once at its source (that one stays refused, as fix(extract)!: take a CSV entry's missing currency from its open directive #2498 made it);
  • an ISO 4217 code means the same in either case, so usd can only mean USD, and upper-casing cannot produce a wrong commodity;
  • refusing would make every row of such an export a row error, leaving no way to import it short of preprocessing.

This is documented at CsvImporter::cell_currency, in docs/commands/extract.md (currency_column), and in the CHANGELOG.

#2517: --currency is checked once, at the start of the run, for every importer type. The CSV path checked it, but OFX and WASM did not. It is checked even when a --ledger profile will outrank it, since an invalid flag is a typo either way. An OFX/WASM entry's currency is now checked the same way, naming the entry. The check is one shared check_currency(value, source), used by resolve_entry_currency (CSV) and the new minimal_currency (OFX/WASM), so both paths refuse the same values with the same message.

How tested

  • a_currency_cell_that_is_not_a_commodity_is_a_row_error: €, US$ and 1USD are each a row error naming row, column and value, while the valid row imports. By index, the column is named by its number.
  • a_lowercase_currency_cell_is_uppercased: usd and Eur come out as USD and EUR.
  • an_invalid_currency_is_refused_for_every_importer_type: usd, € and "" passed as --currency are refused with --currency is … on an OFX file and on a WASM-dispatched file (a WAT importer), and an OFX entry's currency = "usd" is refused naming the entry.
  • Shown to fail without the fix: with cell_currency passing cells through, both importer tests fail. With the start-of-run check and the entry check removed, the CLI test fails.
  • Existing currency tests ([Bug]: Issues with CSV importer #2464/fix(extract)!: take a CSV entry's missing currency from its open directive #2498) pass unchanged. cargo test -p rustledger-importer -p rustledger --all-features, workspace clippy (-D warnings) and rustdoc (-D warnings) are clean.

Closes #2516
Closes #2517

🤖 Generated with Claude Code

https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr

… importer

A currency_column cell that is not a commodity is now a row error naming
the row, the column and the value, instead of a posting that fails to
re-parse later; a lower- or mixed-case ISO code in a cell is upper-cased.
--currency is checked once at the start of the run for every importer
type, and an OFX/WASM entry's currency like a CSV entry's, through one
shared check_currency.

Closes #2516
Closes #2517

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen
robcohen requested a review from a team as a code owner October 8, 2026 18:28
@codecov

codecov Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.45946% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-importer/src/csv_importer.rs 98.86% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Shape rustledger beancount Speedup RSS (rledger / beancount)
simple 44.5ms ± 1.4ms 796.5ms ± 3.4ms 17.9x 51.1 MB / 50.2 MB
investment 68.2ms ± 1.2ms 2956.0ms ± 10.5ms 43.4x 57.3 MB / 58.1 MB
fifo 64.2ms ± 0.4ms 15325.6ms ± 88.7ms 238.5x 59.2 MB / 58.0 MB

vs base, measured in this run

Shape base this PR Change RSS base → PR
simple 44.6ms ± 1.2ms 44.5ms ± 1.4ms -0.0% ✅ within noise 51.0 MB → 51.1 MB (+0.2%)
investment 68.5ms ± 1.3ms 68.2ms ± 1.2ms -0.4% ✅ within noise 57.2 MB → 57.3 MB (+0.0%)
fifo 64.5ms ± 0.5ms 64.2ms ± 0.4ms -0.4% ✅ within noise 55.1 MB → 59.2 MB (+7.4%)

A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: simple 0.8ms, investment 0.8ms, fifo 0.3ms), and is at least 5%.
10,000 transactions per shape · 20 runs, 3 warmup · hyperfine

What do these numbers mean?
  • simple — Two-posting transfers, one currency. The historical workload — keep as the floor, not as the benchmark.
  • investment — Cost specs, per-unit prices and lot reductions. Exercises booking and inventory, which simple leaves at 0.3%.
  • fifo — Bare {} reductions against a FIFO account — how FIFO sells are normally written. The other four shapes reach the ordered-selection path twice in 2k transactions between them, so it was effectively unprofiled until it turned out to be quadratic (plan_ordered scans every lot per reduction: FIFO ledgers are quadratic (8.8s at 20k transactions) #2083).
  • vs base: the PR and the base-branch commit it is built on, built and measured in the same job
  • RSS: peak resident set size, a single sample — reported without a verdict because there is no spread to test it against
  • A shape with no beancount column is one bean-check reports errors on; it is timed for rledger only

@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100% (raw 93%, 179 masked)
Full AST Match 100%
Total Files 819
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
    The headline is the EFFECTIVE rate, after pairs where beanquery
    itself is wrong are masked. raw is the same run with nothing
    masked. A widening gap means the registry grew, not that rledger
    changed.
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

…ency one

"currency_column X is V, which is not a valid commodity (commodities are
upper-case, like `USD` or `EUR`)", matching #2498. Pins that the value is
validated after upper-casing (full-width letters and an inner space stay
refused) and that surrounding whitespace is trimmed first.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
@robcohen

robcohen commented Oct 8, 2026 •

Copy link
Copy Markdown
Member Author

Review (adversarial pass)

  • Validated after upper-casing: confirmed and pinned. to_ascii_uppercase leaves non-ASCII alone, so full-width usd stays invalid and is refused. U SD (inner space) is refused. usd is trimmed (the cell was already trimmed) and imports as USD. Fail-without-fix: a variant that upper-cases (Unicode to_uppercase) and accepts without re-validating fails the new assertions.
  • Message style: fb151ec rewords the row error to match fix(extract)!: take a CSV entry's missing currency from its open directive #2498's exactly: currency_column "Currency" is "€", which is not a valid commodity (commodities are upper-case, like USDorEUR). The row prefix (Row N: ) comes from the importer, as for every row error.
  • Minor, pre-existing (fix(extract)!: take a CSV entry's missing currency from its open directive #2498): an entry's error says "of importers.toml" even when the file was given as --config other.toml.

Cross-PR integration (applies to #2524, #2526, #2527, #2531, #2532, #2533)

All six PRs were merged onto origin/main 25fc6be in two orders: 2524→2526→2527→2531→2532→2533, and the reverse.

@robcohen
robcohen merged commit 9a6fd19 into main Oct 9, 2026
53 of 54 checks passed
@robcohen
robcohen deleted the fix/extract-validate-currencies branch October 9, 2026 03:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant