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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#![deny(warnings)]

mod currency;
mod ops;

pub use currency::{CurrencyCode, Exponent, Rates};
pub use ops::Operation;

use std::convert::TryInto;
use std::fmt;

#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};

/// How much `amount` makes a unit
pub const AMOUNT_UNIT: i128 = 1_000_000;

/// Holds an amount of currency. The `i128` it holds is
/// expressed in fractions of a unit.
/// `CurrencyAmount(`[`AMOUNT_UNIT`](constant.AMOUNT_UNIT.html)`)` makes a unit.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct CurrencyAmount(i128);

impl CurrencyAmount {
    pub fn with_unit(unit: i128) -> Self {
        CurrencyAmount(unit * AMOUNT_UNIT)
    }

    pub fn with_tenths(tenths: i128) -> Self {
        CurrencyAmount(tenths * AMOUNT_UNIT / 10)
    }

    pub fn with_cents(cents: i128) -> Self {
        CurrencyAmount(cents * AMOUNT_UNIT / 100)
    }

    pub fn with_thousands(thousands: i128) -> Self {
        CurrencyAmount(thousands * AMOUNT_UNIT / 1000)
    }

    pub fn into_unit(self) -> Self {
        CurrencyAmount(self.0 / AMOUNT_UNIT)
    }

    pub fn into_tenths(self) -> Self {
        CurrencyAmount(self.0 * 10 / AMOUNT_UNIT)
    }

    pub fn into_cents(self) -> Self {
        CurrencyAmount(self.0 * 100 / AMOUNT_UNIT)
    }

    pub fn into_thousands(self) -> Self {
        CurrencyAmount(self.0 * 1000 / AMOUNT_UNIT)
    }
}

impl std::ops::Deref for CurrencyAmount {
    type Target = i128;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl std::ops::Add for CurrencyAmount {
    type Output = Self;

    fn add(self, other: Self) -> Self::Output {
        CurrencyAmount(self.0 + other.0)
    }
}

impl std::ops::Sub for CurrencyAmount {
    type Output = Self;

    fn sub(self, other: Self) -> Self::Output {
        CurrencyAmount(self.0 - other.0)
    }
}

impl std::ops::Mul for CurrencyAmount {
    type Output = Self;

    fn mul(self, other: Self) -> Self::Output {
        CurrencyAmount(self.0 * other.0)
    }
}

impl std::ops::Div for CurrencyAmount {
    type Output = Self;

    fn div(self, other: Self) -> Self::Output {
        CurrencyAmount(self.0 / other.0)
    }
}

impl From<i128> for CurrencyAmount {
    fn from(i: i128) -> Self {
        CurrencyAmount(i)
    }
}

impl From<CurrencyAmount> for i128 {
    fn from(amount: CurrencyAmount) -> Self {
        *amount
    }
}

/// A struct containing an `amount` of money having a certain `currency_code`.
/// Note that `amount` contains fractions of a unit. See [`AMOUNT_UNIT`](constant.AMOUNT_UNIT.html).
///
/// ## Examples
///
/// ```
///
/// use monet::{Money, CurrencyAmount, Rates, Operation};
/// use std::convert::TryInto;
///
/// // Custom rates.
/// let map = vec![("USD", 1_000_000)].into_iter()
///     .map(|(code, worth)| (code.try_into().unwrap(), worth.into()))
///     .collect();
/// let rates = Rates::with_rates(map);
///
/// let money_owned = Money::with_str_code(CurrencyAmount::with_unit(2), "USD").unwrap();
/// let money_paid = Money::with_str_code(CurrencyAmount::with_unit(1), "USD").unwrap();
///
/// let remaining = (money_owned - money_paid).execute(&rates);
///
/// assert_eq!(remaining, Money::with_str_code(CurrencyAmount::with_unit(1), "USD"));
///
/// ```
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct Money {
    pub amount: CurrencyAmount,
    pub currency_code: CurrencyCode,
}

impl Money {
    pub fn new(amount: CurrencyAmount, currency_code: CurrencyCode) -> Self {
        Money {
            amount,
            currency_code,
        }
    }

    pub fn into_code(self, code: CurrencyCode, rates: &Rates) -> Option<Money> {
        let worth_self = rates.worth(self.currency_code)?;
        let worth_new = rates.worth(code)?;

        Some(Money {
            amount: self.amount * worth_self / worth_new,
            currency_code: code,
        })
    }

    /// Creates `Money` with given amount and code. Returns `None` if the given code is not three characters long.
    pub fn with_str_code(amount: CurrencyAmount, currency_code: &str) -> Option<Money> {
        Some(Money::new(amount, currency_code.try_into().ok()?))
    }
}

/// Money can be displayed in the following format: `12.10 CHF`.
///
/// Default precision is dependent on the currency code (see ISO 4217 exponent).
/// A custom precision in range `0..=6` can be provided like this:
///
/// ```
///
/// use monet::Money;
///
/// let money = Money::with_str_code(12_100_000.into(), "CHF").unwrap();
///
/// assert_eq!(
///     &format!("{}", money),
///     "12.10 CHF"
/// );
///
/// assert_eq!(
///     &format!("{:.2}", money),
///     "12.10 CHF"
/// );
///
/// assert_eq!(
///     &format!("{:.6}", money),
///     "12.100000 CHF"
/// );
///
/// // Note: the formatted version has lost a decimal due to the
/// // lower precision
/// assert_eq!(
///     &format!("{:.0}", money),
///     "12 CHF"
/// );
///
/// ```
impl fmt::Display for Money {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use iso4217::alpha3;

        let code: &str = (&self.currency_code).try_into().unwrap();
        let units = *self.amount / AMOUNT_UNIT;
        let decimals = *self.amount % AMOUNT_UNIT;
        let precision = f
            .precision()
            .unwrap_or_else(|| alpha3(code).unwrap().exp as usize);

        if precision > 0 {
            write!(
                f,
                "{units}.{decimals} {code}",
                units = units,
                decimals = decimals
                    .checked_div(AMOUNT_UNIT / 10i128.pow(precision as u32))
                    .ok_or(fmt::Error)?,
                code = code
            )
        } else {
            write!(f, "{units} {code}", units = units, code = code)
        }
    }
}

// pub trait CurrencyAmount: std::fmt::Debug + Clone + Copy + Eq + PartialEq + Default {}

// impl CurrencyAmount for u8 {}
// impl CurrencyAmount for u16 {}
// impl CurrencyAmount for u32 {}
// impl CurrencyAmount for u64 {}
// impl CurrencyAmount for u128 {}
// impl CurrencyAmount for i8 {}
// impl CurrencyAmount for i16 {}
// impl CurrencyAmount for i32 {}
// impl CurrencyAmount for i64 {}
// impl CurrencyAmount for i128 {}

// pub trait Currency {
//     const NAME: &'static str = std::any::type_name;
//     /// The exponent of this currency
//     const EXPONENT: i8;
//     const VALUE:
// }

#[cfg(test)]
fn rates() -> Rates {
    let map = vec![
        ("USD", 1_000_000),
        ("CHF", 1_100_000),
        ("EUR", 1_200_000),
        ("GBP", 1_500_000),
    ]
    .into_iter()
    .map(|(code, worth)| (code.try_into().unwrap(), worth.into()))
    .collect();
    Rates::with_rates(map)
}

#[cfg(test)]
mod tests {

    mod money {
        use crate::rates;
        use crate::CurrencyAmount;
        use crate::Money;
        use std::convert::TryInto;

        #[cfg(feature = "serialize")]
        use serde::{Serialize, Deserialize};

        #[test]
        fn test_into_code() {
            let money_chf = Money::new(
                CurrencyAmount::with_unit(1_000_000),
                "CHF".try_into().unwrap(),
            );
            let money_usd = money_chf.into_code("USD".try_into().unwrap(), &rates());

            assert_eq!(
                money_usd,
                Some(Money::new(
                    CurrencyAmount::with_unit(1_100_000),
                    "USD".try_into().unwrap()
                ))
            );
        }

        #[cfg(feature = "serialize")]
        #[cfg_attr(feature = "serialize", test)]
        fn test_de_serialize() {
            fn deserialize<'de, D: Deserialize<'de>>() {}
            fn serialize<D: Serialize>() {}

            deserialize::<Money>();
            serialize::<Money>();
        }

        #[test]
        fn test_display() {
            let money = Money::with_str_code(CurrencyAmount::with_cents(2125), "CHF").unwrap();

            assert_eq!(format!("{}", money), "21.25 CHF".to_string());
            assert_eq!(format!("{:.2}", money), "21.25 CHF".to_string());
            assert_eq!(format!("{:.6}", money), "21.250000 CHF".to_string());
            assert_eq!(format!("{:.0}", money), "21 CHF".to_string());
        }

        #[test]
        #[should_panic]
        fn test_display_panic() {
            let money = Money::with_str_code(CurrencyAmount::with_cents(2125), "CHF").unwrap();
            let _formatted = format!("{:.8}", money);
        }
    }
}