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
11 use std::fmt;
12 use std::marker::PhantomData;
13
14 use crate::reflection;
15 use crate::utils;
16 use 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)]
22 pub struct FnPredicate<F, T>
23 where
24 F: Fn(&T) -> bool,
25 T: ?Sized,
26 {
27 function: F,
28 name: &'static str,
29 _phantom: PhantomData<T>,
30 }
31
32 unsafe impl<F, T> Send for FnPredicate<F, T>
33 where
34 F: Send + Fn(&T) -> bool,
35 T: ?Sized,
36 {
37 }
38
39 unsafe impl<F, T> Sync for FnPredicate<F, T>
40 where
41 F: Sync + Fn(&T) -> bool,
42 T: ?Sized,
43 {
44 }
45
46 impl<F, T> FnPredicate<F, T>
47 where
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 /// ```
fn_name(mut self, name: &'static str) -> Self67 pub fn fn_name(mut self, name: &'static str) -> Self {
68 self.name = name;
69 self
70 }
71 }
72
73 impl<F, T> Predicate<T> for FnPredicate<F, T>
74 where
75 F: Fn(&T) -> bool,
76 T: ?Sized,
77 {
eval(&self, variable: &T) -> bool78 fn eval(&self, variable: &T) -> bool {
79 (self.function)(variable)
80 }
81
find_case<'a>(&'a self, expected: bool, variable: &T) -> Option<reflection::Case<'a>>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
87 impl<F, T> reflection::PredicateReflection for FnPredicate<F, T>
88 where
89 F: Fn(&T) -> bool,
90 T: ?Sized,
91 {
92 }
93
94 impl<F, T> fmt::Display for FnPredicate<F, T>
95 where
96 F: Fn(&T) -> bool,
97 T: ?Sized,
98 {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result99 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 /// ```
function<F, T>(function: F) -> FnPredicate<F, T> where F: Fn(&T) -> bool, T: ?Sized,132 pub fn function<F, T>(function: F) -> FnPredicate<F, T>
133 where
134 F: Fn(&T) -> bool,
135 T: ?Sized,
136 {
137 FnPredicate {
138 function,
139 name: "fn",
140 _phantom: PhantomData,
141 }
142 }
143
144 #[test]
str_function()145 fn str_function() {
146 let f = function(|x: &str| x == "hello");
147 assert!(f.eval("hello"));
148 assert!(!f.eval("goodbye"));
149 }
150