11
11
use std:: fmt;
12
12
use std:: marker:: PhantomData ;
13
13
14
+ use reflection;
14
15
use Predicate ;
15
16
16
17
/// Predicate that combines two `Predicate`s, returning the AND of the results.
53
54
fn eval ( & self , item : & Item ) -> bool {
54
55
self . a . eval ( item) && self . b . eval ( item)
55
56
}
57
+
58
+ fn find_case < ' a > ( & ' a self , expected : bool , variable : & Item ) -> Option < reflection:: Case < ' a > > {
59
+ let child_a = self . a . find_case ( expected, variable) ;
60
+ match ( expected, child_a) {
61
+ ( true , Some ( child_a) ) => self . b . find_case ( expected, variable) . map ( |child_b| {
62
+ reflection:: Case :: new ( Some ( self ) , expected)
63
+ . add_child ( child_a)
64
+ . add_child ( child_b)
65
+ } ) ,
66
+ ( true , None ) => None ,
67
+ ( false , Some ( child_a) ) => {
68
+ Some ( reflection:: Case :: new ( Some ( self ) , expected) . add_child ( child_a) )
69
+ }
70
+ ( false , None ) => self . b
71
+ . find_case ( expected, variable)
72
+ . map ( |child_b| reflection:: Case :: new ( Some ( self ) , expected) . add_child ( child_b) ) ,
73
+ }
74
+ }
75
+ }
76
+
77
+ impl < M1 , M2 , Item > reflection:: PredicateReflection for AndPredicate < M1 , M2 , Item >
78
+ where
79
+ M1 : Predicate < Item > ,
80
+ M2 : Predicate < Item > ,
81
+ Item : ?Sized ,
82
+ {
83
+ fn children < ' a > ( & ' a self ) -> Box < Iterator < Item = reflection:: Child < ' a > > + ' a > {
84
+ let params = vec ! [
85
+ reflection:: Child :: new( "left" , & self . a) ,
86
+ reflection:: Child :: new( "right" , & self . b) ,
87
+ ] ;
88
+ Box :: new ( params. into_iter ( ) )
89
+ }
56
90
}
57
91
58
92
impl < M1 , M2 , Item > fmt:: Display for AndPredicate < M1 , M2 , Item >
@@ -66,6 +100,91 @@ where
66
100
}
67
101
}
68
102
103
+ #[ cfg( test) ]
104
+ mod test_and {
105
+ use prelude:: * ;
106
+
107
+ #[ test]
108
+ fn find_case_true ( ) {
109
+ assert ! (
110
+ predicate:: always( )
111
+ . and( predicate:: always( ) )
112
+ . find_case( true , & 5 )
113
+ . is_some( )
114
+ ) ;
115
+ }
116
+
117
+ #[ test]
118
+ fn find_case_true_left_fail ( ) {
119
+ assert ! (
120
+ predicate:: never( )
121
+ . and( predicate:: always( ) )
122
+ . find_case( true , & 5 )
123
+ . is_none( )
124
+ ) ;
125
+ }
126
+
127
+ #[ test]
128
+ fn find_case_true_right_fail ( ) {
129
+ assert ! (
130
+ predicate:: always( )
131
+ . and( predicate:: never( ) )
132
+ . find_case( true , & 5 )
133
+ . is_none( )
134
+ ) ;
135
+ }
136
+
137
+ #[ test]
138
+ fn find_case_true_fails ( ) {
139
+ assert ! (
140
+ predicate:: never( )
141
+ . and( predicate:: never( ) )
142
+ . find_case( true , & 5 )
143
+ . is_none( )
144
+ ) ;
145
+ }
146
+
147
+ #[ test]
148
+ fn find_case_false ( ) {
149
+ assert ! (
150
+ predicate:: never( )
151
+ . and( predicate:: never( ) )
152
+ . find_case( false , & 5 )
153
+ . is_some( )
154
+ ) ;
155
+ }
156
+
157
+ #[ test]
158
+ fn find_case_false_fails ( ) {
159
+ assert ! (
160
+ predicate:: always( )
161
+ . and( predicate:: always( ) )
162
+ . find_case( false , & 5 )
163
+ . is_none( )
164
+ ) ;
165
+ }
166
+
167
+ #[ test]
168
+ fn find_case_false_left_fail ( ) {
169
+ assert ! (
170
+ predicate:: never( )
171
+ . and( predicate:: always( ) )
172
+ . find_case( false , & 5 )
173
+ . is_some( )
174
+ ) ;
175
+ }
176
+
177
+ #[ test]
178
+ fn find_case_false_right_fail ( ) {
179
+ assert ! (
180
+ predicate:: always( )
181
+ . and( predicate:: never( ) )
182
+ . find_case( false , & 5 )
183
+ . is_some( )
184
+ ) ;
185
+ }
186
+ }
187
+
69
188
/// Predicate that combines two `Predicate`s, returning the OR of the results.
70
189
///
71
190
/// This is created by the `Predicate::or` function.
@@ -106,6 +225,39 @@ where
106
225
fn eval ( & self , item : & Item ) -> bool {
107
226
self . a . eval ( item) || self . b . eval ( item)
108
227
}
228
+
229
+ fn find_case < ' a > ( & ' a self , expected : bool , variable : & Item ) -> Option < reflection:: Case < ' a > > {
230
+ let child_a = self . a . find_case ( expected, variable) ;
231
+ match ( expected, child_a) {
232
+ ( true , Some ( child_a) ) => {
233
+ Some ( reflection:: Case :: new ( Some ( self ) , expected) . add_child ( child_a) )
234
+ }
235
+ ( true , None ) => self . b
236
+ . find_case ( expected, variable)
237
+ . map ( |child_b| reflection:: Case :: new ( Some ( self ) , expected) . add_child ( child_b) ) ,
238
+ ( false , Some ( child_a) ) => self . b . find_case ( expected, variable) . map ( |child_b| {
239
+ reflection:: Case :: new ( Some ( self ) , expected)
240
+ . add_child ( child_a)
241
+ . add_child ( child_b)
242
+ } ) ,
243
+ ( false , None ) => None ,
244
+ }
245
+ }
246
+ }
247
+
248
+ impl < M1 , M2 , Item > reflection:: PredicateReflection for OrPredicate < M1 , M2 , Item >
249
+ where
250
+ M1 : Predicate < Item > ,
251
+ M2 : Predicate < Item > ,
252
+ Item : ?Sized ,
253
+ {
254
+ fn children < ' a > ( & ' a self ) -> Box < Iterator < Item = reflection:: Child < ' a > > + ' a > {
255
+ let params = vec ! [
256
+ reflection:: Child :: new( "left" , & self . a) ,
257
+ reflection:: Child :: new( "right" , & self . b) ,
258
+ ] ;
259
+ Box :: new ( params. into_iter ( ) )
260
+ }
109
261
}
110
262
111
263
impl < M1 , M2 , Item > fmt:: Display for OrPredicate < M1 , M2 , Item >
@@ -119,6 +271,91 @@ where
119
271
}
120
272
}
121
273
274
+ #[ cfg( test) ]
275
+ mod test_or {
276
+ use prelude:: * ;
277
+
278
+ #[ test]
279
+ fn find_case_true ( ) {
280
+ assert ! (
281
+ predicate:: always( )
282
+ . or( predicate:: always( ) )
283
+ . find_case( true , & 5 )
284
+ . is_some( )
285
+ ) ;
286
+ }
287
+
288
+ #[ test]
289
+ fn find_case_true_left_fail ( ) {
290
+ assert ! (
291
+ predicate:: never( )
292
+ . or( predicate:: always( ) )
293
+ . find_case( true , & 5 )
294
+ . is_some( )
295
+ ) ;
296
+ }
297
+
298
+ #[ test]
299
+ fn find_case_true_right_fail ( ) {
300
+ assert ! (
301
+ predicate:: always( )
302
+ . or( predicate:: never( ) )
303
+ . find_case( true , & 5 )
304
+ . is_some( )
305
+ ) ;
306
+ }
307
+
308
+ #[ test]
309
+ fn find_case_true_fails ( ) {
310
+ assert ! (
311
+ predicate:: never( )
312
+ . or( predicate:: never( ) )
313
+ . find_case( true , & 5 )
314
+ . is_none( )
315
+ ) ;
316
+ }
317
+
318
+ #[ test]
319
+ fn find_case_false ( ) {
320
+ assert ! (
321
+ predicate:: never( )
322
+ . or( predicate:: never( ) )
323
+ . find_case( false , & 5 )
324
+ . is_some( )
325
+ ) ;
326
+ }
327
+
328
+ #[ test]
329
+ fn find_case_false_fails ( ) {
330
+ assert ! (
331
+ predicate:: always( )
332
+ . or( predicate:: always( ) )
333
+ . find_case( false , & 5 )
334
+ . is_none( )
335
+ ) ;
336
+ }
337
+
338
+ #[ test]
339
+ fn find_case_false_left_fail ( ) {
340
+ assert ! (
341
+ predicate:: never( )
342
+ . or( predicate:: always( ) )
343
+ . find_case( false , & 5 )
344
+ . is_none( )
345
+ ) ;
346
+ }
347
+
348
+ #[ test]
349
+ fn find_case_false_right_fail ( ) {
350
+ assert ! (
351
+ predicate:: always( )
352
+ . or( predicate:: never( ) )
353
+ . find_case( false , & 5 )
354
+ . is_none( )
355
+ ) ;
356
+ }
357
+ }
358
+
122
359
/// Predicate that returns a `Predicate` taking the logical NOT of the result.
123
360
///
124
361
/// This is created by the `Predicate::not` function.
@@ -154,6 +391,23 @@ where
154
391
fn eval ( & self , item : & Item ) -> bool {
155
392
!self . inner . eval ( item)
156
393
}
394
+
395
+ fn find_case < ' a > ( & ' a self , expected : bool , variable : & Item ) -> Option < reflection:: Case < ' a > > {
396
+ self . inner
397
+ . find_case ( !expected, variable)
398
+ . map ( |child| reflection:: Case :: new ( Some ( self ) , expected) . add_child ( child) )
399
+ }
400
+ }
401
+
402
+ impl < M , Item > reflection:: PredicateReflection for NotPredicate < M , Item >
403
+ where
404
+ M : Predicate < Item > ,
405
+ Item : ?Sized ,
406
+ {
407
+ fn children < ' a > ( & ' a self ) -> Box < Iterator < Item = reflection:: Child < ' a > > + ' a > {
408
+ let params = vec ! [ reflection:: Child :: new( "predicate" , & self . inner) ] ;
409
+ Box :: new ( params. into_iter ( ) )
410
+ }
157
411
}
158
412
159
413
impl < M , Item > fmt:: Display for NotPredicate < M , Item >
0 commit comments