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.9.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 needs exchange rates, which are live data rather than a standard, so they are out of scope here. Iterating a bag yields each currency's balance in ISO alphabetic order, which is enough to apply rates of your own.
Features
serde
Off by default. Turning it on gives Money, MoneyBag, Currency,
IsoAlphabeticCode, IsoNumericCode, and RoundingMode a Serialize and a
Deserialize impl.
[]
= { = "0.9.0", = ["serde"] }
Amounts travel as text, which carries a fraction exactly and keeps the scale the amount 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.
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