Skip to content

Commit cc85f7c

Browse files
committed
feat(str): Regex predicate
This is a short term API. Different issues we'll need to consider when evolving this include: - Easy to create predicate - The ability to customize the regex (e.g. case sensitivity, multi-line, ignoring whityespace). - It looks like the flags can be enabled with `(?flag)` inside the regex. Good enough? - How error handling should work. Re-export? A single error type for the whole crate? This is a part of assert-rs#12
1 parent a8541d8 commit cc85f7c

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ appveyor = { repository = "assert-rs/predicates-rs" }
1919

2020
[dependencies]
2121
difference = { version = "2.0", optional = true }
22+
regex = { version="0.2", optional = true }
2223

2324
[features]
24-
default = ["difference"]
25+
default = ["difference", "regex"]
2526
unstable = []

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@
8787

8888
#[cfg(feature = "difference")]
8989
extern crate difference;
90+
#[cfg(feature = "regex")]
91+
extern crate regex;
9092

9193
// core `Predicate` trait
9294
pub mod predicate;

src/predicate/str/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@
1414
mod difference;
1515
#[cfg(feature = "difference")]
1616
pub use self::difference::{diff, similar, DifferencePredicate};
17+
18+
#[cfg(feature = "regex")]
19+
mod regex;
20+
#[cfg(feature = "regex")]
21+
pub use self::regex::{is_match, RegexError, RegexPredicate};

src/predicate/str/regex.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2018 The predicates-rs Project Developers.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
use regex;
10+
11+
use Predicate;
12+
13+
/// An error that occurred during parsing or compiling a regular expression.
14+
pub type RegexError = regex::Error;
15+
16+
/// Predicate that uses regex matching
17+
///
18+
/// This is created by the `predicate::str::is_match`.
19+
#[derive(Clone, Debug)]
20+
pub struct RegexPredicate {
21+
re: regex::Regex,
22+
}
23+
24+
impl Predicate for RegexPredicate {
25+
type Item = str;
26+
27+
fn eval(&self, variable: &str) -> bool {
28+
self.re.is_match(variable)
29+
}
30+
}
31+
32+
/// Creates a new `Predicate` that uses a regular expression to match the string.
33+
///
34+
/// # Examples
35+
///
36+
/// ```
37+
/// use predicates::predicate::*;
38+
///
39+
/// let predicate_fn = str::is_match("^Hel.o.*$").unwrap();
40+
/// assert_eq!(true, predicate_fn.eval("Hello World"));
41+
/// assert_eq!(false, predicate_fn.eval("Food World"));
42+
/// ```
43+
pub fn is_match<S>(pattern: S) -> Result<RegexPredicate, RegexError>
44+
where
45+
S: AsRef<str>,
46+
{
47+
regex::Regex::new(pattern.as_ref()).map(|re| RegexPredicate { re })
48+
}

0 commit comments

Comments
 (0)