Skip to content
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

fix: simplify the lazy approach #483

Merged
merged 5 commits into from
Sep 1, 2024
Merged

fix: simplify the lazy approach #483

merged 5 commits into from
Sep 1, 2024

Conversation

morgante
Copy link
Contributor

@morgante morgante commented Sep 1, 2024

We don't really need a new struct, and leaking memory is safer than transmuting.

Summary by CodeRabbit

  • New Features

    • Introduced a new matches method in MarzanoResolvedPattern for enhanced pattern matching capabilities.
    • Added wrap_with_rewrite and wrap_with_accumulate methods to PatternBuilder for improved pattern manipulation.
    • New method from_str_constant in DynamicPattern allows creation of patterns from string literals.
  • Bug Fixes

    • Streamlined error handling in ResolvedPattern for better readability.
  • Refactor

    • Removed the LazyTraversal struct, simplifying the callback function signatures.
    • Updated test cases to reflect changes in pattern matching logic.
  • Style

    • Improved organization of import statements for better code clarity.

@morgante morgante marked this pull request as ready for review September 1, 2024 20:05
@morgante morgante requested a review from a team as a code owner September 1, 2024 20:05
Copy link
Contributor

coderabbitai bot commented Sep 1, 2024

Walkthrough

Walkthrough

The changes involve significant modifications across multiple files, including the removal of the LazyTraversal struct and its methods, updates to the CallbackFn type, and the introduction of new methods for pattern matching and pattern manipulation. The MarzanoResolvedPattern now includes a new matches method, while the PatternBuilder has new methods for wrapping patterns. Additionally, adjustments have been made to improve code organization and readability in various test and implementation files.

Changes

Files Change Summary
crates/core/src/built_in_functions.rs Removed &mut LazyTraversal<'a, 'b> from CallbackFn type definition, simplifying its signature and updating the BuiltIns implementation accordingly.
crates/core/src/lazy.rs Completely removed the LazyTraversal struct and its methods, including new and matches. Updated tests to replace references to lazy.matches with binding.matches.
crates/core/src/marzano_resolved_pattern.rs Added a new public method matches to MarzanoResolvedPattern, enabling pattern matching functionality. Streamlined error handling in existing match statements.
crates/core/src/pattern_compiler/builder.rs Introduced two new methods, wrap_with_rewrite and wrap_with_accumulate, to PatternBuilder, enhancing pattern manipulation capabilities.
crates/core/src/test_callback.rs Reorganized import statements for clarity and renamed a parameter in the test_callback function to logs for improved readability.
crates/grit-pattern-matcher/src/pattern/accumulate.rs Modified handling of dynamic_right in the Accumulate implementation to streamline the assignment logic.
crates/grit-pattern-matcher/src/pattern/dynamic_snippet.rs Added a new public method from_str_constant to DynamicPattern, allowing creation from a string input.

Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5fa83a1 and c2b7109.

Files selected for processing (2)
  • crates/core/src/marzano_resolved_pattern.rs (3 hunks)
  • crates/core/src/test_callback.rs (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • crates/core/src/marzano_resolved_pattern.rs
  • crates/core/src/test_callback.rs
Additional context used
Learnings (1)
Common learnings
Learnt from: morgante
URL: https://github.com/getgrit/gritql/pull/231

Timestamp: 2024-04-15T05:47:47.743Z
Learning: User morgante prefers not to be asked for documentation.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (3)
crates/grit-pattern-matcher/src/pattern/dynamic_snippet.rs (1)

54-57: LGTM! Consider adding tests for the new method.

The code changes are approved. The new from_str_constant method provides a straightforward way to create a constant DynamicPattern from a string input, which enhances the usability of the DynamicPattern.

To ensure the correctness and maintainability of the code, consider adding tests for the new from_str_constant method. For example:

#[test]
fn test_from_str_constant() {
    let pattern = DynamicPattern::from_str_constant("hello").unwrap();
    assert!(matches!(pattern, DynamicPattern::Snippet(DynamicSnippet { parts }) if parts.len() == 1));
    
    if let DynamicPattern::Snippet(DynamicSnippet { parts }) = pattern {
        assert!(matches!(parts[0], DynamicSnippetPart::String(s) if s == "hello"));
    }
}
crates/core/src/pattern_compiler/builder.rs (1)

244-248: LGTM! Consider adding documentation for the new methods.

The code changes are approved. However, it would be beneficial to add documentation for the new wrap_with_rewrite and wrap_with_accumulate methods to explain their purpose and usage.

+/// Wrap the pattern with a rewrite.
+/// This allows for the substitution of patterns with a specified replacement.
 pub fn wrap_with_rewrite(self, replacement: DynamicPattern<MarzanoQueryContext>) -> Self {
     let pattern = Pattern::Rewrite(Box::new(Rewrite::new(self.pattern, replacement, None)));
     Self { pattern, ..self }
 }

+/// Wrap the pattern with an accumulate.
+/// This facilitates the accumulation of patterns, enabling more complex pattern compositions.
 pub fn wrap_with_accumulate(self, other: Pattern<MarzanoQueryContext>) -> Self {
     let pattern = Pattern::Accumulate(Box::new(Accumulate::new(self.pattern, other, None)));
     Self { pattern, ..self }
 }
crates/core/src/marzano_resolved_pattern.rs (1)

53-65: LGTM! But suppress the clippy warning.

The code changes are approved. The new matches method looks good and the use of the memory leak technique is justified for short-lived programs.

However, the clippy static analysis tool has flagged this method as unused, which is a false positive in this case. The pub(crate) visibility modifier indicates that the method is intended to be used within the same crate, even if it's not used yet.

To suppress the warning, add the #[allow(dead_code)] attribute above the method:

+#[allow(dead_code)]
 pub(crate) fn matches(
     &self,
     pattern: &Pattern<MarzanoQueryContext>,
     state: &mut State<'a, MarzanoQueryContext>,
     context: &'a MarzanoContext<'a>,
     logs: &mut AnalysisLogs,
 ) -> GritResult<bool> {
Tools
GitHub Check: clippy

[failure] 53-53: method matches is never used
error: method matches is never used
--> crates/core/src/marzano_resolved_pattern.rs:53:19
|
41 | impl<'a> MarzanoResolvedPattern<'a> {
| ----------------------------------- method in this implementation
...
53 | pub(crate) fn matches(
| ^^^^^^^
|
= note: -D dead-code implied by -D warnings
= help: to override -D warnings add #[allow(dead_code)]

@morgante morgante merged commit 44e1536 into main Sep 1, 2024
7 checks passed
@github-actions github-actions bot mentioned this pull request Sep 1, 2024
@github-actions github-actions bot mentioned this pull request Sep 11, 2024
@github-actions github-actions bot mentioned this pull request Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant