From 635849ff28c6b9d3298b4726c7d839a51b9288dd Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Sun, 10 Nov 2024 21:27:05 +0900 Subject: [PATCH] update README --- README.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/read_me.rs | 49 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 96 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index eb03325..82d634c 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,58 @@ fn not_example() -> Result<(), Error> { } ``` +### 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, EvenRuleI8>>; + +fn if_example() -> Result<(), Error> { + 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, EvenRuleI8, OddRuleI8>>; + +fn if_else_example() -> Result<(), Error> { + 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` diff --git a/tests/read_me.rs b/tests/read_me.rs index cd7093c..e7de9f8 100644 --- a/tests/read_me.rs +++ b/tests/read_me.rs @@ -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}; @@ -660,3 +661,41 @@ fn custom_length_example() -> Result<(), Error> { assert_eq!(hello.into_value(), Hello); Ok(()) } + +#[test] +fn if_example() -> Result<(), Error> { + type Target = Refined, 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> { + type Target = Refined, 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(()) +}