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
/// Common Ethereum unit types.
pub enum Units {
    /// Ether corresponds to 1e18 Wei
    Ether,
    /// Gwei corresponds to 1e9 Wei
    Gwei,
    /// Wei corresponds to 1 Wei
    Wei,
    /// Use this for other less frequent unit sizes
    Other(u32),
}

impl Units {
    pub fn as_num(&self) -> u32 {
        match self {
            Units::Ether => 18,
            Units::Gwei => 9,
            Units::Wei => 1,
            Units::Other(inner) => *inner,
        }
    }
}

impl From<u32> for Units {
    fn from(src: u32) -> Self {
        Units::Other(src)
    }
}

impl From<i32> for Units {
    fn from(src: i32) -> Self {
        Units::Other(src as u32)
    }
}

impl From<usize> for Units {
    fn from(src: usize) -> Self {
        Units::Other(src as u32)
    }
}

impl From<&str> for Units {
    fn from(src: &str) -> Self {
        match src.to_lowercase().as_str() {
            "ether" => Units::Ether,
            "gwei" => Units::Gwei,
            "wei" => Units::Wei,
            _ => panic!("unrecognized units"),
        }
    }
}