1use crate::Decimal;
2use alloc::string::String;
3use core::fmt;
4
5#[derive(Clone, Debug, PartialEq)]
7pub enum Error {
8 ErrorString(String),
13 ExceedsMaximumPossibleValue,
15 LessThanMinimumPossibleValue,
17 Underflow,
19 ScaleExceedsMaximumPrecision(u32),
21 ConversionTo(String),
24}
25
26impl<S> From<S> for Error
27where
28 S: Into<String>,
29{
30 #[inline]
31 fn from(from: S) -> Self {
32 Self::ErrorString(from.into())
33 }
34}
35
36#[cold]
37pub(crate) fn tail_error(from: &'static str) -> Result<Decimal, Error> {
38 Err(from.into())
39}
40
41#[cfg(feature = "std")]
42impl std::error::Error for Error {}
43
44impl fmt::Display for Error {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 match *self {
47 Self::ErrorString(ref err) => f.pad(err),
48 Self::ExceedsMaximumPossibleValue => {
49 write!(f, "Number exceeds maximum value that can be represented.")
50 }
51 Self::LessThanMinimumPossibleValue => {
52 write!(f, "Number less than minimum value that can be represented.")
53 }
54 Self::Underflow => {
55 write!(f, "Number has a high precision that can not be represented.")
56 }
57 Self::ScaleExceedsMaximumPrecision(ref scale) => {
58 write!(
59 f,
60 "Scale exceeds the maximum precision allowed: {scale} > {}",
61 Decimal::MAX_SCALE
62 )
63 }
64 Self::ConversionTo(ref type_name) => {
65 write!(f, "Error while converting to {type_name}")
66 }
67 }
68 }
69}