predicates_core/
core.rs

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
9use crate::reflection;
10
11/// Trait for generically evaluating a type against a dynamically created
12/// predicate function.
13///
14/// The exact meaning of `eval` depends on the situation, but will usually
15/// mean that the evaluated item is in some sort of pre-defined set.  This is
16/// different from `Ord` and `Eq` in that an `item` will almost never be the
17/// same type as the implementing `Predicate` type.
18pub trait Predicate<Item: ?Sized>: reflection::PredicateReflection {
19    /// Execute this `Predicate` against `variable`, returning the resulting
20    /// boolean.
21    fn eval(&self, variable: &Item) -> bool;
22
23    /// Find a case that proves this predicate as `expected` when run against `variable`.
24    fn find_case<'a>(&'a self, expected: bool, variable: &Item) -> Option<reflection::Case<'a>> {
25        let actual = self.eval(variable);
26        if expected == actual {
27            Some(reflection::Case::new(None, actual))
28        } else {
29            None
30        }
31    }
32}