Skip to content

Commit

Permalink
implement CountGreaterEqual
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoikey committed Nov 25, 2024
1 parent 9a2a813 commit cb19f53
Show file tree
Hide file tree
Showing 2 changed files with 82 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
@@ -1,7 +1,9 @@
mod equal;
mod grater_equal;
mod greater;
mod less;

pub use equal::*;
pub use grater_equal::*;
pub use greater::*;
pub use less::*;
80 changes: 80 additions & 0 deletions src/rule/collection/count/grater_equal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::rule::{CountEqualRule, CountGreaterRule};
use crate::{Or, Refined};

/// A type that holds a value satisfying the `GreaterEqualRule`
pub type CountGreaterEqual<const N: usize, RULE, ITERABLE> =
Refined<CountGreaterEqualRule<N, RULE, ITERABLE>>;

/// A type that holds a `Vec` value satisfying the `CountGreaterEqualRule`
pub type CountGreaterEqualVec<const N: usize, RULE> = Refined<CountGreaterEqualVecRule<N, RULE>>;

/// A type that holds a `VecDeque` value satisfying the `CountGreaterEqualRule`
pub type CountGreaterEqualVecDeque<const N: usize, RULE> =
Refined<CountGreaterEqualVecDequeRule<N, RULE>>;

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

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

/// A type that holds a `String` value satisfying the `CountGreaterEqualRule`
pub type CountGreaterEqualString<const N: usize, RULE> =
Refined<CountGreaterEqualStringRule<N, RULE>>;

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

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

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

/// Rule where the count of items in the `VecDeque` that satisfy the condition is greater than or equal to `N`.
pub type CountGreaterEqualVecDequeRule<const N: usize, RULE> =
CountGreaterEqualRule<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 greater than or equal to `N`.
pub type CountGreaterEqualHashMapRule<const N: usize, RULE, K> =
CountGreaterEqualRule<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 greater than or equal to `N`.
pub type CountGreaterEqualHashSetRule<const N: usize, RULE, K> =
CountGreaterEqualRule<N, RULE, std::collections::HashSet<K>>;

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

/// Rule where the count of items in the `&'a str` that satisfy the condition is greater than or equal to `N`.
pub type CountGreaterEqualStrRule<'a, const N: usize, RULE> =
CountGreaterEqualRule<N, RULE, &'a str>;

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

#[test]
fn count_greater_equal_1() -> Result<(), Error<Vec<String>>> {
let value = vec!["good morning".to_string(), "hello".to_string()];
let count_greater_equal: CountGreaterEqualVec<2, NonEmptyStringRule> =
CountGreaterEqualVec::new(value.clone())?;
assert_eq!(count_greater_equal.into_value(), value);
Ok(())
}

#[test]
fn count_greater_equal_2() -> anyhow::Result<()> {
let value = vec!["".to_string(), "".to_string()];
let count_greater_equal_result =
CountGreaterEqualVec::<2, NonEmptyStringRule>::new(value.clone());
assert!(count_greater_equal_result.is_err());
Ok(())
}
}

0 comments on commit cb19f53

Please sign in to comment.