stefans_utils/
expect_length.rs

1use crate::{
2    comparator::Comparison, expect_comparison::ExpectedComparisonError, with_len::WithLen,
3};
4use core::{error::Error, fmt};
5
6pub trait ExpectLen: WithLen + Sized {
7    fn expect_len_lt(self, length: usize) -> Result<Self, ExpectedLenError>;
8    fn expect_len_lte(self, length: usize) -> Result<Self, ExpectedLenError>;
9    fn expect_len_eq(self, length: usize) -> Result<Self, ExpectedLenError>;
10    fn expect_len_ne(self, length: usize) -> Result<Self, ExpectedLenError>;
11    fn expect_len_gte(self, length: usize) -> Result<Self, ExpectedLenError>;
12    fn expect_len_gt(self, length: usize) -> Result<Self, ExpectedLenError>;
13
14    fn expect_non_empty(self) -> Result<Self, ExpectedLenError> {
15        self.expect_len_gt(0)
16    }
17    fn expect_empty(self) -> Result<Self, ExpectedLenError> {
18        self.expect_len_ne(0)
19    }
20    fn expect_len(self, length: usize) -> Result<Self, ExpectedLenError> {
21        self.expect_len_eq(length)
22    }
23}
24
25#[derive(fmt::Debug, Clone, Copy, PartialEq, Eq)]
26#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
27#[cfg_attr(feature = "schemars", derive(::schemars::JsonSchema))]
28pub struct ExpectedLenError {
29    pub comparison: Comparison,
30    pub comparator: usize,
31    pub actual: usize,
32}
33
34impl From<ExpectedComparisonError<usize>> for ExpectedLenError {
35    fn from(
36        ExpectedComparisonError {
37            comparison,
38            comparator,
39            actual,
40        }: ExpectedComparisonError<usize>,
41    ) -> Self {
42        ExpectedLenError {
43            comparison,
44            comparator,
45            actual,
46        }
47    }
48}
49
50impl Error for ExpectedLenError {}
51
52impl fmt::Display for ExpectedLenError {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        f.write_fmt(format_args!(
55            "Expected length to be {} {}, but it was {}",
56            self.comparison, self.comparator, self.actual
57        ))
58    }
59}
60
61impl Comparison {
62    pub fn expect_len_comparison<T: WithLen + Sized>(
63        self,
64        with_len: T,
65        length_comparator: usize,
66    ) -> Result<T, ExpectedLenError> {
67        self.expect_comparison(with_len.len(), length_comparator)
68            .map_err(ExpectedLenError::from)
69            .map(|_| with_len)
70    }
71}
72
73impl<T: WithLen + Sized> ExpectLen for T {
74    fn expect_len_lt(self, length: usize) -> Result<Self, ExpectedLenError> {
75        Comparison::Less.expect_len_comparison(self, length)
76    }
77
78    fn expect_len_lte(self, length: usize) -> Result<Self, ExpectedLenError> {
79        Comparison::LessOrEqual.expect_len_comparison(self, length)
80    }
81
82    fn expect_len_eq(self, length: usize) -> Result<Self, ExpectedLenError> {
83        Comparison::Equal.expect_len_comparison(self, length)
84    }
85
86    fn expect_len_ne(self, length: usize) -> Result<Self, ExpectedLenError> {
87        Comparison::Unequal.expect_len_comparison(self, length)
88    }
89
90    fn expect_len_gte(self, length: usize) -> Result<Self, ExpectedLenError> {
91        Comparison::GreaterOrEqual.expect_len_comparison(self, length)
92    }
93
94    fn expect_len_gt(self, length: usize) -> Result<Self, ExpectedLenError> {
95        Comparison::Greater.expect_len_comparison(self, length)
96    }
97}