1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use googletest::matcher::{Matcher, MatcherResult};
use predicates::{reflection::PredicateReflection, Predicate};
use std::{
    fmt::{Debug, Display},
    marker::PhantomData,
};

/// Provides a [Predicate] implementation for a given googletest [Matcher].
pub fn predicate<M: Matcher<ActualT = T>, T: Debug>(matcher: M) -> impl Predicate<T> {
    MatcherPredicate {
        matcher,
        _phantom_data_t: Default::default(),
    }
}

struct MatcherPredicate<M, T>
where
    M: Matcher<ActualT = T>,
    T: Debug,
{
    matcher: M,
    _phantom_data_t: PhantomData<T>,
}

impl<M, T> Predicate<T> for MatcherPredicate<M, T>
where
    M: Matcher<ActualT = T>,
    T: Debug,
{
    fn eval(&self, variable: &T) -> bool {
        match self.matcher.matches(variable) {
            MatcherResult::Match => true,
            MatcherResult::NoMatch => false,
        }
    }
}

impl<M, T> PredicateReflection for MatcherPredicate<M, T>
where
    M: Matcher<ActualT = T>,
    T: Debug,
{
}

impl<M, T> Display for MatcherPredicate<M, T>
where
    M: Matcher<ActualT = T>,
    T: Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.matcher.describe(MatcherResult::Match))
    }
}