1
1
use crate :: result:: Error ;
2
2
use crate :: rule:: Rule ;
3
+ use crate :: Refined ;
4
+
3
5
use std:: collections:: { BTreeMap , BTreeSet , HashMap , HashSet } ;
6
+ use std:: fmt:: Debug ;
4
7
use std:: marker:: PhantomData ;
8
+ use std:: ops:: Add ;
9
+
10
+ pub type Empty < T > = Refined < EmptyRule < T > > ;
11
+
12
+ impl < T > Add for Empty < T >
13
+ where
14
+ T : EmptyDefinition ,
15
+ {
16
+ type Output = Self ;
17
+
18
+ fn add ( self , _rhs : Self ) -> Self :: Output {
19
+ self
20
+ }
21
+ }
5
22
6
23
/// Rule where the data is empty
7
24
/// ```rust
8
- /// use refined_type::rule::{Empty , Rule};
25
+ /// use refined_type::rule::{EmptyRule , Rule};
9
26
///
10
- /// assert!(Empty ::<String>::validate("".to_string()).is_ok());
11
- /// assert!(Empty ::<String>::validate("non empty".to_string()).is_err());
27
+ /// assert!(EmptyRule ::<String>::validate("".to_string()).is_ok());
28
+ /// assert!(EmptyRule ::<String>::validate("non empty".to_string()).is_err());
12
29
///
13
- /// assert!(Empty ::<Vec<u8>>::validate(Vec::<u8>::new()).is_ok());
14
- /// assert!(Empty ::<Vec<u8>>::validate(vec![1, 2, 3]).is_err());
30
+ /// assert!(EmptyRule ::<Vec<u8>>::validate(Vec::<u8>::new()).is_ok());
31
+ /// assert!(EmptyRule ::<Vec<u8>>::validate(vec![1, 2, 3]).is_err());
15
32
///
16
- /// assert!(Empty ::<u8>::validate(0).is_ok());
17
- /// assert!(Empty ::<u8>::validate(1).is_err());
33
+ /// assert!(EmptyRule ::<u8>::validate(0).is_ok());
34
+ /// assert!(EmptyRule ::<u8>::validate(1).is_err());
18
35
/// ```
19
36
#[ derive( Debug , Clone , Copy , Eq , PartialEq , Ord , PartialOrd ) ]
20
- pub struct Empty < T > {
37
+ pub struct EmptyRule < T > {
21
38
_phantom_data : PhantomData < T > ,
22
39
}
23
-
24
40
pub trait EmptyDefinition {
25
41
fn empty ( & self ) -> bool ;
26
42
}
@@ -151,7 +167,7 @@ impl EmptyDefinition for f64 {
151
167
}
152
168
}
153
169
154
- impl < T > Rule for Empty < T >
170
+ impl < T > Rule for EmptyRule < T >
155
171
where
156
172
T : EmptyDefinition ,
157
173
{
@@ -165,3 +181,17 @@ where
165
181
}
166
182
}
167
183
}
184
+
185
+ #[ cfg( test) ]
186
+ mod test {
187
+ use crate :: rule:: Empty ;
188
+
189
+ #[ test]
190
+ fn test_add_empty ( ) -> anyhow:: Result < ( ) > {
191
+ let empty_1 = Empty :: new ( 0 ) ?;
192
+ let empty_2 = Empty :: new ( 0 ) ?;
193
+ let empty = empty_1 + empty_2;
194
+ assert_eq ! ( empty. into_value( ) , 0 ) ;
195
+ Ok ( ( ) )
196
+ }
197
+ }
0 commit comments