Skip to content

Commit c42c333

Browse files
committed
implement nothing
1 parent d8733c7 commit c42c333

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

src/rule/collection.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod index;
55
mod init;
66
mod iterable;
77
mod last;
8+
mod nothing;
89
mod reverse;
910
mod skip;
1011
mod tail;
@@ -16,6 +17,7 @@ pub use index::*;
1617
pub use init::*;
1718
pub use iterable::*;
1819
pub use last::*;
20+
pub use nothing::*;
1921
pub use reverse::*;
2022
pub use skip::*;
2123
pub use tail::*;

src/rule/collection/exists.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::{HashMap, HashSet, VecDeque};
22

33
use crate::rule::composer::Not;
4-
use crate::rule::{ForAllRule, Rule};
4+
use crate::rule::{NothingRule, Rule};
55
use crate::Refined;
66

77
/// A type that holds a value satisfying the `ExistsRule`
@@ -23,7 +23,7 @@ pub type ExistsHashMap<K, RULE> = Refined<ExistsHashMapRule<K, RULE>>;
2323
pub type ExistsString<RULE> = Refined<ExistsStringRule<RULE>>;
2424

2525
/// Rule where at least one data in the collection satisfies the condition
26-
pub type ExistsRule<RULE, ITERABLE> = Not<ForAllRule<Not<RULE>, ITERABLE>>;
26+
pub type ExistsRule<RULE, ITERABLE> = Not<NothingRule<RULE, ITERABLE>>;
2727

2828
/// Rule where at least one data in the `Vec` satisfies the condition
2929
pub type ExistsVecRule<RULE> = ExistsRule<RULE, Vec<<RULE as Rule>::Item>>;
@@ -60,4 +60,12 @@ mod tests {
6060
assert!(exists_result.is_err());
6161
Ok(())
6262
}
63+
64+
#[test]
65+
fn exists_3() -> anyhow::Result<()> {
66+
let value = vec![];
67+
let exists_result = Exists::<NonEmptyStringRule, Vec<_>>::new(value.clone());
68+
assert!(exists_result.is_err());
69+
Ok(())
70+
}
6371
}

src/rule/collection/nothing.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)