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
use std::fmt::Display;

/// Custom version of [Result], based on this [crate]'s [CrateError].
pub type CrateResult<T> = Result<T, CrateError>;

/// Custom error scenarios related to this [crate].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum CrateError {
    /// When a character does not represent a 0-9 digit.
    NonDigitChar(char),

    /// When a number does not represent a 0-9 digit.
    NonDigitNumber(u128),

    /// When trying conversion from a negative number.
    NegativeNumber(i128),

    /// When an operation causes a numeric overflow.
    Overflow,
}

impl Display for CrateError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NonDigitChar(non_digit_char) => write!(f, "Non-digit char: {}", non_digit_char),

            Self::NonDigitNumber(non_digit_number) => {
                write!(f, "Non-digit number: {}", non_digit_number)
            }

            Self::NegativeNumber(number) => {
                write!(f, "Cannot convert negative number: {}", number)
            }

            Self::Overflow => write!(f, "Overflow"),
        }
    }
}

impl std::error::Error for CrateError {}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq as eq;
    use speculate2::*;

    speculate! {
        describe "Converting CrateError to string" {
            describe "when a number is not a digit" {
                it "should describe it" {
                    eq!(CrateError::NonDigitNumber(90).to_string(), "Non-digit number: 90");
                }
            }

            describe "when a char is not a digit" {
                it "should describe it" {
                    eq!(CrateError::NonDigitChar('X').to_string(), "Non-digit char: X");
                }
            }

            describe "when trying from a negative number" {
                it "should describe it" {
                    eq!(CrateError::NegativeNumber(-90).to_string(), "Cannot convert negative number: -90");
                }
            }

            describe "when an overflow occurs" {
                it "should describe it" {
                    eq!(CrateError::Overflow.to_string(), "Overflow");
                }
            }
        }
    }
}