diff --git a/src/rule/collection/count.rs b/src/rule/collection/count.rs index 6da2f85..4b03e97 100644 --- a/src/rule/collection/count.rs +++ b/src/rule/collection/count.rs @@ -2,8 +2,10 @@ mod equal; mod grater_equal; mod greater; mod less; +mod less_equal; pub use equal::*; pub use grater_equal::*; pub use greater::*; pub use less::*; +pub use less_equal::*; diff --git a/src/rule/collection/count/less_equal.rs b/src/rule/collection/count/less_equal.rs new file mode 100644 index 0000000..41576d4 --- /dev/null +++ b/src/rule/collection/count/less_equal.rs @@ -0,0 +1,76 @@ +use crate::rule::{CountEqualRule, CountLessRule}; +use crate::{Or, Refined}; + +/// A type that holds a value satisfying the `LessEqualRule` +pub type CountLessEqual = + Refined>; + +/// A type that holds a `Vec` value satisfying the `CountLessEqualRule` +pub type CountLessEqualVec = Refined>; + +/// A type that holds a `VecDeque` value satisfying the `CountLessEqualRule` +pub type CountLessEqualVecDeque = + Refined>; + +/// A type that holds a `HashMap` value satisfying the `CountLessEqualRule` +pub type CountLessEqualHashMap = + Refined>; + +/// A type that holds a `HashSet` value satisfying the `CountLessEqualRule` +pub type CountLessEqualHashSet = + Refined>; + +/// A type that holds a `String` value satisfying the `CountLessEqualRule` +pub type CountLessEqualString = Refined>; + +/// A type that holds a `&'a str` value satisfying the `CountLessEqualRule` +pub type CountLessEqualStr<'a, const N: usize, RULE> = Refined>; + +/// Rule where the count of items in the collection that satisfy the condition is less than or equal to `N`. +pub type CountLessEqualRule = + Or![CountLessRule, CountEqualRule]; + +/// Rule where the count of items in the `Vec` that satisfy the condition is less than or equal to `N`. +pub type CountLessEqualVecRule = + CountLessEqualRule::Item>>; + +/// Rule where the count of items in the `VecDeque` that satisfy the condition is less than or equal to `N`. +pub type CountLessEqualVecDequeRule = + CountLessEqualRule::Item>>; + +/// Rule where the count of items in the `HashMap` that satisfy the condition is less than or equal to `N`. +pub type CountLessEqualHashMapRule = + CountLessEqualRule::Item>>; + +/// Rule where the count of items in the `HashSet` that satisfy the condition is less than or equal to `N`. +pub type CountLessEqualHashSetRule = + CountLessEqualRule>; + +/// Rule where the count of items in the `String` that satisfy the condition is less than or equal to `N`. +pub type CountLessEqualStringRule = CountLessEqualRule; + +/// Rule where the count of items in the `&'a str` that satisfy the condition is less than or equal to `N`. +pub type CountLessEqualStrRule<'a, const N: usize, RULE> = CountLessEqualRule; + +#[cfg(test)] +mod tests { + use crate::result::Error; + use crate::rule::{CountLessEqualVec, NonEmptyStringRule}; + + #[test] + fn count_less_equal_1() -> Result<(), Error>> { + let value = vec!["good morning".to_string(), "hello".to_string()]; + let count_less_equal = CountLessEqualVec::<2, NonEmptyStringRule>::new(value.clone())?; + assert_eq!(count_less_equal.into_value(), value); + Ok(()) + } + + #[test] + fn count_less_equal_2() -> anyhow::Result<()> { + let value = vec!["world".to_string(), "hello".to_string()]; + let count_less_equal_result = + CountLessEqualVec::<1, NonEmptyStringRule>::new(value.clone()); + assert!(count_less_equal_result.is_err()); + Ok(()) + } +}