Skip to content

Commit ee4c45e

Browse files
committed
feat(assert): Simplify passing in Predicate<str>
Fixes #32
1 parent b1376f9 commit ee4c45e

File tree

1 file changed

+71
-2
lines changed

1 file changed

+71
-2
lines changed

src/assert.rs

+71-2
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ where
405405
}
406406

407407
// Keep `predicates` concrete Predicates out of our public API.
408-
/// [Predicate] used by [`IntoCodePredicate`] for bytes.
408+
/// [Predicate] used by [`IntoCodePredicate`] for code.
409409
///
410410
/// [`IntoCodePredicate`]: trait.IntoCodePredicate.html
411411
/// [Predicate]: https://docs.rs/predicates-core/0.9.0/predicates_core/trait.Predicate.html
@@ -461,7 +461,7 @@ impl IntoCodePredicate<EqCodePredicate> for i32 {
461461
}
462462

463463
// Keep `predicates` concrete Predicates out of our public API.
464-
/// [Predicate] used by [`IntoCodePredicate`] for bytes.
464+
/// [Predicate] used by [`IntoCodePredicate`] for iterables of codes.
465465
///
466466
/// [`IntoCodePredicate`]: trait.IntoCodePredicate.html
467467
/// [Predicate]: https://docs.rs/predicates-core/0.9.0/predicates_core/trait.Predicate.html
@@ -691,6 +691,75 @@ impl IntoOutputPredicate<StrContentOutputPredicate> for &'static str {
691691
}
692692
}
693693

694+
// Keep `predicates` concrete Predicates out of our public API.
695+
/// [Predicate] used by [`IntoOutputPredicate`] for [`Predicate<str>`].
696+
///
697+
/// [`IntoOutputPredicate`]: trait.IntoOutputPredicate.html
698+
/// [Predicate]: https://docs.rs/predicates-core/0.9.0/predicates_core/trait.Predicate.html
699+
#[derive(Debug, Clone)]
700+
pub struct StrOutputPredicate<P: predicates_core::Predicate<str>>
701+
(
702+
predicates::str::Utf8Predicate<P>,
703+
);
704+
705+
impl<P> StrOutputPredicate<P>
706+
where P: predicates_core::Predicate<str>
707+
{
708+
pub(crate) fn new(pred: P) -> Self {
709+
let pred = pred.from_utf8();
710+
StrOutputPredicate(pred)
711+
}
712+
}
713+
714+
impl<P> predicates_core::reflection::PredicateReflection for StrOutputPredicate<P>
715+
where P: predicates_core::Predicate<str>
716+
{
717+
fn parameters<'a>(
718+
&'a self,
719+
) -> Box<Iterator<Item = predicates_core::reflection::Parameter<'a>> + 'a> {
720+
self.0.parameters()
721+
}
722+
723+
/// Nested `Predicate`s of the current `Predicate`.
724+
fn children<'a>(&'a self) -> Box<Iterator<Item = predicates_core::reflection::Child<'a>> + 'a> {
725+
self.0.children()
726+
}
727+
}
728+
729+
impl<P> predicates_core::Predicate<[u8]> for StrOutputPredicate<P>
730+
where P: predicates_core::Predicate<str>
731+
{
732+
fn eval(&self, item: &[u8]) -> bool {
733+
self.0.eval(item)
734+
}
735+
736+
fn find_case<'a>(
737+
&'a self,
738+
expected: bool,
739+
variable: &[u8],
740+
) -> Option<predicates_core::reflection::Case<'a>> {
741+
self.0.find_case(expected, variable)
742+
}
743+
}
744+
745+
impl<P> fmt::Display for StrOutputPredicate<P>
746+
where P: predicates_core::Predicate<str>
747+
{
748+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
749+
self.0.fmt(f)
750+
}
751+
}
752+
753+
impl<P> IntoOutputPredicate<StrOutputPredicate<P>> for P
754+
where P: predicates_core::Predicate<str>
755+
{
756+
type Predicate = StrOutputPredicate<P>;
757+
758+
fn into_output(self) -> Self::Predicate {
759+
Self::Predicate::new(self)
760+
}
761+
}
762+
694763
#[cfg(test)]
695764
mod test {
696765
use super::*;

0 commit comments

Comments
 (0)