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
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UncTokenError {
    InvalidTokensAmount(crate::utils::DecimalNumberParsingError),
    InvalidTokenUnit(String),
}

impl std::fmt::Display for UncTokenError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            UncTokenError::InvalidTokensAmount(err) => write!(f, "invalid tokens amount: {}", err),
            UncTokenError::InvalidTokenUnit(unit) => write!(f, "invalid token unit: {}", unit),
        }
    }
}

impl std::error::Error for UncTokenError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            UncTokenError::InvalidTokensAmount(err) => Some(err),
            UncTokenError::InvalidTokenUnit(_) => None,
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_unc_token_error_display() {
        assert_eq!(
            format!(
                "{}",
                UncTokenError::InvalidTokensAmount(
                    crate::utils::DecimalNumberParsingError::InvalidNumber("abc".to_owned())
                )
            ),
            "invalid tokens amount: invalid number: abc"
        );
        assert_eq!(
            format!(
                "{}",
                UncTokenError::InvalidTokensAmount(
                    crate::utils::DecimalNumberParsingError::LongWhole("999999999999.0".to_owned())
                )
            ),
            "invalid tokens amount: too long whole part: 999999999999.0"
        );
        assert_eq!(
            format!(
                "{}",
                UncTokenError::InvalidTokensAmount(
                    crate::utils::DecimalNumberParsingError::LongFractional(
                        "0.999999999999".to_owned()
                    )
                )
            ),
            "invalid tokens amount: too long fractional part: 0.999999999999"
        );
        assert_eq!(
            format!("{}", UncTokenError::InvalidTokenUnit("abc".to_owned())),
            "invalid token unit: abc"
        );
    }
}