refix_message/interop/
rust_decimal.rs1use crate::{Decimal, OutOfRange};
2
3impl TryFrom<Decimal> for rust_decimal_1::Decimal {
4 type Error = OutOfRange;
5
6 fn try_from(decimal: Decimal) -> Result<Self, Self::Error> {
8 let mantissa = i128::from(decimal.mantissa());
9 let power = u32::from(decimal.exponent().unsigned_abs());
10 let (mantissa, scale) = if decimal.exponent() >= 0 {
11 let whole = (0..power).try_fold(mantissa, |value, _| value.checked_mul(10));
12 (whole.ok_or(OutOfRange)?, 0)
13 } else {
14 (mantissa, power)
15 };
16 rust_decimal_1::Decimal::try_from_i128_with_scale(mantissa, scale).map_err(|_| OutOfRange)
17 }
18}
19
20impl TryFrom<rust_decimal_1::Decimal> for Decimal {
21 type Error = OutOfRange;
22
23 fn try_from(decimal: rust_decimal_1::Decimal) -> Result<Self, Self::Error> {
25 let mantissa = i64::try_from(decimal.mantissa()).map_err(|_| OutOfRange)?;
26 let scale = i8::try_from(decimal.scale()).expect("rust_decimal's scale is at most 28");
27 Ok(Decimal::new(mantissa, -scale))
28 }
29}
30
31#[cfg(test)]
32mod tests {
33 use super::*;
34
35 fn rust_decimal(mantissa: i64, scale: u32) -> rust_decimal_1::Decimal {
36 rust_decimal_1::Decimal::new(mantissa, scale)
37 }
38
39 fn to_rust_decimal(mantissa: i64, exponent: i8) -> (i128, u32) {
40 let converted =
41 rust_decimal_1::Decimal::try_from(Decimal::new(mantissa, exponent)).unwrap();
42 (converted.mantissa(), converted.scale())
43 }
44
45 fn from_rust_decimal(decimal: rust_decimal_1::Decimal) -> (i64, i8) {
46 let converted = Decimal::try_from(decimal).unwrap();
47 (converted.mantissa(), converted.exponent())
48 }
49
50 #[test]
51 fn converts_to_rust_decimal_keeping_the_scale() {
52 assert_eq!(to_rust_decimal(10125, -2), (10125, 2));
53 assert_eq!(to_rust_decimal(150, -2), (150, 2));
54 }
55
56 #[test]
57 fn converts_from_rust_decimal_keeping_the_scale() {
58 assert_eq!(from_rust_decimal(rust_decimal(10125, 2)), (10125, -2));
59 assert_eq!(from_rust_decimal(rust_decimal(150, 2)), (150, -2));
60 }
61
62 #[test]
63 fn writes_a_positive_exponent_out_as_whole_digits() {
64 assert_eq!(to_rust_decimal(5, 3), (5000, 0));
65 assert_eq!(to_rust_decimal(0, i8::MAX), (0, 0));
66 }
67
68 #[test]
69 fn round_trips_the_ends_of_an_i64() {
70 for decimal in [Decimal::new(i64::MIN, -28), Decimal::new(i64::MAX, 0)] {
71 let converted = rust_decimal_1::Decimal::try_from(decimal).unwrap();
72 let back = Decimal::try_from(converted).unwrap();
73 assert_eq!(
74 (back.mantissa(), back.exponent()),
75 (decimal.mantissa(), decimal.exponent())
76 );
77 }
78 }
79
80 #[test]
81 fn rejects_more_than_28_fraction_digits() {
82 assert!(rust_decimal_1::Decimal::try_from(Decimal::new(1, -28)).is_ok());
83 assert_eq!(
84 rust_decimal_1::Decimal::try_from(Decimal::new(1, -29)),
85 Err(OutOfRange)
86 );
87 }
88
89 #[test]
90 fn rejects_a_value_past_96_bits() {
91 assert!(rust_decimal_1::Decimal::try_from(Decimal::new(1, 28)).is_ok());
92 assert_eq!(
93 rust_decimal_1::Decimal::try_from(Decimal::new(1, 29)),
94 Err(OutOfRange)
95 );
96 }
97
98 #[test]
99 fn rejects_digits_beyond_an_i64() {
100 let third = rust_decimal_1::Decimal::ONE / rust_decimal_1::Decimal::from(3);
101 assert_eq!(Decimal::try_from(third), Err(OutOfRange));
102 }
103}