A currency pair's notation is decided in both Display and serde #18

Closed
opened 2026-08-14 23:58:33 +00:00 by rosa · 0 comments
Owner

How a currency pair is written down — the priced currency, a slash, the currency
it is priced in — is decided twice.

ExchangeRate's Display (src/exchange.rs:307) builds it from a literal:

write_padded(f, &format!("{}/{} {}", self.base, self.quote, self.rate))

The serde module decides it again (src/serde.rs:474) with a Pair newtype, a
PAIR_SEPARATOR constant, a Serialize that formats
"{base}{PAIR_SEPARATOR}{quote}", and a PairVisitor that reads it back with
split_once(PAIR_SEPARATOR).

Both render the same notation and the serialized table depends on it as a map
key (src/serde.rs:541), but only one of the two names the separator. Changing
it in serde.rs leaves Display writing the old form with no compiler
complaint, and there is no test that holds the two to the same notation.

Pair is also the wrong side of the boundary. It knows what a rate's two
currencies are called and how a board writes them — exchange.rs's subject, not
serde's — and it exists in serde.rs only because that is where it was first
needed.

Move Pair into exchange.rs as pub(crate), with Display and FromStr
carrying the notation. ExchangeRate's Display then renders "{pair} {rate}",
serde's Serialize becomes collect_str(&pair), and PairVisitor becomes a
parse, dropping the hand-rolled split_once and the two
Currency::from_alphabetic_code calls. The separator is decided once, and the
feature-gated module stops owning a decision the core module makes.

How a currency pair is written down — the priced currency, a slash, the currency it is priced in — is decided twice. `ExchangeRate`'s `Display` (`src/exchange.rs:307`) builds it from a literal: ```rust write_padded(f, &format!("{}/{} {}", self.base, self.quote, self.rate)) ``` The serde module decides it again (`src/serde.rs:474`) with a `Pair` newtype, a `PAIR_SEPARATOR` constant, a `Serialize` that formats `"{base}{PAIR_SEPARATOR}{quote}"`, and a `PairVisitor` that reads it back with `split_once(PAIR_SEPARATOR)`. Both render the same notation and the serialized table depends on it as a map key (`src/serde.rs:541`), but only one of the two names the separator. Changing it in `serde.rs` leaves `Display` writing the old form with no compiler complaint, and there is no test that holds the two to the same notation. `Pair` is also the wrong side of the boundary. It knows what a rate's two currencies are called and how a board writes them — `exchange.rs`'s subject, not serde's — and it exists in `serde.rs` only because that is where it was first needed. Move `Pair` into `exchange.rs` as `pub(crate)`, with `Display` and `FromStr` carrying the notation. `ExchangeRate`'s `Display` then renders `"{pair} {rate}"`, serde's `Serialize` becomes `collect_str(&pair)`, and `PairVisitor` becomes a `parse`, dropping the hand-rolled `split_once` and the two `Currency::from_alphabetic_code` calls. The separator is decided once, and the feature-gated module stops owning a decision the core module makes.
rosa closed this issue 2026-08-15 02:17:10 +00:00
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
rosa/lucre#18
No description provided.