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

feat(katana): Cairo short string #2991

Merged
merged 2 commits into from
Feb 5, 2025
Merged

feat(katana): Cairo short string #2991

merged 2 commits into from
Feb 5, 2025

Conversation

kariy
Copy link
Member

@kariy kariy commented Feb 5, 2025

Define Cairo short string type https://book.cairo-lang.org/ch02-02-data-types.html#short-strings

Summary by CodeRabbit

  • New Features
    • Integrated an additional dependency to enhance serialization support and memory efficiency.
    • Introduced a new lightweight text type with fixed capacity that offers robust validation, safe text operations, and seamless type conversions.
    • Expanded the public interface with an added module, extending the library’s overall functionality.

Copy link

coderabbitai bot commented Feb 5, 2025

Ohayo sensei!

Walkthrough

This pull request introduces a new dependency to the katana-primitives crate by adding heapless with the serde feature. It also implements a new ShortString struct in the Cairo module, backed by a fixed-capacity string, and provides methods for creation, string manipulation, and conversion (including error handling and interoperability with Felt). Finally, the new cairo module is publicly exposed in the library.

Changes

File(s) Change Summary
crates/katana/primitives/Cargo.toml Added dependency: heapless = { version = "0.8.0", features: ["serde"] } under [dependencies].
crates/katana/primitives/src/cairo.rs Introduced the ShortString struct (backed by heapless::String<31>) with methods for instantiation, manipulation, and conversion. Implemented multiple traits (Default, Deref, AsRef, FromStr, Display, etc.) and added unit tests.
crates/katana/primitives/src/lib.rs Added a new public module declaration: pub mod cairo;

Sequence Diagram(s)

sequenceDiagram
    participant User as Sensei
    participant SS as ShortString
    participant HStr as heapless::String<31>
    participant Felt as Felt

    User->>SS: Call from_str(input)
    SS->>HStr: Initialize internal string storage
    HStr-->>SS: Return constructed string
    SS-->>User: Return ShortString instance

    User->>SS: Request conversion into Felt (via Into/Fn call)
    SS->>Felt: Convert internal representation
    Felt-->>SS: Return Felt result
    SS-->>User: Return conversion output
Loading

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.

🪧 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.

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

@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 (1)
crates/katana/primitives/src/cairo.rs (1)

26-34: Consider exposing push/push_str as public for flexibility.

Currently, push and push_str are private. If there's a usage scenario where partial or incremental building of a ShortString beyond constructor usage is needed, having public methods could be helpful. Otherwise, if they're private by design, this is fine.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bf0c5ee and 11ccf3f.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (3)
  • crates/katana/primitives/Cargo.toml (1 hunks)
  • crates/katana/primitives/src/cairo.rs (1 hunks)
  • crates/katana/primitives/src/lib.rs (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • crates/katana/primitives/src/lib.rs
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: fmt
🔇 Additional comments (7)
crates/katana/primitives/src/cairo.rs (6)

1-2: Ohayo sensei! Looks good so far.

The use crate::Felt; import is correct and aligned with the usage throughout the file. No concerns.


3-7: Struct definition is concise and tidy, sensei!

Defining ShortString as a tuple struct with heapless::String<31> cleanly encapsulates functionality. This minimal design also helps keep memory usage predictable.


57-63: Error definitions are well-structured, sensei!

Enumerating distinct cases like StringTooLong and InvalidAsciiString ensures clarity in error reporting and debugging. Keep it up.


65-82: Input validation is thorough.

The is_ascii() check and length constraint effectively shield against invalid short strings. The direct push_str(...) call with a pre-checked length is fine. Watch out for any future expansions that might alter length constraints.


112-143: Clever handling of leading zeros and null terminators, sensei!

Your conversion logic systematically guards against malformed data. The design of rejecting null bytes after starting the string is consistent with a contiguous short string approach in Cairo. This is a well-thought-out solution.


175-304: Test coverage is commendable, sensei!

The wide range of unit tests (normal cases, edge cases, and error conditions) inspires confidence in correctness. The rstest usage for repeated cases is especially neat. No further changes needed.

crates/katana/primitives/Cargo.toml (1)

16-16: Ohayo sensei, new dependency is good to go!

Adding heapless = { version = "0.8.0", features = [ "serde" ] } is essential for the short-string functionality. This version and feature set is well-aligned with your usage.

Copy link

codecov bot commented Feb 5, 2025

Codecov Report

Attention: Patch coverage is 86.87500% with 21 lines in your changes missing coverage. Please review.

Project coverage is 57.05%. Comparing base (bf0c5ee) to head (11ccf3f).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
crates/katana/primitives/src/cairo.rs 86.87% 21 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2991      +/-   ##
==========================================
+ Coverage   56.97%   57.05%   +0.08%     
==========================================
  Files         426      427       +1     
  Lines       56376    56536     +160     
==========================================
+ Hits        32119    32257     +138     
- Misses      24257    24279      +22     

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

@kariy kariy merged commit d875ec8 into main Feb 5, 2025
15 checks passed
@kariy kariy deleted the katana/short-string branch February 5, 2025 15:18
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