reifydb_type/value/decimal/
mod.rs1use std::{
5 cmp::Ordering,
6 fmt,
7 fmt::{Display, Formatter},
8 hash,
9 ops::{Add, Deref, Div, Mul, Sub},
10 str::FromStr,
11};
12
13use bigdecimal::{BigDecimal as BigDecimalInner, FromPrimitive};
14use num_traits::{One, Zero};
15use serde::{
16 Deserialize, Deserializer, Serialize, Serializer,
17 de::{self, Visitor},
18};
19
20use super::{int::Int, uint::Uint};
21use crate::{
22 error::{Error, TypeError},
23 fragment::Fragment,
24 value::r#type::Type,
25};
26
27pub mod parse;
28
29#[repr(transparent)]
30#[derive(Clone, Debug)]
31pub struct Decimal(pub BigDecimalInner);
32
33impl Decimal {
34 pub fn zero() -> Self {
35 Self(BigDecimalInner::zero())
36 }
37
38 pub fn one() -> Self {
39 Self(BigDecimalInner::one())
40 }
41}
42
43impl Deref for Decimal {
44 type Target = BigDecimalInner;
45
46 fn deref(&self) -> &Self::Target {
47 &self.0
48 }
49}
50
51impl Decimal {
52 pub fn new(value: BigDecimalInner) -> Self {
53 Self(value)
54 }
55
56 pub fn from_bigdecimal(value: BigDecimalInner) -> Self {
57 Self(value)
58 }
59
60 pub fn with_scale(value: BigDecimalInner, scale: i64) -> Self {
61 Self(value.with_scale(scale))
62 }
63
64 pub fn from_i64(value: i64) -> Self {
65 Self(BigDecimalInner::from(value))
66 }
67
68 pub fn inner(&self) -> &BigDecimalInner {
69 &self.0
70 }
71
72 pub fn to_bigdecimal(self) -> BigDecimalInner {
73 self.0
74 }
75
76 pub fn negate(self) -> Self {
77 Self(-self.0)
78 }
79}
80
81impl PartialEq for Decimal {
82 fn eq(&self, other: &Self) -> bool {
83 self.0 == other.0
84 }
85}
86
87impl Eq for Decimal {}
88
89impl PartialOrd for Decimal {
90 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
91 Some(self.cmp(other))
92 }
93}
94
95impl Ord for Decimal {
96 fn cmp(&self, other: &Self) -> Ordering {
97 self.0.cmp(&other.0)
98 }
99}
100
101impl Display for Decimal {
102 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
103 self.0.fmt(f)
104 }
105}
106
107impl hash::Hash for Decimal {
108 fn hash<H: hash::Hasher>(&self, state: &mut H) {
109 self.0.to_string().hash(state);
110 }
111}
112
113impl FromStr for Decimal {
114 type Err = Error;
115
116 fn from_str(s: &str) -> Result<Self, Self::Err> {
117 let big_decimal = BigDecimalInner::from_str(s).map_err(|_| -> Error {
118 TypeError::InvalidNumberFormat {
119 target: Type::Decimal,
120 fragment: Fragment::None,
121 }
122 .into()
123 })?;
124
125 Ok(Self(big_decimal))
126 }
127}
128
129impl From<i64> for Decimal {
130 fn from(value: i64) -> Self {
131 Self(BigDecimalInner::from(value))
132 }
133}
134
135impl From<i8> for Decimal {
136 fn from(value: i8) -> Self {
137 Self::from(value as i64)
138 }
139}
140
141impl From<i16> for Decimal {
142 fn from(value: i16) -> Self {
143 Self::from(value as i64)
144 }
145}
146
147impl From<i32> for Decimal {
148 fn from(value: i32) -> Self {
149 Self::from(value as i64)
150 }
151}
152
153impl From<i128> for Decimal {
154 fn from(value: i128) -> Self {
155 Self(BigDecimalInner::from(value))
156 }
157}
158
159impl From<u8> for Decimal {
160 fn from(value: u8) -> Self {
161 Self::from(value as i64)
162 }
163}
164
165impl From<u16> for Decimal {
166 fn from(value: u16) -> Self {
167 Self::from(value as i64)
168 }
169}
170
171impl From<u32> for Decimal {
172 fn from(value: u32) -> Self {
173 Self::from(value as i64)
174 }
175}
176
177impl From<u64> for Decimal {
178 fn from(value: u64) -> Self {
179 Self(BigDecimalInner::from(value))
180 }
181}
182
183impl From<u128> for Decimal {
184 fn from(value: u128) -> Self {
185 Self(BigDecimalInner::from(value))
186 }
187}
188
189impl From<f32> for Decimal {
190 fn from(value: f32) -> Self {
191 let inner = BigDecimalInner::from_f32(value).unwrap_or_else(|| BigDecimalInner::from(0));
192 Self(inner)
193 }
194}
195
196impl From<f64> for Decimal {
197 fn from(value: f64) -> Self {
198 let inner = BigDecimalInner::from_f64(value).unwrap_or_else(|| BigDecimalInner::from(0));
199 Self(inner)
200 }
201}
202
203impl From<BigDecimalInner> for Decimal {
204 fn from(value: BigDecimalInner) -> Self {
205 Self(value)
206 }
207}
208
209impl From<Int> for Decimal {
210 fn from(value: Int) -> Self {
211 Self(BigDecimalInner::from_bigint(value.0, 0))
212 }
213}
214
215impl From<Uint> for Decimal {
216 fn from(value: Uint) -> Self {
217 Self(BigDecimalInner::from_bigint(value.0, 0))
218 }
219}
220
221impl Add for Decimal {
223 type Output = Self;
224
225 fn add(self, rhs: Self) -> Self::Output {
226 Self(self.0 + rhs.0)
227 }
228}
229
230impl Sub for Decimal {
231 type Output = Self;
232
233 fn sub(self, rhs: Self) -> Self::Output {
234 Self(self.0 - rhs.0)
235 }
236}
237
238impl Mul for Decimal {
239 type Output = Self;
240
241 fn mul(self, rhs: Self) -> Self::Output {
242 Self(self.0 * rhs.0)
243 }
244}
245
246impl Div for Decimal {
247 type Output = Self;
248
249 fn div(self, rhs: Self) -> Self::Output {
250 Self(self.0 / rhs.0)
251 }
252}
253
254impl Add<&Decimal> for &Decimal {
256 type Output = Decimal;
257
258 fn add(self, rhs: &Decimal) -> Self::Output {
259 Decimal(&self.0 + &rhs.0)
260 }
261}
262
263impl Sub<&Decimal> for &Decimal {
264 type Output = Decimal;
265
266 fn sub(self, rhs: &Decimal) -> Self::Output {
267 Decimal(&self.0 - &rhs.0)
268 }
269}
270
271impl Mul<&Decimal> for &Decimal {
272 type Output = Decimal;
273
274 fn mul(self, rhs: &Decimal) -> Self::Output {
275 Decimal(&self.0 * &rhs.0)
276 }
277}
278
279impl Div<&Decimal> for &Decimal {
280 type Output = Decimal;
281
282 fn div(self, rhs: &Decimal) -> Self::Output {
283 Decimal(&self.0 / &rhs.0)
284 }
285}
286
287impl Default for Decimal {
288 fn default() -> Self {
289 Self::zero()
290 }
291}
292
293impl Serialize for Decimal {
296 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
297 where
298 S: Serializer,
299 {
300 serializer.serialize_str(&self.0.to_string())
301 }
302}
303
304struct DecimalVisitor;
305
306impl<'de> Visitor<'de> for DecimalVisitor {
307 type Value = Decimal;
308
309 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
310 formatter.write_str("a decimal number as a string")
311 }
312
313 fn visit_str<E>(self, value: &str) -> Result<Decimal, E>
314 where
315 E: de::Error,
316 {
317 BigDecimalInner::from_str(value).map(Decimal).map_err(|e| E::custom(format!("invalid decimal: {}", e)))
318 }
319}
320
321impl<'de> Deserialize<'de> for Decimal {
322 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
323 where
324 D: Deserializer<'de>,
325 {
326 deserializer.deserialize_str(DecimalVisitor)
327 }
328}
329
330#[cfg(test)]
331pub mod tests {
332 use postcard::{from_bytes, to_stdvec};
333 use serde_json::{from_str, to_string};
334
335 use super::*;
336
337 #[test]
338 fn test_new_decimal_valid() {
339 let bd = BigDecimalInner::from_str("123.45").unwrap();
340 let decimal = Decimal::new(bd);
341 assert_eq!(decimal.to_string(), "123.45");
342 }
343
344 #[test]
345 fn test_from_str() {
346 let decimal = Decimal::from_str("123.45").unwrap();
347 assert_eq!(decimal.to_string(), "123.45");
348 }
349
350 #[test]
351 fn test_comparison() {
352 let d1 = Decimal::from_str("123.45").unwrap();
353 let d2 = Decimal::from_str("123.46").unwrap();
354 let d3 = Decimal::from_str("123.45").unwrap();
355
356 assert!(d1 < d2);
357 assert_eq!(d1, d3);
358 }
359
360 #[test]
361 fn test_display() {
362 let decimal = Decimal::from_str("123.45").unwrap();
363 assert_eq!(format!("{}", decimal), "123.45");
364 }
365
366 #[test]
367 fn test_serde_json() {
368 let decimal = Decimal::from_str("123.456789").unwrap();
369 let json = to_string(&decimal).unwrap();
370 assert_eq!(json, "\"123.456789\"");
371
372 let deserialized: Decimal = from_str(&json).unwrap();
373 assert_eq!(deserialized, decimal);
374 }
375
376 #[test]
377 fn test_serde_json_negative() {
378 let decimal = Decimal::from_str("-987.654321").unwrap();
379 let json = to_string(&decimal).unwrap();
380 assert_eq!(json, "\"-987.654321\"");
381
382 let deserialized: Decimal = from_str(&json).unwrap();
383 assert_eq!(deserialized, decimal);
384 }
385
386 #[test]
387 fn test_serde_json_zero() {
388 let decimal = Decimal::zero();
389 let json = to_string(&decimal).unwrap();
390 assert_eq!(json, "\"0\"");
391
392 let deserialized: Decimal = from_str(&json).unwrap();
393 assert_eq!(deserialized, decimal);
394 }
395
396 #[test]
397 fn test_serde_json_high_precision() {
398 let decimal = Decimal::from_str("123456789.123456789123456789").unwrap();
399 let json = to_string(&decimal).unwrap();
400
401 let deserialized: Decimal = from_str(&json).unwrap();
402 assert_eq!(deserialized, decimal);
403 }
404
405 #[test]
406 fn test_serde_postcard() {
407 let decimal = Decimal::from_str("123.456789").unwrap();
408 let encoded = to_stdvec(&decimal).unwrap();
409
410 let decoded: Decimal = from_bytes(&encoded).unwrap();
411 assert_eq!(decoded, decimal);
412 }
413
414 #[test]
415 fn test_serde_postcard_negative() {
416 let decimal = Decimal::from_str("-987.654321").unwrap();
417 let encoded = to_stdvec(&decimal).unwrap();
418
419 let decoded: Decimal = from_bytes(&encoded).unwrap();
420 assert_eq!(decoded, decimal);
421 }
422
423 #[test]
424 fn test_serde_postcard_zero() {
425 let decimal = Decimal::zero();
426 let encoded = to_stdvec(&decimal).unwrap();
427
428 let decoded: Decimal = from_bytes(&encoded).unwrap();
429 assert_eq!(decoded, decimal);
430 }
431
432 #[test]
433 fn test_serde_postcard_high_precision() {
434 let decimal = Decimal::from_str("123456789.123456789123456789").unwrap();
435 let encoded = to_stdvec(&decimal).unwrap();
436
437 let decoded: Decimal = from_bytes(&encoded).unwrap();
438 assert_eq!(decoded, decimal);
439 }
440
441 #[test]
442 fn test_serde_postcard_large_number() {
443 let decimal = Decimal::from_str("999999999999999999999999999999.999999999999999999999999").unwrap();
444 let encoded = to_stdvec(&decimal).unwrap();
445
446 let decoded: Decimal = from_bytes(&encoded).unwrap();
447 assert_eq!(decoded, decimal);
448 }
449}