gluesql_core/data/
bigdecimal_ext.rs1use bigdecimal::BigDecimal;
2
3pub trait BigDecimalExt {
4 fn to_i8(&self) -> Option<i8>;
5 fn to_i16(&self) -> Option<i16>;
6 fn to_i32(&self) -> Option<i32>;
7 fn to_i64(&self) -> Option<i64>;
8 fn to_i128(&self) -> Option<i128>;
9 fn to_u8(&self) -> Option<u8>;
10 fn to_u16(&self) -> Option<u16>;
11 fn to_u32(&self) -> Option<u32>;
12 fn to_u128(&self) -> Option<u128>;
13 fn to_u64(&self) -> Option<u64>;
14 fn to_f32(&self) -> Option<f32>;
15 fn to_f64(&self) -> Option<f64>;
16 fn is_integer_representation(&self) -> bool;
17}
18
19impl BigDecimalExt for BigDecimal {
20 fn to_i8(&self) -> Option<i8> {
21 self.is_integer_representation()
22 .then(|| bigdecimal::ToPrimitive::to_i8(self))?
23 }
24 fn to_i16(&self) -> Option<i16> {
25 self.is_integer_representation()
26 .then(|| bigdecimal::ToPrimitive::to_i16(self))?
27 }
28 fn to_i32(&self) -> Option<i32> {
29 self.is_integer_representation()
30 .then(|| bigdecimal::ToPrimitive::to_i32(self))?
31 }
32 fn to_i64(&self) -> Option<i64> {
33 self.is_integer_representation()
34 .then(|| bigdecimal::ToPrimitive::to_i64(self))?
35 }
36 fn to_i128(&self) -> Option<i128> {
37 self.is_integer_representation()
38 .then(|| bigdecimal::ToPrimitive::to_i128(self))?
39 }
40 fn to_u8(&self) -> Option<u8> {
41 self.is_integer_representation()
42 .then(|| bigdecimal::ToPrimitive::to_u8(self))?
43 }
44 fn to_u16(&self) -> Option<u16> {
45 self.is_integer_representation()
46 .then(|| bigdecimal::ToPrimitive::to_u16(self))?
47 }
48 fn to_u32(&self) -> Option<u32> {
49 self.is_integer_representation()
50 .then(|| bigdecimal::ToPrimitive::to_u32(self))?
51 }
52 fn to_u64(&self) -> Option<u64> {
53 self.is_integer_representation()
54 .then(|| bigdecimal::ToPrimitive::to_u64(self))?
55 }
56 fn to_u128(&self) -> Option<u128> {
57 self.is_integer_representation()
58 .then(|| bigdecimal::ToPrimitive::to_u128(self))?
59 }
60 fn to_f32(&self) -> Option<f32> {
61 bigdecimal::ToPrimitive::to_f32(self)
62 }
63 fn to_f64(&self) -> Option<f64> {
64 bigdecimal::ToPrimitive::to_f64(self)
65 }
66 fn is_integer_representation(&self) -> bool {
67 self.fractional_digit_count() == 0
68 }
69}