|
| 1 | +//! |
| 2 | +//! Support for serde implementations |
| 3 | +//! |
| 4 | +use crate::*; |
| 5 | +use serde::{de, ser}; |
| 6 | + |
| 7 | + |
| 8 | +impl ser::Serialize for BigDecimal { |
| 9 | + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 10 | + where |
| 11 | + S: ser::Serializer, |
| 12 | + { |
| 13 | + serializer.collect_str(&self) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +/// Used by SerDe to construct a BigDecimal |
| 18 | +struct BigDecimalVisitor; |
| 19 | + |
| 20 | +impl<'de> de::Visitor<'de> for BigDecimalVisitor { |
| 21 | + type Value = BigDecimal; |
| 22 | + |
| 23 | + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
| 24 | + write!(formatter, "a number or formatted decimal string") |
| 25 | + } |
| 26 | + |
| 27 | + fn visit_str<E>(self, value: &str) -> Result<BigDecimal, E> |
| 28 | + where |
| 29 | + E: de::Error, |
| 30 | + { |
| 31 | + BigDecimal::from_str(value).map_err(|err| E::custom(format!("{}", err))) |
| 32 | + } |
| 33 | + |
| 34 | + fn visit_u64<E>(self, value: u64) -> Result<BigDecimal, E> |
| 35 | + where |
| 36 | + E: de::Error, |
| 37 | + { |
| 38 | + Ok(BigDecimal::from(value)) |
| 39 | + } |
| 40 | + |
| 41 | + fn visit_i64<E>(self, value: i64) -> Result<BigDecimal, E> |
| 42 | + where |
| 43 | + E: de::Error, |
| 44 | + { |
| 45 | + Ok(BigDecimal::from(value)) |
| 46 | + } |
| 47 | + |
| 48 | + fn visit_f64<E>(self, value: f64) -> Result<BigDecimal, E> |
| 49 | + where |
| 50 | + E: de::Error, |
| 51 | + { |
| 52 | + BigDecimal::try_from(value).map_err(|err| E::custom(format!("{}", err))) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +#[cfg(not(feature = "string-only"))] |
| 57 | +impl<'de> de::Deserialize<'de> for BigDecimal { |
| 58 | + fn deserialize<D>(d: D) -> Result<Self, D::Error> |
| 59 | + where |
| 60 | + D: de::Deserializer<'de>, |
| 61 | + { |
| 62 | + d.deserialize_any(BigDecimalVisitor) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[cfg(feature = "string-only")] |
| 67 | +impl<'de> de::Deserialize<'de> for BigDecimal { |
| 68 | + fn deserialize<D>(d: D) -> Result<Self, D::Error> |
| 69 | + where |
| 70 | + D: de::Deserializer<'de>, |
| 71 | + { |
| 72 | + d.deserialize_str(BigDecimalVisitor) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +#[cfg(test)] |
| 77 | +mod test { |
| 78 | + use super::*; |
| 79 | + use paste::paste; |
| 80 | + |
| 81 | + use serde_test::{ |
| 82 | + Token, assert_tokens, assert_de_tokens, assert_de_tokens_error |
| 83 | + }; |
| 84 | + |
| 85 | + mod serde_serialize_deserialize_str { |
| 86 | + use super::*; |
| 87 | + |
| 88 | + macro_rules! impl_case { |
| 89 | + ($name:ident : $input:literal => $output:literal) => { |
| 90 | + #[test] |
| 91 | + fn $name() { |
| 92 | + let expected = Token::Str($output); |
| 93 | + let decimal: BigDecimal = $input.parse().unwrap(); |
| 94 | + assert_tokens(&decimal, &[expected]); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + impl_case!(case_1d0: "1.0" => "1.0"); |
| 100 | + impl_case!(case_0d5: "0.5" => "0.5"); |
| 101 | + impl_case!(case_50: "50" => "50"); |
| 102 | + impl_case!(case_50000: "50000" => "50000"); |
| 103 | + impl_case!(case_1en3: "1e-3" => "0.001"); |
| 104 | + impl_case!(case_1e12: "1e12" => "1000000000000"); |
| 105 | + impl_case!(case_d25: ".25" => "0.25"); |
| 106 | + impl_case!(case_12d34e1: "12.34e1" => "123.4"); |
| 107 | + impl_case!(case_40d0010: "40.0010" => "40.0010"); |
| 108 | + } |
| 109 | + |
| 110 | + #[cfg(not(feature = "string-only"))] |
| 111 | + mod serde_deserialize_int { |
| 112 | + use super::*; |
| 113 | + |
| 114 | + macro_rules! impl_case { |
| 115 | + ( $( $ttype:ident ),+ : -$input:literal ) => { |
| 116 | + $( paste! { impl_case!([< case_n $input _ $ttype:lower >] : $ttype : -$input); } )* |
| 117 | + }; |
| 118 | + ( $( $ttype:ident ),+ : $input:literal ) => { |
| 119 | + $( paste! { impl_case!([< case_ $input _ $ttype:lower >] : $ttype : $input); } )* |
| 120 | + }; |
| 121 | + ($name:ident : $type:ident : $input:literal) => { |
| 122 | + #[test] |
| 123 | + fn $name() { |
| 124 | + let expected = BigDecimal::from($input); |
| 125 | + let token = Token::$type($input); |
| 126 | + assert_de_tokens(&expected, &[token]); |
| 127 | + } |
| 128 | + }; |
| 129 | + } |
| 130 | + |
| 131 | + impl_case!(I8, I16, I32, I64, U8, U16, U32, U64 : 0); |
| 132 | + impl_case!(I8, I16, I32, I64, U8, U16, U32, U64 : 1); |
| 133 | + impl_case!(I8, I16, I32, I64 : -1); |
| 134 | + impl_case!(I64: -99999999999i64); |
| 135 | + impl_case!(I64: -9_223_372_036_854_775_808i64); |
| 136 | + } |
| 137 | + |
| 138 | + #[cfg(not(feature = "string-only"))] |
| 139 | + mod serde_deserialize_float { |
| 140 | + use super::*; |
| 141 | + |
| 142 | + macro_rules! impl_case { |
| 143 | + ( $name:ident : $input:literal => $ttype:ident : $expected:literal ) => { |
| 144 | + paste! { |
| 145 | + #[test] |
| 146 | + fn [< $name _ $ttype:lower >]() { |
| 147 | + let expected: BigDecimal = $expected.parse().unwrap(); |
| 148 | + let token = Token::$ttype($input); |
| 149 | + assert_de_tokens(&expected, &[token]); |
| 150 | + } |
| 151 | + } |
| 152 | + }; |
| 153 | + ( $name:ident : $input:literal => $( $ttype:ident : $expected:literal )+ ) => { |
| 154 | + $( impl_case!($name : $input => $ttype : $expected); )* |
| 155 | + }; |
| 156 | + ( $name:ident : $input:literal => $( $ttype:ident ),+ : $expected:literal ) => { |
| 157 | + $( impl_case!($name : $input => $ttype : $expected); )* |
| 158 | + }; |
| 159 | + } |
| 160 | + |
| 161 | + impl_case!(case_1d0 : 1.0 => F32, F64 : "1"); |
| 162 | + impl_case!(case_1d1 : 1.1 => F32 : "1.10000002384185791015625" |
| 163 | + F64 : "1.100000000000000088817841970012523233890533447265625"); |
| 164 | + |
| 165 | + impl_case!(case_0d001834988943300: |
| 166 | + 0.001834988943300 => F32 : "0.001834988943301141262054443359375" |
| 167 | + F64 : "0.00183498894330000003084768511740776375518180429935455322265625"); |
| 168 | + |
| 169 | + impl_case!(case_n869651d9131236838: |
| 170 | + -869651.9131236838 => F32 : "-869651.9375" |
| 171 | + F64 : "-869651.91312368377111852169036865234375"); |
| 172 | + |
| 173 | + impl_case!(case_n1en20: |
| 174 | + -1e-20 => F32 : "-9.999999682655225388967887463487205224055287544615566730499267578125E-21" |
| 175 | + F64 : "-999999999999999945153271454209571651729503702787392447107715776066783064379706047475337982177734375e-119"); |
| 176 | + } |
| 177 | + |
| 178 | + #[cfg(not(feature = "string-only"))] |
| 179 | + mod serde_deserialize_nan { |
| 180 | + use super::*; |
| 181 | + |
| 182 | + #[test] |
| 183 | + fn case_f32() { |
| 184 | + let tokens = [ Token::F32(f32::NAN) ]; |
| 185 | + assert_de_tokens_error::<BigDecimal>(&tokens, "NAN"); |
| 186 | + } |
| 187 | + |
| 188 | + #[test] |
| 189 | + fn case_f64() { |
| 190 | + let tokens = [ Token::F64(f64::NAN) ]; |
| 191 | + assert_de_tokens_error::<BigDecimal>(&tokens, "NAN"); |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments