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
#[doc(hidden)]
#[macro_export]
macro_rules! impl_curr_code {
    ($(#[$attr:meta])* ($($vis:tt)*)
     enum $name:ident {
         $($var:ident { num: $num:expr, digit: $digit:expr, name: $currency:expr, },)*
     }) => {
        $(#[$attr])*
        $($vis)* enum $name { $($var = $num),* }

        impl $name {
            /// `alphabetic code` represented by three letters.
            pub fn alpha(&self) -> &'static str {
                match self { $($name::$var => stringify!($var),)* }
            }

            /// `numeric code` represented by a three-digit number code.
            pub fn num(&self) -> u32 {
                match self { $($name::$var => $num,)* }
            }

            /// Currency name.
            pub fn name(&self) -> &'static str {
                match self { $($name::$var => $currency,)* }
            }

            /// The number of digits after the decimal separator.
            pub fn digit(&self) -> Option<u32> {
                match self { $($name::$var => $digit,)* }
            }
        }

        impl ::std::str::FromStr for $name {
            type Err = crate::error::ParseCodeError;
            fn from_str(v: &str) -> Result<Self, Self::Err> {
                ::std::convert::TryFrom::try_from(v)
            }
        }

        impl ::std::convert::TryFrom<&str> for $name {
            type Error = crate::error::ParseCodeError;
            fn try_from(v: &str) -> Result<Self, Self::Error> {
                match v {
                    $(stringify!($var) => Ok($name::$var),)*
                    v => Err(crate::error::ParseCodeError::Alpha(v.into())),
                }
            }
        }

        impl ::std::convert::TryFrom<u32> for $name {
            type Error = crate::error::ParseCodeError;
            fn try_from(v: u32) -> Result<Self, Self::Error> {
                match v {
                    $($num => Ok($name::$var),)*
                    v => Err(crate::error::ParseCodeError::Num(v)),
                }
            }
        }

        impl ::std::convert::From<$name> for &str {
            fn from(v: $name) -> Self {
                v.alpha()
            }
        }

        impl ::std::convert::From<$name> for u32 {
            fn from(v: $name) -> Self {
                v.num()
            }
        }
    }
}