|
| 1 | +use crate::rule::{CountEqualRule, CountGreaterRule}; |
| 2 | +use crate::{Or, Refined}; |
| 3 | + |
| 4 | +/// A type that holds a value satisfying the `GreaterEqualRule` |
| 5 | +pub type CountGreaterEqual<const N: usize, RULE, ITERABLE> = |
| 6 | + Refined<CountGreaterEqualRule<N, RULE, ITERABLE>>; |
| 7 | + |
| 8 | +/// A type that holds a `Vec` value satisfying the `CountGreaterEqualRule` |
| 9 | +pub type CountGreaterEqualVec<const N: usize, RULE> = Refined<CountGreaterEqualVecRule<N, RULE>>; |
| 10 | + |
| 11 | +/// A type that holds a `VecDeque` value satisfying the `CountGreaterEqualRule` |
| 12 | +pub type CountGreaterEqualVecDeque<const N: usize, RULE> = |
| 13 | + Refined<CountGreaterEqualVecDequeRule<N, RULE>>; |
| 14 | + |
| 15 | +/// A type that holds a `HashMap` value satisfying the `CountGreaterEqualRule` |
| 16 | +pub type CountGreaterEqualHashMap<const N: usize, RULE, K> = |
| 17 | + Refined<CountGreaterEqualHashMapRule<N, RULE, K>>; |
| 18 | + |
| 19 | +/// A type that holds a `HashSet` value satisfying the `CountGreaterEqualRule` |
| 20 | +pub type CountGreaterEqualHashSet<const N: usize, RULE, K> = |
| 21 | + Refined<CountGreaterEqualHashSetRule<N, RULE, K>>; |
| 22 | + |
| 23 | +/// A type that holds a `String` value satisfying the `CountGreaterEqualRule` |
| 24 | +pub type CountGreaterEqualString<const N: usize, RULE> = |
| 25 | + Refined<CountGreaterEqualStringRule<N, RULE>>; |
| 26 | + |
| 27 | +/// A type that holds a `&'a str` value satisfying the `CountGreaterEqualRule` |
| 28 | +pub type CountGreaterEqualStr<'a, const N: usize, RULE> = |
| 29 | + Refined<CountGreaterEqualStrRule<'a, N, RULE>>; |
| 30 | + |
| 31 | +/// Rule where the count of items in the collection that satisfy the condition is greater than or equal to `N`. |
| 32 | +pub type CountGreaterEqualRule<const N: usize, RULE, ITERABLE> = |
| 33 | + Or![CountGreaterRule<N, RULE, ITERABLE>, CountEqualRule<N, RULE, ITERABLE>]; |
| 34 | + |
| 35 | +/// Rule where the count of items in the `Vec` that satisfy the condition is greater than or equal to `N`. |
| 36 | +pub type CountGreaterEqualVecRule<const N: usize, RULE> = |
| 37 | + CountGreaterEqualRule<N, RULE, Vec<<RULE as crate::rule::Rule>::Item>>; |
| 38 | + |
| 39 | +/// Rule where the count of items in the `VecDeque` that satisfy the condition is greater than or equal to `N`. |
| 40 | +pub type CountGreaterEqualVecDequeRule<const N: usize, RULE> = |
| 41 | + CountGreaterEqualRule<N, RULE, std::collections::VecDeque<<RULE as crate::rule::Rule>::Item>>; |
| 42 | + |
| 43 | +/// Rule where the count of items in the `HashMap` that satisfy the condition is greater than or equal to `N`. |
| 44 | +pub type CountGreaterEqualHashMapRule<const N: usize, RULE, K> = |
| 45 | + CountGreaterEqualRule<N, RULE, std::collections::HashMap<K, <RULE as crate::rule::Rule>::Item>>; |
| 46 | + |
| 47 | +/// Rule where the count of items in the `HashSet` that satisfy the condition is greater than or equal to `N`. |
| 48 | +pub type CountGreaterEqualHashSetRule<const N: usize, RULE, K> = |
| 49 | + CountGreaterEqualRule<N, RULE, std::collections::HashSet<K>>; |
| 50 | + |
| 51 | +/// Rule where the count of items in the `String` that satisfy the condition is greater than or equal to `N`. |
| 52 | +pub type CountGreaterEqualStringRule<const N: usize, RULE> = CountGreaterEqualRule<N, RULE, String>; |
| 53 | + |
| 54 | +/// Rule where the count of items in the `&'a str` that satisfy the condition is greater than or equal to `N`. |
| 55 | +pub type CountGreaterEqualStrRule<'a, const N: usize, RULE> = |
| 56 | + CountGreaterEqualRule<N, RULE, &'a str>; |
| 57 | + |
| 58 | +#[cfg(test)] |
| 59 | +mod tests { |
| 60 | + use crate::result::Error; |
| 61 | + use crate::rule::{CountGreaterEqualVec, NonEmptyStringRule}; |
| 62 | + |
| 63 | + #[test] |
| 64 | + fn count_greater_equal_1() -> Result<(), Error<Vec<String>>> { |
| 65 | + let value = vec!["good morning".to_string(), "hello".to_string()]; |
| 66 | + let count_greater_equal: CountGreaterEqualVec<2, NonEmptyStringRule> = |
| 67 | + CountGreaterEqualVec::new(value.clone())?; |
| 68 | + assert_eq!(count_greater_equal.into_value(), value); |
| 69 | + Ok(()) |
| 70 | + } |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn count_greater_equal_2() -> anyhow::Result<()> { |
| 74 | + let value = vec!["".to_string(), "".to_string()]; |
| 75 | + let count_greater_equal_result = |
| 76 | + CountGreaterEqualVec::<2, NonEmptyStringRule>::new(value.clone()); |
| 77 | + assert!(count_greater_equal_result.is_err()); |
| 78 | + Ok(()) |
| 79 | + } |
| 80 | +} |
0 commit comments