Exchange::rate answers par from a rule the rest of the table does not follow #17

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

Exchange::rate (src/exchange.rs:495) answers par for a currency against
itself when the table holds no such entry:

self.rates
    .get(&(base, quote))
    .map(|&rate| ExchangeRate { base, quote, rate })
    .or_else(|| (base == quote).then(|| ExchangeRate::identity(base)))

The rule is sound — restating an amount in the currency it already carries
should never want for a rate — but it lives in a lookup, where it contradicts
every other view of the table. rate(c, c) answers Some while iter does not
yield that pair, len does not count it, and remove_rate(c, c) answers
None.

The cost is visible in the docs, which have to describe five members in terms of
the sixth:

  • set_rate (src/exchange.rs:385) — "which is not always what rate would
    have answered: a pair quoted for the first time hands back nothing, a currency
    against itself included."
  • remove_rate (src/exchange.rs:421) — "withdrawing it leaves rate
    answering par all the same."
  • is_empty (src/exchange.rs:504) — "Par ... is a rule rate applies rather
    than an entry, so an empty table still answers that pair."
  • len (src/exchange.rs:533) — "Par counts only where it was quoted
    outright."
  • iter (src/exchange.rs:564) — "Par ... is among them only where it was
    quoted outright, since rate answers that pair from the rule rather than from
    the table."

Six tests exist to pin down the interaction rather than any behavior worth
having on its own: exchange_answers_par_for_a_currency_against_itself_test,
exchange_takes_par_in_as_it_takes_any_pair_test,
exchange_counts_only_the_par_it_was_given_test,
exchange_lets_a_quoted_pair_outrank_par_test,
exchange_answers_par_after_withdrawing_it_test, and
exchange_withdraws_nothing_from_an_unquoted_pair_test.

The rule belongs to the operation that would otherwise fail, and that operation
does not exist. Every caller does lookup-then-convert by hand — the README
(README.md:119) and both Exchange serde doctests (src/serde.rs:567) all
spell out .rate(a, b).ok_or("...")? before converting. Exchange hands out its
contents and leaves the work to the caller.

Give the table the operation:

pub fn convert(&self, money: Money, into: Currency) -> Result<Money, ExchangeError> {
    if money.currency() == into {
        return Ok(money);
    }

    self.rate(money.currency(), into)
        .ok_or(ExchangeError::Unquoted { base: money.currency(), quote: into })?
        .convert(money)
        .map_err(Into::into)
}

Converting into the currency an amount already carries then needs no rate at
all, and rate collapses to a plain lookup with no or_else. That removes the
five caveats, four of the six par tests, and the ok_or from every call site.
It also serves pricing a MoneyBag against a table, where the target currency's
own balance should not require an entry.

ExchangeRate::convert reports MoneyError, which has no variant for a pair
the table does not quote, so the new operation wants an error type of its own —
an unquoted pair plus a passthrough for the overflow the multiplication can
still produce, in the shape CrossRateError (src/exchange.rs:823) already
uses.

This breaks rate: an unquoted rate(c, c) would answer None where it now
answers par. The cost of that break is at its lowest now, while 0.11 is already
carrying several.

`Exchange::rate` (`src/exchange.rs:495`) answers par for a currency against itself when the table holds no such entry: ```rust self.rates .get(&(base, quote)) .map(|&rate| ExchangeRate { base, quote, rate }) .or_else(|| (base == quote).then(|| ExchangeRate::identity(base))) ``` The rule is sound — restating an amount in the currency it already carries should never want for a rate — but it lives in a lookup, where it contradicts every other view of the table. `rate(c, c)` answers `Some` while `iter` does not yield that pair, `len` does not count it, and `remove_rate(c, c)` answers `None`. The cost is visible in the docs, which have to describe five members in terms of the sixth: - `set_rate` (`src/exchange.rs:385`) — "which is not always what `rate` would have answered: a pair quoted for the first time hands back nothing, a currency against itself included." - `remove_rate` (`src/exchange.rs:421`) — "withdrawing it leaves `rate` answering par all the same." - `is_empty` (`src/exchange.rs:504`) — "Par ... is a rule `rate` applies rather than an entry, so an empty table still answers that pair." - `len` (`src/exchange.rs:533`) — "Par counts only where it was quoted outright." - `iter` (`src/exchange.rs:564`) — "Par ... is among them only where it was quoted outright, since `rate` answers that pair from the rule rather than from the table." Six tests exist to pin down the interaction rather than any behavior worth having on its own: `exchange_answers_par_for_a_currency_against_itself_test`, `exchange_takes_par_in_as_it_takes_any_pair_test`, `exchange_counts_only_the_par_it_was_given_test`, `exchange_lets_a_quoted_pair_outrank_par_test`, `exchange_answers_par_after_withdrawing_it_test`, and `exchange_withdraws_nothing_from_an_unquoted_pair_test`. The rule belongs to the operation that would otherwise fail, and that operation does not exist. Every caller does lookup-then-convert by hand — the README (`README.md:119`) and both `Exchange` serde doctests (`src/serde.rs:567`) all spell out `.rate(a, b).ok_or("...")?` before converting. `Exchange` hands out its contents and leaves the work to the caller. Give the table the operation: ```rust pub fn convert(&self, money: Money, into: Currency) -> Result<Money, ExchangeError> { if money.currency() == into { return Ok(money); } self.rate(money.currency(), into) .ok_or(ExchangeError::Unquoted { base: money.currency(), quote: into })? .convert(money) .map_err(Into::into) } ``` Converting into the currency an amount already carries then needs no rate at all, and `rate` collapses to a plain lookup with no `or_else`. That removes the five caveats, four of the six par tests, and the `ok_or` from every call site. It also serves pricing a `MoneyBag` against a table, where the target currency's own balance should not require an entry. `ExchangeRate::convert` reports `MoneyError`, which has no variant for a pair the table does not quote, so the new operation wants an error type of its own — an unquoted pair plus a passthrough for the overflow the multiplication can still produce, in the shape `CrossRateError` (`src/exchange.rs:823`) already uses. This breaks `rate`: an unquoted `rate(c, c)` would answer `None` where it now answers par. The cost of that break is at its lowest now, while 0.11 is already carrying several.
rosa closed this issue 2026-08-15 00:51:07 +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#17
No description provided.