4
4
use crate :: * ;
5
5
use serde:: { de, ser} ;
6
6
7
- #[ allow( unused_imports) ]
8
- use num_traits:: FromPrimitive ;
9
7
10
8
impl ser:: Serialize for BigDecimal {
11
9
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
@@ -16,6 +14,7 @@ impl ser::Serialize for BigDecimal {
16
14
}
17
15
}
18
16
17
+ /// Used by SerDe to construct a BigDecimal
19
18
struct BigDecimalVisitor ;
20
19
21
20
impl < ' de > de:: Visitor < ' de > for BigDecimalVisitor {
@@ -77,88 +76,117 @@ impl<'de> de::Deserialize<'de> for BigDecimal {
77
76
#[ cfg( test) ]
78
77
mod test {
79
78
use super :: * ;
80
-
81
- #[ test]
82
- fn test_serde_serialize ( ) {
83
- let vals = vec ! [
84
- ( "1.0" , "1.0" ) ,
85
- ( "0.5" , "0.5" ) ,
86
- ( "50" , "50" ) ,
87
- ( "50000" , "50000" ) ,
88
- ( "1e-3" , "0.001" ) ,
89
- ( "1e12" , "1000000000000" ) ,
90
- ( "0.25" , "0.25" ) ,
91
- ( "12.34" , "12.34" ) ,
92
- ( "0.15625" , "0.15625" ) ,
93
- ( "0.3333333333333333" , "0.3333333333333333" ) ,
94
- ( "3.141592653589793" , "3.141592653589793" ) ,
95
- ( "94247.77960769380" , "94247.77960769380" ) ,
96
- ( "10.99" , "10.99" ) ,
97
- ( "12.0010" , "12.0010" ) ,
98
- ] ;
99
- for ( s, v) in vals {
100
- let expected = format ! ( "\" {}\" " , v) ;
101
- let value = serde_json:: to_string ( & BigDecimal :: from_str ( s) . unwrap ( ) ) . unwrap ( ) ;
102
- assert_eq ! ( expected, value) ;
79
+ use paste:: paste;
80
+
81
+ mod serde_serialize {
82
+ use super :: * ;
83
+
84
+ macro_rules! impl_case {
85
+ ( $name: ident : $input: literal => $output: literal) => {
86
+ #[ test]
87
+ fn $name( ) {
88
+ let expected = format!( "\" {}\" " , $output) ;
89
+ let decimal: BigDecimal = $input. parse( ) . unwrap( ) ;
90
+ let value = serde_json:: to_string( & decimal) . unwrap( ) ;
91
+ assert_eq!( expected, value) ;
92
+ }
93
+ }
103
94
}
95
+
96
+ impl_case ! ( case_1d0: "1.0" => "1.0" ) ;
97
+ impl_case ! ( case_0d5: "0.5" => "0.5" ) ;
98
+ impl_case ! ( case_50: "50" => "50" ) ;
99
+ impl_case ! ( case_50000: "50000" => "50000" ) ;
100
+ impl_case ! ( case_1en3: "1e-3" => "0.001" ) ;
101
+ impl_case ! ( case_1e12: "1e12" => "1000000000000" ) ;
102
+ impl_case ! ( case_d25: ".25" => "0.25" ) ;
103
+ impl_case ! ( case_12d34e1: "12.34e1" => "123.4" ) ;
104
+ impl_case ! ( case_40d0010: "40.0010" => "40.0010" ) ;
104
105
}
105
106
106
- #[ test]
107
- fn test_serde_deserialize_str ( ) {
108
- let vals = vec ! [
109
- ( "1.0" , "1.0" ) ,
110
- ( "0.5" , "0.5" ) ,
111
- ( "50" , "50" ) ,
112
- ( "50000" , "50000" ) ,
113
- ( "1e-3" , "0.001" ) ,
114
- ( "1e12" , "1000000000000" ) ,
115
- ( "0.25" , "0.25" ) ,
116
- ( "12.34" , "12.34" ) ,
117
- ( "0.15625" , "0.15625" ) ,
118
- ( "0.3333333333333333" , "0.3333333333333333" ) ,
119
- ( "3.141592653589793" , "3.141592653589793" ) ,
120
- ( "94247.77960769380" , "94247.77960769380" ) ,
121
- ( "10.99" , "10.99" ) ,
122
- ( "12.0010" , "12.0010" ) ,
123
- ] ;
124
- for ( s, v) in vals {
125
- let expected = BigDecimal :: from_str ( v) . unwrap ( ) ;
126
- let value: BigDecimal = serde_json:: from_str ( & format ! ( "\" {}\" " , s) ) . unwrap ( ) ;
127
- assert_eq ! ( expected, value) ;
107
+ mod serde_deserialize_str {
108
+ use super :: * ;
109
+
110
+ macro_rules! impl_case {
111
+ ( $name: ident : $input: literal => $output: literal) => {
112
+ #[ test]
113
+ fn $name( ) {
114
+ let expected: BigDecimal = $output. parse( ) . unwrap( ) ;
115
+
116
+ let s = $input;
117
+ let value: BigDecimal = serde_json:: from_str( & format!( "\" {}\" " , s) ) . unwrap( ) ;
118
+ assert_eq!( expected, value) ;
119
+ }
120
+ }
128
121
}
122
+
123
+ impl_case ! ( case_1d0: "1.0" => "1.0" ) ;
124
+ impl_case ! ( case_0d5: "0.5" => "0.5" ) ;
125
+ impl_case ! ( case_50: "50" => "50" ) ;
126
+ impl_case ! ( case_50000: "50000" => "50000" ) ;
127
+ impl_case ! ( case_1en3: "1e-3" => "0.001" ) ;
128
+ impl_case ! ( case_1e12: "1e12" => "1000000000000" ) ;
129
+ impl_case ! ( case_d25: ".25" => "0.25" ) ;
130
+ impl_case ! ( case_12d34e1: "12.34e1" => "123.4" ) ;
131
+ impl_case ! ( case_40d0010: "40.0010" => "40.0010" ) ;
129
132
}
130
133
131
- # [ test ]
134
+
132
135
#[ cfg( not( feature = "string-only" ) ) ]
133
- fn test_serde_deserialize_int ( ) {
134
- let vals = vec ! [ 0 , 1 , 81516161 , -370 , -8 , -99999999999 ] ;
135
- for n in vals {
136
- let expected = BigDecimal :: from_i64 ( n) . unwrap ( ) ;
137
- let value: BigDecimal = serde_json:: from_str ( & serde_json:: to_string ( & n) . unwrap ( ) ) . unwrap ( ) ;
138
- assert_eq ! ( expected, value) ;
136
+ mod serde_deserialize_int {
137
+ use super :: * ;
138
+
139
+ macro_rules! impl_case {
140
+ ( -$input: literal) => {
141
+ paste! { impl_case!( [ < case_n $input >] : -$input) ; }
142
+ } ;
143
+ ( $input: literal) => {
144
+ paste! { impl_case!( [ < case_ $input >] : $input) ; }
145
+ } ;
146
+ ( $name: ident : $input: literal) => {
147
+ #[ test]
148
+ fn $name( ) {
149
+ let n = $input;
150
+ let expected = BigDecimal :: from_i64( n) . unwrap( ) ;
151
+ let value: BigDecimal = serde_json:: from_str( & serde_json:: to_string( & n) . unwrap( ) ) . unwrap( ) ;
152
+ assert_eq!( expected, value) ;
153
+ }
154
+ }
139
155
}
156
+
157
+ impl_case ! ( 0 ) ;
158
+ impl_case ! ( 1 ) ;
159
+ impl_case ! ( 81516161 ) ;
160
+ impl_case ! ( -370 ) ;
161
+ impl_case ! ( -8 ) ;
162
+ impl_case ! ( -99999999999 ) ;
140
163
}
141
164
142
- # [ test ]
165
+
143
166
#[ cfg( not( feature = "string-only" ) ) ]
144
- fn test_serde_deserialize_f64 ( ) {
145
- let vals = vec ! [
146
- 1.0 ,
147
- 0.5 ,
148
- 0.25 ,
149
- 50.0 ,
150
- 50000. ,
151
- 0.001 ,
152
- 12.34 ,
153
- 5.0 * 0.03125 ,
154
- stdlib:: f64 :: consts:: PI ,
155
- stdlib:: f64 :: consts:: PI * 10000.0 ,
156
- stdlib:: f64 :: consts:: PI * 30000.0 ,
157
- ] ;
158
- for n in vals {
159
- let expected = BigDecimal :: from_f64 ( n) . unwrap ( ) ;
160
- let value: BigDecimal = serde_json:: from_str ( & serde_json:: to_string ( & n) . unwrap ( ) ) . unwrap ( ) ;
161
- assert_eq ! ( expected, value) ;
167
+ mod serde_deserialize_f64 {
168
+ use super :: * ;
169
+ use stdlib:: f64:: consts;
170
+
171
+ macro_rules! impl_case {
172
+ ( $name: ident : $input: expr) => {
173
+ #[ test]
174
+ fn $name( ) {
175
+ let n = $input;
176
+ let expected = BigDecimal :: from_f64( n) . unwrap( ) ;
177
+ let value: BigDecimal = serde_json:: from_str( & serde_json:: to_string( & n) . unwrap( ) ) . unwrap( ) ;
178
+ assert_eq!( expected, value) ;
179
+ }
180
+ }
162
181
}
182
+
183
+ impl_case ! ( case_1d0: 1.0 ) ;
184
+ impl_case ! ( case_0d1: 0.1 ) ;
185
+ impl_case ! ( case_0d5: 0.5 ) ;
186
+ impl_case ! ( case_50d0: 50.0 ) ;
187
+ impl_case ! ( case_pi: consts:: PI ) ;
188
+ impl_case ! ( case_pi_times_100: consts:: PI * 100.0 ) ;
189
+ impl_case ! ( case_pi_times_30000: consts:: PI * 30000.0 ) ;
163
190
}
191
+
164
192
}
0 commit comments