Skip to content

feat(price): implement custom price sources with pluggable registry - #624

Merged
robcohen merged 4 commits into
mainfrom
feature/custom-price-sources
Mar 31, 2026
Merged

robcohen merged 4 commits into
mainfrom
feature/custom-price-sources

Conversation

@robcohen

Copy link
Copy Markdown
Member

Summary

Implements #571 - a pluggable price source system with 11 native Rust sources and external command support.

New price sources (no API key required):

  • yahoo - stocks, ETFs, crypto, forex
  • coinbase - cryptocurrency spot prices
  • coincap - cryptocurrency prices
  • ecb - European Central Bank exchange rates
  • ratesapi - currency exchange rates
  • tsp - Thrift Savings Plan fund prices
  • eastmoneyfund - Chinese mutual fund prices

New price sources (API key required):

  • alphavantage - stocks, forex, crypto
  • oanda - forex rates
  • coinmarketcap - cryptocurrency prices
  • quandl - Nasdaq Data Link financial datasets

Additional features:

  • External command source for custom price fetchers
  • Configurable commodity mappings with fallback chains
  • Source-specific ticker transformations
  • CLI options: --source, --source-cmd, --list-sources
  • Ticker validation to prevent command injection

Test plan

  • All 185 unit tests pass
  • Clippy clean
  • Pre-push hooks pass (rustfmt, typos, gitleaks, clippy, cargo-test, cargo-doc)
  • Manual testing: rledger price AAPL --source yahoo
  • Manual testing: rledger price BTC --source coinbase
  • Manual testing: rledger price --list-sources
  • Manual testing: rledger price AAPL --source-cmd "echo 150.00 USD"

Closes #571

🤖 Generated with Claude Code

robcohen and others added 2 commits March 31, 2026 11:20
Adds 11 native Rust price sources and external command support for
fetching commodity prices. This replaces the previous Yahoo-only
implementation with a flexible, configurable system.

New price sources (no API key required):
- yahoo: stocks, ETFs, crypto, forex
- coinbase: cryptocurrency spot prices
- coincap: cryptocurrency prices
- ecb: European Central Bank exchange rates
- ratesapi: currency exchange rates
- tsp: Thrift Savings Plan fund prices
- eastmoneyfund: Chinese mutual fund prices

New price sources (API key required):
- alphavantage: stocks, forex, crypto
- oanda: forex rates
- coinmarketcap: cryptocurrency prices
- quandl: Nasdaq Data Link financial datasets

Additional features:
- External command source for custom price fetchers
- Configurable commodity mappings with fallback chains
- Source-specific ticker transformations
- CLI options: --source, --source-cmd, --list-sources

Closes #571

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Security:
- Add ticker validation to prevent command injection in external sources
- Reject shell metacharacters, path separators, and flag-like tickers

Improvements:
- Register all API key sources unconditionally (clearer error on use)
- Add UTF-8 safe string slicing in eastmoneyfund date parsing
- Document timeout field limitations with ureq 3.x
- Add TODO for unimplemented cache_ttl feature
- Add tests for ticker validation

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Copilot AI review requested due to automatic review settings March 31, 2026 17:00
@robcohen
robcohen requested a review from a team as a code owner March 31, 2026 17:00
@github-actions

github-actions Bot commented Mar 31, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 43.7ms ± 4.3ms 25.3 MB
beancount 892.1ms ± 4.9ms 50.2 MB
Comparison 20.4x faster 2.0x less

vs main branch (2026-03-31)

Metric main this PR Change
Time 44.5ms 43.7ms -1.8% ✅
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

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

Implements a new pluggable price-fetching system for rledger price, adding a configurable registry of built-in HTTP sources plus external command execution, and wiring the new price configuration through CLI/config loading.

Changes:

  • Added [price] configuration (default source, timeout, source definitions, commodity mappings) with merge/effective-default helpers.
  • Introduced cmd::price module with PriceSourceRegistry, PriceRequest/Response, built-in sources, and an external-command source.
  • Updated price CLI to support --source, --source-cmd, and --list-sources, and to use the registry/mappings.

Reviewed changes

Copilot reviewed 17 out of 18 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
crates/rustledger/src/config.rs Adds PriceConfig + serde models for sources/mappings and merge logic.
crates/rustledger/src/cmd/price/mod.rs New price module (registry, request/response, mapping resolution).
crates/rustledger/src/cmd/price/external.rs External command-backed price source with output parsing and ticker validation.
crates/rustledger/src/cmd/price/sources/mod.rs Defines PriceSource trait + exports built-in sources.
crates/rustledger/src/cmd/price/sources/yahoo.rs Yahoo Finance implementation.
crates/rustledger/src/cmd/price/sources/coinbase.rs Coinbase spot price implementation.
crates/rustledger/src/cmd/price/sources/coincap.rs CoinCap implementation.
crates/rustledger/src/cmd/price/sources/ecb.rs ECB exchange-rate implementation.
crates/rustledger/src/cmd/price/sources/ratesapi.rs exchangerate.host-based FX implementation.
crates/rustledger/src/cmd/price/sources/tsp.rs TSP fund price implementation.
crates/rustledger/src/cmd/price/sources/eastmoneyfund.rs EastMoney fund NAV implementation.
crates/rustledger/src/cmd/price/sources/alphavantage.rs AlphaVantage implementation (API key).
crates/rustledger/src/cmd/price/sources/oanda.rs OANDA implementation (API key).
crates/rustledger/src/cmd/price/sources/coinmarketcap.rs CoinMarketCap implementation (API key).
crates/rustledger/src/cmd/price/sources/quandl.rs Nasdaq Data Link/Quandl implementation (API key).
crates/rustledger/src/cmd/price_cmd.rs Reworks CLI command to use registry + new flags.
crates/rustledger/src/cmd/mod.rs Registers cmd::price module.
crates/rustledger/src/bin/rledger.rs Wires config.price into price_cmd::run.

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

Comment thread crates/rustledger/src/config.rs Resolved
Comment thread crates/rustledger/src/cmd/price_cmd.rs Resolved
Comment thread crates/rustledger/src/cmd/price/mod.rs Resolved
Comment thread crates/rustledger/src/cmd/price_cmd.rs Resolved
Comment thread crates/rustledger/src/cmd/price/mod.rs Resolved
@codecov

codecov Bot commented Mar 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions

github-actions Bot commented Mar 31, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 98%
Total Files 784
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

Fixes several issues identified in PR #624 review:

1. Yahoo f64 precision (issue #3): Use pattern matching with
   serde_json::Value::Number to preserve decimal precision instead of
   converting through f64.

2. ECB cross-rate handling (issue #2): Rewrite fetch_price to correctly
   handle EUR→X, X→EUR, and X→Y cross-rate conversions.

3. --source-cmd early return (issue #4): Move external command handling
   after symbol discovery so -f flag works with --source-cmd.

4. Hardcoded timeout (issue #7): Use config timeout for --source-cmd
   instead of hardcoded 30 seconds.

5. External source naming (issue #8): Derive source name from command
   binary name instead of always returning "external". Add with_name()
   constructor for configured sources.

6. Unknown sources warning (issue #5): Track and report unknown sources
   in fallback chains when price fetching fails.

7. Timeout limitation (issue #6): Document that timeout is stored but
   not enforced due to std::process::Command limitations.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@robcohen
robcohen added this pull request to the merge queue Mar 31, 2026
Merged via the queue into main with commit af934c7 Mar 31, 2026
31 of 33 checks passed
@robcohen
robcohen deleted the feature/custom-price-sources branch March 31, 2026 17:31
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.

[Compat]: Custom price downloaders (was: bean-price differences)

2 participants