1use fastnum::D128;
24use fastnum::decimal::Context;
25
26use crate::error::invalid_data;
27use crate::{Result, ensure_data_valid};
28
29pub type Decimal = D128;
31
32pub fn decimal_from_i128_with_scale(mantissa: i128, scale: u32) -> Decimal {
41 if scale == 0 {
42 return D128::from_i128(mantissa).expect("i128 always fits in D128");
43 }
44
45 let is_negative = mantissa < 0;
47 let abs_str = mantissa.unsigned_abs().to_string();
48 let scale_usize = scale as usize;
49
50 let decimal_str = if abs_str.len() <= scale_usize {
51 let zeros_needed = scale_usize - abs_str.len();
54 format!(
55 "{}0.{}{}",
56 if is_negative { "-" } else { "" },
57 "0".repeat(zeros_needed),
58 abs_str
59 )
60 } else {
61 let decimal_pos = abs_str.len() - scale_usize;
63 format!(
64 "{}{}.{}",
65 if is_negative { "-" } else { "" },
66 &abs_str[..decimal_pos],
67 &abs_str[decimal_pos..]
68 )
69 };
70
71 D128::from_str(&decimal_str, Context::default())
72 .expect("constructed decimal string is always valid")
73}
74
75pub fn try_decimal_from_i128_with_scale(mantissa: i128, scale: u32) -> Result<Decimal> {
80 Ok(decimal_from_i128_with_scale(mantissa, scale))
82}
83
84#[allow(dead_code)]
88pub fn decimal_new(mantissa: i64, scale: u32) -> Decimal {
89 decimal_from_i128_with_scale(mantissa as i128, scale)
90}
91
92pub fn decimal_from_str_exact(s: &str) -> Result<Decimal> {
96 let decimal = D128::from_str(s, Context::default())
97 .map_err(|e| invalid_data!("Can't parse decimal: {e}"))?;
98 ensure_data_valid!(
99 decimal.is_finite(),
100 "Decimal value must be finite: {}",
101 decimal
102 );
103 Ok(decimal)
104}
105
106pub fn decimal_mantissa(d: &Decimal) -> i128 {
112 let digits = d.digits();
115
116 let unsigned: u128 = digits
119 .to_u128()
120 .expect("Iceberg decimals (max 38 digits) always fit in u128");
121
122 let signed = unsigned as i128;
123 if d.is_sign_negative() {
124 -signed
125 } else {
126 signed
127 }
128}
129
130pub fn decimal_precision(mantissa: i128) -> u32 {
134 mantissa
135 .unsigned_abs()
136 .checked_ilog10()
137 .map_or(1, |x| x + 1)
138}
139
140pub fn decimal_scale(d: &Decimal) -> u32 {
144 let frac = d.fractional_digits_count();
145 if frac < 0 { 0 } else { frac as u32 }
146}
147
148pub fn decimal_rescale(d: Decimal, scale: u32) -> Decimal {
152 d.rescale(scale as i16)
153}
154
155pub fn i128_from_be_bytes(bytes: &[u8]) -> Option<i128> {
160 if bytes.is_empty() {
161 return Some(0);
162 }
163 if bytes.len() > 16 {
164 return None; }
166
167 let is_negative = bytes[0] & 0x80 != 0;
169
170 let mut padded = if is_negative { [0xFF; 16] } else { [0; 16] };
172 let start = 16 - bytes.len();
173 padded[start..].copy_from_slice(bytes);
174
175 Some(i128::from_be_bytes(padded))
176}
177
178pub fn i128_to_be_bytes_min(value: i128) -> Vec<u8> {
183 let bytes = value.to_be_bytes();
184
185 let is_negative = value < 0;
189 let skip_byte = if is_negative { 0xFF } else { 0x00 };
190
191 let mut start = 0;
192 while start < 15 && bytes[start] == skip_byte {
193 let next_byte = bytes[start + 1];
195 let next_is_negative = (next_byte & 0x80) != 0;
196 if next_is_negative == is_negative {
197 start += 1;
198 } else {
199 break;
200 }
201 }
202
203 bytes[start..].to_vec()
204}
205
206pub(crate) fn decimal_to_fixed_length_bytes_exact(value: i128, len: usize) -> Option<Vec<u8>> {
212 if len == 0 || len > 16 {
213 return None;
214 }
215
216 let be_bytes = value.to_be_bytes();
217 let offset = 16 - len;
218 let sign_byte = if value < 0 { 0xFF } else { 0x00 };
219
220 if be_bytes[..offset].iter().any(|&b| b != sign_byte) {
222 return None;
223 }
224
225 if (be_bytes[offset] & 0x80 != 0) != (value < 0) {
227 return None;
228 }
229
230 Some(be_bytes[offset..].to_vec())
231}
232
233#[cfg(test)]
234mod tests {
235 use super::*;
236
237 #[test]
238 fn test_decimal_from_i128_with_scale() {
239 let d = decimal_from_i128_with_scale(12345, 2);
240 assert_eq!(d.to_string(), "123.45");
241
242 let d = decimal_from_i128_with_scale(-12345, 2);
243 assert_eq!(d.to_string(), "-123.45");
244
245 let d = decimal_from_i128_with_scale(0, 5);
246 assert_eq!(d.to_string(), "0.00000");
247 }
248
249 #[test]
250 fn test_decimal_new() {
251 let d = decimal_new(123, 2);
252 assert_eq!(d.to_string(), "1.23");
253
254 let d = decimal_new(-456, 3);
255 assert_eq!(d.to_string(), "-0.456");
256 }
257
258 #[test]
259 fn test_decimal_from_str_exact() {
260 let d = decimal_from_str_exact("123.45").unwrap();
261 assert_eq!(d.to_string(), "123.45");
262
263 let d = decimal_from_str_exact("-0.001").unwrap();
264 assert_eq!(d.to_string(), "-0.001");
265
266 let d = decimal_from_str_exact("99999999999999999999999999999999999999").unwrap();
267 assert_eq!(d.to_string(), "99999999999999999999999999999999999999");
268 }
269
270 #[test]
271 fn test_decimal_mantissa() {
272 let d = decimal_from_i128_with_scale(12345, 2);
273 assert_eq!(decimal_mantissa(&d), 12345);
274
275 let d = decimal_from_i128_with_scale(-12345, 2);
276 assert_eq!(decimal_mantissa(&d), -12345);
277 }
278
279 #[test]
280 fn test_decimal_precision() {
281 assert_eq!(decimal_precision(0), 1);
282 assert_eq!(decimal_precision(5), 1);
283 assert_eq!(decimal_precision(42), 2);
284 assert_eq!(decimal_precision(-42), 2);
285 assert_eq!(decimal_precision(9), 1);
287 assert_eq!(decimal_precision(10), 2);
288 assert_eq!(decimal_precision(99), 2);
289 assert_eq!(decimal_precision(100), 3);
290
291 assert_eq!(
293 decimal_precision(99999999999999999999999999999999999999),
294 38
295 );
296 assert_eq!(
297 decimal_precision(-99999999999999999999999999999999999999),
298 38
299 );
300
301 assert_eq!(decimal_precision(i128::MAX), 39);
303 assert_eq!(decimal_precision(i128::MIN), 39);
304 }
305
306 #[test]
307 fn test_decimal_scale() {
308 let d = decimal_from_i128_with_scale(12345, 2);
309 assert_eq!(decimal_scale(&d), 2);
310
311 let d = decimal_from_i128_with_scale(12345, 0);
312 assert_eq!(decimal_scale(&d), 0);
313 }
314
315 #[test]
316 fn test_decimal_rescale() {
317 let d = decimal_from_str_exact("123.45").unwrap();
318 let rescaled = decimal_rescale(d, 4);
319 assert_eq!(decimal_scale(&rescaled), 4);
320 assert_eq!(decimal_mantissa(&rescaled), 1234500);
321 }
322
323 #[test]
324 fn test_38_digit_precision() {
325 let max_38_digits = "99999999999999999999999999999999999999";
327 let d = decimal_from_str_exact(max_38_digits).unwrap();
328 assert_eq!(d.to_string(), max_38_digits);
329
330 let min_38_digits = "-99999999999999999999999999999999999999";
331 let d = decimal_from_str_exact(min_38_digits).unwrap();
332 assert_eq!(d.to_string(), min_38_digits);
333 }
334
335 #[test]
336 fn test_i128_from_be_bytes() {
337 assert_eq!(i128_from_be_bytes(&[]), Some(0));
339
340 assert_eq!(i128_from_be_bytes(&[0x01]), Some(1));
342 assert_eq!(i128_from_be_bytes(&[0x7F]), Some(127));
343 assert_eq!(i128_from_be_bytes(&[0x00, 0xFF]), Some(255));
344 assert_eq!(i128_from_be_bytes(&[0x04, 0xD2]), Some(1234));
345
346 assert_eq!(i128_from_be_bytes(&[0xFF]), Some(-1));
348 assert_eq!(i128_from_be_bytes(&[0x80]), Some(-128));
349 assert_eq!(i128_from_be_bytes(&[0xFB, 0x2E]), Some(-1234));
350
351 assert_eq!(i128_from_be_bytes(&[0; 17]), None);
353 }
354
355 #[test]
356 fn test_i128_to_be_bytes_min() {
357 assert_eq!(i128_to_be_bytes_min(0), vec![0x00]);
359 assert_eq!(i128_to_be_bytes_min(1), vec![0x01]);
360 assert_eq!(i128_to_be_bytes_min(127), vec![0x7F]);
361 assert_eq!(i128_to_be_bytes_min(128), vec![0x00, 0x80]);
362 assert_eq!(i128_to_be_bytes_min(255), vec![0x00, 0xFF]);
363 assert_eq!(i128_to_be_bytes_min(1234), vec![0x04, 0xD2]);
364
365 assert_eq!(i128_to_be_bytes_min(-1), vec![0xFF]);
367 assert_eq!(i128_to_be_bytes_min(-128), vec![0x80]);
368 assert_eq!(i128_to_be_bytes_min(-129), vec![0xFF, 0x7F]);
369 assert_eq!(i128_to_be_bytes_min(-1234), vec![0xFB, 0x2E]);
370
371 for val in [
373 0i128,
374 1,
375 -1,
376 127,
377 -128,
378 255,
379 -256,
380 12345,
381 -12345,
382 i128::MAX,
383 i128::MIN,
384 ] {
385 let bytes = i128_to_be_bytes_min(val);
386 assert_eq!(
387 i128_from_be_bytes(&bytes),
388 Some(val),
389 "Round trip failed for {val}"
390 );
391 }
392 }
393
394 #[test]
395 fn test_decimal_to_fixed_length_bytes_exact_positive() {
396 let bytes = decimal_to_fixed_length_bytes_exact(12345, 9).unwrap();
397 assert_eq!(bytes.len(), 9);
398 assert_eq!(bytes[7], 0x30);
400 assert_eq!(bytes[8], 0x39);
401 assert!(bytes[..7].iter().all(|&b| b == 0x00));
402 }
403
404 #[test]
405 fn test_decimal_to_fixed_length_bytes_exact_negative() {
406 let bytes = decimal_to_fixed_length_bytes_exact(-1, 9).unwrap();
407 assert_eq!(bytes.len(), 9);
408 assert!(bytes.iter().all(|&b| b == 0xFF));
409 }
410
411 #[test]
412 fn test_decimal_to_fixed_length_bytes_exact_round_trip() {
413 for value in [
414 0i128,
415 1,
416 -1,
417 12345,
418 -12345,
419 i64::MAX as i128,
420 i64::MIN as i128,
421 ] {
422 let bytes = decimal_to_fixed_length_bytes_exact(value, 16).unwrap();
423 assert_eq!(bytes.len(), 16);
424 assert_eq!(
425 i128_from_be_bytes(&bytes),
426 Some(value),
427 "Round trip failed for value={value}"
428 );
429 }
430 }
431
432 #[test]
436 fn test_decimal_to_fixed_length_bytes_exact_rejects_overflow() {
437 assert_eq!(decimal_to_fixed_length_bytes_exact(200, 1), None);
439 assert_eq!(
440 decimal_to_fixed_length_bytes_exact(200, 2),
441 Some(vec![0x00, 0xC8])
442 );
443
444 assert_eq!(decimal_to_fixed_length_bytes_exact(-200, 1), None);
445 assert_eq!(
446 decimal_to_fixed_length_bytes_exact(-200, 2),
447 Some(vec![0xFF, 0x38])
448 );
449
450 assert_eq!(
452 decimal_to_fixed_length_bytes_exact(127, 1),
453 Some(vec![0x7F])
454 );
455 assert_eq!(decimal_to_fixed_length_bytes_exact(128, 1), None);
456 assert_eq!(
457 decimal_to_fixed_length_bytes_exact(-128, 1),
458 Some(vec![0x80])
459 );
460 assert_eq!(decimal_to_fixed_length_bytes_exact(-129, 1), None);
461
462 assert_eq!(decimal_to_fixed_length_bytes_exact(i128::MAX, 15), None);
463 assert_eq!(decimal_to_fixed_length_bytes_exact(0, 0), None);
464 assert_eq!(decimal_to_fixed_length_bytes_exact(0, 17), None);
465 }
466}