Skip to main content

refix_message/interop/
bigdecimal.rs

1use crate::{Decimal, OutOfRange};
2use bigdecimal_04::BigDecimal;
3use bigdecimal_04::num_bigint::BigInt;
4
5impl From<Decimal> for BigDecimal {
6    fn from(decimal: Decimal) -> Self {
7        BigDecimal::new(
8            BigInt::from(decimal.mantissa()),
9            -i64::from(decimal.exponent()),
10        )
11    }
12}
13
14impl TryFrom<&BigDecimal> for Decimal {
15    type Error = OutOfRange;
16
17    /// Fails when the digits don't fit an `i64` or the scale an `i8`, so round first.
18    fn try_from(decimal: &BigDecimal) -> Result<Self, Self::Error> {
19        let (digits, scale) = decimal.as_bigint_and_scale();
20        let mantissa = i64::try_from(digits.as_ref()).map_err(|_| OutOfRange)?;
21        let exponent = scale
22            .checked_neg()
23            .and_then(|exponent| i8::try_from(exponent).ok())
24            .ok_or(OutOfRange)?;
25        Ok(Decimal::new(mantissa, exponent))
26    }
27}
28
29impl TryFrom<BigDecimal> for Decimal {
30    type Error = OutOfRange;
31
32    /// Fails when the digits don't fit an `i64` or the scale an `i8`, so round first.
33    fn try_from(decimal: BigDecimal) -> Result<Self, Self::Error> {
34        Decimal::try_from(&decimal)
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    fn big_decimal(digits: impl Into<BigInt>, scale: i64) -> BigDecimal {
43        BigDecimal::new(digits.into(), scale)
44    }
45
46    fn to_big_decimal(mantissa: i64, exponent: i8) -> (BigInt, i64) {
47        BigDecimal::from(Decimal::new(mantissa, exponent)).into_bigint_and_scale()
48    }
49
50    fn from_big_decimal(decimal: BigDecimal) -> (i64, i8) {
51        let converted = Decimal::try_from(decimal).unwrap();
52        (converted.mantissa(), converted.exponent())
53    }
54
55    #[test]
56    fn converts_to_bigdecimal_keeping_the_scale() {
57        assert_eq!(to_big_decimal(10125, -2), (BigInt::from(10125), 2));
58        assert_eq!(to_big_decimal(150, -2), (BigInt::from(150), 2));
59    }
60
61    #[test]
62    fn converts_from_bigdecimal_keeping_the_scale() {
63        assert_eq!(from_big_decimal(big_decimal(10125, 2)), (10125, -2));
64        assert_eq!(from_big_decimal(big_decimal(150, 2)), (150, -2));
65    }
66
67    #[test]
68    fn keeps_a_positive_exponent_as_a_negative_scale() {
69        assert_eq!(to_big_decimal(5, 3), (BigInt::from(5), -3));
70        assert_eq!(from_big_decimal(big_decimal(5, -3)), (5, 3));
71    }
72
73    #[test]
74    fn round_trips_the_ends_of_an_i64_and_an_i8() {
75        for (mantissa, exponent) in [(i64::MIN, i8::MIN), (i64::MAX, i8::MAX)] {
76            let converted = BigDecimal::from(Decimal::new(mantissa, exponent));
77            assert_eq!(from_big_decimal(converted), (mantissa, exponent));
78        }
79    }
80
81    #[test]
82    fn converts_a_borrowed_bigdecimal() {
83        let decimal = big_decimal(10125, 2);
84        assert_eq!(Decimal::try_from(&decimal), Ok(Decimal::new(10125, -2)));
85    }
86
87    #[test]
88    fn rejects_digits_beyond_an_i64() {
89        let past_the_end = big_decimal(BigInt::from(i64::MAX) + 1, 0);
90        let third = BigDecimal::from(1) / BigDecimal::from(3);
91        for decimal in [past_the_end, third] {
92            assert_eq!(Decimal::try_from(decimal), Err(OutOfRange));
93        }
94    }
95
96    #[test]
97    fn rejects_a_scale_beyond_an_i8() {
98        for scale in [129, -128, i64::MIN, i64::MAX] {
99            assert_eq!(Decimal::try_from(big_decimal(1, scale)), Err(OutOfRange));
100        }
101    }
102}