Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoikey committed Nov 10, 2024
1 parent f693cb3 commit 635849f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 5 deletions.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,58 @@ fn not_example() -> Result<(), Error<u8>> {
}
```

### 4: `If` Rule Composer

`If` Rule Composer is a rule that applies a specific rule only when a certain condition is met.

```rust
type Target = Refined<If<GreaterEqualRuleI8<10>, EvenRuleI8>>;

fn if_example() -> Result<(), Error<i8>> {
let target = Target::new(8)?;
assert_eq!(target.into_value(), 8);

let target = Target::new(9)?;
assert_eq!(target.into_value(), 9);

let target = Target::new(10)?;
assert_eq!(target.into_value(), 10);

let target = Target::new(11);
assert!(target.is_err());

Ok(())
}
```

### 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.

```rust
type Target = Refined<IfElse<GreaterEqualRuleI8<10>, EvenRuleI8, OddRuleI8>>;

fn if_else_example() -> Result<(), Error<i8>> {
let target = Target::new(8);
assert!(target.is_err());

let target = Target::new(9)?;
assert_eq!(target.into_value(), 9);

let target = Target::new(10)?;
assert_eq!(target.into_value(), 10);

let target = Target::new(11);
assert!(target.is_err());

Ok(())
}
```

### 6: Other Rule Composer

`Equiv`, `Nand`, `Nor` and `Xor` are also available.

# Number

## `MinMax`
Expand Down
49 changes: 44 additions & 5 deletions tests/read_me.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use serde::{Deserialize, Serialize};
use serde_json::json;

use refined_type::result::Error;
use refined_type::rule::composer::Not;
use refined_type::rule::composer::{If, IfElse, Not};
use refined_type::rule::{
EqualU8, ExistsVec, ForAllVec, GreaterEqualU8, GreaterU8, HeadVec, IndexRuleVec, IndexVec,
InitVec, LastVec, LengthDefinition, LengthEqual, LengthEqualRule, LengthGreater, LengthLess,
LengthMinMax, LessEqualU8, LessU8, MinMaxU8, NonEmptyString, NonEmptyStringRule, NonEmptyVec,
NonEmptyVecDeque, RangeU8, Reverse, Rule, SkipFirst, SkipVec, TailVec,
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,
};
use refined_type::{And, Or, Refined};

Expand Down Expand Up @@ -660,3 +661,41 @@ fn custom_length_example() -> Result<(), Error<Hello>> {
assert_eq!(hello.into_value(), Hello);
Ok(())
}

#[test]
fn if_example() -> Result<(), Error<i8>> {
type Target = Refined<If<GreaterEqualRuleI8<10>, EvenRuleI8>>;

let target = Target::new(8)?;
assert_eq!(target.into_value(), 8);

let target = Target::new(9)?;
assert_eq!(target.into_value(), 9);

let target = Target::new(10)?;
assert_eq!(target.into_value(), 10);

let target = Target::new(11);
assert!(target.is_err());

Ok(())
}

#[test]
fn if_else_example() -> Result<(), Error<i8>> {
type Target = Refined<IfElse<GreaterEqualRuleI8<10>, EvenRuleI8, OddRuleI8>>;

let target = Target::new(8);
assert!(target.is_err());

let target = Target::new(9)?;
assert_eq!(target.into_value(), 9);

let target = Target::new(10)?;
assert_eq!(target.into_value(), 10);

let target = Target::new(11);
assert!(target.is_err());

Ok(())
}

0 comments on commit 635849f

Please sign in to comment.