predicates/function.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/license/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
9//! Definition of `Predicate` for wrapping a `Fn(&T) -> bool`
10
11use std::fmt;
12use std::marker::PhantomData;
13
14use crate::reflection;
15use crate::utils;
16use crate::Predicate;
17
18/// Predicate that wraps a function over a reference that returns a `bool`.
19/// This type is returned by the `predicate::function` function.
20#[allow(clippy::derive_partial_eq_without_eq)]
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub struct FnPredicate<F, T>
23where
24 F: Fn(&T) -> bool,
25 T: ?Sized,
26{
27 function: F,
28 name: &'static str,
29 _phantom: PhantomData<T>,
30}
31
32unsafe impl<F, T> Send for FnPredicate<F, T>
33where
34 F: Send + Fn(&T) -> bool,
35 T: ?Sized,
36{
37}
38
39unsafe impl<F, T> Sync for FnPredicate<F, T>
40where
41 F: Sync + Fn(&T) -> bool,
42 T: ?Sized,
43{
44}
45
46impl<F, T> FnPredicate<F, T>
47where
48 F: Fn(&T) -> bool,
49 T: ?Sized,
50{
51 /// Provide a descriptive name for this function.
52 ///
53 /// # Examples
54 ///
55 /// ```
56 /// use predicates::prelude::*;
57 ///
58 /// struct Example {
59 /// string: String,
60 /// number: i32,
61 /// }
62 ///
63 /// let string_check = predicate::function(|x: &Example| x.string == "hello")
64 /// .fn_name("is_hello");
65 /// println!("predicate: {}", string_check);
66 /// ```
67 pub fn fn_name(mut self, name: &'static str) -> Self {
68 self.name = name;
69 self
70 }
71}
72
73impl<F, T> Predicate<T> for FnPredicate<F, T>
74where
75 F: Fn(&T) -> bool,
76 T: ?Sized,
77{
78 fn eval(&self, variable: &T) -> bool {
79 (self.function)(variable)
80 }
81
82 fn find_case<'a>(&'a self, expected: bool, variable: &T) -> Option<reflection::Case<'a>> {
83 utils::default_find_case(self, expected, variable)
84 }
85}
86
87impl<F, T> reflection::PredicateReflection for FnPredicate<F, T>
88where
89 F: Fn(&T) -> bool,
90 T: ?Sized,
91{
92}
93
94impl<F, T> fmt::Display for FnPredicate<F, T>
95where
96 F: Fn(&T) -> bool,
97 T: ?Sized,
98{
99 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100 let palette = crate::Palette::new(f.alternate());
101 write!(
102 f,
103 "{}({})",
104 palette.description(self.name),
105 palette.var("var"),
106 )
107 }
108}
109
110/// Creates a new predicate that wraps over the given function. The returned
111/// type implements `Predicate` and therefore has all combinators available to
112/// it.
113///
114/// # Examples
115///
116/// ```
117/// use predicates::prelude::*;
118///
119/// struct Example {
120/// string: String,
121/// number: i32,
122/// }
123///
124/// let string_check = predicate::function(|x: &Example| x.string == "hello");
125/// let number_check = predicate::function(|x: &Example| x.number == 42);
126/// let predicate_fn = string_check.and(number_check);
127/// let good_example = Example { string: "hello".into(), number: 42 };
128/// assert_eq!(true, predicate_fn.eval(&good_example));
129/// let bad_example = Example { string: "goodbye".into(), number: 0 };
130/// assert_eq!(false, predicate_fn.eval(&bad_example));
131/// ```
132pub fn function<F, T>(function: F) -> FnPredicate<F, T>
133where
134 F: Fn(&T) -> bool,
135 T: ?Sized,
136{
137 FnPredicate {
138 function,
139 name: "fn",
140 _phantom: PhantomData,
141 }
142}
143
144#[test]
145fn str_function() {
146 let f = function(|x: &str| x == "hello");
147 assert!(f.eval("hello"));
148 assert!(!f.eval("goodbye"));
149}