Skip to content

Commit

Permalink
Merge pull request #18 from tomoikey/fix/for_all
Browse files Browse the repository at this point in the history
fix: for_all
  • Loading branch information
tomoikey authored Sep 16, 2024
2 parents 05039fd + 659caa5 commit 1f4a0c7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,11 @@ I have also prepared several useful refined types for Iterators.
```rust
fn example_11() -> anyhow::Result<()> {
let vec = vec!["Hello".to_string(), "World".to_string()];
let for_all_ok = ForAll::<NonEmptyStringRule, _>::new(vec.clone())?;
let for_all_ok = ForAllVec::<NonEmptyStringRule>::new(vec.clone())?;
assert_eq!(vec, for_all_ok.into_value());

let vec = vec!["Hello".to_string(), "".to_string()];
let for_all_err = ForAll::<NonEmptyStringRule>::new(vec.clone());
let for_all_err = ForAllVec::<NonEmptyStringRule>::new(vec.clone());
assert!(for_all_err.is_err());
Ok(())
}
Expand All @@ -350,11 +350,11 @@ fn example_11() -> anyhow::Result<()> {
```rust
fn example_12() -> anyhow::Result<()> {
let vec = vec!["Hello".to_string(), "".to_string()];
let exists_ok = Exists::<NonEmptyStringRule, _>::new(vec.clone())?;
let exists_ok = ExistsVec::<NonEmptyStringRule>::new(vec.clone())?;
assert_eq!(vec, exists_ok.into_value());

let vec = vec!["".to_string(), "".to_string()];
let exists_err = Exists::<NonEmptyStringRule>::new(vec.clone());
let exists_err = ExistsVec::<NonEmptyStringRule>::new(vec.clone());
assert!(exists_err.is_err());
Ok(())
}
Expand Down
15 changes: 14 additions & 1 deletion src/rule/exists.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
use crate::rule::composer::Not;
use crate::rule::ForAllRule;
use crate::rule::{ForAllRule, Rule};
use crate::Refined;
use std::collections::{HashSet, VecDeque};

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

/// A type that holds a Vec value satisfying the `ExistsRule`
pub type ExistsVec<RULE> = Exists<RULE, Vec<<RULE as Rule>::Item>>;

/// A type that holds a VecDeque value satisfying the `ExistsRule`
pub type ExistsVecDeque<RULE> = Exists<RULE, VecDeque<<RULE as Rule>::Item>>;

/// A type that holds a HashSet value satisfying the `ExistsRule`
pub type ExistsHashSet<RULE> = Exists<RULE, HashSet<<RULE as Rule>::Item>>;

/// A type that holds a String value satisfying the `ExistsRule`
pub type ExistsString<RULE> = Exists<RULE, String>;

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

Expand Down
21 changes: 17 additions & 4 deletions src/rule/for_all.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod collection;
mod string;

use std::collections::{HashSet, VecDeque};
use std::marker::PhantomData;

use crate::rule::Rule;
Expand All @@ -9,6 +10,18 @@ use crate::Refined;
/// A type that holds a value satisfying the `ForAllRule`
pub type ForAll<RULE, ITERABLE> = Refined<ForAllRule<RULE, ITERABLE>>;

/// A type that holds a Vec value satisfying the `ForAllRule`
pub type ForAllVec<RULE> = ForAll<RULE, Vec<<RULE as Rule>::Item>>;

/// A type that holds a VecDeque value satisfying the `ForAllRule`
pub type ForAllVecDeque<RULE> = ForAll<RULE, VecDeque<<RULE as Rule>::Item>>;

/// A type that holds a HashSet value satisfying the `ForAllRule`
pub type ForAllHashSet<RULE> = ForAll<RULE, HashSet<<RULE as Rule>::Item>>;

/// A type that holds a String value satisfying the `ForAllRule`
pub type ForAllString<RULE> = ForAll<RULE, String>;

/// Rule where all the data in the collection satisfies the condition
pub struct ForAllRule<RULE, ITERABLE>
where
Expand All @@ -21,20 +34,20 @@ where
mod tests {
use crate::result::Error;
use crate::rule::for_all::ForAll;
use crate::rule::{NonEmptyStringRule, Rule};
use crate::rule::{ForAllString, ForAllVec, NonEmptyStringRule, Rule};

#[test]
fn for_all_1() -> anyhow::Result<()> {
let value = vec!["good morning".to_string(), "hello".to_string()];
let for_all: ForAll<NonEmptyStringRule, Vec<_>> = ForAll::new(value.clone())?;
let for_all: ForAllVec<NonEmptyStringRule> = ForAll::new(value.clone())?;
assert_eq!(for_all.into_value(), value);
Ok(())
}

#[test]
fn for_all_2() -> anyhow::Result<()> {
let value = vec!["good morning".to_string(), "".to_string()];
let for_all_result = ForAll::<NonEmptyStringRule, Vec<_>>::new(value.clone());
let for_all_result = ForAllVec::<NonEmptyStringRule>::new(value.clone());
assert!(for_all_result.is_err());
Ok(())
}
Expand All @@ -55,7 +68,7 @@ mod tests {
}

let value = "hello".to_string();
let for_all: ForAll<CharRule, String> = ForAll::new(value.clone())?;
let for_all: ForAllString<CharRule> = ForAll::new(value.clone())?;
assert_eq!(for_all.into_value(), value);
Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions tests/read_me.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde_json::json;
use refined_type::result::Error;
use refined_type::rule::composer::{And, Not, Or};
use refined_type::rule::{
Exists, ForAll, LengthDefinition, NonEmptyRule, NonEmptyString, NonEmptyStringRule,
ExistsVec, ForAllVec, LengthDefinition, NonEmptyRule, NonEmptyString, NonEmptyStringRule,
NonEmptyVec, NonEmptyVecDeque, Rule,
};
use refined_type::{
Expand Down Expand Up @@ -269,23 +269,23 @@ type TargetAgeRule = And<TargetAge18OrMore, TargetAge80OrLess>;
#[test]
fn example_11() -> anyhow::Result<()> {
let vec = vec!["Hello".to_string(), "World".to_string()];
let for_all_ok = ForAll::<NonEmptyStringRule, Vec<_>>::new(vec.clone())?;
let for_all_ok = ForAllVec::<NonEmptyStringRule>::new(vec.clone())?;
assert_eq!(vec, for_all_ok.into_value());

let vec = vec!["Hello".to_string(), "".to_string()];
let for_all_err = ForAll::<NonEmptyStringRule, Vec<_>>::new(vec.clone());
let for_all_err = ForAllVec::<NonEmptyStringRule>::new(vec.clone());
assert!(for_all_err.is_err());
Ok(())
}

#[test]
fn example_12() -> anyhow::Result<()> {
let vec = vec!["Hello".to_string(), "".to_string()];
let exists_ok = Exists::<NonEmptyStringRule, Vec<_>>::new(vec.clone())?;
let exists_ok = ExistsVec::<NonEmptyStringRule>::new(vec.clone())?;
assert_eq!(vec, exists_ok.into_value());

let vec = vec!["".to_string(), "".to_string()];
let exists_err = Exists::<NonEmptyStringRule, Vec<_>>::new(vec.clone());
let exists_err = ExistsVec::<NonEmptyStringRule>::new(vec.clone());
assert!(exists_err.is_err());
Ok(())
}
Expand Down

0 comments on commit 1f4a0c7

Please sign in to comment.