|
| 1 | +use crate::rule::{CountEqualRule, CountLessRule}; |
| 2 | +use crate::{Or, Refined}; |
| 3 | + |
| 4 | +/// A type that holds a value satisfying the `LessEqualRule` |
| 5 | +pub type CountLessEqual<const N: usize, RULE, ITERABLE> = |
| 6 | + Refined<CountLessEqualRule<N, RULE, ITERABLE>>; |
| 7 | + |
| 8 | +/// A type that holds a `Vec` value satisfying the `CountLessEqualRule` |
| 9 | +pub type CountLessEqualVec<const N: usize, RULE> = Refined<CountLessEqualVecRule<N, RULE>>; |
| 10 | + |
| 11 | +/// A type that holds a `VecDeque` value satisfying the `CountLessEqualRule` |
| 12 | +pub type CountLessEqualVecDeque<const N: usize, RULE> = |
| 13 | + Refined<CountLessEqualVecDequeRule<N, RULE>>; |
| 14 | + |
| 15 | +/// A type that holds a `HashMap` value satisfying the `CountLessEqualRule` |
| 16 | +pub type CountLessEqualHashMap<const N: usize, RULE, K> = |
| 17 | + Refined<CountLessEqualHashMapRule<N, RULE, K>>; |
| 18 | + |
| 19 | +/// A type that holds a `HashSet` value satisfying the `CountLessEqualRule` |
| 20 | +pub type CountLessEqualHashSet<const N: usize, RULE, K> = |
| 21 | + Refined<CountLessEqualHashSetRule<N, RULE, K>>; |
| 22 | + |
| 23 | +/// A type that holds a `String` value satisfying the `CountLessEqualRule` |
| 24 | +pub type CountLessEqualString<const N: usize, RULE> = Refined<CountLessEqualStringRule<N, RULE>>; |
| 25 | + |
| 26 | +/// A type that holds a `&'a str` value satisfying the `CountLessEqualRule` |
| 27 | +pub type CountLessEqualStr<'a, const N: usize, RULE> = Refined<CountLessEqualStrRule<'a, N, RULE>>; |
| 28 | + |
| 29 | +/// Rule where the count of items in the collection that satisfy the condition is less than or equal to `N`. |
| 30 | +pub type CountLessEqualRule<const N: usize, RULE, ITERABLE> = |
| 31 | + Or![CountLessRule<N, RULE, ITERABLE>, CountEqualRule<N, RULE, ITERABLE>]; |
| 32 | + |
| 33 | +/// Rule where the count of items in the `Vec` that satisfy the condition is less than or equal to `N`. |
| 34 | +pub type CountLessEqualVecRule<const N: usize, RULE> = |
| 35 | + CountLessEqualRule<N, RULE, Vec<<RULE as crate::rule::Rule>::Item>>; |
| 36 | + |
| 37 | +/// Rule where the count of items in the `VecDeque` that satisfy the condition is less than or equal to `N`. |
| 38 | +pub type CountLessEqualVecDequeRule<const N: usize, RULE> = |
| 39 | + CountLessEqualRule<N, RULE, std::collections::VecDeque<<RULE as crate::rule::Rule>::Item>>; |
| 40 | + |
| 41 | +/// Rule where the count of items in the `HashMap` that satisfy the condition is less than or equal to `N`. |
| 42 | +pub type CountLessEqualHashMapRule<const N: usize, RULE, K> = |
| 43 | + CountLessEqualRule<N, RULE, std::collections::HashMap<K, <RULE as crate::rule::Rule>::Item>>; |
| 44 | + |
| 45 | +/// Rule where the count of items in the `HashSet` that satisfy the condition is less than or equal to `N`. |
| 46 | +pub type CountLessEqualHashSetRule<const N: usize, RULE, K> = |
| 47 | + CountLessEqualRule<N, RULE, std::collections::HashSet<K>>; |
| 48 | + |
| 49 | +/// Rule where the count of items in the `String` that satisfy the condition is less than or equal to `N`. |
| 50 | +pub type CountLessEqualStringRule<const N: usize, RULE> = CountLessEqualRule<N, RULE, String>; |
| 51 | + |
| 52 | +/// Rule where the count of items in the `&'a str` that satisfy the condition is less than or equal to `N`. |
| 53 | +pub type CountLessEqualStrRule<'a, const N: usize, RULE> = CountLessEqualRule<N, RULE, &'a str>; |
| 54 | + |
| 55 | +#[cfg(test)] |
| 56 | +mod tests { |
| 57 | + use crate::result::Error; |
| 58 | + use crate::rule::{CountLessEqualVec, NonEmptyStringRule}; |
| 59 | + |
| 60 | + #[test] |
| 61 | + fn count_less_equal_1() -> Result<(), Error<Vec<String>>> { |
| 62 | + let value = vec!["good morning".to_string(), "hello".to_string()]; |
| 63 | + let count_less_equal = CountLessEqualVec::<2, NonEmptyStringRule>::new(value.clone())?; |
| 64 | + assert_eq!(count_less_equal.into_value(), value); |
| 65 | + Ok(()) |
| 66 | + } |
| 67 | + |
| 68 | + #[test] |
| 69 | + fn count_less_equal_2() -> anyhow::Result<()> { |
| 70 | + let value = vec!["world".to_string(), "hello".to_string()]; |
| 71 | + let count_less_equal_result = |
| 72 | + CountLessEqualVec::<1, NonEmptyStringRule>::new(value.clone()); |
| 73 | + assert!(count_less_equal_result.is_err()); |
| 74 | + Ok(()) |
| 75 | + } |
| 76 | +} |
0 commit comments