fpdec/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: 2023-12-10T21:29:34+01:00 $
9
10use core::fmt::{Debug, Display, Formatter};
11
12/// An error which can be returned from converting numbers to `Decimal` or
13/// from binary operators on `Decimal`.
14///
15/// This error is used as the error type for the `TryFrom` implementation of
16/// `Decimal`. It is also used when the implementations of the numerical
17/// operators panic.
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub enum DecimalError {
20 /// More than [MAX_N_FRAC_DIGITS](crate::MAX_N_FRAC_DIGITS) fractional
21 /// decimal digits requested.
22 MaxNFracDigitsExceeded,
23 /// The result would exceed the internal representation of `Decimal`.
24 InternalOverflow,
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 #[must_use]
36 pub const fn _description(&self) -> &str {
37 match self {
38 Self::MaxNFracDigitsExceeded => {
39 "More than MAX_N_FRAC_DIGITS fractional decimal digits \
40 requested."
41 }
42 Self::InternalOverflow => "Internal representation exceeded.",
43 Self::InfiniteValue => "Can't convert infinite value to Decimal.",
44 Self::NotANumber => "Given value is not a number.",
45 Self::DivisionByZero => "Division by Zero.",
46 }
47 }
48}
49
50impl Display for DecimalError {
51 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
52 Display::fmt(self._description(), f)
53 }
54}
55
56#[cfg(feature = "std")]
57impl std::error::Error for DecimalError {}
58
59/// An error which can be returned from converting `Decimal` values to ints
60/// or floats.
61///
62/// This error is used as the error type for the `TryFrom<Decimal>`
63/// implementation of the primitive int and float types.
64#[derive(Debug, Clone, PartialEq, Eq)]
65pub enum TryFromDecimalError {
66 /// Given value does not represent an int.
67 NotAnIntValue,
68 /// Given value exceeds the range of the target value.
69 ValueOutOfRange,
70}
71
72impl TryFromDecimalError {
73 #[doc(hidden)]
74 #[must_use]
75 pub const fn _description(&self) -> &str {
76 match self {
77 Self::NotAnIntValue => "Given value does not represent an int.",
78 Self::ValueOutOfRange => {
79 "Given value exceeds the range of the target value."
80 }
81 }
82 }
83}
84
85impl Display for TryFromDecimalError {
86 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
87 Display::fmt(self._description(), f)
88 }
89}
90
91#[cfg(feature = "std")]
92impl std::error::Error for TryFromDecimalError {}