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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#![deny(
missing_docs,
trivial_casts,
trivial_numeric_casts,
unstable_features,
unused_import_braces,
unused_qualifications
)]
#[cfg(feature = "parsing")]
extern crate regex;
#[cfg(feature = "serialization")]
extern crate serde;
#[cfg(feature = "serialization")]
extern crate serde_json;
#[cfg(feature = "serialization")]
#[macro_use]
extern crate serde_derive;
use std::default::Default;
pub type Symbol = char;
pub mod display;
pub mod math;
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd, Eq)]
#[cfg_attr(feature = "serialization", derive(Serialize))]
pub struct Currency {
pub symbol: Option<Symbol>,
pub value: i64,
}
impl Currency {
#[inline]
pub fn new() -> Currency {
Currency {
symbol: None,
value: 0,
}
}
pub fn from_value(value: i64) -> Currency {
Currency {
symbol: None,
value,
}
}
#[cfg(feature = "parsing")]
pub fn from_string(s: &str) -> Option<Currency> {
use regex::Regex;
let s = s.trim();
let re =
Regex::new(r"^(?:\b|(-)?)(\p{Currency_Symbol})?((?:(?:\d{1,3}[\.,])+\d{3})|\d+)(?:[\.,](\d{2}))?\b$")
.unwrap();
let mut multiplier = 1;
let mut sign: Option<Symbol> = None;
let coin_str: String;
match re.captures(s) {
None => return None,
Some(caps) => {
if caps.get(1).is_some() {
multiplier = -1;
}
if caps.get(2).is_some() {
if multiplier < 0 {
sign = Some(s.chars().skip(1).next().unwrap());
} else {
sign = Some(s.chars().next().unwrap());
}
}
coin_str = caps
.get(3)
.map(|m| m.as_str())
.unwrap()
.replace(".", "")
.replace(",", "")
+ caps.get(4).map(|m| m.as_str()).unwrap_or("00");
}
}
if let Ok(coin) = coin_str.parse::<i64>() {
return Some(Currency {
symbol: sign,
value: multiplier * coin,
});
}
None
}
pub fn postfix(&self) -> Postfix<'_> {
Postfix { money: self }
}
pub fn prefix(&self) -> Prefix<'_> {
Prefix { money: self }
}
pub fn as_float(&self) -> f64 {
self.value as f64 / 100.0
}
pub fn value(&self) -> i64 {
self.value
}
pub fn symbol(&self) -> Option<Symbol> {
self.symbol
}
}
use std::ops::Deref;
impl Deref for Currency {
type Target = i64;
fn deref(&self) -> &i64 {
&self.value
}
}
impl From<i64> for Currency {
fn from(value: i64) -> Currency {
Currency::from_value(value)
}
}
impl From<(Symbol, i64)> for Currency {
fn from(tpl: (Symbol, i64)) -> Currency {
let (symbol, cents) = tpl;
Currency {
symbol: Some(symbol),
value: cents,
}
}
}
impl From<(i64, Symbol)> for Currency {
fn from(tpl: (i64, Symbol)) -> Currency {
let (cents, symbol) = tpl;
Currency {
symbol: Some(symbol),
value: cents,
}
}
}
impl Default for Currency {
fn default() -> Self {
Currency {
symbol: None,
value: 0,
}
}
}
pub struct Postfix<'a> {
money: &'a Currency,
}
pub struct Prefix<'a> {
money: &'a Currency,
}