Support non-ISO and custom currencies #41
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#41
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?
Problem
Currencyis a closed set: the ISO 4217 table generated into the crate bybuild.rs. There is no way to represent Bitcoin (8 minor digits), loyalty points, in-game currencies, or historical currencies from ISO 4217-3. Every mature money library (Joda-Money, JSR-354, Ruby'smoney) supports user-defined currencies, and it is among their most-used features.Recommendation: registry via
inventory, not a traitKeep
Currencya single concrete type and let downstream crates register custom currencies withinventory, behind a cargo feature.The trait design (
Money<C: Currency>) is rejected. The type parameter spreads into every public type —MoneyBag,Exchange,ExchangeRate,Parser,Format, the serde impls — and into every downstream signature that mentions money. That trades a large interface cost for a problem the registry solves inside the module.An explicit registry value (
CurrencyRegistrypassed to each parser) is also rejected. Serde deserialization has no way to receive such a value, soDeserializeforMoneycould never see custom currencies. A process-wide collection is what letsFromStr,TryFrom, and serde keep working unchanged, andinventoryprovides one without mutable global state: submissions are collected during startup, before any lookup runs.Design sketch
Currencyalready suits this shape: its fields are inline andCopy, with&'static strname and symbol, so a custom currency is just anotherCurrencyvalue built from statics. Equality stays derived field equality.Currency::customis aconst fn, so definitions work inconstitems and ininventory::submit!.FromStr,TryFrom<&str>,Parser, andDeserializeconsult the generated ISOmatchfirst, then aOnceLockindex built once frominventory::iter.inventorydependency and the lookup layer sit behind acustom-currenciesfeature. The default build keeps today's dependency footprint.Collision policy
Currencystill works for arithmetic and formatting; only text lookup requires submission.Breaking changes required
The current code types are ISO-shaped, and custom currencies do not fit them:
IsoAlphabeticCodeholds exactly 3 ASCII uppercase letters.USDT,DOGE, and points codes need more. Add a widenedCurrencyCodeholding 1 to 8 ASCII uppercase letters, stored inline soCurrencystaysCopy. It is returned by a newCurrency::code; ISO codes stay 3 letters within it.alphabetic_codecan stay for ISO values or be deprecated.numeric_codereturnsIsoNumericCodeunconditionally. Custom currencies have none, so the return type becomesOption<IsoNumericCode>.Both are semver-major. They can land in the same release as the registry.
Accepted trade-offs
&'static strfields already assume static definitions.inventoryregisters during life-before-main. On its supported platforms (Linux, macOS, Windows, FreeBSD, Android, iOS, WebAssembly) this is transparent. On an unsupported platform the collection is silently empty: custom codes fail to parse or deserialize there, but nothing corrupts. Document this in the feature's docs.