rust_fixed_point_decimal/errors.rs
1// ---------------------------------------------------------------------------
2// Copyright: (c) 2021 ff. Michael Amrhein (michael@adrhinum.de)
3// License: This program is part of a larger application. For license
4// details please read the file LICENSE.TXT provided together
5// with the application.
6// ---------------------------------------------------------------------------
7// $Source: src/errors.rs $
8// $Revision: 2021-10-29T12:14:58+02:00 $
9
10use std::fmt::{Debug, Display, Formatter};
11
12/// An error which can be returned from converting numbers to Decimal or from
13/// binary operators on Decimal.
14///
15/// This error is used as the error type for the [`TryFrom`] implementation of
16/// `Decimal<P>`. It is also used when the implementations of the numerical
17/// operators panic.
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub enum DecimalError {
20 /// The precise result would have more than [crate::MAX_PREC] fractional
21 /// decimal digits.
22 PrecLimitExceeded,
23 /// The result would exceed the maximum value representable by the type.
24 MaxValueExceeded,
25 /// Attempt to convert an infinite value to `Decimal`.
26 InfiniteValue,
27 /// Attempt to convert a 'not-a-number' value to a `Decimal`.
28 NotANumber,
29 /// A division op called with a divisor equal to zero.
30 DivisionByZero,
31}
32
33impl DecimalError {
34 #[doc(hidden)]
35 pub fn _description(&self) -> &str {
36 match self {
37 DecimalError::PrecLimitExceeded => {
38 "Result exceeds the precision limit."
39 }
40 DecimalError::MaxValueExceeded => {
41 "Maximum representable value exceeded."
42 }
43 DecimalError::InfiniteValue => {
44 "Can't convert infinite value to Decimal."
45 }
46 DecimalError::NotANumber => "Given value is not a number.",
47 DecimalError::DivisionByZero => "Division by Zero.",
48 }
49 }
50}
51
52impl Display for DecimalError {
53 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
54 Display::fmt(self._description(), f)
55 }
56}
57
58impl std::error::Error for DecimalError {}