1
1
<h2 align =" center " >Refined Type</h2 >
2
2
3
3
<div align =" center " >
4
- <img src="https://github.com/tomoikey/refined_type/actions/workflows/ci.yml/badge.svg"/>
5
- <img src="https://github.com/tomoikey/refined_type/actions/workflows/publish.yml/badge.svg"/>
6
- <br/>
4
+ <div>
5
+ <img src="https://img.shields.io/crates/v/refined_type.svg"/>
6
+ <img src="https://img.shields.io/crates/d/refined_type"/>
7
+ </div>
7
8
<i>Code More simply, More safely, for all Rustaceans.🦀</i>
8
9
<br/>
9
10
<img width=550 src="https://github.com/user-attachments/assets/2ae4bfee-1d42-4ed7-820a-b13260d359ef">
10
11
<br/>
11
- <a href="https://github.com/tomoikey/refined_type/stargazers">
12
- <img src="https://img.shields.io/github/stars/tomoikey/refined_type" alt="Stars Badge"/>
13
- </a>
14
- <a href="https://github.com/tomoikey/refined_type/network/members">
15
- <img src="https://img.shields.io/github/forks/tomoikey/refined_type" alt="Forks Badge"/>
16
- </a>
12
+ <div>
13
+ <a href="https://github.com/tomoikey/refined_type/stargazers">
14
+ <img src="https://img.shields.io/github/stars/tomoikey/refined_type" alt="Stars Badge"/>
15
+ </a>
16
+ <a href="https://github.com/tomoikey/refined_type/network/members">
17
+ <img src="https://img.shields.io/github/forks/tomoikey/refined_type" alt="Forks Badge"/>
18
+ </a>
19
+ </div>
17
20
<a href="https://github.com/tomoikey/refined_type/pulls">
18
21
<img src="https://img.shields.io/github/issues-pr/tomoikey/refined_type" alt="Pull Requests Badge"/>
19
22
</a>
@@ -132,9 +135,11 @@ By using Rule Composer, composite rules can be easily created.
132
135
133
136
` And ` Rule Composer is a rule that satisfies both of the two rules.
134
137
It is generally effective when you want to narrow down the condition range.
138
+
135
139
``` rust
136
140
type Target = Refined <And! [EvenRuleU8 , MinMaxRuleU8 <0 , 100 >]>;
137
141
```
142
+
138
143
``` rust
139
144
fn and_example () -> Result <(), Error <u8 >> {
140
145
let target = Target :: new (50 )? ;
@@ -155,6 +160,7 @@ It is generally effective when you want to expand the condition range.
155
160
``` rust
156
161
type Target = Refined <Or! [LessRuleU8 <10 >, GreaterRuleU8 <50 >]>;
157
162
```
163
+
158
164
``` rust
159
165
fn or_example () -> Result <(), Error <u8 >> {
160
166
let target = Target :: new (5 )? ;
@@ -181,6 +187,7 @@ It is generally effective when you want to discard only certain situations.
181
187
``` rust
182
188
type Target = Refined <Not <EqualRuleU8 <50 >>>;
183
189
```
190
+
184
191
``` rust
185
192
fn not_example () -> Result <(), Error <u8 >> {
186
193
let target = Target :: new (49 )? ;
@@ -222,7 +229,8 @@ fn if_example() -> Result<(), Error<i8>> {
222
229
223
230
### 5: ` IfElse ` Rule Composer
224
231
225
- ` 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.
226
234
227
235
``` rust
228
236
type Target = Refined <IfElse <GreaterEqualRuleI8 <10 >, EvenRuleI8 , OddRuleI8 >>;
@@ -393,7 +401,6 @@ fn range_example() -> Result<(), Error<u8>> {
393
401
}
394
402
```
395
403
396
-
397
404
# Iterator
398
405
399
406
` refined_type ` has several useful refined types for Iterators.
@@ -552,9 +559,121 @@ fn example_17() -> anyhow::Result<()> {
552
559
}
553
560
```
554
561
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
+
555
674
## ` Reverse `
556
675
557
- ` 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.
558
677
559
678
``` rust
560
679
fn example_18 () -> Result <(), Error <Vec <i32 >>> {
@@ -622,6 +741,7 @@ impl<ITEM> SkipOption for NoSkip<ITEM> {
622
741
You can impose constraints on objects that have a length, such as ` String ` or ` Vec ` .
623
742
624
743
## ` LengthMinMax `
744
+
625
745
` LengthMinMax ` is a type that signifies the target has a length between a certain number and another number.
626
746
627
747
``` rust
@@ -642,6 +762,7 @@ fn length_min_max_example() -> Result<(), Error<String>> {
642
762
```
643
763
644
764
## ` LengthGreater `
765
+
645
766
` LengthGreater ` is a type that signifies the target has a length greater than a certain number.
646
767
647
768
``` rust
@@ -659,6 +780,7 @@ fn length_greater_example() -> Result<(), Error<String>> {
659
780
```
660
781
661
782
## ` LengthLess `
783
+
662
784
` LengthLess ` is a type that signifies the target has a length less than a certain number.
663
785
664
786
``` rust
@@ -676,6 +798,7 @@ fn length_less_example() -> Result<(), Error<String>> {
676
798
```
677
799
678
800
## ` LengthEqual `
801
+
679
802
` LengthEqual ` is a type that signifies the target has a length equal to a certain number.
680
803
681
804
``` rust
0 commit comments