Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
lucre
An ergonomic Rust library for handling money.
Represent money in your applications without fussing over generics or lifetimes, while still being safe and fast. ISO 4217 currency definitions are built in.
Install
Add lucre to your Cargo.toml.
[]
= "0.10.0"
Usage
The Money struct is the primary interface, with the Currency struct supporting it.
Currency contains constants for all current ISO 4217 currencies.
use ;
Several currencies at once
Money refuses to mix currencies — + panics and comparisons answer None.
When amounts in different currencies have to travel together, put them in a
MoneyBag, which keeps each currency on its own books.
use ;
let mut wallet = new;
wallet += from_major;
wallet += from_major;
assert_eq!;
// A currency the wallet has never held answers zero, not nothing
assert_eq!;
// Both sums are available over the same iterator, so the type asked for is
// the decision: `Option<Money>` insists the currencies match, a bag keeps
// them apart
let refunds = ;
assert_eq!;
assert_eq!;
// Once the currencies disagree, only the bag still answers
let mixed = ;
assert_eq!;
Converting between currencies
Exchange rates are live data rather than a standard, so lucre quotes none of
its own. State a rate you already have and it handles the arithmetic; an
Exchange keeps a set of them to look up by currency pair.
use Error;
use ;
use dec;
Rates are held in one direction at a time, so quoting dollars against euros
says nothing about euros against dollars. Iterating a MoneyBag yields each
currency's balance in ISO alphabetic order, which is enough to price a whole
bag against a rate apiece.
Features
serde
Off by default. Turning it on gives Money, MoneyBag, ExchangeRate,
Exchange, Currency, IsoAlphabeticCode, IsoNumericCode, and
RoundingMode a Serialize and a Deserialize impl.
[]
= { = "0.10.0", = ["serde"] }
Amounts and rates travel as text, which carries a fraction exactly and keeps the scale the figure was built with. Numbers are read as well, floats included, but only text makes the round trip untouched.
A bag is a balance per currency, keyed by ISO alphabetic code. Reading one totals whatever the document says, rather than insisting it already be in the shape a bag would have written: a zero balance leaves no currency behind, and a currency named twice is summed.
A quote states the pair it spans and the multiplier between them, and a rate of
zero or less is refused on the way in, as ExchangeRate::new refuses it.
An Exchange is a rate per pair, keyed the way a rate board names one. Each
direction is an entry of its own, and a pair named twice keeps the rate given
last.
The smaller types are single values rather than objects:
| Type | Shape | Accepts |
|---|---|---|
Currency |
"USD" |
the three-letter code, unassigned codes refused |
IsoAlphabeticCode |
"ZZZ" |
three capitals, assigned or not |
IsoNumericCode |
840 |
an integer of at most three digits |
RoundingMode |
"half-up" |
or "half-down", or "half-even" |
JSON, TOML, YAML and their kin work. Formats that carry no type tags, such as bincode and postcard, do not.
Maintainer
This project is maintained by Rosa Richter. For ways to contact her, see her contact page.
Contributing
Questions and contributions are absolutely welcome! Please create an issue for bugs, feature requests, or questions.
License
BSD-2-Clause-Patent © Rosa Richter