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