Commodity
A library for representing commodities/currencies, and exchange rates/conversions between them in Rust. Values are backed by the rust_decimal library.
Optional Features
The following features can be enabled to provide extra functionality:
serde-support
- Enables support for serialization/de-serialization via
serde
- Enables support for serialization/de-serialization via
Example
use ;
use Decimal;
use FromStr;
// Create a commodity type from a currency's iso4317 three character code.
// The CommodityType stores information associated with that currency,
// such as the full name ("United States dollar" for this one).
let usd = from_currency_alpha3.unwrap;
// Create a commodity with a value of "2.02 USD"
let commodity1 = new;
// Create commodities using the `from_str` method
let commodity2 = from_str.unwrap;
// Create commodity using a CommodityTypeID
let nzd_code = from_str.unwrap;
let commodity3 = new;
// Add two compatible (same currency) commodities, the result has
// the same currency (in this case, "USD").
let commodity4 = commodity1.add.unwrap;
// Try to subtract two incompatible commodities
let result = commodity3.sub;
assert!;