#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)]
#[cfg(not(feature = "std"))]
extern crate core as std;
extern crate mitochondria;
extern crate phf;
#[cfg(feature = "serde")]
extern crate serde;
#[cfg(feature = "serde")]
#[macro_use]
extern crate serde_derive;
use mitochondria::OnceCell;
mod currencies;
pub use currencies::Currency;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize))]
pub struct CurrencyInfo {
code: &'static str,
name: &'static str,
countries: &'static [&'static str],
fund: bool,
number: u16,
minor_units: Option<u8>,
}
impl CurrencyInfo {
pub fn code(&self) -> &'static str {
self.code
}
pub fn name(&self) -> &'static str {
self.name
}
pub fn countries(&self) -> &'static [&'static str] {
self.countries
}
pub fn is_fund(&self) -> bool {
self.fund
}
pub fn number(&self) -> u16 {
self.number
}
pub fn minor_units(&self) -> Option<u8> {
self.minor_units
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
pub struct Money {
amount: i64,
currency: Currency,
#[cfg_attr(feature = "serde-serialize", serde(skip))]
currency_info: OnceCell<&'static CurrencyInfo>,
}
impl Money {
pub fn new(amount: i64, currency: Currency) -> Money {
Money {
amount,
currency,
currency_info: OnceCell::default(),
}
}
pub fn amount(&self) -> i64 {
self.amount
}
pub fn currency(&self) -> Currency {
self.currency
}
pub fn currency_info(&self) -> &'static CurrencyInfo {
self.currency_info.init_once(|| self.currency.info())
}
}