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
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
//! A library that handles calculating, rounding, displaying, and parsing units of money according
//! to ISO 4217 standards. The main item exported by the library is `Money`.
//!
//! # Use
//!
//! ```edition2018
//! use rusty_money::money;
//! use rusty_money::Money;
//!
//! // The easiest way to create Money objects is by using the money! macro
//! // which accepts amounts strings or integers and currencies as strings:
//!
//! money!("-200.00", "USD") == money!(-200, "USD"); // true
//!
//! // Money objects can be initialized in a few other convenient ways:
//!
//! use rusty_money::Currency;
//! use rusty_money::Iso::*;
//!
//! Money::new(200000, Currency::get(USD));         // amount = 2000 USD
//! Money::from_major(2000, Currency::get(USD));    // amount = 2000 USD
//! Money::from_minor(200000, Currency::get(USD));  // amount = 2000 USD
//! Money::from_str("2,000.00", "USD").unwrap();    // amount = 2000 USD
//!
//! // Money objects support arithmetic operations:
//!
//! money!(100, "USD") + money!(100, "USD"); // amount = 200 USD
//! money!(100, "USD") - money!(100, "USD"); // amount = 0 USD
//! money!(1, "USD") * 3;                    // amount = 3 USD
//! money!(3, "USD") / 3;                    // amount = 0.333333333... USD
//!
//! // Money objects can be compared:
//!
//! let hundred = money!(100, "USD");
//! let thousand = money!(1000, "USD");
//! println!("{}", thousand > hundred);     // false
//! println!("{}", thousand.is_positive()); // true
//!
//! // Money objects format themselves when printed:
//!
//! let usd = money!("-2000.009", "USD");
//! let eur = money!("-2000.009", "EUR");
//! println!("{}", usd); // -$2,000.01
//! println!("{}", eur); // -€2.000,01;
//!
//!
//! // Money objects can be exchange from one currency to another by setting up an ExchangeRate:
//!
//! use rusty_money::Exchange;
//! use rusty_money::ExchangeRate;
//! use rust_decimal_macros::*;
//!
//! let rate = ExchangeRate::new(Currency::get(USD), Currency::get(EUR), dec!(1.1)).unwrap();
//! rate.convert(money!(1000, "USD")); // 1,100 EUR
//!
//! // ExchangeRate objects can be stored and retrieved from a central Exchange:
//!
//! let mut exchange = Exchange::new();
//! exchange.add_or_update_rate(&rate);
//! exchange.get_rate(Currency::get(USD), Currency::get(EUR));
//! ```
//! ### Money
//!
//! Money represents financial amounts through a Decimal (owned) and a Currency (refernce). Operations on Money
//! objects always create new instances of Money.
//!
//! ### Currency
//!
//! Currency represents an ISO-4217 currency, and stores metadata like its numeric code, full name and symbol.
//! Operations on Currencies pass around references, since they are unchanging. Only 117 currencies are supported,
//! though the next release will include all ISO-4217 currencies.
//!
//! ### Precision and Rounding
//!
//! The [Decimal](https://github.com/paupino/rust-decimal) used in Money is a 128 bit fixed precision decimal number,
//! and can represent values as large as 2<sup>96</sup> / 10<sup>28</sup>. Calculations applied on Money objects do
//! not round until this limit. You can use `format!()` to display the currency in its native precision, though the
//! Decimal will remain unaffected. `.round()` will create a new instance with permanently reduced precision.
//!
//! Money supports three types of rounding modes: [Half Up](https://en.wikipedia.org/wiki/Rounding#Round_half_up),
//! [Half Down](https://en.wikipedia.org/wiki/Rounding#Round_half_down) and
//! [Half Even](https://en.wikipedia.org/wiki/Rounding#Round_half_even), which is the default.
//!
//! ### Formatting
//!
//! `Money.format!()` converts a currency into a string and follows local conventions for formatting and displaying
//! amounts. If you need to format output in a more customized way, `Formatter` accepts a more detailed set of
//! parameters that lets you customize precisely how currencies are displayed as strings.

mod currency;
mod error;
mod exchange;
mod format;
mod locale;
mod money;

pub use currency::*;
pub use error::MoneyError;
pub use exchange::*;
pub use format::*;
pub use locale::*;
pub use money::*;

#[macro_use]
extern crate lazy_static;