Skip to content

Commit

Permalink
add if and if-else
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoikey committed Nov 10, 2024
1 parent 03cea9c commit ab0f638
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/rule/composer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod and;
mod equiv;
mod if_else;
mod imply;
mod nand;
mod nor;
Expand All @@ -9,7 +10,8 @@ mod xor;

pub use and::And;
pub use equiv::Equiv;
pub use imply::Imply;
pub use if_else::IfElse;
pub use imply::{If, Imply};
pub use nand::Nand;
pub use nor::Nor;
pub use not::Not;
Expand Down
6 changes: 3 additions & 3 deletions src/rule/composer/equiv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use crate::rule::composer::imply::Imply;
use crate::And;

/// This is a type that represents logical equivalence in logic.
///
///
/// # Example
/// ```rust
/// use refined_type::rule::composer::Equiv;
/// use refined_type::rule::{EvenRuleI8, GreaterEqualRuleI8, Rule};
///
///
/// type Target = Equiv<GreaterEqualRuleI8<10>, EvenRuleI8>;
///
///
/// for value in vec![1, 10] {
/// assert!(Target::validate(value).is_ok());
/// }
Expand Down
47 changes: 47 additions & 0 deletions src/rule/composer/if_else.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::rule::composer::Not;
use crate::{And, Or};

/// This is a type that represents logical if-else in logic.
/// # Example
/// ```rust
/// use refined_type::rule::composer::IfElse;
///
/// use refined_type::rule::{EvenRuleI8, GreaterEqualRuleI8, OddRuleI8, Rule};
///
/// type Target = IfElse<GreaterEqualRuleI8<10>, EvenRuleI8, OddRuleI8>;
///
/// for value in vec![1, 10] {
/// assert!(Target::validate(value).is_ok());
/// }
///
/// for value in vec![2, 11] {
/// assert!(Target::validate(value).is_err());
/// }
/// ```
pub type IfElse<CONDITION, THEN, ELSE> = Or![And![CONDITION, THEN], And![Not<CONDITION>, ELSE]];

#[cfg(test)]
mod test {
use crate::rule::composer::IfElse;
use crate::rule::{EvenRuleI8, GreaterEqualRuleI8, OddRuleI8, Rule};

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

#[test]
fn test_rule_binder_ok() {
let table = vec![1, 10];

for value in table {
assert!(Target::validate(value).is_ok());
}
}

#[test]
fn test_rule_binder_err() {
let table = vec![2, 11];

for value in table {
assert!(Target::validate(value).is_err());
}
}
}
24 changes: 21 additions & 3 deletions src/rule/composer/imply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,36 @@ use crate::Or;
/// ```rust
/// use refined_type::rule::composer::Imply;
/// use refined_type::rule::{EvenRuleI8, GreaterEqualRuleI8, Rule};
///
///
/// type IfGreaterOrEqual10ThenEven = Imply<GreaterEqualRuleI8<10>, EvenRuleI8>;
///
///
/// for value in vec![8, 9, 10, 12] {
/// assert!(IfGreaterOrEqual10ThenEven::validate(value).is_ok());
/// }
///
///
/// for value in vec![11, 13] {
/// assert!(IfGreaterOrEqual10ThenEven::validate(value).is_err());
/// }
pub type Imply<RULE1, RULE2> = Or![Not<RULE1>, RULE2];

/// This is a type that represents logical if in logic.
///
/// # Example
/// ```rust
/// use refined_type::rule::composer::If;
/// use refined_type::rule::{EvenRuleI8, GreaterEqualRuleI8, Rule};
///
/// type IfGreaterOrEqual10ThenEven = If<GreaterEqualRuleI8<10>, EvenRuleI8>;
///
/// for value in vec![8, 9, 10, 12] {
/// assert!(IfGreaterOrEqual10ThenEven::validate(value).is_ok());
/// }
///
/// for value in vec![11, 13] {
/// assert!(IfGreaterOrEqual10ThenEven::validate(value).is_err());
/// }
pub type If<CONDITION, THEN> = Imply<CONDITION, THEN>;

#[cfg(test)]
mod test {
use crate::rule::composer::Imply;
Expand Down
4 changes: 2 additions & 2 deletions src/rule/composer/nand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod test_3 {
type Target6 = Nand![InvalidI8, ValidI8, InvalidI8]; // PASS
type Target7 = Nand![InvalidI8, InvalidI8, ValidI8]; // PASS
type Target8 = Nand![InvalidI8, InvalidI8, InvalidI8]; // PASS

#[test]
fn test_rule_binder_ok() {
assert!(Target2::validate(0).is_ok());
Expand All @@ -66,7 +66,7 @@ mod test_3 {
assert!(Target7::validate(0).is_ok());
assert!(Target8::validate(0).is_ok());
}

#[test]
fn test_rule_binder_err() {
assert!(Target1::validate(0).is_err());
Expand Down
2 changes: 1 addition & 1 deletion src/rule/composer/nor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Or;
use crate::rule::composer::Not;
use crate::Or;

/// This is a type that represents logical NOR in logic.
pub type Nor<RULE1, RULE2> = Not<Or![RULE1, RULE2]>;
Expand Down
4 changes: 2 additions & 2 deletions src/rule/composer/xor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ use crate::{And, Or};
/// type Target2 = Xor![ValidI8, InvalidI8]; // 1: PASS
/// type Target3 = Xor![InvalidI8, ValidI8]; // 1: PASS
/// type Target4 = Xor![InvalidI8, InvalidI8]; // 0: ERR
///
///
/// assert!(Target2::validate(0).is_ok());
/// assert!(Target3::validate(0).is_ok());
///
///
/// assert!(Target1::validate(0).is_err());
/// assert!(Target4::validate(0).is_err());
/// ```
Expand Down

0 comments on commit ab0f638

Please sign in to comment.