Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

String Conversion Predicates #21

Closed
epage opened this issue Apr 1, 2018 · 3 comments
Closed

String Conversion Predicates #21

epage opened this issue Apr 1, 2018 · 3 comments
Labels
enhancement Improve the expected
Milestone

Comments

@epage
Copy link
Contributor

epage commented Apr 1, 2018

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 a from_utf8 on the variable 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 predicate
@epage epage added the enhancement Improve the expected label Apr 1, 2018
@nastevens
Copy link
Collaborator

You can currently accomplish this using predicate::function:

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.

@epage
Copy link
Contributor Author

epage commented Apr 2, 2018

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 function to handle each case? Thats a bit rough because of the amount of boiler plate (struct, re-implementing predicates) and can't provide clear messages once we get #7.

@nastevens
Copy link
Collaborator

I was crunched for time so I just grabbed the example from predicate::function, but yeah, it's not clear what I meant. Because predicate::function operates on any function, it's possible to change types on the "way down". So something like your example above where the predicate is on [u8] you can still make a predicate that accepts a str:

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 &T for eval that #20 could potentially fix.

Again this is not an ideal approach but it is currently possible to change the accepted inputs for predicates.

@epage epage added this to the 0.4 milestone Apr 27, 2018
@epage epage closed this as completed in b929216 May 12, 2018
epage added a commit to epage/predicates-rs that referenced this issue Jul 26, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement Improve the expected
Projects
None yet
Development

No branches or pull requests

2 participants