1use std::fmt;
18use std::num::ParseFloatError;
19
20#[derive(Debug, Clone, PartialEq, Eq)]
22pub enum DecimalParseError {
23 Empty,
25 Invalid,
27 Overflow,
29 Underflow,
31}
32
33impl fmt::Display for DecimalParseError {
34 #[inline]
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 match &self {
37 DecimalParseError::Empty => write!(f, "cannot parse number from empty string"),
38 DecimalParseError::Invalid => write!(f, "invalid number"),
39 DecimalParseError::Overflow => write!(f, "numeric overflow"),
40 DecimalParseError::Underflow => write!(f, "numeric underflow"),
41 }
42 }
43}
44
45#[derive(Clone, Debug, Eq, PartialEq)]
47pub enum DecimalConvertError {
48 Invalid,
50 Overflow,
52}
53
54impl fmt::Display for DecimalConvertError {
55 #[inline]
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 match &self {
58 DecimalConvertError::Invalid => write!(f, "invalid number"),
59 DecimalConvertError::Overflow => write!(f, "numeric overflow"),
60 }
61 }
62}
63
64#[derive(Clone, Debug, Eq, PartialEq)]
66pub enum DecimalFormatError {
67 Format(fmt::Error),
69 OutOfRange,
71}
72
73impl std::error::Error for DecimalFormatError {
74 #[inline]
75 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
76 match &self {
77 DecimalFormatError::Format(e) => Some(e),
78 DecimalFormatError::OutOfRange => None,
79 }
80 }
81}
82
83impl fmt::Display for DecimalFormatError {
84 #[inline]
85 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86 match &self {
87 DecimalFormatError::Format(e) => write!(f, "{}", e),
88 DecimalFormatError::OutOfRange => write!(f, "Data value out of range"),
89 }
90 }
91}
92
93impl From<DecimalParseError> for DecimalConvertError {
94 #[inline]
95 fn from(e: DecimalParseError) -> Self {
96 match e {
97 DecimalParseError::Empty | DecimalParseError::Invalid => DecimalConvertError::Invalid,
98 DecimalParseError::Overflow | DecimalParseError::Underflow => DecimalConvertError::Overflow,
99 }
100 }
101}
102
103impl From<ParseFloatError> for DecimalConvertError {
104 #[inline]
105 fn from(_: ParseFloatError) -> Self {
106 DecimalConvertError::Invalid
107 }
108}
109
110impl From<fmt::Error> for DecimalFormatError {
111 #[inline]
112 fn from(e: fmt::Error) -> Self {
113 DecimalFormatError::Format(e)
114 }
115}