decimal_scaled/support/error.rs
1// SPDX-FileCopyrightText: 2026 John Moxley
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Error types used across the decimal-scaled crate.
5//!
6//! Two enums live here:
7//!
8//! - [`ConvertError`] — returned by fallible `TryFrom` impls between
9//! primitive types (`i128`, `u128`, `f32`, `f64`) and any decimal
10//! width. Distinguishes overflow from non-finite float input.
11//! - [`ParseError`] — returned by `FromStr` when the input is not a
12//! valid canonical decimal literal. One variant per failure mode so
13//! callers can surface a precise diagnostic.
14//!
15//! Both types are width-neutral — the same enum is returned by every
16//! width (D18, D38, D57, D76, D115, D153, D230, D307, D462, D616, D924, D1232).
17
18/// Error returned by the fallible [`TryFrom`] impls.
19///
20/// Covers the two distinct failure modes:
21/// - [`ConvertError::Overflow`] — the input, after scaling by
22/// `10^SCALE`, exceeds the destination's representable range.
23/// - [`ConvertError::NotFinite`] — the float input is `NaN`,
24/// `+inf`, or `-inf`.
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
26pub enum ConvertError {
27 /// Input magnitude is outside the destination type's range after scaling.
28 Overflow,
29 /// Input is `NaN`, `+inf`, or `-inf` (only reachable from the
30 /// `TryFrom<f32>` / `TryFrom<f64>` impls).
31 NotFinite,
32}
33
34impl core::fmt::Display for ConvertError {
35 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36 match self {
37 Self::Overflow => f.write_str("decimal conversion overflow"),
38 Self::NotFinite => f.write_str("decimal conversion from non-finite float"),
39 }
40 }
41}
42
43#[cfg(feature = "std")]
44impl std::error::Error for ConvertError {}
45
46/// Error returned by `FromStr` when the input is not a valid canonical
47/// decimal literal.
48///
49/// Each variant identifies one specific failure mode so callers can
50/// surface a precise diagnostic.
51#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
52pub enum ParseError {
53 /// Input string is empty.
54 Empty,
55 /// Input is `-` or `+` with no following digits.
56 SignOnly,
57 /// Integer part has a redundant leading zero (e.g. `01.5`).
58 LeadingZero,
59 /// Fractional part has more digits than `SCALE`.
60 OverlongFractional,
61 /// Input uses scientific notation (`1e3`); not accepted.
62 ScientificNotation,
63 /// Input contains a character that is not a digit, sign, or dot.
64 InvalidChar,
65 /// Parsed value exceeds the destination type's range after scaling.
66 OutOfRange,
67 /// Decimal point with no digit on one side (e.g. `.5` or `5.`).
68 MissingDigits,
69}
70
71impl core::fmt::Display for ParseError {
72 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
73 let msg = match self {
74 Self::Empty => "empty input",
75 Self::SignOnly => "sign with no digits",
76 Self::LeadingZero => "redundant leading zero in integer part",
77 Self::OverlongFractional => "fractional part exceeds SCALE digits",
78 Self::ScientificNotation => "scientific notation not accepted",
79 Self::InvalidChar => "invalid character",
80 Self::OutOfRange => "value out of representable range",
81 Self::MissingDigits => "decimal point with no adjacent digits",
82 };
83 f.write_str(msg)
84 }
85}
86
87#[cfg(feature = "std")]
88impl std::error::Error for ParseError {}