devela/num/
error.rs

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// devela::num::error
//
//!
//

use crate::Sign;

#[doc = crate::TAG_RESULT!()]
/// A numeric-related result.
pub type NumResult<T> = crate::Result<T, NumError>;

#[doc = crate::TAG_ERROR_COMPOSITE!()]
/// A numeric-related error.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum NumError {
    /// The requested numerical functionality is not implemented.
    ///
    /// This is the default implementation of every numeric trait method.
    NotImplemented,

    /// The requested numerical functionality is not supported.
    NotSupported,

    /// Unspecified error.
    ///
    /// When no clearer error can be given.
    // RETHINK
    Unspecified,

    /// An invalid value was received for the given type or operation.
    Invalid,

    /// An inverse doesn't exist.
    NoInverse,

    /// The provided values are not compatible in size.
    MismatchedSizes,

    /// The given bounds are not compatible.
    IncompatibleBounds,

    /// A non-negative value is required.
    NonNegativeRequired,

    /// A positive value is required.
    PositiveRequired,

    /// A non-zero value is required.
    NonZeroRequired,

    /// An arithmetic overflow error, with an optional associated sign.
    Overflow(Option<Sign>),
}

#[allow(dead_code)]
impl NumError {
    pub(crate) const fn ni<T>() -> NumResult<T> {
        Err(NumError::NotImplemented)
    }
    pub(crate) const fn ns<T>() -> NumResult<T> {
        Err(NumError::NotSupported)
    }
}

mod core_impls {
    use crate::{Display, FmtResult, Formatter, NumError, Sign};

    impl crate::Error for NumError {}
    impl crate::ExtError for NumError {
        type Kind = ();
        fn error_eq(&self, other: &Self) -> bool {
            self == other
        }
        fn error_kind(&self) -> Self::Kind {}
    }

    impl Display for NumError {
        fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult<()> {
            use NumError as E;
            match self {
                E::NotImplemented => write!(f, "Not implemented."),
                E::NotSupported => write!(f, "Not supported."),
                E::Unspecified => write!(f, "Unspecified."),
                E::Invalid => write!(f, "Invalid value."),
                E::NoInverse => write!(f, "An inverse doesn't exist."),
                E::IncompatibleBounds => {
                    write!(f, "The given bounds are incompatible.")
                }
                E::MismatchedSizes => {
                    write!(f, "The provided values are not compatible in size.")
                }
                E::NonNegativeRequired => write!(f, "A non-negative value is required."),
                E::PositiveRequired => write!(f, "A positive value is required.."),
                E::NonZeroRequired => write!(f, "A non-zero value is required."),
                E::Overflow(sign) => {
                    if let Some(sign) = sign {
                        match sign {
                            Sign::Positive => write!(f, "Positive overflow."),
                            Sign::Negative => write!(f, "Negative overflow."),
                            Sign::None => write!(f, "Unsigned overflow."), // not meaningful
                        }
                    } else {
                        write!(f, "Overflow.")
                    }
                }
            }
        }
    }
}