Skip to content

Commit

Permalink
implement nothing
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoikey committed Nov 10, 2024
1 parent d8733c7 commit c42c333
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/rule/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod index;
mod init;
mod iterable;
mod last;
mod nothing;
mod reverse;
mod skip;
mod tail;
Expand All @@ -16,6 +17,7 @@ pub use index::*;
pub use init::*;
pub use iterable::*;
pub use last::*;
pub use nothing::*;
pub use reverse::*;
pub use skip::*;
pub use tail::*;
12 changes: 10 additions & 2 deletions src/rule/collection/exists.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::{HashMap, HashSet, VecDeque};

use crate::rule::composer::Not;
use crate::rule::{ForAllRule, Rule};
use crate::rule::{NothingRule, Rule};
use crate::Refined;

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

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

/// Rule where at least one data in the `Vec` satisfies the condition
pub type ExistsVecRule<RULE> = ExistsRule<RULE, Vec<<RULE as Rule>::Item>>;
Expand Down Expand Up @@ -60,4 +60,12 @@ mod tests {
assert!(exists_result.is_err());
Ok(())
}

#[test]
fn exists_3() -> anyhow::Result<()> {
let value = vec![];
let exists_result = Exists::<NonEmptyStringRule, Vec<_>>::new(value.clone());
assert!(exists_result.is_err());
Ok(())
}
}
73 changes: 73 additions & 0 deletions src/rule/collection/nothing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use crate::rule::composer::Not;
use crate::rule::{ForAllRule, Rule};
use crate::Refined;
use std::collections::{HashMap, HashSet, VecDeque};

/// A type that holds a value satisfying the `NothingRule`
pub type Nothing<RULE, ITERABLE> = Refined<NothingRule<RULE, ITERABLE>>;

/// A type that holds a `Vec` value satisfying the `NothingRule`
pub type NothingVec<RULE> = Refined<NothingVecRule<RULE>>;

/// A type that holds a `VecDeque` value satisfying the `NothingRule`
pub type NothingVecDeque<RULE> = Refined<NothingVecDequeRule<RULE>>;

/// A type that holds a `HashSet` value satisfying the `NothingRule`
pub type NothingHashSet<RULE> = Refined<NothingHashSetRule<RULE>>;

/// A type that holds a `HashMap` value satisfying the `NothingRule`
pub type NothingHashMap<K, RULE> = Refined<NothingHashMapRule<K, RULE>>;

/// A type that holds a `String` value satisfying the `NothingRule`
pub type NothingString<RULE> = Refined<NothingStringRule<RULE>>;

/// Rule where no data in the collection satisfies the condition
pub type NothingRule<RULE, ITERABLE> = ForAllRule<Not<RULE>, ITERABLE>;

/// Rule where no data in the `Vec` satisfies the condition
pub type NothingVecRule<RULE> = NothingRule<RULE, Vec<<RULE as Rule>::Item>>;

/// Rule where no data in the `VecDeque` satisfies the condition
pub type NothingVecDequeRule<RULE> = NothingRule<RULE, VecDeque<<RULE as Rule>::Item>>;

/// Rule where no data in the `HashSet` satisfies the condition
pub type NothingHashSetRule<RULE> = NothingRule<RULE, HashSet<<RULE as Rule>::Item>>;

/// Rule where no data in the `HashMap` satisfies the condition
pub type NothingHashMapRule<K, RULE> = NothingRule<RULE, HashMap<K, <RULE as Rule>::Item>>;

/// Rule where no data in the `String` satisfies the condition
pub type NothingStringRule<RULE> = NothingRule<RULE, String>;

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

#[test]
fn nothing_valid() -> Result<(), Error<Vec<String>>> {
let table = vec![vec![], vec!["".to_string()]];

for value in table {
let nothing = NothingVec::<NonEmptyStringRule>::new(value.clone())?;
assert_eq!(nothing.into_value(), value);
}

Ok(())
}

#[test]
fn nothing_invalid() -> anyhow::Result<()> {
let table = vec![
vec!["good morning".to_string(), "hello".to_string()],
vec!["good morning".to_string()],
];

for value in table {
let nothing_result = NothingVec::<NonEmptyStringRule>::new(value.clone());
assert!(nothing_result.is_err());
}

Ok(())
}
}

0 comments on commit c42c333

Please sign in to comment.