Skip to main content

reifydb_type/value/decimal/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use 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 {
222	type Output = Self;
223
224	fn add(self, rhs: Self) -> Self::Output {
225		Self(self.0 + rhs.0)
226	}
227}
228
229impl Sub for Decimal {
230	type Output = Self;
231
232	fn sub(self, rhs: Self) -> Self::Output {
233		Self(self.0 - rhs.0)
234	}
235}
236
237impl Mul for Decimal {
238	type Output = Self;
239
240	fn mul(self, rhs: Self) -> Self::Output {
241		Self(self.0 * rhs.0)
242	}
243}
244
245impl Div for Decimal {
246	type Output = Self;
247
248	fn div(self, rhs: Self) -> Self::Output {
249		Self(self.0 / rhs.0)
250	}
251}
252
253impl Add<&Decimal> for &Decimal {
254	type Output = Decimal;
255
256	fn add(self, rhs: &Decimal) -> Self::Output {
257		Decimal(&self.0 + &rhs.0)
258	}
259}
260
261impl Sub<&Decimal> for &Decimal {
262	type Output = Decimal;
263
264	fn sub(self, rhs: &Decimal) -> Self::Output {
265		Decimal(&self.0 - &rhs.0)
266	}
267}
268
269impl Mul<&Decimal> for &Decimal {
270	type Output = Decimal;
271
272	fn mul(self, rhs: &Decimal) -> Self::Output {
273		Decimal(&self.0 * &rhs.0)
274	}
275}
276
277impl Div<&Decimal> for &Decimal {
278	type Output = Decimal;
279
280	fn div(self, rhs: &Decimal) -> Self::Output {
281		Decimal(&self.0 / &rhs.0)
282	}
283}
284
285impl Default for Decimal {
286	fn default() -> Self {
287		Self::zero()
288	}
289}
290
291impl Serialize for Decimal {
292	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
293	where
294		S: Serializer,
295	{
296		serializer.serialize_str(&self.0.to_string())
297	}
298}
299
300struct DecimalVisitor;
301
302impl<'de> Visitor<'de> for DecimalVisitor {
303	type Value = Decimal;
304
305	fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
306		formatter.write_str("a decimal number as a string")
307	}
308
309	fn visit_str<E>(self, value: &str) -> Result<Decimal, E>
310	where
311		E: de::Error,
312	{
313		BigDecimalInner::from_str(value).map(Decimal).map_err(|e| E::custom(format!("invalid decimal: {}", e)))
314	}
315}
316
317impl<'de> Deserialize<'de> for Decimal {
318	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
319	where
320		D: Deserializer<'de>,
321	{
322		deserializer.deserialize_str(DecimalVisitor)
323	}
324}
325
326#[cfg(test)]
327pub mod tests {
328	use postcard::{from_bytes, to_stdvec};
329	use serde_json::{from_str, to_string};
330
331	use super::*;
332
333	#[test]
334	fn test_new_decimal_valid() {
335		let bd = BigDecimalInner::from_str("123.45").unwrap();
336		let decimal = Decimal::new(bd);
337		assert_eq!(decimal.to_string(), "123.45");
338	}
339
340	#[test]
341	fn test_from_str() {
342		let decimal = Decimal::from_str("123.45").unwrap();
343		assert_eq!(decimal.to_string(), "123.45");
344	}
345
346	#[test]
347	fn test_comparison() {
348		let d1 = Decimal::from_str("123.45").unwrap();
349		let d2 = Decimal::from_str("123.46").unwrap();
350		let d3 = Decimal::from_str("123.45").unwrap();
351
352		assert!(d1 < d2);
353		assert_eq!(d1, d3);
354	}
355
356	#[test]
357	fn test_display() {
358		let decimal = Decimal::from_str("123.45").unwrap();
359		assert_eq!(format!("{}", decimal), "123.45");
360	}
361
362	#[test]
363	fn test_serde_json() {
364		let decimal = Decimal::from_str("123.456789").unwrap();
365		let json = to_string(&decimal).unwrap();
366		assert_eq!(json, "\"123.456789\"");
367
368		let deserialized: Decimal = from_str(&json).unwrap();
369		assert_eq!(deserialized, decimal);
370	}
371
372	#[test]
373	fn test_serde_json_negative() {
374		let decimal = Decimal::from_str("-987.654321").unwrap();
375		let json = to_string(&decimal).unwrap();
376		assert_eq!(json, "\"-987.654321\"");
377
378		let deserialized: Decimal = from_str(&json).unwrap();
379		assert_eq!(deserialized, decimal);
380	}
381
382	#[test]
383	fn test_serde_json_zero() {
384		let decimal = Decimal::zero();
385		let json = to_string(&decimal).unwrap();
386		assert_eq!(json, "\"0\"");
387
388		let deserialized: Decimal = from_str(&json).unwrap();
389		assert_eq!(deserialized, decimal);
390	}
391
392	#[test]
393	fn test_serde_json_high_precision() {
394		let decimal = Decimal::from_str("123456789.123456789123456789").unwrap();
395		let json = to_string(&decimal).unwrap();
396
397		let deserialized: Decimal = from_str(&json).unwrap();
398		assert_eq!(deserialized, decimal);
399	}
400
401	#[test]
402	fn test_serde_postcard() {
403		let decimal = Decimal::from_str("123.456789").unwrap();
404		let encoded = to_stdvec(&decimal).unwrap();
405
406		let decoded: Decimal = from_bytes(&encoded).unwrap();
407		assert_eq!(decoded, decimal);
408	}
409
410	#[test]
411	fn test_serde_postcard_negative() {
412		let decimal = Decimal::from_str("-987.654321").unwrap();
413		let encoded = to_stdvec(&decimal).unwrap();
414
415		let decoded: Decimal = from_bytes(&encoded).unwrap();
416		assert_eq!(decoded, decimal);
417	}
418
419	#[test]
420	fn test_serde_postcard_zero() {
421		let decimal = Decimal::zero();
422		let encoded = to_stdvec(&decimal).unwrap();
423
424		let decoded: Decimal = from_bytes(&encoded).unwrap();
425		assert_eq!(decoded, decimal);
426	}
427
428	#[test]
429	fn test_serde_postcard_high_precision() {
430		let decimal = Decimal::from_str("123456789.123456789123456789").unwrap();
431		let encoded = to_stdvec(&decimal).unwrap();
432
433		let decoded: Decimal = from_bytes(&encoded).unwrap();
434		assert_eq!(decoded, decimal);
435	}
436
437	#[test]
438	fn test_serde_postcard_large_number() {
439		let decimal = Decimal::from_str("999999999999999999999999999999.999999999999999999999999").unwrap();
440		let encoded = to_stdvec(&decimal).unwrap();
441
442		let decoded: Decimal = from_bytes(&encoded).unwrap();
443		assert_eq!(decoded, decimal);
444	}
445}