decimal_rs/
error.rs

1// Copyright 2021 CoD Technologies Corp.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Decimal error definitions.
16
17use std::fmt;
18use std::num::ParseFloatError;
19
20/// An error which can be returned when parsing a decimal.
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub enum DecimalParseError {
23    /// Empty string.
24    Empty,
25    /// Invalid decimal.
26    Invalid,
27    /// Decimal is overflowed.
28    Overflow,
29    /// Decimal is underflow.
30    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/// An error which can be returned when a conversion between other type and decimal fails.
46#[derive(Clone, Debug, Eq, PartialEq)]
47pub enum DecimalConvertError {
48    /// Invalid decimal.
49    Invalid,
50    /// Decimal is overflowed.
51    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/// An error which can be returned when format decimal to string.
65#[derive(Clone, Debug, Eq, PartialEq)]
66pub enum DecimalFormatError {
67    /// std::fmt::Error
68    Format(fmt::Error),
69    /// Decimal is out of range.
70    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}