Conversation
Summary of ChangesHello @noamteyssier, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant enhancement to the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds a useful feature to grep, allowing patterns to be read from a file. The implementation involves significant refactoring of the argument parsing and pattern matching logic to accommodate both CLI and file-based patterns, which is handled well. My review focuses on improving robustness and maintainability. I've pointed out a potential panic from invalid CLI regex patterns, a case where invalid patterns from a file are silently ignored, and opportunities to reduce code duplication in the matcher implementations. Addressing these points will make the new functionality more reliable and the codebase easier to maintain.
| for line in contents.lines() { | ||
| if let Ok(regex) = regex::bytes::Regex::new(&line) { | ||
| regexes.push(regex); | ||
| } else { | ||
| eprintln!("Invalid regex pattern: {}", line); | ||
| } | ||
| } |
There was a problem hiding this comment.
Invalid regular expressions from a pattern file are currently skipped, with an error message printed to stderr. This might be missed by the user, leading them to believe all patterns were used when some were silently ignored. It would be more robust to fail and return an error, forcing the user to fix the invalid pattern.
Also, regex::bytes::Regex::new(&line) should be regex::bytes::Regex::new(line) as line is already a &str.
for line in contents.lines().filter(|l| !l.is_empty()) {
match regex::bytes::Regex::new(line) {
Ok(regex) => regexes.push(regex),
Err(e) => bail!("Invalid regex pattern in file: '{}'. Error: {}", line, e),
}
}| fn match_primary( | ||
| &mut self, | ||
| sequence: &[u8], | ||
| matches: &mut MatchRanges, | ||
| and_logic: bool, | ||
| ) -> bool { | ||
| if self.pat1.is_empty() { | ||
| return true; | ||
| } | ||
| self.pat1.iter().all(|pat| { | ||
| find_and_insert_matches( | ||
| pat, | ||
| sequence, | ||
| matches, | ||
| &mut self.searcher, | ||
| self.k, | ||
| self.inexact, | ||
| ) | ||
| }) | ||
| if and_logic { | ||
| self.pat1.iter().all(|pat| { | ||
| find_and_insert_matches( | ||
| pat, | ||
| sequence, | ||
| matches, | ||
| &mut self.searcher, | ||
| self.k, | ||
| self.inexact, | ||
| ) | ||
| }) | ||
| } else { | ||
| self.pat1.iter().any(|pat| { | ||
| find_and_insert_matches( | ||
| pat, | ||
| sequence, | ||
| matches, | ||
| &mut self.searcher, | ||
| self.k, | ||
| self.inexact, | ||
| ) | ||
| }) | ||
| } | ||
| } |
There was a problem hiding this comment.
There's significant code duplication between match_primary and match_secondary. The logic for iterating with all or any based on and_logic is identical. This could be extracted into a helper function to improve maintainability and reduce redundancy. The same pattern of duplication appears in match_either and also in regex_matcher.rs.
No description provided.