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
/*!
This crate provides a library for dealing with currency as specified by ISO-4217. It currently only
provides a native representation of the data from [SNV's][snv] [current currency & funds code
list][list-one].

[snv]: https://www.currency-iso.org/en/home.html
[list-one]: https://www.currency-iso.org/en/home/tables/table-a1.html
*/

#![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;

/// A set of information returned from the [`Currency::info`][info] method.
///
/// [info]: enum.Currency.html#method.info
#[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 {
    /// Returns the 3 letter code of the currency.
    pub fn code(&self) -> &'static str {
        self.code
    }
    /// Returns the name of the currency.
    pub fn name(&self) -> &'static str {
        self.name
    }
    /// Returns the countries that use the currency.
    pub fn countries(&self) -> &'static [&'static str] {
        self.countries
    }
    /// Returns `true` if the currency is a fund code.
    pub fn is_fund(&self) -> bool {
        self.fund
    }
    /// Returns the 3 digit code of the currency.
    pub fn number(&self) -> u16 {
        self.number
    }
    /// Returns the decimal places of the currency.
    pub fn minor_units(&self) -> Option<u8> {
        self.minor_units
    }
}

/// A bundle of an amount of money and the currency it's in.
#[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 {
    /// Creates a new `Money` of the given amount and currency.
    pub fn new(amount: i64, currency: Currency) -> Money {
        Money {
            amount,
            currency,
            currency_info: OnceCell::default(),
        }
    }

    /// Returns the contained amount of money.
    pub fn amount(&self) -> i64 {
        self.amount
    }
    /// Returns the type of currency.
    pub fn currency(&self) -> Currency {
        self.currency
    }
    /// Returns information about this money's currency.
    ///
    /// Caches the reference to make future accesses quicker.
    pub fn currency_info(&self) -> &'static CurrencyInfo {
        self.currency_info.init_once(|| self.currency.info())
    }
}