File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 81
81
/// `in_hash`. The implementation-specific predicates will be
82
82
/// deprecated when Rust supports trait specialization.
83
83
///
84
+ /// If you need to optimize this
85
+ /// - Type is `Ord`, call `sort()` on this predicate.
86
+ /// - Type is `Hash`, replace `in_iter` with `in_hash`.
87
+ ///
84
88
/// # Examples
85
89
///
86
90
/// ```
Original file line number Diff line number Diff line change @@ -21,12 +21,43 @@ pub struct RegexPredicate {
21
21
re : regex:: Regex ,
22
22
}
23
23
24
+ impl RegexPredicate {
25
+ /// Require a specific count of matches.
26
+ ///
27
+ /// # Examples
28
+ ///
29
+ /// ```
30
+ /// use predicates::prelude::*;
31
+ ///
32
+ /// let predicate_fn = predicate::str::is_match("T[a-z]*").unwrap().count(3);
33
+ /// assert_eq!(true, predicate_fn.eval("One Two Three Two One"));
34
+ /// assert_eq!(false, predicate_fn.eval("One Two Three"));
35
+ /// ```
36
+ pub fn count ( self , count : usize ) -> RegexMatchesPredicate {
37
+ RegexMatchesPredicate { re : self . re , count }
38
+ }
39
+ }
40
+
24
41
impl Predicate < str > for RegexPredicate {
25
42
fn eval ( & self , variable : & str ) -> bool {
26
43
self . re . is_match ( variable)
27
44
}
28
45
}
29
46
47
+ /// Predicate that checks for repeated patterns.
48
+ ///
49
+ /// This is created by `predicates::str::is_match(...).count`.
50
+ #[ derive( Clone , Debug ) ]
51
+ pub struct RegexMatchesPredicate {
52
+ re : regex:: Regex ,
53
+ count : usize ,
54
+ }
55
+
56
+ impl Predicate < str > for RegexMatchesPredicate {
57
+ fn eval ( & self , variable : & str ) -> bool {
58
+ self . re . find_iter ( variable) . count ( ) == self . count
59
+ }
60
+ }
30
61
/// Creates a new `Predicate` that uses a regular expression to match the string.
31
62
///
32
63
/// # Examples
You can’t perform that action at this time.
0 commit comments