Skip to content

Commit e7f174a

Browse files
committed
feat(assert): Accept Predicate<str>
Fixes #25
1 parent 1c63651 commit e7f174a

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/assert.rs

+64
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,70 @@ impl IntoPathPredicate<StrContentPathPredicate> for &'static str {
222222
}
223223
}
224224

225+
// Keep `predicates` concrete Predicates out of our public API.
226+
/// Predicate used by `IntoPathPredicate` for `str`
227+
#[derive(Debug, Clone)]
228+
pub struct StrPathPredicate<P: predicates_core::Predicate<str>>(
229+
predicates::path::FileContentPredicate<
230+
predicates::str::Utf8Predicate<P>,
231+
>,
232+
);
233+
234+
impl<P> StrPathPredicate<P>
235+
where
236+
P: predicates_core::Predicate<str>,
237+
{
238+
pub(crate) fn new(value: P) -> Self {
239+
let pred = value.from_utf8().from_file_path();
240+
StrPathPredicate(pred)
241+
}
242+
}
243+
244+
impl<P> predicates_core::reflection::PredicateReflection for StrPathPredicate<P>
245+
where
246+
P: predicates_core::Predicate<str>,
247+
{
248+
fn parameters<'a>(
249+
&'a self,
250+
) -> Box<Iterator<Item = predicates_core::reflection::Parameter<'a>> + 'a> {
251+
self.0.parameters()
252+
}
253+
254+
/// Nested `Predicate`s of the current `Predicate`.
255+
fn children<'a>(&'a self) -> Box<Iterator<Item = predicates_core::reflection::Child<'a>> + 'a> {
256+
self.0.children()
257+
}
258+
}
259+
260+
impl<P> predicates_core::Predicate<path::Path> for StrPathPredicate<P>
261+
where
262+
P: predicates_core::Predicate<str>,
263+
{
264+
fn eval(&self, item: &path::Path) -> bool {
265+
self.0.eval(item)
266+
}
267+
}
268+
269+
impl<P> fmt::Display for StrPathPredicate<P>
270+
where
271+
P: predicates_core::Predicate<str>,
272+
{
273+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
274+
self.0.fmt(f)
275+
}
276+
}
277+
278+
impl<P> IntoPathPredicate<StrPathPredicate<P>> for P
279+
where
280+
P: predicates_core::Predicate<str>,
281+
{
282+
type Predicate = StrPathPredicate<P>;
283+
284+
fn into_path(self) -> Self::Predicate {
285+
Self::Predicate::new(self)
286+
}
287+
}
288+
225289
#[cfg(test)]
226290
mod test {
227291
use super::*;

0 commit comments

Comments
 (0)