Skip to content

Commit 098b9c0

Browse files
committed
update readme
1 parent 67eded6 commit 098b9c0

File tree

2 files changed

+211
-5
lines changed

2 files changed

+211
-5
lines changed

README.md

+123-3
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@ By using Rule Composer, composite rules can be easily created.
135135

136136
`And` Rule Composer is a rule that satisfies both of the two rules.
137137
It is generally effective when you want to narrow down the condition range.
138+
138139
```rust
139140
type Target = Refined<And![EvenRuleU8, MinMaxRuleU8<0, 100>]>;
140141
```
142+
141143
```rust
142144
fn and_example() -> Result<(), Error<u8>> {
143145
let target = Target::new(50)?;
@@ -158,6 +160,7 @@ It is generally effective when you want to expand the condition range.
158160
```rust
159161
type Target = Refined<Or![LessRuleU8<10>, GreaterRuleU8<50>]>;
160162
```
163+
161164
```rust
162165
fn or_example() -> Result<(), Error<u8>> {
163166
let target = Target::new(5)?;
@@ -184,6 +187,7 @@ It is generally effective when you want to discard only certain situations.
184187
```rust
185188
type Target = Refined<Not<EqualRuleU8<50>>>;
186189
```
190+
187191
```rust
188192
fn not_example() -> Result<(), Error<u8>> {
189193
let target = Target::new(49)?;
@@ -225,7 +229,8 @@ fn if_example() -> Result<(), Error<i8>> {
225229

226230
### 5: `IfElse` Rule Composer
227231

228-
`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.
232+
`IfElse` Rule Composer is a rule that applies a specific rule when a certain condition is met and another rule when it
233+
is not met.
229234

230235
```rust
231236
type Target = Refined<IfElse<GreaterEqualRuleI8<10>, EvenRuleI8, OddRuleI8>>;
@@ -396,7 +401,6 @@ fn range_example() -> Result<(), Error<u8>> {
396401
}
397402
```
398403

399-
400404
# Iterator
401405

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

562+
## `CountEqual`
563+
564+
`CountEqual` is a type that signifies the number of elements that satisfy the rule is a specific number.
565+
566+
```rust
567+
fn count_equal_example() -> Result<(), Error<Vec<i32>>> {
568+
let table = vec![
569+
(vec!["good morning".to_string(), "hello".to_string()], false),
570+
(vec!["good morning".to_string(), "".to_string()], true),
571+
(vec!["".to_string(), "hello".to_string()], true),
572+
(vec!["".to_string(), "".to_string()], false),
573+
];
574+
575+
for (value, expected) in table {
576+
let refined = CountEqualVec::<1, NonEmptyStringRule>::new(value.clone());
577+
assert_eq!(refined.is_ok(), expected);
578+
}
579+
580+
Ok(())
581+
}
582+
```
583+
584+
## `CountLess`
585+
586+
`CountLess` is a type that signifies the number of elements that satisfy the rule is less than a specific number.
587+
588+
```rust
589+
fn count_less_example() -> Result<(), Error<Vec<i32>>> {
590+
let table = vec![
591+
(vec!["good morning".to_string(), "hello".to_string()], false),
592+
(vec!["good morning".to_string(), "".to_string()], true),
593+
(vec!["".to_string(), "hello".to_string()], true),
594+
(vec!["".to_string(), "".to_string()], true),
595+
];
596+
597+
for (value, expected) in table {
598+
let refined = CountLessVec::<2, NonEmptyStringRule>::new(value.clone());
599+
assert_eq!(refined.is_ok(), expected);
600+
}
601+
602+
Ok(())
603+
}
604+
```
605+
606+
## `CountGreater`
607+
608+
`CountGreater` is a type that signifies the number of elements that satisfy the rule is greater than a specific number.
609+
610+
```rust
611+
fn count_greater_example() -> Result<(), Error<Vec<i32>>> {
612+
let table = vec![
613+
(vec!["good morning".to_string(), "hello".to_string()], true),
614+
(vec!["good morning".to_string(), "".to_string()], false),
615+
(vec!["".to_string(), "hello".to_string()], false),
616+
(vec!["".to_string(), "".to_string()], false),
617+
];
618+
619+
for (value, expected) in table {
620+
let refined = CountGreaterVec::<1, NonEmptyStringRule>::new(value.clone());
621+
assert_eq!(refined.is_ok(), expected);
622+
}
623+
624+
Ok(())
625+
}
626+
```
627+
628+
## `CountLessEqual`
629+
630+
`CountLessEqual` is a type that signifies the number of elements that satisfy the rule is less than or equal to a
631+
specific number.
632+
633+
```rust
634+
fn count_less_equal_example() -> Result<(), Error<Vec<i32>>> {
635+
let table = vec![
636+
(vec!["good morning".to_string(), "hello".to_string()], true),
637+
(vec!["good morning".to_string(), "".to_string()], true),
638+
(vec!["".to_string(), "hello".to_string()], true),
639+
(vec!["".to_string(), "".to_string()], true),
640+
];
641+
642+
for (value, expected) in table {
643+
let refined = CountLessEqualVec::<2, NonEmptyStringRule>::new(value.clone());
644+
assert_eq!(refined.is_ok(), expected);
645+
}
646+
647+
Ok(())
648+
}
649+
```
650+
651+
## `CountGreaterEqual`
652+
653+
`CountGreaterEqual` is a type that signifies the number of elements that satisfy the rule is greater than or equal to a
654+
specific number.
655+
656+
```rust
657+
fn count_greater_equal_example() -> Result<(), Error<Vec<i32>>> {
658+
let table = vec![
659+
(vec!["good morning".to_string(), "hello".to_string()], true),
660+
(vec!["good morning".to_string(), "".to_string()], true),
661+
(vec!["".to_string(), "hello".to_string()], true),
662+
(vec!["".to_string(), "".to_string()], false),
663+
];
664+
665+
for (value, expected) in table {
666+
let refined = CountGreaterEqualVec::<1, NonEmptyStringRule>::new(value.clone());
667+
assert_eq!(refined.is_ok(), expected);
668+
}
669+
670+
Ok(())
671+
}
672+
```
673+
558674
## `Reverse`
559675

560-
`Reverse` is a rule that applies a specific rule to all elements in the Iterator in reverse order.
676+
`Reverse` is a rule that applies a specific rule to all elements in the Iterator in reverse order.
561677

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

627743
## `LengthMinMax`
744+
628745
`LengthMinMax` is a type that signifies the target has a length between a certain number and another number.
629746

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

647764
## `LengthGreater`
765+
648766
`LengthGreater` is a type that signifies the target has a length greater than a certain number.
649767

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

664782
## `LengthLess`
783+
665784
`LengthLess` is a type that signifies the target has a length less than a certain number.
666785

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

681800
## `LengthEqual`
801+
682802
`LengthEqual` is a type that signifies the target has a length equal to a certain number.
683803

684804
```rust

tests/read_me.rs

+88-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use serde_json::json;
44
use refined_type::result::Error;
55
use refined_type::rule::composer::{If, IfElse, Not};
66
use refined_type::rule::{
7-
EqualU8, EvenRuleI8, ExistsVec, ForAllVec, GreaterEqualRuleI8, GreaterEqualU8, GreaterU8,
8-
HeadVec, IndexRuleVec, IndexVec, InitVec, LastVec, LengthDefinition, LengthEqual,
7+
CountEqualVec, CountGreaterEqualVec, CountGreaterVec, CountLessEqualVec,
8+
CountLessVec, EqualU8, EvenRuleI8, ExistsVec, ForAllVec, GreaterEqualRuleI8, GreaterEqualU8,
9+
GreaterU8, HeadVec, IndexRuleVec, IndexVec, InitVec, LastVec, LengthDefinition, LengthEqual,
910
LengthEqualRule, LengthGreater, LengthLess, LengthMinMax, LessEqualU8, LessU8, MinMaxU8,
1011
NonEmptyString, NonEmptyStringRule, NonEmptyVec, NonEmptyVecDeque, OddRuleI8, RangeU8, Reverse,
1112
Rule, SkipFirst, SkipVec, TailVec,
@@ -525,6 +526,91 @@ fn example_17() -> anyhow::Result<()> {
525526
Ok(())
526527
}
527528

529+
#[test]
530+
fn count_equal_example() -> Result<(), Error<Vec<i32>>> {
531+
let table = vec![
532+
(vec!["good morning".to_string(), "hello".to_string()], false),
533+
(vec!["good morning".to_string(), "".to_string()], true),
534+
(vec!["".to_string(), "hello".to_string()], true),
535+
(vec!["".to_string(), "".to_string()], false),
536+
];
537+
538+
for (value, expected) in table {
539+
let refined = CountEqualVec::<1, NonEmptyStringRule>::new(value.clone());
540+
assert_eq!(refined.is_ok(), expected);
541+
}
542+
543+
Ok(())
544+
}
545+
546+
#[test]
547+
fn count_less_example() -> Result<(), Error<Vec<i32>>> {
548+
let table = vec![
549+
(vec!["good morning".to_string(), "hello".to_string()], false),
550+
(vec!["good morning".to_string(), "".to_string()], true),
551+
(vec!["".to_string(), "hello".to_string()], true),
552+
(vec!["".to_string(), "".to_string()], true),
553+
];
554+
555+
for (value, expected) in table {
556+
let refined = CountLessVec::<2, NonEmptyStringRule>::new(value.clone());
557+
assert_eq!(refined.is_ok(), expected);
558+
}
559+
560+
Ok(())
561+
}
562+
563+
#[test]
564+
fn count_greater_example() -> Result<(), Error<Vec<i32>>> {
565+
let table = vec![
566+
(vec!["good morning".to_string(), "hello".to_string()], true),
567+
(vec!["good morning".to_string(), "".to_string()], false),
568+
(vec!["".to_string(), "hello".to_string()], false),
569+
(vec!["".to_string(), "".to_string()], false),
570+
];
571+
572+
for (value, expected) in table {
573+
let refined = CountGreaterVec::<1, NonEmptyStringRule>::new(value.clone());
574+
assert_eq!(refined.is_ok(), expected);
575+
}
576+
577+
Ok(())
578+
}
579+
580+
#[test]
581+
fn count_less_equal_example() -> Result<(), Error<Vec<i32>>> {
582+
let table = vec![
583+
(vec!["good morning".to_string(), "hello".to_string()], true),
584+
(vec!["good morning".to_string(), "".to_string()], true),
585+
(vec!["".to_string(), "hello".to_string()], true),
586+
(vec!["".to_string(), "".to_string()], true),
587+
];
588+
589+
for (value, expected) in table {
590+
let refined = CountLessEqualVec::<2, NonEmptyStringRule>::new(value.clone());
591+
assert_eq!(refined.is_ok(), expected);
592+
}
593+
594+
Ok(())
595+
}
596+
597+
#[test]
598+
fn count_greater_equal_example() -> Result<(), Error<Vec<i32>>> {
599+
let table = vec![
600+
(vec!["good morning".to_string(), "hello".to_string()], true),
601+
(vec!["good morning".to_string(), "".to_string()], true),
602+
(vec!["".to_string(), "hello".to_string()], true),
603+
(vec!["".to_string(), "".to_string()], false),
604+
];
605+
606+
for (value, expected) in table {
607+
let refined = CountGreaterEqualVec::<1, NonEmptyStringRule>::new(value.clone());
608+
assert_eq!(refined.is_ok(), expected);
609+
}
610+
611+
Ok(())
612+
}
613+
528614
#[test]
529615
fn example_18() -> Result<(), Error<Vec<i32>>> {
530616
let table = vec![

0 commit comments

Comments
 (0)