paft-decimal 0.9.0

Backend-agnostic decimal helpers for the paft ecosystem.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
//! Backend-agnostic decimal helpers shared across the `paft` workspace.
//!
//! The crate wraps [`rust_decimal`](https://docs.rs/rust_decimal) by default and can
//! switch to [`bigdecimal`](https://docs.rs/bigdecimal) via the optional
//! `bigdecimal` feature. It exposes a consistent [`Decimal`] type alongside rounding
//! strategies and utility helpers for parsing, scaling, and canonical rendering
//! without pulling in higher-level money abstractions.

#![cfg_attr(docsrs, feature(doc_cfg))]
#![forbid(unsafe_code)]
#![warn(missing_docs)]

use std::{borrow::Cow, str::FromStr};

mod constrained;

pub use constrained::{DecimalConstraintError, NonNegativeDecimal, PositiveDecimal, Ratio};

/// Rounding strategy supported by decimal operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum RoundingStrategy {
    /// Round halves toward the nearest even digit.
    MidpointNearestEven,
    /// Round halves away from zero.
    MidpointAwayFromZero,
    /// Round halves toward zero.
    MidpointTowardZero,
    /// Always round toward zero.
    ToZero,
    /// Always round away from zero.
    AwayFromZero,
    /// Always round toward negative infinity.
    ToNegativeInfinity,
    /// Always round toward positive infinity.
    ToPositiveInfinity,
}

#[cfg(not(feature = "bigdecimal"))]
mod backend {
    use super::{
        FromStr, RoundingStrategy, rust_decimal_to_i128_mantissa, rust_decimal_to_scaled_units,
    };

    pub use rust_decimal::Decimal;
    use rust_decimal::RoundingStrategy as RustRoundingStrategy;
    pub use rust_decimal::prelude::ToPrimitive;

    pub fn parse_decimal(value: &str) -> Option<Decimal> {
        Decimal::from_str(value).ok()
    }

    pub const MAX_DECIMAL_PRECISION: u8 = 28;

    pub const fn clone_decimal(value: &Decimal) -> Decimal {
        *value
    }

    pub fn fractional_digit_count(value: &Decimal) -> i64 {
        i64::from(value.scale())
    }

    pub const fn zero() -> Decimal {
        Decimal::ZERO
    }

    pub const fn one() -> Decimal {
        Decimal::ONE
    }

    pub fn from_minor_units(value: i128, scale: u32) -> Decimal {
        Decimal::from_i128_with_scale(value, scale)
    }

    pub fn try_from_scaled_units(value: i128, scale: u32) -> Option<Decimal> {
        Decimal::try_from_i128_with_scale(value, scale).ok()
    }

    pub fn round_dp_with_strategy(
        value: &Decimal,
        scale: u32,
        strategy: RoundingStrategy,
    ) -> Decimal {
        let strategy: RustRoundingStrategy = strategy.into();
        value.round_dp_with_strategy(scale, strategy)
    }

    pub fn to_plain_string(value: &Decimal) -> String {
        value.to_string()
    }

    pub fn checked_add(lhs: &Decimal, rhs: &Decimal) -> Option<Decimal> {
        lhs.checked_add(*rhs)
    }

    pub fn checked_sub(lhs: &Decimal, rhs: &Decimal) -> Option<Decimal> {
        lhs.checked_sub(*rhs)
    }

    pub fn checked_mul(lhs: &Decimal, rhs: &Decimal) -> Option<Decimal> {
        lhs.checked_mul(*rhs)
    }

    pub fn checked_div(lhs: &Decimal, rhs: &Decimal) -> Option<Decimal> {
        lhs.checked_div(*rhs)
    }

    pub fn try_to_scaled_units(value: &Decimal, target_scale: u32) -> Option<i128> {
        rust_decimal_to_scaled_units(value, target_scale)
    }

    pub fn try_to_i128_mantissa(value: &Decimal, target_scale: u32) -> Option<i128> {
        rust_decimal_to_i128_mantissa(value, target_scale)
    }

    impl From<RoundingStrategy> for RustRoundingStrategy {
        fn from(value: RoundingStrategy) -> Self {
            match value {
                RoundingStrategy::MidpointNearestEven => Self::MidpointNearestEven,
                RoundingStrategy::MidpointAwayFromZero => Self::MidpointAwayFromZero,
                RoundingStrategy::MidpointTowardZero => Self::MidpointTowardZero,
                RoundingStrategy::ToZero => Self::ToZero,
                RoundingStrategy::AwayFromZero => Self::AwayFromZero,
                RoundingStrategy::ToNegativeInfinity => Self::ToNegativeInfinity,
                RoundingStrategy::ToPositiveInfinity => Self::ToPositiveInfinity,
            }
        }
    }
}

#[cfg(feature = "bigdecimal")]
mod backend {
    use super::{DECIMAL128_PRECISION, FromStr, RoundingStrategy};

    pub use bigdecimal::BigDecimal as Decimal;
    use bigdecimal::RoundingMode;
    use num_bigint::BigInt;
    pub use num_traits::ToPrimitive;
    use num_traits::{One, Zero};

    pub fn parse_decimal(value: &str) -> Option<Decimal> {
        Decimal::from_str(value).ok()
    }

    pub const MAX_DECIMAL_PRECISION: u8 = u8::MAX;

    pub fn clone_decimal(value: &Decimal) -> Decimal {
        value.clone()
    }

    pub fn fractional_digit_count(value: &Decimal) -> i64 {
        value.fractional_digit_count()
    }

    pub fn zero() -> Decimal {
        Decimal::zero()
    }

    pub fn one() -> Decimal {
        Decimal::one()
    }

    pub fn from_minor_units(value: i128, scale: u32) -> Decimal {
        Decimal::new(BigInt::from(value), i64::from(scale))
    }

    #[expect(
        clippy::unnecessary_wraps,
        reason = "bigdecimal accepts every i128 coefficient and u32 scale, but the backend API mirrors rust_decimal"
    )]
    pub fn try_from_scaled_units(value: i128, scale: u32) -> Option<Decimal> {
        Some(Decimal::new(BigInt::from(value), i64::from(scale)))
    }

    pub fn round_dp_with_strategy(
        value: &Decimal,
        scale: u32,
        strategy: RoundingStrategy,
    ) -> Decimal {
        let mode = match strategy {
            RoundingStrategy::MidpointNearestEven => RoundingMode::HalfEven,
            RoundingStrategy::MidpointAwayFromZero => RoundingMode::HalfUp,
            RoundingStrategy::MidpointTowardZero => RoundingMode::HalfDown,
            RoundingStrategy::ToZero => RoundingMode::Down,
            RoundingStrategy::AwayFromZero => RoundingMode::Up,
            RoundingStrategy::ToNegativeInfinity => RoundingMode::Floor,
            RoundingStrategy::ToPositiveInfinity => RoundingMode::Ceiling,
        };

        value.with_scale_round(i64::from(scale), mode)
    }

    pub fn to_plain_string(value: &Decimal) -> String {
        value.to_plain_string()
    }

    #[expect(
        clippy::unnecessary_wraps,
        reason = "bigdecimal addition cannot overflow here, but the backend API mirrors rust_decimal checked arithmetic"
    )]
    pub fn checked_add(lhs: &Decimal, rhs: &Decimal) -> Option<Decimal> {
        Some(lhs + rhs)
    }

    #[expect(
        clippy::unnecessary_wraps,
        reason = "bigdecimal subtraction cannot overflow here, but the backend API mirrors rust_decimal checked arithmetic"
    )]
    pub fn checked_sub(lhs: &Decimal, rhs: &Decimal) -> Option<Decimal> {
        Some(lhs - rhs)
    }

    #[expect(
        clippy::unnecessary_wraps,
        reason = "bigdecimal multiplication cannot overflow here, but the backend API mirrors rust_decimal checked arithmetic"
    )]
    pub fn checked_mul(lhs: &Decimal, rhs: &Decimal) -> Option<Decimal> {
        Some(lhs * rhs)
    }

    pub fn checked_div(lhs: &Decimal, rhs: &Decimal) -> Option<Decimal> {
        if rhs.is_zero() {
            return None;
        }

        Some(lhs / rhs)
    }

    pub fn try_to_scaled_units(value: &Decimal, target_scale: u32) -> Option<i128> {
        let (mantissa, source_scale) = value.as_bigint_and_exponent();
        if mantissa.is_zero() {
            return Some(0);
        }

        let target_scale = i64::from(target_scale);
        let units = match source_scale.cmp(&target_scale) {
            std::cmp::Ordering::Equal => mantissa,
            std::cmp::Ordering::Less => {
                let diff = u32::try_from(target_scale - source_scale).ok()?;
                mantissa * BigInt::from(10_u8).pow(diff)
            }
            std::cmp::Ordering::Greater => {
                let diff = u32::try_from(source_scale - target_scale).ok()?;
                let divisor = BigInt::from(10_u8).pow(diff);
                if (&mantissa % &divisor) != BigInt::zero() {
                    return None;
                }
                mantissa / divisor
            }
        };

        i128::try_from(units).ok()
    }

    pub fn try_to_i128_mantissa(value: &Decimal, target_scale: u32) -> Option<i128> {
        if target_scale > DECIMAL128_PRECISION {
            return None;
        }

        let target = i64::from(target_scale);
        let rescaled = value.with_scale_round(target, bigdecimal::RoundingMode::HalfEven);
        if rescaled.digits() > 38 {
            return None;
        }
        let (bigint, _) = rescaled.into_bigint_and_exponent();
        i128::try_from(bigint).ok()
    }
}

pub use backend::{Decimal, ToPrimitive};

const DECIMAL128_PRECISION: u32 = 38;
const MAX_I128_MANTISSA: i128 = 10_i128.pow(DECIMAL128_PRECISION);

#[cfg(not(feature = "bigdecimal"))]
fn rust_decimal_to_scaled_units(value: &rust_decimal::Decimal, target_scale: u32) -> Option<i128> {
    let source_scale = value.scale();
    let mantissa = value.mantissa();
    match source_scale.cmp(&target_scale) {
        std::cmp::Ordering::Equal => Some(mantissa),
        std::cmp::Ordering::Less => {
            let diff = target_scale - source_scale;
            let pow = 10_i128.checked_pow(diff)?;
            mantissa.checked_mul(pow)
        }
        std::cmp::Ordering::Greater => {
            let diff = source_scale - target_scale;
            let pow = 10_i128.checked_pow(diff)?;
            if mantissa % pow != 0 {
                return None;
            }
            Some(mantissa / pow)
        }
    }
}

fn rust_decimal_to_i128_mantissa(value: &rust_decimal::Decimal, target_scale: u32) -> Option<i128> {
    if target_scale > DECIMAL128_PRECISION {
        return None;
    }

    let source_scale = value.scale();
    let mantissa: i128 = value.mantissa();
    let rescaled = match source_scale.cmp(&target_scale) {
        std::cmp::Ordering::Equal => mantissa,
        std::cmp::Ordering::Less => {
            let diff = target_scale - source_scale;
            let pow = 10_i128.checked_pow(diff)?;
            mantissa.checked_mul(pow)?
        }
        std::cmp::Ordering::Greater => {
            let diff = source_scale - target_scale;
            let pow = 10_i128.checked_pow(diff)?.cast_unsigned();
            let neg = mantissa < 0;
            let abs = mantissa.unsigned_abs();
            let q = (abs / pow).cast_signed();
            let r = abs % pow;
            let half = pow / 2;
            let rounded = match r.cmp(&half) {
                std::cmp::Ordering::Greater => q + 1,
                std::cmp::Ordering::Less => q,
                std::cmp::Ordering::Equal => q + (q & 1),
            };
            if neg { -rounded } else { rounded }
        }
    };
    if rescaled.unsigned_abs() >= MAX_I128_MANTISSA.cast_unsigned() {
        return None;
    }
    Some(rescaled)
}

/// Maximum fractional precision supported by the active decimal backend.
pub const MAX_DECIMAL_PRECISION: u8 = backend::MAX_DECIMAL_PRECISION;

/// Returns the maximum fractional precision supported by the active decimal backend.
#[must_use]
pub const fn max_decimal_precision() -> u8 {
    backend::MAX_DECIMAL_PRECISION
}

/// Clones a decimal value using the active backend's cheapest owned-value path.
#[must_use]
#[cfg_attr(
    not(feature = "bigdecimal"),
    expect(
        clippy::missing_const_for_fn,
        reason = "the public helper stays non-const because bigdecimal cloning is not const"
    )
)]
pub fn clone_decimal(value: &Decimal) -> Decimal {
    backend::clone_decimal(value)
}

/// Returns the number of fractional digits represented by the active backend.
#[must_use]
pub fn fractional_digit_count(value: &Decimal) -> i64 {
    backend::fractional_digit_count(value)
}

/// Adds two decimals if the active backend can represent the result.
#[must_use]
pub fn checked_add(lhs: &Decimal, rhs: &Decimal) -> Option<Decimal> {
    backend::checked_add(lhs, rhs)
}

/// Subtracts two decimals if the active backend can represent the result.
#[must_use]
pub fn checked_sub(lhs: &Decimal, rhs: &Decimal) -> Option<Decimal> {
    backend::checked_sub(lhs, rhs)
}

/// Multiplies two decimals if the active backend can represent the result.
#[must_use]
pub fn checked_mul(lhs: &Decimal, rhs: &Decimal) -> Option<Decimal> {
    backend::checked_mul(lhs, rhs)
}

/// Divides two decimals if the active backend can represent the result.
#[must_use]
pub fn checked_div(lhs: &Decimal, rhs: &Decimal) -> Option<Decimal> {
    backend::checked_div(lhs, rhs)
}

/// Encodes decimal-like values into Polars-compatible decimal128 mantissas.
pub trait Decimal128Mantissa {
    /// Returns the mantissa after rescaling to `target_scale`, or `None` when
    /// the result exceeds decimal128 precision or the active backend cannot
    /// represent the conversion.
    fn try_to_i128_mantissa(&self, target_scale: u32) -> Option<i128>;
}

impl Decimal128Mantissa for Decimal {
    fn try_to_i128_mantissa(&self, target_scale: u32) -> Option<i128> {
        backend::try_to_i128_mantissa(self, target_scale)
    }
}

#[cfg(feature = "bigdecimal")]
impl Decimal128Mantissa for rust_decimal::Decimal {
    fn try_to_i128_mantissa(&self, target_scale: u32) -> Option<i128> {
        rust_decimal_to_i128_mantissa(self, target_scale)
    }
}

impl Decimal128Mantissa for NonNegativeDecimal {
    fn try_to_i128_mantissa(&self, target_scale: u32) -> Option<i128> {
        self.as_decimal().try_to_i128_mantissa(target_scale)
    }
}

impl Decimal128Mantissa for PositiveDecimal {
    fn try_to_i128_mantissa(&self, target_scale: u32) -> Option<i128> {
        self.as_decimal().try_to_i128_mantissa(target_scale)
    }
}

impl Decimal128Mantissa for Ratio {
    fn try_to_i128_mantissa(&self, target_scale: u32) -> Option<i128> {
        self.as_decimal().try_to_i128_mantissa(target_scale)
    }
}

/// Parses a plain decimal string using the active backend.
///
/// Surrounding whitespace is ignored, an optional leading sign is accepted, and
/// scientific notation, digit separators, and internal whitespace are rejected
/// so both decimal backends share identical parsing semantics.
#[must_use]
pub fn parse_decimal(value: &str) -> Option<Decimal> {
    let normalized = normalize_decimal_literal(value)?;
    backend::parse_decimal(&normalized)
}

fn normalize_decimal_literal(value: &str) -> Option<Cow<'_, str>> {
    let trimmed = value.trim();
    if trimmed.is_empty() {
        return None;
    }

    let (sign, unsigned) = match trimmed.as_bytes().first() {
        Some(b'+') => ("", &trimmed[1..]),
        Some(b'-') => ("-", &trimmed[1..]),
        Some(_) => ("", trimmed),
        None => return None,
    };

    if unsigned.is_empty() {
        return None;
    }

    let mut seen_dot = false;
    let mut seen_digit = false;
    for byte in unsigned.bytes() {
        match byte {
            b'0'..=b'9' => seen_digit = true,
            b'.' if !seen_dot => seen_dot = true,
            _ => return None,
        }
    }

    if !seen_digit {
        return None;
    }

    let needs_leading_zero = unsigned.starts_with('.');
    let needs_trailing_zero = unsigned.ends_with('.');
    if needs_leading_zero || needs_trailing_zero {
        let mut normalized = String::with_capacity(trimmed.len() + 2);
        normalized.push_str(sign);
        if needs_leading_zero {
            normalized.push('0');
        }
        normalized.push_str(unsigned);
        if needs_trailing_zero {
            normalized.push('0');
        }
        Some(Cow::Owned(normalized))
    } else if sign == "-" {
        Some(Cow::Borrowed(trimmed))
    } else {
        Some(Cow::Borrowed(unsigned))
    }
}

/// Returns the zero value for the active decimal backend.
#[must_use]
#[cfg_attr(
    not(feature = "bigdecimal"),
    expect(
        clippy::missing_const_for_fn,
        reason = "the public helper stays non-const because bigdecimal zero construction is not const"
    )
)]
pub fn zero() -> Decimal {
    backend::zero()
}

/// Returns the one value for the active decimal backend.
#[must_use]
#[cfg_attr(
    not(feature = "bigdecimal"),
    expect(
        clippy::missing_const_for_fn,
        reason = "the public helper stays non-const because bigdecimal one construction is not const"
    )
)]
pub fn one() -> Decimal {
    backend::one()
}

/// Builds a decimal from an integer count of minor units and the provided scale.
///
/// # Panics
///
/// With the default backend, panics when the scale or integer coefficient cannot
/// be represented by `rust_decimal`. Use [`try_from_scaled_units`] when the input
/// is not already known to fit the active backend.
#[must_use]
pub fn from_minor_units(value: i128, scale: u32) -> Decimal {
    backend::from_minor_units(value, scale)
}

/// Builds a decimal from an integer coefficient and scale if the active backend can represent it.
///
/// Returns `None` when the default backend rejects either the scale or the
/// 96-bit mantissa. The `bigdecimal` backend accepts every `i128` coefficient
/// and `u32` scale.
#[must_use]
pub fn try_from_scaled_units(value: i128, scale: u32) -> Option<Decimal> {
    backend::try_from_scaled_units(value, scale)
}

/// Converts a decimal into exact base-10 scaled integer units.
///
/// Returns `None` when converting to `target_scale` would require rounding or
/// when the exact scaled unit count cannot be stored in `i128`.
#[must_use]
pub fn try_to_scaled_units(value: &Decimal, target_scale: u32) -> Option<i128> {
    backend::try_to_scaled_units(value, target_scale)
}

/// Rounds a decimal to the requested scale using a rounding strategy.
#[must_use]
pub fn round_dp_with_strategy(value: &Decimal, scale: u32, strategy: RoundingStrategy) -> Decimal {
    backend::round_dp_with_strategy(value, scale, strategy)
}

/// Serde helpers for backend-stable decimal wire formats.
///
/// These modules serialize decimals as canonical strings rendered by
/// [`to_canonical_string`] and deserialize with [`parse_decimal`], avoiding
/// backend-native differences such as scale preservation or exponent output.
pub mod serde {
    use super::{Cow, Decimal};
    use serde::{Deserialize, Serializer, de};

    fn invalid_decimal<E>(value: &str) -> E
    where
        E: de::Error,
    {
        E::custom(format_args!("invalid decimal string `{value}`"))
    }

    /// Serde adapter for a required canonical decimal string.
    pub mod canonical_str {
        use super::{Cow, Decimal, Deserialize, Serializer, invalid_decimal};
        use crate::{parse_decimal, to_canonical_string};

        /// Serializes a decimal as a canonical string.
        ///
        /// # Errors
        /// Returns the serializer error when writing the string fails.
        pub fn serialize<S>(value: &Decimal, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            serializer.serialize_str(&to_canonical_string(value))
        }

        /// Deserializes a decimal from a string accepted by [`crate::parse_decimal`].
        ///
        /// # Errors
        /// Returns the deserializer error when the input is not a string or
        /// when [`crate::parse_decimal`] rejects the string.
        pub fn deserialize<'de, D>(deserializer: D) -> Result<Decimal, D::Error>
        where
            D: ::serde::Deserializer<'de>,
        {
            let value = Cow::<str>::deserialize(deserializer)?;
            parse_decimal(&value).ok_or_else(|| invalid_decimal(&value))
        }
    }

    /// Serde adapter for an optional canonical decimal string.
    pub mod option_canonical_str {
        use super::{Cow, Decimal, Deserialize, Serializer, invalid_decimal};
        use crate::{parse_decimal, to_canonical_string};
        use serde::Serialize;

        /// Serializes an optional decimal as a canonical string or `null`.
        ///
        /// # Errors
        /// Returns the serializer error when writing the option fails.
        pub fn serialize<S>(value: &Option<Decimal>, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            let canonical = value.as_ref().map(to_canonical_string);
            canonical.serialize(serializer)
        }

        /// Deserializes an optional decimal from strings accepted by [`crate::parse_decimal`].
        ///
        /// # Errors
        /// Returns the deserializer error when the input is not `null` or a
        /// string, or when [`crate::parse_decimal`] rejects the string.
        pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Decimal>, D::Error>
        where
            D: ::serde::Deserializer<'de>,
        {
            Option::<Cow<'de, str>>::deserialize(deserializer)?
                .map(|value| parse_decimal(&value).ok_or_else(|| invalid_decimal(&value)))
                .transpose()
        }
    }
}

/// Converts a decimal into a canonical string without scientific notation and
/// without gratuitous trailing zeros.
#[must_use]
pub fn to_canonical_string(value: &Decimal) -> String {
    let zero = zero();
    if value == &zero {
        return "0".to_owned();
    }

    let mut repr = backend::to_plain_string(value);
    if let Some(dot) = repr.find('.') {
        let mut end = repr.len();
        while end > dot + 1 && repr.as_bytes()[end - 1] == b'0' {
            end -= 1;
        }
        if end == dot + 1 {
            end -= 1;
        }
        repr.truncate(end);
    }
    repr
}

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use super::{
        Decimal, RoundingStrategy, checked_div, parse_decimal, round_dp_with_strategy,
        to_canonical_string, try_from_scaled_units, try_to_scaled_units,
    };

    #[test]
    fn parse_rejects_scientific_notation() {
        assert!(parse_decimal("1e3").is_none());
        assert!(parse_decimal("2E-3").is_none());
    }

    #[test]
    fn parse_accepts_standard_forms() {
        assert_eq!(
            parse_decimal("  +123.4500 ").unwrap(),
            parse_decimal("123.45").unwrap()
        );
        assert_eq!(
            parse_decimal("-42.1").unwrap(),
            Decimal::from_str("-42.1").unwrap()
        );
    }

    #[test]
    fn parse_uses_backend_stable_plain_decimal_grammar() {
        for (literal, canonical) in [
            (".5", "0.5"),
            ("1.", "1"),
            ("+1", "1"),
            ("-0.00", "0"),
            ("001.2300", "1.23"),
            (" \t\n+001.2300\r", "1.23"),
        ] {
            let parsed = parse_decimal(literal).unwrap_or_else(|| panic!("{literal} should parse"));
            assert_eq!(to_canonical_string(&parsed), canonical);
        }
    }

    #[test]
    fn parse_rejects_non_plain_decimal_grammar() {
        for literal in [
            "", " ", "+", "-", ".", "+.", "-.", "+-1", "++1", "--1", "1_000", "1e3", "2E-3", "1 2",
            "1.2.3",
        ] {
            assert!(parse_decimal(literal).is_none(), "{literal} should fail");
        }
    }

    #[test]
    fn parse_rejects_duplicate_explicit_signs() {
        assert!(parse_decimal("+-1").is_none());
        assert!(parse_decimal("++1").is_none());
        assert!(parse_decimal("+").is_none());
        assert!(parse_decimal("+1").is_some());
        assert!(parse_decimal("-1").is_some());
    }

    #[test]
    fn canonical_string_trims_trailing_zeros() {
        let value = parse_decimal("123.4500").unwrap();
        assert_eq!(to_canonical_string(&value), "123.45");
        let integer = parse_decimal("1000").unwrap();
        assert_eq!(to_canonical_string(&integer), "1000");
    }

    #[test]
    fn canonical_string_normalizes_zero_sign() {
        let negative_zero = parse_decimal("-0.00").unwrap();
        assert_eq!(to_canonical_string(&negative_zero), "0");

        let rounded_negative_zero = round_dp_with_strategy(
            &parse_decimal("-0.0049").unwrap(),
            2,
            RoundingStrategy::ToZero,
        );
        assert_eq!(to_canonical_string(&rounded_negative_zero), "0");
    }

    #[test]
    fn checked_div_returns_none_for_zero_divisor() {
        let lhs = parse_decimal("10").unwrap();
        let zero = parse_decimal("0.00").unwrap();
        assert!(checked_div(&lhs, &zero).is_none());

        let two = parse_decimal("2").unwrap();
        let quotient = checked_div(&lhs, &two).unwrap();
        assert_eq!(to_canonical_string(&quotient), "5");
    }

    #[test]
    fn canonical_decimal_serde_uses_strings() {
        #[derive(::serde::Serialize, ::serde::Deserialize, PartialEq, Debug)]
        struct Payload {
            #[serde(with = "crate::serde::canonical_str")]
            value: Decimal,
            #[serde(default, with = "crate::serde::option_canonical_str")]
            optional: Option<Decimal>,
        }

        let payload = Payload {
            value: parse_decimal("123.4500").unwrap(),
            optional: Some(parse_decimal("0.5000").unwrap()),
        };

        let value = serde_json::to_value(&payload).unwrap();
        assert_eq!(value["value"], serde_json::json!("123.45"));
        assert_eq!(value["optional"], serde_json::json!("0.5"));
        assert_eq!(serde_json::from_value::<Payload>(value).unwrap(), payload);

        let missing_optional = serde_json::json!({ "value": "+1.2300" });
        let parsed = serde_json::from_value::<Payload>(missing_optional).unwrap();
        assert_eq!(to_canonical_string(&parsed.value), "1.23");
        assert_eq!(parsed.optional, None);
    }

    #[test]
    fn try_from_scaled_units_accepts_representable_values() {
        let value = try_from_scaled_units(123_456, 3).unwrap();
        assert_eq!(to_canonical_string(&value), "123.456");
    }

    #[test]
    fn try_to_scaled_units_accepts_exact_values() {
        let value = parse_decimal("123.4560").unwrap();
        assert_eq!(try_to_scaled_units(&value, 6), Some(123_456_000));
        assert_eq!(try_to_scaled_units(&value, 3), Some(123_456));

        let negative = parse_decimal("-1.25").unwrap();
        assert_eq!(try_to_scaled_units(&negative, 2), Some(-125));
    }

    #[test]
    fn try_to_scaled_units_rejects_inexact_values_instead_of_rounding() {
        let above_half = parse_decimal("1.250001").unwrap();
        assert_eq!(try_to_scaled_units(&above_half, 1), None);

        let tie = parse_decimal("1.25").unwrap();
        assert_eq!(try_to_scaled_units(&tie, 1), None);
    }

    #[cfg(not(feature = "bigdecimal"))]
    #[test]
    fn try_from_scaled_units_rejects_rust_decimal_limits() {
        assert!(try_from_scaled_units(i128::MAX, 0).is_none());
        assert!(try_from_scaled_units(1, 29).is_none());
    }
}