1
+ #![ allow( dead_code) ]
2
+
1
3
use crate :: Error ;
2
4
use bigdecimal:: { BigDecimal , FromPrimitive } ;
3
5
use rbs:: Value ;
@@ -15,25 +17,43 @@ impl Decimal {
15
17
pub fn new ( arg : & str ) -> Result < Self , Error > {
16
18
Decimal :: from_str ( arg)
17
19
}
18
-
20
+ # [ inline ]
19
21
pub fn from_f64 ( arg : f64 ) -> Option < Decimal > {
20
22
match BigDecimal :: from_f64 ( arg) {
21
- None => { None }
22
- Some ( v) => {
23
- Some ( Decimal :: from ( v) )
24
- }
23
+ None => None ,
24
+ Some ( v) => Some ( Decimal :: from ( v) ) ,
25
25
}
26
26
}
27
-
27
+
28
+ #[ inline]
28
29
pub fn from_f32 ( arg : f32 ) -> Option < Decimal > {
29
30
match BigDecimal :: from_f32 ( arg) {
30
- None => { None }
31
- Some ( v) => {
32
- Some ( Decimal :: from ( v) )
33
- }
31
+ None => None ,
32
+ Some ( v) => Some ( Decimal :: from ( v) ) ,
34
33
}
35
34
}
36
35
36
+ #[ inline]
37
+ fn from_i64 ( n : i64 ) -> Option < Self > {
38
+ Some ( Decimal :: from ( n) )
39
+ }
40
+
41
+ #[ inline]
42
+ fn from_u64 ( n : u64 ) -> Option < Self > {
43
+ Some ( Decimal :: from ( n) )
44
+ }
45
+
46
+ #[ inline]
47
+ fn from_i128 ( n : i128 ) -> Option < Self > {
48
+ Some ( Decimal :: from ( n) )
49
+ }
50
+
51
+ #[ inline]
52
+ fn from_u128 ( n : u128 ) -> Option < Self > {
53
+ Some ( Decimal :: from ( n) )
54
+ }
55
+
56
+
37
57
///Return a new Decimal object equivalent to self,
38
58
/// with internal scaling set to the number specified.
39
59
/// If the new_scale is lower than the current value (indicating a larger power of 10),
@@ -94,7 +114,6 @@ impl From<Decimal> for Value {
94
114
}
95
115
}
96
116
97
-
98
117
impl From < i32 > for Decimal {
99
118
fn from ( arg : i32 ) -> Self {
100
119
Self :: from ( BigDecimal :: from ( arg) )
@@ -119,15 +138,49 @@ impl From<u64> for Decimal {
119
138
}
120
139
}
121
140
141
+ impl From < i128 > for Decimal {
142
+ fn from ( arg : i128 ) -> Self {
143
+ Self :: from ( BigDecimal :: from ( arg) )
144
+ }
145
+ }
146
+
147
+
148
+ impl From < u128 > for Decimal {
149
+ fn from ( arg : u128 ) -> Self {
150
+ Self :: from ( BigDecimal :: from ( arg) )
151
+ }
152
+ }
153
+
154
+ impl TryFrom < f32 > for Decimal {
155
+ type Error = Error ;
156
+
157
+ fn try_from ( value : f32 ) -> Result < Self , Error > {
158
+ Ok ( Self (
159
+ BigDecimal :: try_from ( value) . map_err ( |e| Error :: from ( e. to_string ( ) ) ) ?,
160
+ ) )
161
+ }
162
+ }
163
+
164
+ impl TryFrom < f64 > for Decimal {
165
+ type Error = Error ;
166
+
167
+ fn try_from ( value : f64 ) -> Result < Self , Error > {
168
+ Ok ( Self (
169
+ BigDecimal :: try_from ( value) . map_err ( |e| Error :: from ( e. to_string ( ) ) ) ?,
170
+ ) )
171
+ }
172
+ }
173
+
122
174
impl FromStr for Decimal {
123
175
type Err = Error ;
124
176
125
177
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
126
- Ok ( Decimal ( BigDecimal :: from_str ( & s) . map_err ( |e| Error :: from ( e. to_string ( ) ) ) ?) )
178
+ Ok ( Decimal (
179
+ BigDecimal :: from_str ( & s) . map_err ( |e| Error :: from ( e. to_string ( ) ) ) ?,
180
+ ) )
127
181
}
128
182
}
129
183
130
-
131
184
impl Deref for Decimal {
132
185
type Target = BigDecimal ;
133
186
@@ -284,20 +337,42 @@ mod test {
284
337
let v1 = Decimal :: new ( "1.123456" ) . unwrap ( ) ;
285
338
let v = v1. with_scale ( 2 ) ;
286
339
println ! ( "{}" , v. to_string( ) ) ;
287
- assert_eq ! ( v. to_string( ) , "1.12" ) ;
340
+ assert_eq ! ( v. to_string( ) , "1.12" ) ;
288
341
}
289
342
290
343
#[ test]
291
344
fn test_with_prec ( ) {
292
345
let v1 = Decimal :: new ( "1.123456" ) . unwrap ( ) ;
293
346
let v = v1. with_prec ( 2 ) ;
294
347
println ! ( "{}" , v. to_string( ) ) ;
295
- assert_eq ! ( v. to_string( ) , "1.1" ) ;
348
+ assert_eq ! ( v. to_string( ) , "1.1" ) ;
296
349
}
297
350
298
351
#[ test]
299
352
fn test_parse ( ) {
300
353
let v1 = "1.123456" . parse :: < Decimal > ( ) . unwrap ( ) ;
301
- assert_eq ! ( v1. to_string( ) , "1.123456" ) ;
354
+ assert_eq ! ( v1. to_string( ) , "1.123456" ) ;
355
+ }
356
+
357
+ #[ test]
358
+ fn test_from ( ) {
359
+ let v = Decimal :: from_i64 ( 1 ) ;
360
+ assert_eq ! ( v, Some ( Decimal :: from( 1 ) ) ) ;
361
+ let v = Decimal :: from_u64 ( 1 ) ;
362
+ assert_eq ! ( v, Some ( Decimal :: from( 1 ) ) ) ;
363
+ let v = Decimal :: from_i128 ( 1 ) ;
364
+ assert_eq ! ( v, Some ( Decimal :: from( 1 ) ) ) ;
365
+ let v = Decimal :: from_u128 ( 1 ) ;
366
+ assert_eq ! ( v, Some ( Decimal :: from( 1 ) ) ) ;
367
+ }
368
+
369
+ #[ test]
370
+ fn test_try_from_f64 ( ) {
371
+ let f=1.1 ;
372
+ let v = Decimal :: from_f64 ( f) ;
373
+ println ! ( "{:?}" , v) ;
374
+ if let Some ( v) =v{
375
+ println ! ( "{}" , v. to_string( ) ) ;
376
+ }
302
377
}
303
378
}
0 commit comments