Skip to content

fix(price): config quote_currency, dotted-ticker discovery (closes #952) - #953

Merged
robcohen merged 3 commits into
mainfrom
fix/price-config-quote-currency
Apr 30, 2026
Merged

robcohen merged 3 commits into
mainfrom
fix/price-config-quote-currency

Conversation

@robcohen

@robcohen robcohen commented Apr 30, 2026 •

Copy link
Copy Markdown
Member

Summary

Issue #952 surfaced three gaps in the price command after #951 landed metadata-driven discovery. This PR addresses two of them; the third (Yahoo returning a TRY/GBP cross-rate when EUR was requested) needs a reproducer from the user and is being followed up separately.

Changes

1. [price.mapping.X] quote_currency (was: currency) is now honored

The user wrote:

[price.mapping.AUD]
source = "ecb"
currency = "EUR"

and got USD prices. Two reasons:

  • CommodityMapping::Detailed had no quote-currency field at all. The value was dropped silently at deserialization.
  • The struct didn't enforce deny_unknown_fields, so the typo passed without warning.

Fix:

  • Pulled Detailed out of the inline untagged enum variant into its own DetailedMapping struct so it can carry #[serde(deny_unknown_fields)]. Future typos error at config load with a clear "unknown field" diagnostic.
  • Added a quote_currency: Option<String> field. Per-symbol quote currency from config now resolves with the same precedence as the ledger-side quote_currency: metadata added in [Feature]: Get Symbols from active commodities #948.

New resolve_quote_currency helper centralizes the precedence chain so the network and --source-cmd paths apply it identically. High-to-low:

  1. Discovered quote_currency: / first price: entry from a commodity directive
  2. quote_currency = "..." in [price.mapping.X] (this PR)
  3. Global --currency flag

2. Exchange-suffixed tickers like VECP.AS, BRK.B, 7203.T were silently skipped

The looks_like_ticker heuristic in discovery.rs rejected . even though beancount's commodity grammar permits it. Added . to the allowed character set; extended the existing test.

Tests

7 new test functions:

  • test_parse_commodity_mapping_with_quote_currency — config field round-trips through TOML deserialization.
  • test_parse_commodity_mapping_rejects_unknown_field — the currency = "EUR" typo errors at load, with the error message referencing the bad block.
  • 5 new resolve_quote_currency unit tests covering each level of the precedence chain (discovery wins over config, config wins over default, default fallback for empty maps, default fallback for Detailed { quote_currency: None }, Simple-mapping carries no currency).

Plus 3 new assertions in the existing ticker_heuristic_rejects_long_or_lowercase: VECP.AS, BRK.B, 7203.T.

Docs

docs/commands/price.md:

  • Config example now shows quote_currency = "EUR" on [price.mapping.AUD].
  • New "Quote currency resolution" section enumerates the three-step precedence.
  • Note that [price.mapping.X] blocks reject unknown keys with an explicit error.

Migration impact

This is a small wire-format tightening: previously-permissive configs that contained typos under [price.mapping.X] (e.g. currency instead of quote_currency) now fail loud at config load. Real-world impact is low because typos in those blocks were silently dropping the user's intent anyway, so loud failure is strictly better.

Out of scope

The user's third report (2026-04-29 price TRY 311.0 GBP returned for a USD-quoted query) is not addressed here. It looks like a Yahoo Finance source-side quirk; needs a reproducer from the user with -v --no-cache output and the relevant config block.

Verification

  • cargo test -p rustledger: 232 + 27 + 43 + 7 + 65 + 17 = 391 tests pass (was 384, +7 new test functions plus assertion updates)
  • cargo clippy --all-features --all-targets -- -D warnings: clean
  • cargo fmt --check: clean

Test plan

  • CI: Clippy, Test, Doctests, Format, Compatibility
  • Manual: rledger price AUD -b with [price.mapping.AUD] source = "ecb", quote_currency = "EUR" returns EUR-quoted price (was returning USD)
  • Manual: [price.mapping.AUD] currency = "EUR" (typo) errors at config load with a clear message
  • Manual: rledger price -f main.beancount picks up commodity VECP.AS (was silently skipped)

Closes #952

🤖 Generated with Claude Code

Issue #952 surfaced three distinct gaps in the price command after #951
landed metadata-driven discovery. This PR addresses two of them; the
third (Yahoo returning a TRY/GBP cross-rate when EUR was requested)
needs a reproducer from the user and is being followed up separately.

1. **`[price.mapping.X] quote_currency` was silently ignored.**
   The user wrote `[price.mapping.AUD] currency = "EUR"` and got USD
   prices because:
   - Our `CommodityMapping::Detailed` had no `currency`/`quote_currency`
     field at all, so the value was dropped at deserialization.
   - The struct didn't enforce `deny_unknown_fields`, so the typo passed
     silently.

   Fix is two-fold:
   - Pull `Detailed` out of the inline `untagged` enum variant into its
     own `DetailedMapping` struct so it can carry
     `#[serde(deny_unknown_fields)]`. Future typos error at config load
     with a clear "unknown field" diagnostic.
   - Add a `quote_currency: Option<String>` field. Per-symbol quote
     currency from config now resolves with the same precedence as the
     ledger-side `quote_currency:` metadata added in #948.

   New `resolve_quote_currency` helper centralizes the
   high-to-low precedence lookup (discovered metadata, then config
   mapping, then global `--currency`) so the network and `--source-cmd`
   paths apply it identically.

2. **Exchange-suffixed tickers (`VECP.AS`, `BRK.B`, `7203.T`) were
   silently skipped.** The `looks_like_ticker` heuristic in
   `discovery.rs` rejected `.` even though beancount's commodity grammar
   permits it. Added `.` to the allowed character set, and extended the
   existing test.

Tests:
- `test_parse_commodity_mapping_with_quote_currency` — config field
  round-trips through TOML deserialization.
- `test_parse_commodity_mapping_rejects_unknown_field` — the
  `currency = "EUR"` typo now errors instead of being dropped.
- 4 new `resolve_quote_currency` unit tests covering each level of the
  precedence chain.
- Existing `ticker_heuristic_rejects_long_or_lowercase` extended with
  `VECP.AS`, `BRK.B`, `7203.T`.

Docs:
- `docs/commands/price.md` config example shows
  `quote_currency = "EUR"`. New "Quote currency resolution" section
  enumerates the three-step precedence and notes that unknown keys now
  error.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Copilot AI review requested due to automatic review settings April 30, 2026 02:33
@robcohen
robcohen requested a review from a team as a code owner April 30, 2026 02:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR closes gaps in the price command’s configuration and discovery logic identified in #952, aligning behavior between config, ledger metadata discovery, and ticker heuristics.

Changes:

  • Add quote_currency support to per-commodity [price.mapping.X] config by introducing a dedicated DetailedMapping (with deny_unknown_fields to prevent silent typos like currency = ...).
  • Centralize quote-currency precedence via resolve_quote_currency and apply it to both network fetch and --source-cmd paths.
  • Expand ticker discovery heuristic to allow dotted tickers (e.g., VECP.AS, BRK.B, 7203.T) and extend tests/docs accordingly.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
docs/commands/price.md Documents quote-currency precedence and updates config examples; notes unknown-key rejection.
crates/rustledger/src/config.rs Adds DetailedMapping with quote_currency and deny_unknown_fields; updates parsing tests and adds new ones.
crates/rustledger/src/cmd/price_cmd.rs Introduces resolve_quote_currency helper; threads effective currency through cache + fetch paths; adds unit tests.
crates/rustledger/src/cmd/price/mod.rs Updates mapping resolution code/tests to new DetailedMapping shape.
crates/rustledger/src/cmd/price/discovery.rs Allows . in ticker heuristic; updates discovery mapping construction for new struct; extends tests.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/rustledger/src/cmd/price_cmd.rs Resolved
@github-actions

github-actions Bot commented Apr 30, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 30.4ms ± 0.4ms 21.7 MB
beancount 947.9ms ± 13.1ms 50.4 MB
Comparison 31.2x faster 2.3x less

vs main branch (2026-04-29)

Metric main this PR Change
Time 31.5ms 30.4ms -3.5% ✅
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

@codecov

codecov Bot commented Apr 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.97222% with 13 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger/src/cmd/price_cmd.rs 90.72% 9 Missing ⚠️
crates/rustledger/src/cmd/price/discovery.rs 70.00% 3 Missing ⚠️
crates/rustledger/src/config.rs 96.77% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

robcohen and others added 2 commits April 29, 2026 20:40
Self-review found five small improvements:

1. `test_parse_commodity_mapping_rejects_unknown_field` now asserts the
   error message references the bad block (AUD) or mentions the
   unknown-field reason, not just `is_err()`.

2. New `test_resolve_quote_currency_detailed_without_quote_currency_uses_default`
   covers the most common shape: a `[price.mapping.X]` block with
   `source = "..."` and no `quote_currency` falls through to the global
   default.

3. Code comment at the `--source-cmd` callsite explains why it passes
   `&price_config.mapping` instead of the merged `combined_mapping`
   (CLI `--mapping` only creates `Simple` entries with no quote_currency,
   so merging wouldn't add anything for currency resolution).

4. Code comment in `looks_like_ticker` documents the deliberate
   permissiveness about leading characters (`7203.T` legitimately starts
   with a digit) so a future maintainer doesn't tighten it and break
   valid tickers.

5. PR description amended to reflect actual test count (separate edit).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…opilot #953)

Copilot review on PR #953 caught a real precedence bug. With the
network path passing `&combined_mapping` to `resolve_quote_currency`,
a CLI override like `--mapping AUD:NEW-TICKER` overwrites the
config-file `Detailed` entry with `Simple` in the merged map and
silently drops the config's `quote_currency`. The user's intended
override was the ticker only, but the merge took the currency with it.

Fix: resolve quote currency against `&price_config.mapping` (the raw
config map) on both the network and `--source-cmd` paths. Source/ticker
lookup still uses the merged map further down — only currency
resolution stays config-only, matching the documented precedence
"config quote_currency wins over global default".

Updated the call-site comments in both paths to record the contract:
"use the raw config map so a CLI Simple override can't wipe out a
Detailed.quote_currency".

New regression test
`test_resolve_quote_currency_uses_raw_config_not_merged_mapping`
locks in the helper-level invariant.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@robcohen
robcohen merged commit 6a718f1 into main Apr 30, 2026
31 checks passed
@robcohen
robcohen deleted the fix/price-config-quote-currency branch April 30, 2026 02:50
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Compatibility Test Results

Metric Result
Check Compatibility 99%
BQL Compatibility 85%
Full AST Match 99%
Total Files 782
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve price configuration / doc

2 participants