Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoikey committed Nov 25, 2024
1 parent 67eded6 commit 098b9c0
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 5 deletions.
126 changes: 123 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ By using Rule Composer, composite rules can be easily created.

`And` Rule Composer is a rule that satisfies both of the two rules.
It is generally effective when you want to narrow down the condition range.

```rust
type Target = Refined<And![EvenRuleU8, MinMaxRuleU8<0, 100>]>;
```

```rust
fn and_example() -> Result<(), Error<u8>> {
let target = Target::new(50)?;
Expand All @@ -158,6 +160,7 @@ It is generally effective when you want to expand the condition range.
```rust
type Target = Refined<Or![LessRuleU8<10>, GreaterRuleU8<50>]>;
```

```rust
fn or_example() -> Result<(), Error<u8>> {
let target = Target::new(5)?;
Expand All @@ -184,6 +187,7 @@ It is generally effective when you want to discard only certain situations.
```rust
type Target = Refined<Not<EqualRuleU8<50>>>;
```

```rust
fn not_example() -> Result<(), Error<u8>> {
let target = Target::new(49)?;
Expand Down Expand Up @@ -225,7 +229,8 @@ fn if_example() -> Result<(), Error<i8>> {

### 5: `IfElse` Rule Composer

`IfElse` Rule Composer is a rule that applies a specific rule when a certain condition is met and another rule when it is not met.
`IfElse` Rule Composer is a rule that applies a specific rule when a certain condition is met and another rule when it
is not met.

```rust
type Target = Refined<IfElse<GreaterEqualRuleI8<10>, EvenRuleI8, OddRuleI8>>;
Expand Down Expand Up @@ -396,7 +401,6 @@ fn range_example() -> Result<(), Error<u8>> {
}
```


# Iterator

`refined_type` has several useful refined types for Iterators.
Expand Down Expand Up @@ -555,9 +559,121 @@ fn example_17() -> anyhow::Result<()> {
}
```

## `CountEqual`

`CountEqual` is a type that signifies the number of elements that satisfy the rule is a specific number.

```rust
fn count_equal_example() -> Result<(), Error<Vec<i32>>> {
let table = vec![
(vec!["good morning".to_string(), "hello".to_string()], false),
(vec!["good morning".to_string(), "".to_string()], true),
(vec!["".to_string(), "hello".to_string()], true),
(vec!["".to_string(), "".to_string()], false),
];

for (value, expected) in table {
let refined = CountEqualVec::<1, NonEmptyStringRule>::new(value.clone());
assert_eq!(refined.is_ok(), expected);
}

Ok(())
}
```

## `CountLess`

`CountLess` is a type that signifies the number of elements that satisfy the rule is less than a specific number.

```rust
fn count_less_example() -> Result<(), Error<Vec<i32>>> {
let table = vec![
(vec!["good morning".to_string(), "hello".to_string()], false),
(vec!["good morning".to_string(), "".to_string()], true),
(vec!["".to_string(), "hello".to_string()], true),
(vec!["".to_string(), "".to_string()], true),
];

for (value, expected) in table {
let refined = CountLessVec::<2, NonEmptyStringRule>::new(value.clone());
assert_eq!(refined.is_ok(), expected);
}

Ok(())
}
```

## `CountGreater`

`CountGreater` is a type that signifies the number of elements that satisfy the rule is greater than a specific number.

```rust
fn count_greater_example() -> Result<(), Error<Vec<i32>>> {
let table = vec![
(vec!["good morning".to_string(), "hello".to_string()], true),
(vec!["good morning".to_string(), "".to_string()], false),
(vec!["".to_string(), "hello".to_string()], false),
(vec!["".to_string(), "".to_string()], false),
];

for (value, expected) in table {
let refined = CountGreaterVec::<1, NonEmptyStringRule>::new(value.clone());
assert_eq!(refined.is_ok(), expected);
}

Ok(())
}
```

## `CountLessEqual`

`CountLessEqual` is a type that signifies the number of elements that satisfy the rule is less than or equal to a
specific number.

```rust
fn count_less_equal_example() -> Result<(), Error<Vec<i32>>> {
let table = vec![
(vec!["good morning".to_string(), "hello".to_string()], true),
(vec!["good morning".to_string(), "".to_string()], true),
(vec!["".to_string(), "hello".to_string()], true),
(vec!["".to_string(), "".to_string()], true),
];

for (value, expected) in table {
let refined = CountLessEqualVec::<2, NonEmptyStringRule>::new(value.clone());
assert_eq!(refined.is_ok(), expected);
}

Ok(())
}
```

## `CountGreaterEqual`

`CountGreaterEqual` is a type that signifies the number of elements that satisfy the rule is greater than or equal to a
specific number.

```rust
fn count_greater_equal_example() -> Result<(), Error<Vec<i32>>> {
let table = vec![
(vec!["good morning".to_string(), "hello".to_string()], true),
(vec!["good morning".to_string(), "".to_string()], true),
(vec!["".to_string(), "hello".to_string()], true),
(vec!["".to_string(), "".to_string()], false),
];

for (value, expected) in table {
let refined = CountGreaterEqualVec::<1, NonEmptyStringRule>::new(value.clone());
assert_eq!(refined.is_ok(), expected);
}

Ok(())
}
```

## `Reverse`

`Reverse` is a rule that applies a specific rule to all elements in the Iterator in reverse order.
`Reverse` is a rule that applies a specific rule to all elements in the Iterator in reverse order.

```rust
fn example_18() -> Result<(), Error<Vec<i32>>> {
Expand Down Expand Up @@ -625,6 +741,7 @@ impl<ITEM> SkipOption for NoSkip<ITEM> {
You can impose constraints on objects that have a length, such as `String` or `Vec`.

## `LengthMinMax`

`LengthMinMax` is a type that signifies the target has a length between a certain number and another number.

```rust
Expand All @@ -645,6 +762,7 @@ fn length_min_max_example() -> Result<(), Error<String>> {
```

## `LengthGreater`

`LengthGreater` is a type that signifies the target has a length greater than a certain number.

```rust
Expand All @@ -662,6 +780,7 @@ fn length_greater_example() -> Result<(), Error<String>> {
```

## `LengthLess`

`LengthLess` is a type that signifies the target has a length less than a certain number.

```rust
Expand All @@ -679,6 +798,7 @@ fn length_less_example() -> Result<(), Error<String>> {
```

## `LengthEqual`

`LengthEqual` is a type that signifies the target has a length equal to a certain number.

```rust
Expand Down
90 changes: 88 additions & 2 deletions tests/read_me.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use serde_json::json;
use refined_type::result::Error;
use refined_type::rule::composer::{If, IfElse, Not};
use refined_type::rule::{
EqualU8, EvenRuleI8, ExistsVec, ForAllVec, GreaterEqualRuleI8, GreaterEqualU8, GreaterU8,
HeadVec, IndexRuleVec, IndexVec, InitVec, LastVec, LengthDefinition, LengthEqual,
CountEqualVec, CountGreaterEqualVec, CountGreaterVec, CountLessEqualVec,
CountLessVec, EqualU8, EvenRuleI8, ExistsVec, ForAllVec, GreaterEqualRuleI8, GreaterEqualU8,
GreaterU8, HeadVec, IndexRuleVec, IndexVec, InitVec, LastVec, LengthDefinition, LengthEqual,
LengthEqualRule, LengthGreater, LengthLess, LengthMinMax, LessEqualU8, LessU8, MinMaxU8,
NonEmptyString, NonEmptyStringRule, NonEmptyVec, NonEmptyVecDeque, OddRuleI8, RangeU8, Reverse,
Rule, SkipFirst, SkipVec, TailVec,
Expand Down Expand Up @@ -525,6 +526,91 @@ fn example_17() -> anyhow::Result<()> {
Ok(())
}

#[test]
fn count_equal_example() -> Result<(), Error<Vec<i32>>> {
let table = vec![
(vec!["good morning".to_string(), "hello".to_string()], false),
(vec!["good morning".to_string(), "".to_string()], true),
(vec!["".to_string(), "hello".to_string()], true),
(vec!["".to_string(), "".to_string()], false),
];

for (value, expected) in table {
let refined = CountEqualVec::<1, NonEmptyStringRule>::new(value.clone());
assert_eq!(refined.is_ok(), expected);
}

Ok(())
}

#[test]
fn count_less_example() -> Result<(), Error<Vec<i32>>> {
let table = vec![
(vec!["good morning".to_string(), "hello".to_string()], false),
(vec!["good morning".to_string(), "".to_string()], true),
(vec!["".to_string(), "hello".to_string()], true),
(vec!["".to_string(), "".to_string()], true),
];

for (value, expected) in table {
let refined = CountLessVec::<2, NonEmptyStringRule>::new(value.clone());
assert_eq!(refined.is_ok(), expected);
}

Ok(())
}

#[test]
fn count_greater_example() -> Result<(), Error<Vec<i32>>> {
let table = vec![
(vec!["good morning".to_string(), "hello".to_string()], true),
(vec!["good morning".to_string(), "".to_string()], false),
(vec!["".to_string(), "hello".to_string()], false),
(vec!["".to_string(), "".to_string()], false),
];

for (value, expected) in table {
let refined = CountGreaterVec::<1, NonEmptyStringRule>::new(value.clone());
assert_eq!(refined.is_ok(), expected);
}

Ok(())
}

#[test]
fn count_less_equal_example() -> Result<(), Error<Vec<i32>>> {
let table = vec![
(vec!["good morning".to_string(), "hello".to_string()], true),
(vec!["good morning".to_string(), "".to_string()], true),
(vec!["".to_string(), "hello".to_string()], true),
(vec!["".to_string(), "".to_string()], true),
];

for (value, expected) in table {
let refined = CountLessEqualVec::<2, NonEmptyStringRule>::new(value.clone());
assert_eq!(refined.is_ok(), expected);
}

Ok(())
}

#[test]
fn count_greater_equal_example() -> Result<(), Error<Vec<i32>>> {
let table = vec![
(vec!["good morning".to_string(), "hello".to_string()], true),
(vec!["good morning".to_string(), "".to_string()], true),
(vec!["".to_string(), "hello".to_string()], true),
(vec!["".to_string(), "".to_string()], false),
];

for (value, expected) in table {
let refined = CountGreaterEqualVec::<1, NonEmptyStringRule>::new(value.clone());
assert_eq!(refined.is_ok(), expected);
}

Ok(())
}

#[test]
fn example_18() -> Result<(), Error<Vec<i32>>> {
let table = vec![
Expand Down

0 comments on commit 098b9c0

Please sign in to comment.