@@ -35,29 +35,17 @@ pub fn is_empty() -> IsEmptyPredicate {
35
35
IsEmptyPredicate { }
36
36
}
37
37
38
- #[ derive( Copy , Clone , Debug ) ]
39
- enum PatternOp {
40
- StartsWith ,
41
- EndsWith ,
42
- Contains ,
43
- }
44
-
45
- /// Predicate that checks for patterns.
38
+ /// Predicate checks start of str
46
39
///
47
- /// This is created by `predicates::str::{ starts_with, ends_with, contains} `.
40
+ /// This is created by `predicates::str::starts_with`.
48
41
#[ derive( Clone , Debug ) ]
49
- pub struct PatternPredicate {
42
+ pub struct StartsWithPredicate {
50
43
pattern : String ,
51
- op : PatternOp ,
52
44
}
53
45
54
- impl Predicate < str > for PatternPredicate {
46
+ impl Predicate < str > for StartsWithPredicate {
55
47
fn eval ( & self , variable : & str ) -> bool {
56
- match self . op {
57
- PatternOp :: StartsWith => variable. starts_with ( & self . pattern ) ,
58
- PatternOp :: EndsWith => variable. ends_with ( & self . pattern ) ,
59
- PatternOp :: Contains => variable. contains ( & self . pattern ) ,
60
- }
48
+ variable. starts_with ( & self . pattern )
61
49
}
62
50
}
63
51
@@ -72,13 +60,26 @@ impl Predicate<str> for PatternPredicate {
72
60
/// assert_eq!(true, predicate_fn.eval("Hello World"));
73
61
/// assert_eq!(false, predicate_fn.eval("Goodbye World"));
74
62
/// ```
75
- pub fn starts_with < P > ( pattern : P ) -> PatternPredicate
63
+ pub fn starts_with < P > ( pattern : P ) -> StartsWithPredicate
76
64
where
77
65
P : Into < String > ,
78
66
{
79
- PatternPredicate {
67
+ StartsWithPredicate {
80
68
pattern : pattern. into ( ) ,
81
- op : PatternOp :: StartsWith ,
69
+ }
70
+ }
71
+
72
+ /// Predicate checks end of str
73
+ ///
74
+ /// This is created by `predicates::str::ends_with`.
75
+ #[ derive( Clone , Debug ) ]
76
+ pub struct EndsWithPredicate {
77
+ pattern : String ,
78
+ }
79
+
80
+ impl Predicate < str > for EndsWithPredicate {
81
+ fn eval ( & self , variable : & str ) -> bool {
82
+ variable. ends_with ( & self . pattern )
82
83
}
83
84
}
84
85
@@ -93,13 +94,61 @@ where
93
94
/// assert_eq!(true, predicate_fn.eval("Hello World"));
94
95
/// assert_eq!(false, predicate_fn.eval("Hello Moon"));
95
96
/// ```
96
- pub fn ends_with < P > ( pattern : P ) -> PatternPredicate
97
+ pub fn ends_with < P > ( pattern : P ) -> EndsWithPredicate
97
98
where
98
99
P : Into < String > ,
99
100
{
100
- PatternPredicate {
101
+ EndsWithPredicate {
101
102
pattern : pattern. into ( ) ,
102
- op : PatternOp :: EndsWith ,
103
+ }
104
+ }
105
+
106
+ /// Predicate that checks for patterns.
107
+ ///
108
+ /// This is created by `predicates::str:contains`.
109
+ #[ derive( Clone , Debug ) ]
110
+ pub struct ContainsPredicate {
111
+ pattern : String ,
112
+ }
113
+
114
+ impl ContainsPredicate {
115
+ /// Require a specific count of matches.
116
+ ///
117
+ /// # Examples
118
+ ///
119
+ /// ```
120
+ /// use predicates::prelude::*;
121
+ ///
122
+ /// let predicate_fn = predicate::str::contains("Two").count(2);
123
+ /// assert_eq!(true, predicate_fn.eval("One Two Three Two One"));
124
+ /// assert_eq!(false, predicate_fn.eval("One Two Three"));
125
+ /// ```
126
+ pub fn count ( self , count : usize ) -> MatchesPredicate {
127
+ MatchesPredicate {
128
+ pattern : self . pattern ,
129
+ count,
130
+ }
131
+ }
132
+ }
133
+
134
+ impl Predicate < str > for ContainsPredicate {
135
+ fn eval ( & self , variable : & str ) -> bool {
136
+ variable. contains ( & self . pattern )
137
+ }
138
+ }
139
+
140
+ /// Predicate that checks for repeated patterns.
141
+ ///
142
+ /// This is created by `predicates::str::contains(...).count`.
143
+ #[ derive( Clone , Debug ) ]
144
+ pub struct MatchesPredicate {
145
+ pattern : String ,
146
+ count : usize ,
147
+ }
148
+
149
+ impl Predicate < str > for MatchesPredicate {
150
+ fn eval ( & self , variable : & str ) -> bool {
151
+ variable. matches ( & self . pattern ) . count ( ) == self . count
103
152
}
104
153
}
105
154
@@ -114,12 +163,11 @@ where
114
163
/// assert_eq!(true, predicate_fn.eval("One Two Three"));
115
164
/// assert_eq!(false, predicate_fn.eval("Four Five Six"));
116
165
/// ```
117
- pub fn contains < P > ( pattern : P ) -> PatternPredicate
166
+ pub fn contains < P > ( pattern : P ) -> ContainsPredicate
118
167
where
119
168
P : Into < String > ,
120
169
{
121
- PatternPredicate {
170
+ ContainsPredicate {
122
171
pattern : pattern. into ( ) ,
123
- op : PatternOp :: Contains ,
124
172
}
125
173
}
0 commit comments