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
use crate::{UncToken, UncTokenError, ONE_UNC};

impl std::str::FromStr for UncToken {
    type Err = UncTokenError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let uppercase_s = s.trim().to_ascii_uppercase();
        let (value, unit) = uppercase_s.split_at(
            s.find(|c: char| c.is_ascii_alphabetic())
                .ok_or_else(|| UncTokenError::InvalidTokenUnit(s.to_owned()))?,
        );
        let unit_precision = match unit {
            "YN" | "YUNC" | "YOCTOUNC" => 1,
            "UNC" | "N" => ONE_UNC,
            _ => return Err(UncTokenError::InvalidTokenUnit(s.to_owned())),
        };
        Ok(UncToken::from_attounc(
            crate::utils::parse_decimal_number(value.trim(), unit_precision)
                .map_err(UncTokenError::InvalidTokensAmount)?,
        ))
    }
}

#[cfg(test)]
mod test {
    use std::str::FromStr;

    use crate::{DecimalNumberParsingError, UncToken, UncTokenError};

    #[test]
    fn parse_decimal_number() {
        let data = "0.123456 unc";
        let gas: Result<UncToken, UncTokenError> = FromStr::from_str(data);
        assert_eq!(
            gas.unwrap(),
            UncToken::from_attounc(123456000000000000000000)
        );
    }
    #[test]
    fn parse_number_with_decimal_part() {
        let data = "11.123456 unc";
        let gas: Result<UncToken, UncTokenError> = FromStr::from_str(data);
        assert_eq!(
            gas.unwrap(),
            UncToken::from_attounc(11123456000000000000000000)
        );
    }

    #[test]
    fn parse_atto_number() {
        let data = "123456 YN";
        let gas: Result<UncToken, UncTokenError> = FromStr::from_str(data);
        assert_eq!(gas.unwrap(), UncToken::from_attounc(123456));
    }

    #[test]
    fn doubledot() {
        let data = "1.1.1 Near";
        let gas: Result<UncToken, UncTokenError> = FromStr::from_str(data);
        assert_eq!(
            gas,
            Err(UncTokenError::InvalidTokensAmount(
                DecimalNumberParsingError::InvalidNumber("1.1.1".to_owned())
            ))
        )
    }

    #[test]
    fn space_after_dot() {
        let data = "1. 0 unc";
        let gas: Result<UncToken, UncTokenError> = FromStr::from_str(data);
        assert_eq!(
            gas,
            Err(UncTokenError::InvalidTokensAmount(
                DecimalNumberParsingError::InvalidNumber("1. 0".to_owned())
            ))
        )
    }

    #[test]
    fn incorect_currency() {
        let data = "0 pas";
        let gas: Result<UncToken, UncTokenError> = FromStr::from_str(data);
        assert_eq!(gas, Err(UncTokenError::InvalidTokenUnit(data.to_owned())))
    }

    #[test]
    fn without_currency() {
        let data = "0";
        let gas: Result<UncToken, UncTokenError> = FromStr::from_str(data);
        assert_eq!(gas, Err(UncTokenError::InvalidTokenUnit("0".to_owned())))
    }

    #[test]
    fn invalid_whole() {
        let data = "-1 Near";
        let gas: Result<UncToken, UncTokenError> = FromStr::from_str(data);
        assert_eq!(
            gas,
            Err(UncTokenError::InvalidTokensAmount(
                DecimalNumberParsingError::InvalidNumber("-1".to_owned())
            ))
        )
    }

    #[test]
    fn test_from_str_f64_gas_without_int() {
        let unc_gas = UncToken::from_str(".055 yunc").unwrap_err();
        assert_eq!(
            unc_gas,
            UncTokenError::InvalidTokensAmount(DecimalNumberParsingError::InvalidNumber(
                ".055".to_string()
            ))
        );
    }

    #[test]
    fn test_from_str_without_unit() {
        let unc_gas = UncToken::from_str("100").unwrap_err();
        assert_eq!(
            unc_gas,
            UncTokenError::InvalidTokenUnit("100".to_string())
        );
    }

    #[test]
    fn test_from_str_incorrect_unit() {
        let unc_gas = UncToken::from_str("100 UAH").unwrap_err();
        assert_eq!(
            unc_gas,
            UncTokenError::InvalidTokenUnit("100 UAH".to_string())
        );
    }

    #[test]
    fn test_from_str_invalid_double_dot() {
        let unc_gas = UncToken::from_str("100.55.").unwrap_err();
        assert_eq!(
            unc_gas,
            UncTokenError::InvalidTokenUnit("100.55.".to_string())
        );
    }

    #[test]
    fn test_from_str_large_fractional_part() {
        let unc_gas = UncToken::from_str("100.1111122222333 yunc").unwrap_err(); // 13 digits after "."
        assert_eq!(
            unc_gas,
            UncTokenError::InvalidTokensAmount(DecimalNumberParsingError::LongFractional(
                "1111122222333".to_string()
            ))
        );
    }
}