Skip to content

Commit d6af116

Browse files
committed
feat(str): Trimming predicate decorator
Part of #25
1 parent f981fac commit d6af116

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/prelude.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
pub use core::Predicate;
1212
pub use boolean::PredicateBooleanExt;
1313
pub use boxed::PredicateBoxExt;
14+
pub use str::PredicateStrExt;
1415

1516
/// Predicate factories
1617
pub mod predicate {

src/str/adapters.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use Predicate;
2+
3+
/// Predicate adaper that trims the variable being tested.
4+
///
5+
/// This is created by `pred.trim()`.
6+
#[derive(Copy, Clone, Debug)]
7+
pub struct TrimPedicate<P>
8+
where
9+
P: Predicate<str>,
10+
{
11+
p: P,
12+
}
13+
14+
impl<P> Predicate<str> for TrimPedicate<P>
15+
where
16+
P: Predicate<str>,
17+
{
18+
fn eval(&self, variable: &str) -> bool {
19+
self.p.eval(variable.trim())
20+
}
21+
}
22+
23+
/// `Predicate` extension adapting a `str` Predicate.
24+
pub trait PredicateStrExt
25+
where
26+
Self: Predicate<str>,
27+
Self: Sized,
28+
{
29+
/// Returns a `TrimPedicate` that ensures the data passed to `Self` is trimmed.
30+
///
31+
/// # Examples
32+
///
33+
/// ```
34+
/// use predicates::prelude::*;
35+
///
36+
/// let predicate_fn = predicate::str::is_empty().trim();
37+
/// assert_eq!(true, predicate_fn.eval(" "));
38+
/// assert_eq!(false, predicate_fn.eval(" Hello "));
39+
/// ```
40+
fn trim(self) -> TrimPedicate<Self> {
41+
TrimPedicate { p: self }
42+
}
43+
}
44+
45+
impl<P> PredicateStrExt for P
46+
where
47+
P: Predicate<str>,
48+
{
49+
}

src/str/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
1313
mod basics;
1414
pub use self::basics::*;
15+
mod adapters;
16+
pub use self::adapters::*;
1517

1618
#[cfg(feature = "difference")]
1719
mod difference;

0 commit comments

Comments
 (0)