Exchange::rate answers par from a rule the rest of the table does not follow #17
Labels
No labels
kind
bug
kind
enhancement
kind
refactor
workflow
needs-info
workflow
needs-triage
workflow
ready-for-agent
workflow
ready-for-human
workflow
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
rosa/lucre#17
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Exchange::rate(src/exchange.rs:495) answers par for a currency againstitself when the table holds no such entry:
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)answersSomewhileiterdoes notyield that pair,
lendoes not count it, andremove_rate(c, c)answersNone.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 whatratewouldhave answered: a pair quoted for the first time hands back nothing, a currency
against itself included."
remove_rate(src/exchange.rs:421) — "withdrawing it leavesrateanswering par all the same."
is_empty(src/exchange.rs:504) — "Par ... is a rulerateapplies ratherthan an entry, so an empty table still answers that pair."
len(src/exchange.rs:533) — "Par counts only where it was quotedoutright."
iter(src/exchange.rs:564) — "Par ... is among them only where it wasquoted outright, since
rateanswers that pair from the rule rather than fromthe 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, andexchange_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 bothExchangeserde doctests (src/serde.rs:567) allspell out
.rate(a, b).ok_or("...")?before converting.Exchangehands out itscontents and leaves the work to the caller.
Give the table the operation:
Converting into the currency an amount already carries then needs no rate at
all, and
ratecollapses to a plain lookup with noor_else. That removes thefive caveats, four of the six par tests, and the
ok_orfrom every call site.It also serves pricing a
MoneyBagagainst a table, where the target currency'sown balance should not require an entry.
ExchangeRate::convertreportsMoneyError, which has no variant for a pairthe 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) alreadyuses.
This breaks
rate: an unquotedrate(c, c)would answerNonewhere it nowanswers par. The cost of that break is at its lowest now, while 0.11 is already
carrying several.
exchange.rsholds two subjects and is past a thousand lines #21