-
Notifications
You must be signed in to change notification settings - Fork 29
String Conversion Predicates #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You can currently accomplish this using use predicates::predicate::{self, Predicate};
struct Example {
string: String,
number: i32,
}
let string_check = predicate::function(|x: &Example| x.string == "hello");
let number_check = predicate::function(|x: &Example| x.number == 42);
let predicate_fn = string_check.and(number_check);
let good_example = Example { string: "hello".into(), number: 42 };
assert_eq!(true, predicate_fn.eval(&good_example)); Totally agree that this could be improved, just wanted to make sure you knew it was at least possible. |
I'm trying to understand how the example is a workaround for the problem. Is it by requiring someone to first pre-process their value to be tested into a struct / enum with the various cases and then using |
I was crunched for time so I just grabbed the example from extern crate predicates;
use predicates::{predicate, Predicate};
fn main() {
let u8_predicate = predicate::eq(&[b'a', b'b', b'c'][..]);
let str_predicate = predicate::function(|x: &&str| u8_predicate.eval(&x.as_bytes()));
assert_eq!(true, str_predicate.eval(&"abc"));
} Side note, this shows some of the grossness around taking Again this is not an ideal approach but it is currently possible to change the accepted inputs for predicates. |
Fix typos in CONTRIBUTING.md
Adapters from one
Item
type to another would be really useful.Example: In creating a predicate for a file, it'd be helpful to wrap a predicate on
str
with a predicate on[u8]
that will do afrom_utf8
on thevariable
before passing it down.These would be exposed on a
PredicateStrExt
.trim
,.trim_left
,.trim_right
: trim the value before passing it to the wrapped predicate.from_utf8
,.from_utf8_lossy
,.from_utf16
,.from_utf16_lossy
: Wrap a str predicate as a byte predicateThe text was updated successfully, but these errors were encountered: