Skip to content

Commit

Permalink
implement CountLessEqual
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoikey committed Nov 25, 2024
1 parent cb19f53 commit 67eded6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/rule/collection/count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
76 changes: 76 additions & 0 deletions src/rule/collection/count/less_equal.rs
Original file line number Diff line number Diff line change
@@ -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<const N: usize, RULE, ITERABLE> =
Refined<CountLessEqualRule<N, RULE, ITERABLE>>;

/// A type that holds a `Vec` value satisfying the `CountLessEqualRule`
pub type CountLessEqualVec<const N: usize, RULE> = Refined<CountLessEqualVecRule<N, RULE>>;

/// A type that holds a `VecDeque` value satisfying the `CountLessEqualRule`
pub type CountLessEqualVecDeque<const N: usize, RULE> =
Refined<CountLessEqualVecDequeRule<N, RULE>>;

/// A type that holds a `HashMap` value satisfying the `CountLessEqualRule`
pub type CountLessEqualHashMap<const N: usize, RULE, K> =
Refined<CountLessEqualHashMapRule<N, RULE, K>>;

/// A type that holds a `HashSet` value satisfying the `CountLessEqualRule`
pub type CountLessEqualHashSet<const N: usize, RULE, K> =
Refined<CountLessEqualHashSetRule<N, RULE, K>>;

/// A type that holds a `String` value satisfying the `CountLessEqualRule`
pub type CountLessEqualString<const N: usize, RULE> = Refined<CountLessEqualStringRule<N, RULE>>;

/// A type that holds a `&'a str` value satisfying the `CountLessEqualRule`
pub type CountLessEqualStr<'a, const N: usize, RULE> = Refined<CountLessEqualStrRule<'a, N, RULE>>;

/// Rule where the count of items in the collection that satisfy the condition is less than or equal to `N`.
pub type CountLessEqualRule<const N: usize, RULE, ITERABLE> =
Or![CountLessRule<N, RULE, ITERABLE>, CountEqualRule<N, RULE, ITERABLE>];

/// Rule where the count of items in the `Vec` that satisfy the condition is less than or equal to `N`.
pub type CountLessEqualVecRule<const N: usize, RULE> =
CountLessEqualRule<N, RULE, Vec<<RULE as crate::rule::Rule>::Item>>;

/// Rule where the count of items in the `VecDeque` that satisfy the condition is less than or equal to `N`.
pub type CountLessEqualVecDequeRule<const N: usize, RULE> =
CountLessEqualRule<N, RULE, std::collections::VecDeque<<RULE as crate::rule::Rule>::Item>>;

/// Rule where the count of items in the `HashMap` that satisfy the condition is less than or equal to `N`.
pub type CountLessEqualHashMapRule<const N: usize, RULE, K> =
CountLessEqualRule<N, RULE, std::collections::HashMap<K, <RULE as crate::rule::Rule>::Item>>;

/// Rule where the count of items in the `HashSet` that satisfy the condition is less than or equal to `N`.
pub type CountLessEqualHashSetRule<const N: usize, RULE, K> =
CountLessEqualRule<N, RULE, std::collections::HashSet<K>>;

/// Rule where the count of items in the `String` that satisfy the condition is less than or equal to `N`.
pub type CountLessEqualStringRule<const N: usize, RULE> = CountLessEqualRule<N, RULE, String>;

/// 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<N, RULE, &'a str>;

#[cfg(test)]
mod tests {
use crate::result::Error;
use crate::rule::{CountLessEqualVec, NonEmptyStringRule};

#[test]
fn count_less_equal_1() -> Result<(), Error<Vec<String>>> {
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(())
}
}

0 comments on commit 67eded6

Please sign in to comment.