@@ -13,13 +13,13 @@ use refined_type::rule::MinMaxU8Rule;
13
13
use refined_type :: Refined ;
14
14
use std :: ops :: Deref ;
15
15
16
- fn main () {
17
- let rule = MinMaxU8Rule :: new (1 , 6 ). unwrap ();
16
+ type NonEmptyString = Refined <NonEmptyStringRule >;
18
17
19
- let five = Refined :: new (5 , & rule );
20
- assert_eq! (five . deref (), 5 );
18
+ fn main () {
19
+ let hello_world = NonEmptyString :: new (" hello world" . to_string ());
20
+ assert_eq! (five . deref (), " hello world" );
21
21
22
- let eight = Refined :: new (8 , & rule );
22
+ let empty_string = NonEmptyString :: new ("" . to_string () );
23
23
assert! (eight . is_err ());
24
24
}
25
25
```
@@ -37,42 +37,15 @@ Once the definition is complete, all you need to do is implement the Rule trait.
37
37
Add your preferred conditions as you like.
38
38
39
39
``` rust
40
- use refined_type :: rule :: Rule ;
41
- use refined_type :: result :: Error ;
40
+ use refined_type :: rule :: {NonEmptyString , NonEmptyStringRule };
42
41
use refined_type :: Refined ;
43
42
44
- struct BiggerRule {
45
- than : u32
46
- }
47
-
48
- impl BiggerRule {
49
- pub fn new (than : u32 ) -> Self {
50
- Self { than }
51
- }
52
- }
53
-
54
- impl Rule for BiggerRule {
55
- type Item = u32 ;
56
- fn validate (& self , target : Self :: Item ) -> Result <Self :: Item , Error <Self :: Item >> {
57
- if target > self . than {
58
- Ok (target )
59
- }
60
- else {
61
- Err (Error :: new (
62
- format! (" {} is not bigger than {}" , target , self . than)
63
- ))
64
- }
65
- }
66
- }
67
-
68
43
fn main () {
69
- let bigger_than_five_rule = BiggerRule :: new (5 );
44
+ let non_empty_string_result = Refined :: <NonEmptyStringRule >:: new (" Hello World" . to_string ());
45
+ assert_eq! (non_empty_string_result . unwrap (). deref (), " Hello World" );
70
46
71
- let bigger_than_five_result_ok = Refined :: new (7 , & bigger_than_five_rule );
72
- let bigger_than_five_result_err = Refined :: new (3 , & bigger_than_five_rule );
73
-
74
- assert! (bigger_than_five_result_ok . is_ok ());
75
- assert! (bigger_than_five_result_err . is_err ());
47
+ let empty_string_result = Refined :: <NonEmptyStringRule >:: new ("" . to_string ());
48
+ assert! (empty_string_result . is_err ())
76
49
}
77
50
```
78
51
@@ -96,7 +69,7 @@ struct ContainsWorldRule;
96
69
impl Rule for ContainsHelloRule {
97
70
type Item = String ;
98
71
99
- fn validate (& self , target : Self :: Item ) -> Result <Self :: Item , Error <Self :: Item >> {
72
+ fn validate (target : Self :: Item ) -> Result <Self :: Item , Error <Self :: Item >> {
100
73
if target . contains (" Hello" ) {
101
74
Ok (target )
102
75
}
@@ -109,7 +82,7 @@ impl Rule for ContainsHelloRule {
109
82
impl Rule for ContainsWorldRule {
110
83
type Item = String ;
111
84
112
- fn validate (& self , target : Self :: Item ) -> Result <Self :: Item , Error <Self :: Item >> {
85
+ fn validate (target : Self :: Item ) -> Result <Self :: Item , Error <Self :: Item >> {
113
86
if target . contains (" World" ) {
114
87
Ok (target )
115
88
}
@@ -125,12 +98,12 @@ impl Rule for ContainsWorldRule {
125
98
It is generally effective when you want to narrow down the condition range.
126
99
``` rust
127
100
fn main () {
128
- let rule = And :: new ( ContainsHelloRule , ContainsWorldRule ) ;
101
+ type HelloAndWorldRule = And < ContainsHelloRule , ContainsWorldRule > ;
129
102
130
- let rule_ok = Refined :: new (" Hello! World!" . to_string (), & rule );
103
+ let rule_ok = Refind :: < HelloAndWorldRule > :: new (" Hello! World!" . to_string ());
131
104
assert! (rule_ok . is_ok ());
132
105
133
- let rule_err = Refined :: new (" Hello, world!" . to_string (), & rule );
106
+ let rule_err = Refined :: < HelloAndWorldRule > :: new (" Hello, world!" . to_string ());
134
107
assert! (rule_err . is_err ());
135
108
}
136
109
```
@@ -140,15 +113,15 @@ fn main() {
140
113
It is generally effective when you want to expand the condition range.
141
114
``` rust
142
115
fn main () {
143
- let rule = Or :: new ( ContainsHelloRule , ContainsWorldRule ) ;
116
+ type HelloOrWorldRule = Or < ContainsHelloRule , ContainsWorldRule > ;
144
117
145
- let rule_ok_1 = Refined :: new (" Hello! World!" . to_string (), & rule );
118
+ let rule_ok_1 = Refined :: < HelloOrWorldRule > :: new (" Hello! World!" . to_string ());
146
119
assert! (rule_ok_1 . is_ok ());
147
120
148
- let rule_ok_2 = Refined :: new (" hello World!" . to_string (), & rule );
121
+ let rule_ok_2 = Refined :: < HelloOrWorldRule > :: new (" hello World!" . to_string ());
149
122
assert! (rule_ok_2 . is_ok ());
150
123
151
- let rule_err = Refined :: new (" hello, world!" . to_string (), & rule );
124
+ let rule_err = Refined :: < HelloOrWorldRule > :: new (" hello, world!" . to_string ());
152
125
assert! (rule_err . is_err ());
153
126
}
154
127
```
@@ -158,12 +131,12 @@ fn main() {
158
131
It is generally effective when you want to discard only certain situations.
159
132
``` rust
160
133
fn main () {
161
- let rule = Not :: new ( ContainsHelloRule ) ;
134
+ type NotHelloRule = Not < ContainsHelloRule > ;
162
135
163
- let rule_ok = Refined :: new (" hello! World!" . to_string (), & rule );
136
+ let rule_ok = Refined :: < NotHelloRule > :: new (" hello! World!" . to_string ());
164
137
assert! (rule_ok . is_ok ());
165
138
166
- let rule_err = Refined :: new (" Hello, World!" . to_string (), & rule );
139
+ let rule_err = Refined :: < NotHelloRule > :: new (" Hello, World!" . to_string ());
167
140
assert! (rule_err . is_err ());
168
141
}
169
142
```
@@ -173,37 +146,20 @@ Rule Composer is also a rule.
173
146
Therefore, it can be treated much like a composite function
174
147
``` rust
175
148
fn main () {
176
- let less_than_3 = LessI8Rule :: new (3 );
177
- let more_than_1 = MoreI8Rule :: new (1 );
178
-
179
- // (1 <= x <= 3)
180
- let more_than_1_and_less_than_3 = And :: new (less_than_3 , more_than_1 );
181
-
182
- assert! (more_than_1_and_less_than_3 . validate (0 ). is_err ());
183
- assert! (more_than_1_and_less_than_3 . validate (2 ). is_ok ());
184
- assert! (more_than_1_and_less_than_3 . validate (4 ). is_err ());
185
-
186
- let more_than_5 = MoreI8Rule :: new (5 );
187
-
188
- // (1 <= x <= 3) or (5 <= x)
189
- let or_more_than_5 = Or :: new (more_than_1_and_less_than_3 , more_than_5 );
149
+ struct StartWithHelloRule ;
150
+ struct StartWithByeRule ;
190
151
191
- assert! (or_more_than_5 . validate (0 ). is_err ());
192
- assert! (or_more_than_5 . validate (2 ). is_ok ());
193
- assert! (or_more_than_5 . validate (4 ). is_err ());
194
- assert! (or_more_than_5 . validate (5 ). is_ok ());
195
- assert! (or_more_than_5 . validate (100 ). is_ok ());
152
+ struct EndWithJohnRule ;
196
153
197
- let more_than_7 = MoreI8Rule :: new (7 );
154
+ type StartWithHelloOrByeRule = Or <StartWithHelloRule , StartWithByeRule >;
155
+ type GreetingRule = And <StartWithHelloOrByeRule , EndWithJohnRule >;
198
156
199
- // ((1 <= x <= 3) or (5 <= x)) & (x < 7)
200
- let not_more_than_7 = And :: new (or_more_than_5 , Not :: new (more_than_7 ));
157
+ type Greeting = Refined <GreetingRule >;
201
158
202
- assert! (not_more_than_7 . validate (0 ). is_err ());
203
- assert! (not_more_than_7 . validate (2 ). is_ok ());
204
- assert! (not_more_than_7 . validate (4 ). is_err ());
205
- assert! (not_more_than_7 . validate (5 ). is_ok ());
206
- assert! (not_more_than_7 . validate (100 ). is_err ());
159
+ assert! (GreetingRule :: validate (" Hello! Nice to meet you John" . to_string ()). is_ok ());
160
+ assert! (Greeting :: validate (" Bye! Have a good day John" . to_string ()). is_ok ());
161
+ assert! (Greeting :: validate (" How are you? Have a good day John" . to_string ()). is_err ());
162
+ assert! (Greeting :: validate (" Bye! Have a good day Tom" . to_string ()). is_err ());
207
163
}
208
164
```
209
165
@@ -212,15 +168,15 @@ Directly writing `And`, `Or`, `Not` or `Refined` can often lead to a decrease in
212
168
Therefore, using ** type aliases** can help make your code clearer.
213
169
214
170
``` rust
215
- type ContainsHelloAndWorldRule = And <String , ContainsHelloRule , ContainsWorldRule >;
171
+ type ContainsHelloAndWorldRule = And <ContainsHelloRule , ContainsWorldRule >;
216
172
217
- type ContainsHelloAndWorld = Refined <ContainsHelloAndWorldRule , String >;
173
+ type ContainsHelloAndWorld = Refined <ContainsHelloAndWorldRule >;
218
174
```
219
175
220
176
# License
221
177
MIT License
222
178
223
- Copyright (c) 2023 Tomoki Someya
179
+ Copyright (c) 2024 Tomoki Someya
224
180
225
181
Permission is hereby granted, free of charge, to any person obtaining a copy
226
182
of this software and associated documentation files (the "Software"), to deal
0 commit comments