Skip to content

fix: ensure SerializableStopBy serialization matches deserialization …#1804

Merged
HerringtonDarkholme merged 1 commit intoast-grep:mainfrom
leon-rs:fix-serializable-bug
Feb 15, 2025
Merged

fix: ensure SerializableStopBy serialization matches deserialization …#1804
HerringtonDarkholme merged 1 commit intoast-grep:mainfrom
leon-rs:fix-serializable-bug

Conversation

@leon-rs
Copy link
Copy Markdown
Contributor

@leon-rs leon-rs commented Feb 14, 2025

…(#1802)

SerializableStopBy previously had a custom deserialization implementation, but relied on Serde's derive macro for serialization. This caused issues when round-tripping YAML configurations.

This commit implements a custom Serialize trait to ensure that:

  • "neighbor" and "end" are serialized as strings.
  • Rule variants are serialized as maps.

Fixes #1802.

Summary by CodeRabbit

  • Refactor

    • The serialization and deserialization logic for the SerializableStopBy enum has been enhanced for clarity and maintainability, improving the handling of string keys associated with enum variants. This update contributes to a more robust configuration processing experience.
  • Tests

    • A new test has been added to verify the symmetry between serialization and deserialization processes.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 14, 2025

Walkthrough

The code changes update the serialization logic for the SerializableStopBy enum in the configuration module. The changes remove the previous #[serde(rename_all = "camelCase")] attribute and replace hardcoded string literals in the visitor with two new constant string keys. A custom Serialize implementation is added, leveraging these constants for clarity and maintainability while preserving the original enum structure.

Changes

File Path Change Summary
crates/config/.../rule/stop_by.rs Removed #[serde(rename_all = "camelCase")] attribute; added constants NEIGHBOR_KEY and END_KEY; updated visit_str to use these constants; added a custom Serialize implementation; added test for serialization/deserialization symmetry.

Sequence Diagram(s)

sequenceDiagram
    participant S as Serializer
    participant E as SerializableStopBy
    participant C as Constants (NEIGHBOR_KEY / END_KEY)

    S->>E: Call serialize(serializer)
    E->>C: Retrieve appropriate constant for variant (Neighbor/End)
    C-->>E: Return string key
    E->>S: Return serialized string key
Loading

Poem

I'm a rabbit on the run,
With constants gleaming in the sun,
Neighbor and End now clearly shine,
Serialization's smooth by design,
Hop along, code, your new day's begun!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2f2501e and bfad826.

📒 Files selected for processing (1)
  • crates/config/src/rule/stop_by.rs (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/config/src/rule/stop_by.rs

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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
Copy Markdown
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

🧹 Nitpick comments (2)
crates/config/src/rule/stop_by.rs (2)

24-27: LGTM! Good use of constants for string literals.

Using constants instead of string literals improves maintainability and reduces the risk of typos.

Consider adding a doc comment explaining the purpose of these constants:

 impl SerializableStopBy {
+  /// String key used for serializing the Neighbor variant
   const NEIGHBOR_KEY: &str = "neighbor";
+  /// String key used for serializing the End variant
   const END_KEY: &str = "end";
 }

41-45: Use constants in error message for consistency.

The error message still uses string literals while the match arms use constants.

       SerializableStopBy::NEIGHBOR_KEY => Ok(SerializableStopBy::Neighbor),
       SerializableStopBy::END_KEY => Ok(SerializableStopBy::End),
       v => Err(de::Error::custom(format!(
-        "unknown variant `{v}`, expected `neighbor`, `end` or a rule object",
+        "unknown variant `{v}`, expected `{}`, `{}` or a rule object",
+        SerializableStopBy::NEIGHBOR_KEY,
+        SerializableStopBy::END_KEY,
       ))),
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 394e385 and b730ca9.

📒 Files selected for processing (1)
  • crates/config/src/rule/stop_by.rs (3 hunks)
🔇 Additional comments (2)
crates/config/src/rule/stop_by.rs (2)

16-22: LGTM! Removal of camelCase renaming aligns with serialization fix.

The removal of the rename_all attribute is appropriate as we're now handling serialization explicitly through a custom implementation.


67-78:

Details

❓ Verification inconclusive

LGTM! Clean serialization implementation that matches deserialization.

The implementation correctly handles all variants and maintains consistency with the deserialization logic.

Consider adding round-trip serialization tests to verify the fix. Here's a script to check for existing tests:


🏁 Script executed:

#!/bin/bash
# Description: Check for round-trip serialization tests
rg -A 5 'test.*serializ' crates/config/src/rule/stop_by.rs

Length of output: 60


LGTM! The custom serialization implementation for SerializableStopBy is clean and properly delegates each variant. However, the recent check did not reveal any existing round-trip serialization tests in the vicinity of crates/config/src/rule/stop_by.rs. Please manually verify if such tests exist elsewhere or consider adding round-trip tests to ensure that every variant serializes and deserializes correctly.

@HerringtonDarkholme
Copy link
Copy Markdown
Member

Can you also add a test for this? Thanks

@HerringtonDarkholme
Copy link
Copy Markdown
Member

Also the test failed, can you give it a look?

…1802)

SerializableStopBy previously had a custom deserialization implementation, but
relied on Serde's derive macro for serialization. This caused issues when
round-tripping YAML configurations.

This commit implements a custom Serialize trait to ensure that:
- "neighbor" and "end" are serialized as strings.
- Rule variants are serialized as maps.

Fixes #1802.
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 15, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.43%. Comparing base (394e385) to head (bfad826).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1804      +/-   ##
==========================================
+ Coverage   87.36%   87.43%   +0.06%     
==========================================
  Files          96       96              
  Lines       15528    15553      +25     
==========================================
+ Hits        13566    13598      +32     
+ Misses       1962     1955       -7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@HerringtonDarkholme HerringtonDarkholme added this pull request to the merge queue Feb 15, 2025
Merged via the queue into ast-grep:main with commit f73a9cb Feb 15, 2025
6 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Jul 27, 2025
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.

[bug] YAML Serialization Issue with SerializableStopBy in ast-grep

2 participants