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
use std::{
    fmt::{Display, Formatter},
    str::FromStr,
};

use anyhow::Context;
use cosmwasm_std::Decimal256;

use crate::prelude::Signed;

use super::types::{NonZero, UnsignedDecimal};

impl<T: UnsignedDecimal> NonZero<T> {
    /// Get the multiplicative inverse.
    ///
    /// Guaranteed not to fail, since all values here must be great than zero.
    pub fn inverse(self) -> Self {
        NonZero::new(T::from_decimal256(
            Decimal256::one() / self.raw().into_decimal256(),
        ))
        .expect("NonZero::inverse failed but that should be impossible!")
    }
}

impl<T: UnsignedDecimal> Display for NonZero<T> {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        write!(f, "{}", self.raw())
    }
}

impl<T: UnsignedDecimal> TryFrom<u128> for NonZero<T> {
    type Error = anyhow::Error;

    fn try_from(val: u128) -> Result<Self, Self::Error> {
        Signed::<T>::from(val).try_into()
    }
}

impl<T: UnsignedDecimal> TryFrom<u64> for NonZero<T> {
    type Error = anyhow::Error;

    fn try_from(val: u64) -> Result<Self, Self::Error> {
        u128::from(val).try_into()
    }
}

impl<T: UnsignedDecimal> TryFrom<&str> for NonZero<T> {
    type Error = anyhow::Error;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::from_str(value)
    }
}

impl<T: UnsignedDecimal> FromStr for NonZero<T> {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Decimal256::from_str(s)
            .ok()
            .map(T::from_decimal256)
            .and_then(NonZero::new)
            .with_context(|| format!("Could not parse a non-zero value from: {s}"))
    }
}

#[cfg(test)]
mod tests {
    use crate::prelude::{Number, NumberGtZero};

    use quickcheck::quickcheck;

    quickcheck! {
        fn number_over_zero_roundtrip(num: u128) -> bool {
            let number = (Number::from(num) + Number::ONE).unwrap();
            let number_over_zero = NumberGtZero::try_from(number).unwrap();
            let number2 = Number::from(number_over_zero);
            assert_eq!(number, number2);
            number == number2
        }

        fn negative_fails(num: u128) -> bool {
            NumberGtZero::try_from(-Number::from(num)).unwrap_err();
            true
        }
    }
}