Skip to main content

reliakit_primitives/
error.rs

1use core::fmt;
2
3/// Error returned when a primitive value fails validation.
4#[non_exhaustive]
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum PrimitiveError {
7    /// The value was empty or contained only whitespace.
8    Empty,
9    /// The value was shorter than the minimum allowed length.
10    TooShort { min: usize, actual: usize },
11    /// The value was longer than the maximum allowed length.
12    TooLong { max: usize, actual: usize },
13    /// The value was outside the inclusive allowed range.
14    OutOfRange { min: u128, max: u128, actual: u128 },
15    /// The value did not match the expected format or pattern.
16    Invalid { message: &'static str },
17}
18
19/// Result alias used by Reliakit primitive constructors.
20pub type PrimitiveResult<T> = Result<T, PrimitiveError>;
21
22impl fmt::Display for PrimitiveError {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        match self {
25            Self::Empty => f.write_str("value must not be empty"),
26            Self::TooShort { min, actual } => {
27                write!(
28                    f,
29                    "value is too short: minimum is {min}, actual is {actual}"
30                )
31            }
32            Self::TooLong { max, actual } => {
33                write!(f, "value is too long: maximum is {max}, actual is {actual}")
34            }
35            Self::OutOfRange { min, max, actual } => {
36                write!(
37                    f,
38                    "value is out of range: expected {min}..={max}, actual is {actual}"
39                )
40            }
41            Self::Invalid { message } => write!(f, "invalid value: {message}"),
42        }
43    }
44}
45
46#[cfg(feature = "std")]
47impl std::error::Error for PrimitiveError {}
48
49#[cfg(test)]
50mod tests {
51    use super::PrimitiveError;
52    use alloc::string::ToString;
53
54    #[test]
55    fn display_empty() {
56        assert_eq!(PrimitiveError::Empty.to_string(), "value must not be empty");
57    }
58
59    #[test]
60    fn display_too_short() {
61        assert_eq!(
62            PrimitiveError::TooShort { min: 3, actual: 1 }.to_string(),
63            "value is too short: minimum is 3, actual is 1"
64        );
65    }
66
67    #[test]
68    fn display_too_long() {
69        assert_eq!(
70            PrimitiveError::TooLong { max: 5, actual: 8 }.to_string(),
71            "value is too long: maximum is 5, actual is 8"
72        );
73    }
74
75    #[test]
76    fn display_out_of_range() {
77        assert_eq!(
78            PrimitiveError::OutOfRange {
79                min: 1,
80                max: 100,
81                actual: 200
82            }
83            .to_string(),
84            "value is out of range: expected 1..=100, actual is 200"
85        );
86    }
87
88    #[test]
89    fn display_invalid() {
90        assert_eq!(
91            PrimitiveError::Invalid {
92                message: "bad format"
93            }
94            .to_string(),
95            "invalid value: bad format"
96        );
97    }
98}