-
Notifications
You must be signed in to change notification settings - Fork 29
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
feat: Add str::contains_all function #147
base: master
Are you sure you want to change the base?
Conversation
In your example, shouldn't it be a let predicate_fn = predicate::str::contains_all(vec!["One", "Two", "Three"]); |
You are absolutely correct. I somehow forgot to add the commas. EDIT: I added the commas in my comment above and in the doc test in 45d4a17. |
/// let predicate_fn = predicate::str::contains_all(vec!["One", "Two", "Three"]); | ||
/// assert_eq!(true, predicate_fn.eval("One Two Three")); | ||
/// assert_eq!(false, predicate_fn.eval("One Two Four")); | ||
/// assert_eq!(false, predicate_fn.eval("Four Five Six")); | ||
/// ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whats the use case for this vs just doing multiple contains
checks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using multiple contains
for a small number of strings to check is fine. This, however, becomes cumbersome when dealing with 10+ different strings to check. Here it would be neat to be able to check a list of strings, either coming from a file or hardcoded ones in the test case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a way, this is a special casing of other predicates (anding a bunch of contains) and I'm trying to understand when we are justified in baking that in vs having people build it up from other parts.
Speaking of, I wonder if we should implement Predicate
for tuples for easier and-ing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Speaking of, I wonder if we should implement
Predicate
for tuples for easier and-ing.
Yes, that could work. However, this again breaks when the user reaches N, with N being the maximum number of items in a tuple we implemented Predicate
for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tuples can be composed of tuples.
And for a case like this, the types will all be the same, so we could also support arrays
If we move forward with this, can you squash your commits? |
Yes, I can do that once we are ready with this PR. |
fn eval(&self, variable: &str) -> bool { | ||
for pattern in &self.patterns { | ||
if !variable.contains(pattern) { | ||
return false; | ||
} | ||
} | ||
|
||
true | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we do this, we should probably have a find_case
implementation to either report which items matched so people don't have to manually scan the string for them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I will look into this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a basic find_case
impl. I'm unsure if the current impl does what this function expects. This is because I'm not quite sure how it integrates into the predicates-rs
ecosystem.
But, the implementation can be improved by implementing reflection::PredicateReflection
and fmt::Display
for ContainsAllPredicate
.
The downside to that is it requires an extra ping-pong between us to get this in, causing more context switches, and being fairly easy to fall through the cracks |
Can't we just use squash-merge? Then GitHub would do the heavy lifting for us :) |
Not the most ideal experience and I don't like blindly squashing PRs but only doing it as a last resort because we should preserve multiple commits when they are relevant. |
c25eac8
to
699d288
Compare
I now squashed all previous commits (Adding the function, updating the docs, and merging changes from main). I will now continue to work on the unresolved conversations above. |
This PR adds the
str::contains_all
predicate function. It allows one to check if the input contains all provided needles.