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

Complex enums as Odra Type #394

Merged
merged 5 commits into from
Apr 10, 2024

Conversation

kpob
Copy link
Contributor

@kpob kpob commented Apr 4, 2024

Makes this code working:

#[odra::odra_type]
enum IP {
    Unknown,                                               // no data,
    IPv4(IPv4),                                            // single unnamed element,
    IPv4WithDescription(IPv4, String),                     // multiple unnamed elements,
    IPv6 { ip: IPv6 },                                     // single named element,
    IPv6WithDescription { ip: IPv6, description: String }, // multiple named elements,
}

#[odra::odra_type]
pub enum Fieldless {
    Tuple(),
    Struct {},
    Unit
}

Resolves #172

Summary by CodeRabbit

  • New Features
    • Introduced custom types and structures for enhanced data representation, including IP addresses and enums.
    • Added a new contract MyContract with initialization and retrieval capabilities for custom types.
  • Refactor
    • Improved handling of enums and structs across the codebase, enhancing the processing and differentiation of various types.
    • Streamlined type handling through the introduction of TypeKind enum, optimizing the construction of custom types.

@kpob kpob linked an issue Apr 4, 2024 that may be closed by this pull request
Copy link
Contributor

coderabbitai bot commented Apr 4, 2024

Walkthrough

This update enhances the Odra framework's handling of complex enums and structs, improving type processing capabilities. The changes refine how enums and structs are processed, introducing a TypeKind enum for better type differentiation. These modifications enable more effective code generation and error handling, expanding the framework's support for complex data structures in smart contract development.

Changes

Files Change Summary
examples/Odra.toml, .../src/features/... Introduced MyContract and custom types module, detailing IP address structures and error handling.
odra-macros/src/ast/..., .../src/ir/... Enhanced processing of enums and structs, introducing TypeKind for better type differentiation.
odra-macros/src/test_utils.rs, .../src/utils/... Adjusted utils for improved enum variant handling and testing.
odra-schema/src/lib.rs Added functions for creating and testing enum variants with custom types.

Assessment against linked issues

Objective Addressed Explanation
Handle complex Enums in OdraType (#172)

🐇🎉
In the realm of code, where structures intertwine,
A rabbit hopped forth, making enums align.
With a twitch of its nose, and a flick of its ear,
Complex types and patterns began to appear.
"To the future," it whispered, under the moon's light,
Where contracts are crafted, both nimble and bright.
🌟🐾


Recent Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 6e9093b and 573f726.
Files selected for processing (4)
  • CHANGELOG.md (1 hunks)
  • odra-macros/src/ast/odra_type_item.rs (15 hunks)
  • odra-macros/src/ast/schema/custom_item.rs (7 hunks)
  • odra-macros/src/test_utils.rs (1 hunks)
Files skipped from review as they are similar to previous changes (3)
  • odra-macros/src/ast/odra_type_item.rs
  • odra-macros/src/ast/schema/custom_item.rs
  • odra-macros/src/test_utils.rs
Additional comments not posted (1)
CHANGELOG.md (1)

5-7: Consider specifying the release date for version 0.9.1.

While the changelog correctly highlights the new capabilities of the #[odra::odra_type] attribute with enums, specifying the release date (currently marked as "2024-04-xx") would provide clarity and help users understand the timeline of changes.

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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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

codecov bot commented Apr 4, 2024

Codecov Report

Attention: Patch coverage is 98.85321% with 5 lines in your changes are missing coverage. Please review.

❗ No coverage uploaded for pull request base (release/0.9.1@b19e647). Click here to learn what that means.

Files Patch % Lines
odra-macros/src/ir/mod.rs 82.60% 4 Missing ⚠️
odra-macros/src/ast/schema/custom_item.rs 99.13% 1 Missing ⚠️
Additional details and impacted files
@@               Coverage Diff                @@
##             release/0.9.1     #394   +/-   ##
================================================
  Coverage                 ?   86.35%           
================================================
  Files                    ?      136           
  Lines                    ?    15342           
  Branches                 ?        0           
================================================
  Hits                     ?    13249           
  Misses                   ?     2093           
  Partials                 ?        0           

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

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: 1

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b19e647 and 6e9093b.
Files selected for processing (10)
  • examples/Odra.toml (1 hunks)
  • examples/src/features/custom_types.rs (1 hunks)
  • examples/src/features/mod.rs (1 hunks)
  • odra-macros/src/ast/odra_type_item.rs (15 hunks)
  • odra-macros/src/ast/schema/custom_item.rs (6 hunks)
  • odra-macros/src/ast/schema/errors.rs (1 hunks)
  • odra-macros/src/ir/mod.rs (2 hunks)
  • odra-macros/src/test_utils.rs (1 hunks)
  • odra-macros/src/utils/syn.rs (4 hunks)
  • odra-schema/src/lib.rs (2 hunks)
Additional comments not posted (14)
examples/src/features/mod.rs (1)

5-5: The addition of the custom_types module is appropriate for organizing related examples and tests.

examples/Odra.toml (1)

67-68: The addition of MyContract under features::custom_types in the Odra.toml file is correctly configured to demonstrate the application of complex enums within contract features.

examples/src/features/custom_types.rs (1)

1-92: Ensure that the odra::odra_type attribute is correctly implemented and tested for each enum and struct type defined here, as these are crucial for the framework's ability to handle complex enums.

odra-macros/src/ast/schema/errors.rs (1)

96-96: Verify the impact of the modification to the enum_variants function, ensuring it aligns with the intended improvements in handling enum variants.

odra-macros/src/test_utils.rs (1)

153-165: The addition of the custom_complex_enum() function enhances testing capabilities for complex enums, facilitating the creation of test cases for enums with various configurations.

odra-macros/src/utils/syn.rs (1)

199-214: Verify the impact of the modifications to the transform_variants function, including the addition of the Fields parameter, ensuring they align with the intended improvements in handling enum variants.

Verification successful

The modifications to the transform_variants function, including the addition of the Fields parameter, are justified based on its usage within the codebase. The function is actively used in handling schema errors and custom items, suggesting that the changes align with the intended improvements in handling enum variants.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Placeholder script to verify the impact of modifications to the `transform_variants` function.
echo "Verify the modifications to the `transform_variants` function."

Length of output: 202



Script:

#!/bin/bash
# Search for references to the `transform_variants` function within the codebase
rg "transform_variants" --vimgrep

Length of output: 563

odra-macros/src/ast/schema/custom_item.rs (1)

79-145: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [7-142]

Verify the integration of the TypeKind enum and its impact on the handling of custom types, ensuring it enhances the framework's ability to differentiate between unit enums, enums with variants, and structs.

Verification successful

---

<!--
Monologue:
[task review_new_context_2/2] Analyze the new context provided from the shell script execution.
[observations] The output from the `rg` command shows multiple references to `TypeKind` across different files in the `odra-macros` module. This includes its definition in `odra-macros/src/ir/mod.rs`, usage in `odra-macros/src/ast/odra_type_item.rs` and `odra-macros/src/ast/schema/custom_item.rs`, among others. The references indicate that `TypeKind` is integrated into the codebase, being used to differentiate between unit enums, enums with variants, and structs in various parts of the code.
[analysis] The presence of `TypeKind` in multiple files and its usage in handling different types of custom items suggest that the integration of `TypeKind` has been successful. It appears to be a central part of the framework's ability to handle complex enums and structs effectively. The specific references to handling unit enums, enums, and structs in `odra_type_item.rs` and `schema/custom_item.rs` align with the intended improvements mentioned in the review comment.
[actions] Conclude that the integration of `TypeKind` and its impact on handling custom types has been verified successfully. The evidence from the script output supports the initial assessment that `TypeKind` enhances the framework's ability to differentiate between unit enums, enums with variants, and structs.
-->

The integration of the `TypeKind` enum and its impact on handling custom types has been successfully verified. The usage of `TypeKind` across various parts of the codebase, as shown in the script output, confirms its role in enhancing the framework's ability to differentiate between unit enums, enums with variants, and structs. This aligns with the improvements mentioned in the review comment.



</details>

<details>
<summary>Scripts Executed</summary>

The following scripts were executed for the analysis:



---

Script:
```shell
#!/bin/bash
# Placeholder script to verify the integration of `TypeKind` enum.
echo "Verify the integration of the `TypeKind` enum."

Length of output: 170



Script:

#!/bin/bash
# Search for references to `TypeKind` and its variants to verify its integration within the codebase.
rg "TypeKind" --vimgrep

Length of output: 1992

odra-schema/src/lib.rs (1)

328-344: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [115-342]

Ensure the new functions enum_custom_type_variant and enum_typed_variant function correctly by extending the test_custom_enum function with additional test cases covering various enum configurations.

odra-macros/src/ir/mod.rs (1)

687-695: The TypeKind enum definition looks good and effectively categorizes the types into UnitEnum, Enum, and Struct.

odra-macros/src/ast/odra_type_item.rs (5)

19-22: The implementations of TryFrom<&'_ TypeIR> for various items using the TypeKind enum to handle different types are correctly done.


153-223: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [147-198]

The from_enum method in FromBytesFnItem correctly handles deserialization of enums with unit, named, and unnamed fields.


232-249: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [235-258]

The from_struct method in FromBytesFnItem correctly generates deserialization code for struct fields.


311-338: The from_enum method in ToBytesFnItem correctly generates serialization code for enums with different types of fields.


290-300: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [282-308]

The from_struct method in ToBytesFnItem correctly generates serialization code for struct fields.

odra-macros/src/ir/mod.rs Show resolved Hide resolved
@kpob kpob merged commit 2edf85d into release/0.9.1 Apr 10, 2024
4 checks passed
@kpob kpob deleted the feature/handle-complex-enums-as-odratype branch April 10, 2024 09:50
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.

Handle complex Enums in OdraType.
2 participants