summaryrefslogtreecommitdiff
path: root/src/core.rs
diff options
context:
space:
mode:
authorInna Palant <ipalant@google.com>2023-12-13 13:42:12 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2023-12-13 13:42:12 +0000
commite3add1d9cc9a13d487ce63bca96cf9bc426add6d (patch)
tree1d048800858fbab4133f7293e262528b225b5bb4 /src/core.rs
parent5ca6d8052723b3c36f0c1f5aa60514b1448e9e89 (diff)
parent33f8c696864e31927c5a4db0476deba652cd53b3 (diff)
downloadpredicates-core-e3add1d9cc9a13d487ce63bca96cf9bc426add6d.tar.gz
Original change: undetermined Change-Id: Ieaf1f2013ae6663eb57747f0ecbca44b54faaa88 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
Diffstat (limited to 'src/core.rs')
-rw-r--r--src/core.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/core.rs b/src/core.rs
new file mode 100644
index 0000000..b5aaa7b
--- /dev/null
+++ b/src/core.rs
@@ -0,0 +1,32 @@
+// Copyright (c) 2018 The predicates-rs Project Developers.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use crate::reflection;
+
+/// Trait for generically evaluating a type against a dynamically created
+/// predicate function.
+///
+/// The exact meaning of `eval` depends on the situation, but will usually
+/// mean that the evaluated item is in some sort of pre-defined set. This is
+/// different from `Ord` and `Eq` in that an `item` will almost never be the
+/// same type as the implementing `Predicate` type.
+pub trait Predicate<Item: ?Sized>: reflection::PredicateReflection {
+ /// Execute this `Predicate` against `variable`, returning the resulting
+ /// boolean.
+ fn eval(&self, variable: &Item) -> bool;
+
+ /// Find a case that proves this predicate as `expected` when run against `variable`.
+ fn find_case<'a>(&'a self, expected: bool, variable: &Item) -> Option<reflection::Case<'a>> {
+ let actual = self.eval(variable);
+ if expected == actual {
+ Some(reflection::Case::new(None, actual))
+ } else {
+ None
+ }
+ }
+}