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
//! `Display` Implementation.

use std::fmt;
use std::ops::Deref;

use super::{Currency,Postfix, Prefix};

macro_rules! impl_deref_to_currency{
    ($s:ty) => {
        impl<'a> Deref for $s {
            type Target = Currency;

            fn deref(&self) -> &Currency {
                &self.currency
            }
        }
    }
}

impl_deref_to_currency!(Postfix<'a>);
impl_deref_to_currency!(Prefix<'a>);

/// Allows Currencies to be displayed as Strings.
/// The format includes no comma delimiting with a two digit precision decimal.
///
/// # Examples
/// ```
/// use claude::Currency;
///
/// assert!(Currency(None, 1210).postfix().to_string() == "12,10");
///
/// println!("{}", Currency(Some('€'), 100099).postfix());
/// ```
/// The last line prints the following:
/// ```text
/// "1000,99€"
/// ```
impl<'a> fmt::Display for Postfix<'a> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let decimal = format!("{:.2}", (self.1 as f32 / 100.0)).replace(".", ",");
        match self.0 {
            Some(symbol) => write!(f, "{d}{s}", s = symbol, d = decimal),
            None => write!(f, "{}", decimal),
        }
    }
}

/// Allows Currencies to be displayed as Strings. The format includes no comma delimiting with a
/// two digit precision decimal.
///
/// # Examples
/// ```
/// use claude::Currency;
///
/// assert!(Currency(Some('$'), 1210).prefix().to_string() == "$12.10");
/// assert!(Currency(None, 1210).prefix().to_string() == "12.10");
///
/// println!("{}", Currency(Some('$'), 100099).prefix());
/// ```
/// The last line prints the following:
/// ```text
/// "$1000.99"
/// ```
impl<'a> fmt::Display for Prefix<'a> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let decimal = format!("{:.2}", (self.1 as f32 / 100.0));
        match self.0 {
            Some(symbol) => write!(f, "{s}{d}", s = symbol, d = decimal),
            None => write!(f, "{}", decimal),
        }
    }
}