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
use super::ConversionError;

/// 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 => 0,
            Units::Other(inner) => *inner,
        }
    }
}

use std::convert::TryFrom;

impl TryFrom<u32> for Units {
    type Error = ConversionError;

    fn try_from(src: u32) -> Result<Self, Self::Error> {
        Ok(Units::Other(src))
    }
}

impl TryFrom<i32> for Units {
    type Error = ConversionError;

    fn try_from(src: i32) -> Result<Self, Self::Error> {
        Ok(Units::Other(src as u32))
    }
}

impl TryFrom<usize> for Units {
    type Error = ConversionError;

    fn try_from(src: usize) -> Result<Self, Self::Error> {
        Ok(Units::Other(src as u32))
    }
}

impl std::convert::TryFrom<&str> for Units {
    type Error = ConversionError;

    fn try_from(src: &str) -> Result<Self, Self::Error> {
        Ok(match src.to_lowercase().as_str() {
            "ether" => Units::Ether,
            "gwei" => Units::Gwei,
            "wei" => Units::Wei,
            _ => return Err(ConversionError::UnrecognizedUnits(src.to_string())),
        })
    }
}