Skip to content

Commit ab0f638

Browse files
committed
add if and if-else
1 parent 03cea9c commit ab0f638

File tree

7 files changed

+79
-12
lines changed

7 files changed

+79
-12
lines changed

src/rule/composer.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod and;
22
mod equiv;
3+
mod if_else;
34
mod imply;
45
mod nand;
56
mod nor;
@@ -9,7 +10,8 @@ mod xor;
910

1011
pub use and::And;
1112
pub use equiv::Equiv;
12-
pub use imply::Imply;
13+
pub use if_else::IfElse;
14+
pub use imply::{If, Imply};
1315
pub use nand::Nand;
1416
pub use nor::Nor;
1517
pub use not::Not;

src/rule/composer/equiv.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use crate::rule::composer::imply::Imply;
22
use crate::And;
33

44
/// This is a type that represents logical equivalence in logic.
5-
///
5+
///
66
/// # Example
77
/// ```rust
88
/// use refined_type::rule::composer::Equiv;
99
/// use refined_type::rule::{EvenRuleI8, GreaterEqualRuleI8, Rule};
10-
///
10+
///
1111
/// type Target = Equiv<GreaterEqualRuleI8<10>, EvenRuleI8>;
12-
///
12+
///
1313
/// for value in vec![1, 10] {
1414
/// assert!(Target::validate(value).is_ok());
1515
/// }

src/rule/composer/if_else.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crate::rule::composer::Not;
2+
use crate::{And, Or};
3+
4+
/// This is a type that represents logical if-else in logic.
5+
/// # Example
6+
/// ```rust
7+
/// use refined_type::rule::composer::IfElse;
8+
///
9+
/// use refined_type::rule::{EvenRuleI8, GreaterEqualRuleI8, OddRuleI8, Rule};
10+
///
11+
/// type Target = IfElse<GreaterEqualRuleI8<10>, EvenRuleI8, OddRuleI8>;
12+
///
13+
/// for value in vec![1, 10] {
14+
/// assert!(Target::validate(value).is_ok());
15+
/// }
16+
///
17+
/// for value in vec![2, 11] {
18+
/// assert!(Target::validate(value).is_err());
19+
/// }
20+
/// ```
21+
pub type IfElse<CONDITION, THEN, ELSE> = Or![And![CONDITION, THEN], And![Not<CONDITION>, ELSE]];
22+
23+
#[cfg(test)]
24+
mod test {
25+
use crate::rule::composer::IfElse;
26+
use crate::rule::{EvenRuleI8, GreaterEqualRuleI8, OddRuleI8, Rule};
27+
28+
type Target = IfElse<GreaterEqualRuleI8<10>, EvenRuleI8, OddRuleI8>;
29+
30+
#[test]
31+
fn test_rule_binder_ok() {
32+
let table = vec![1, 10];
33+
34+
for value in table {
35+
assert!(Target::validate(value).is_ok());
36+
}
37+
}
38+
39+
#[test]
40+
fn test_rule_binder_err() {
41+
let table = vec![2, 11];
42+
43+
for value in table {
44+
assert!(Target::validate(value).is_err());
45+
}
46+
}
47+
}

src/rule/composer/imply.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,36 @@ use crate::Or;
77
/// ```rust
88
/// use refined_type::rule::composer::Imply;
99
/// use refined_type::rule::{EvenRuleI8, GreaterEqualRuleI8, Rule};
10-
///
10+
///
1111
/// type IfGreaterOrEqual10ThenEven = Imply<GreaterEqualRuleI8<10>, EvenRuleI8>;
12-
///
12+
///
1313
/// for value in vec![8, 9, 10, 12] {
1414
/// assert!(IfGreaterOrEqual10ThenEven::validate(value).is_ok());
1515
/// }
16-
///
16+
///
1717
/// for value in vec![11, 13] {
1818
/// assert!(IfGreaterOrEqual10ThenEven::validate(value).is_err());
1919
/// }
2020
pub type Imply<RULE1, RULE2> = Or![Not<RULE1>, RULE2];
2121

22+
/// This is a type that represents logical if in logic.
23+
///
24+
/// # Example
25+
/// ```rust
26+
/// use refined_type::rule::composer::If;
27+
/// use refined_type::rule::{EvenRuleI8, GreaterEqualRuleI8, Rule};
28+
///
29+
/// type IfGreaterOrEqual10ThenEven = If<GreaterEqualRuleI8<10>, EvenRuleI8>;
30+
///
31+
/// for value in vec![8, 9, 10, 12] {
32+
/// assert!(IfGreaterOrEqual10ThenEven::validate(value).is_ok());
33+
/// }
34+
///
35+
/// for value in vec![11, 13] {
36+
/// assert!(IfGreaterOrEqual10ThenEven::validate(value).is_err());
37+
/// }
38+
pub type If<CONDITION, THEN> = Imply<CONDITION, THEN>;
39+
2240
#[cfg(test)]
2341
mod test {
2442
use crate::rule::composer::Imply;

src/rule/composer/nand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ mod test_3 {
5555
type Target6 = Nand![InvalidI8, ValidI8, InvalidI8]; // PASS
5656
type Target7 = Nand![InvalidI8, InvalidI8, ValidI8]; // PASS
5757
type Target8 = Nand![InvalidI8, InvalidI8, InvalidI8]; // PASS
58-
58+
5959
#[test]
6060
fn test_rule_binder_ok() {
6161
assert!(Target2::validate(0).is_ok());
@@ -66,7 +66,7 @@ mod test_3 {
6666
assert!(Target7::validate(0).is_ok());
6767
assert!(Target8::validate(0).is_ok());
6868
}
69-
69+
7070
#[test]
7171
fn test_rule_binder_err() {
7272
assert!(Target1::validate(0).is_err());

src/rule/composer/nor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::Or;
21
use crate::rule::composer::Not;
2+
use crate::Or;
33

44
/// This is a type that represents logical NOR in logic.
55
pub type Nor<RULE1, RULE2> = Not<Or![RULE1, RULE2]>;

src/rule/composer/xor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ use crate::{And, Or};
1414
/// type Target2 = Xor![ValidI8, InvalidI8]; // 1: PASS
1515
/// type Target3 = Xor![InvalidI8, ValidI8]; // 1: PASS
1616
/// type Target4 = Xor![InvalidI8, InvalidI8]; // 0: ERR
17-
///
17+
///
1818
/// assert!(Target2::validate(0).is_ok());
1919
/// assert!(Target3::validate(0).is_ok());
20-
///
20+
///
2121
/// assert!(Target1::validate(0).is_err());
2222
/// assert!(Target4::validate(0).is_err());
2323
/// ```

0 commit comments

Comments
 (0)