|
| 1 | +use crate::rule::composer::Not; |
| 2 | +use crate::rule::{ForAllRule, Rule}; |
| 3 | +use crate::Refined; |
| 4 | +use std::collections::{HashMap, HashSet, VecDeque}; |
| 5 | + |
| 6 | +/// A type that holds a value satisfying the `NothingRule` |
| 7 | +pub type Nothing<RULE, ITERABLE> = Refined<NothingRule<RULE, ITERABLE>>; |
| 8 | + |
| 9 | +/// A type that holds a `Vec` value satisfying the `NothingRule` |
| 10 | +pub type NothingVec<RULE> = Refined<NothingVecRule<RULE>>; |
| 11 | + |
| 12 | +/// A type that holds a `VecDeque` value satisfying the `NothingRule` |
| 13 | +pub type NothingVecDeque<RULE> = Refined<NothingVecDequeRule<RULE>>; |
| 14 | + |
| 15 | +/// A type that holds a `HashSet` value satisfying the `NothingRule` |
| 16 | +pub type NothingHashSet<RULE> = Refined<NothingHashSetRule<RULE>>; |
| 17 | + |
| 18 | +/// A type that holds a `HashMap` value satisfying the `NothingRule` |
| 19 | +pub type NothingHashMap<K, RULE> = Refined<NothingHashMapRule<K, RULE>>; |
| 20 | + |
| 21 | +/// A type that holds a `String` value satisfying the `NothingRule` |
| 22 | +pub type NothingString<RULE> = Refined<NothingStringRule<RULE>>; |
| 23 | + |
| 24 | +/// Rule where no data in the collection satisfies the condition |
| 25 | +pub type NothingRule<RULE, ITERABLE> = ForAllRule<Not<RULE>, ITERABLE>; |
| 26 | + |
| 27 | +/// Rule where no data in the `Vec` satisfies the condition |
| 28 | +pub type NothingVecRule<RULE> = NothingRule<RULE, Vec<<RULE as Rule>::Item>>; |
| 29 | + |
| 30 | +/// Rule where no data in the `VecDeque` satisfies the condition |
| 31 | +pub type NothingVecDequeRule<RULE> = NothingRule<RULE, VecDeque<<RULE as Rule>::Item>>; |
| 32 | + |
| 33 | +/// Rule where no data in the `HashSet` satisfies the condition |
| 34 | +pub type NothingHashSetRule<RULE> = NothingRule<RULE, HashSet<<RULE as Rule>::Item>>; |
| 35 | + |
| 36 | +/// Rule where no data in the `HashMap` satisfies the condition |
| 37 | +pub type NothingHashMapRule<K, RULE> = NothingRule<RULE, HashMap<K, <RULE as Rule>::Item>>; |
| 38 | + |
| 39 | +/// Rule where no data in the `String` satisfies the condition |
| 40 | +pub type NothingStringRule<RULE> = NothingRule<RULE, String>; |
| 41 | + |
| 42 | +#[cfg(test)] |
| 43 | +mod tests { |
| 44 | + use crate::result::Error; |
| 45 | + use crate::rule::{NonEmptyStringRule, NothingVec}; |
| 46 | + |
| 47 | + #[test] |
| 48 | + fn nothing_valid() -> Result<(), Error<Vec<String>>> { |
| 49 | + let table = vec![vec![], vec!["".to_string()]]; |
| 50 | + |
| 51 | + for value in table { |
| 52 | + let nothing = NothingVec::<NonEmptyStringRule>::new(value.clone())?; |
| 53 | + assert_eq!(nothing.into_value(), value); |
| 54 | + } |
| 55 | + |
| 56 | + Ok(()) |
| 57 | + } |
| 58 | + |
| 59 | + #[test] |
| 60 | + fn nothing_invalid() -> anyhow::Result<()> { |
| 61 | + let table = vec![ |
| 62 | + vec!["good morning".to_string(), "hello".to_string()], |
| 63 | + vec!["good morning".to_string()], |
| 64 | + ]; |
| 65 | + |
| 66 | + for value in table { |
| 67 | + let nothing_result = NothingVec::<NonEmptyStringRule>::new(value.clone()); |
| 68 | + assert!(nothing_result.is_err()); |
| 69 | + } |
| 70 | + |
| 71 | + Ok(()) |
| 72 | + } |
| 73 | +} |
0 commit comments