Skip to main content

rstl_stack/
error.rs

1use core::fmt;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub enum StackError {
5    InvalidCapacity { capacity: usize },
6}
7
8impl fmt::Display for StackError {
9    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10        match self {
11            Self::InvalidCapacity { capacity } => {
12                write!(
13                    f,
14                    "[CircularStack] capacity is expected to be a positive integer, but got ({capacity})."
15                )
16            }
17        }
18    }
19}
20
21impl std::error::Error for StackError {}
22
23#[cfg(test)]
24mod tests {
25    use std::error::Error as StdError;
26
27    use super::StackError;
28
29    #[test]
30    fn display_should_format_invalid_capacity() {
31        let err = StackError::InvalidCapacity { capacity: 0 };
32
33        assert_eq!(
34            err.to_string(),
35            "[CircularStack] capacity is expected to be a positive integer, but got (0)."
36        );
37    }
38
39    #[test]
40    fn error_source_should_be_none() {
41        let err = StackError::InvalidCapacity { capacity: 1 };
42
43        assert!(StdError::source(&err).is_none());
44    }
45}