1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
//! A library with primatives representing commodities/money. //! //! # Optional Features //! //! The commodity package has the following optional cargo features: //! //! + `serde-support` //! + Disabled by default //! + Enables support for serialization/de-serialization via `serde` //! //! # Usage //! //! This library revolves around the [Commodity](Commodity) struct, //! which stores a value using //! [rust_decimal::Decimal](rust_decimal::Decimal), and a //! [CurrencyCode](CurrencyCode) which denotes the type of commodity. //! Commodities with different currencies cannot interact with //! mathematical operations such as `add`, `sub`, etc, this is checked //! at runtime. //! //! [CurrencyCode](CurrencyCode) designed to be used directly when //! when working with commodities, it is backed by a small fixed size //! array which supports the [Copy](std::marker::Copy) trait, //! hopefully making it easier fast, lock-free concurrent code that //! deals with commodities. //! //! [Currency](Currency) is designed to store useful user-facing //! information about the currency being referenced by the //! [CurrencyCode](CurrencyCode), such as its full name/description. //! //! ``` //! use commodity::{Commodity, Currency, CurrencyCode}; //! use rust_decimal::Decimal; //! use std::str::FromStr; //! //! // Create a currency from its iso4317 three character code. //! // The currency stores information associated with the currency, //! // such as the full name ("United States dollar" for this one). //! let usd = Currency::from_alpha3("USD").unwrap(); //! //! // Create a commodity with a value of "2.02 USD" //! let commodity1 = Commodity::new(Decimal::from_str("2.02").unwrap(), &usd); //! //! // Create commodities using the `from_str` method //! let commodity2 = Commodity::from_str("24.00 USD").unwrap(); //! //! // Create commodity using a CurrencyCode //! let nzd_code = CurrencyCode::from_str("NZD").unwrap(); //! let commodity3 = Commodity::new(Decimal::from_str("24.00").unwrap(), nzd_code); //! //! // Add two compatible (same currency) commodities, the result has //! // the same currency (in this case, "USD"). //! let commodity4 = commodity1.add(&commodity2).unwrap(); //! //! // Try to subtract two incompatible commodities //! let result = commodity3.sub(&commodity2); //! assert!(result.is_err()); //! ``` extern crate arrayvec; extern crate chrono; extern crate iso4217; extern crate rust_decimal; #[cfg(feature = "serde-support")] extern crate serde; #[cfg(test)] #[cfg(feature = "serde-support")] extern crate serde_json; mod commodity; pub mod exchange_rate; pub use crate::commodity::*;