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
#![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())
}
}