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

[TRIVIAL] Format hex encoding with derive_more #3273

Closed
wants to merge 1 commit into from

Conversation

m-lord-renkse
Copy link
Contributor

@m-lord-renkse m-lord-renkse commented Feb 7, 2025

Description

Use the crate derive_more in order to avoid repeated Debug implementation for the hex encoding.

Summary by CodeRabbit

  • Chores
    • Updated project dependencies to enable enhanced internal functionality.
  • Refactor
    • Improved how binary data is represented in debug outputs, making diagnostic information clearer for troubleshooting.

Copy link

coderabbitai bot commented Feb 7, 2025

Walkthrough

This pull request introduces two changes in the database crate. In the Cargo.toml file, a new dependency, derive_more, is added with workspace configuration. In the byte_array.rs file, the ByteArray struct is updated to automatically derive the Debug trait using derive_more, replacing a custom implementation. The changes simplify import formatting and maintain existing functionality.

Changes

File(s) Change Summary
crates/database/Cargo.toml Added new dependency: derive_more = { workspace = true } under [dependencies].
crates/database/src/byte_array.rs Updated ByteArray: removed custom Debug implementation and added #[debug("0x{}", hex::encode(...))] attribute via derive_more.

Poem

I'm a rabbit who loves to code,
Hopping through changes on each new road.
Cargo.toml now sings a dependency tune,
ByteArray shines with Debug like a bright moon.
I nibble carrots while errors take flight,
Celebrating our changes from morning till night!


📜 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 afe10d0 and 29a3404.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (2)
  • crates/database/Cargo.toml (1 hunks)
  • crates/database/src/byte_array.rs (1 hunks)
🔇 Additional comments (3)
crates/database/src/byte_array.rs (2)

1-9: LGTM!

The import block has been simplified without any functional changes.


13-16: LGTM! Clean implementation using derive_more.

The use of derive_more::Debug with the debug attribute provides a cleaner way to implement hex encoding for debug output.

Note: As mentioned in the past review comments, this approach cannot be used for generic types due to limitations in the derive_more crate with struct-level bounds.

crates/database/Cargo.toml (1)

12-12: LGTM!

The addition of derive_more follows the project's pattern of using workspace-level dependency management.


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

@m-lord-renkse m-lord-renkse force-pushed the format-hex-with-derive-more branch 2 times, most recently from ce427e6 to 47a3fb0 Compare February 7, 2025 15:25
@m-lord-renkse m-lord-renkse force-pushed the format-hex-with-derive-more branch from 47a3fb0 to 29a3404 Compare February 7, 2025 15:26
}
#[derive(Clone, Copy, Eq, PartialEq, Hash, sqlx::FromRow, derive_more::Debug)]
pub struct ByteArray<const N: usize>(
#[debug("0x{}", hex::encode::<&[u8]>(self.0.as_ref()))] pub [u8; N],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Originally I wanted to make this PR to change it in all the occurrences, but it cannot be changed for the occurrences which have a generic type (e.g. pub Type<T>(T)).

The reason is that hex::encode() requires the implementation of AsRef<[u8]>. So, theoretically we could do pub Type<T: AsRef<[u8]>(T), but it won't work. The reason is that the macro expands before the compiler acknowledges the trait limitation. This is a limitation in the derive_more crate which does not acknowledge struct-level bounds.

So, in the end this PR only changes one occurrence. I am up to close it if it does not add any benefit.

@m-lord-renkse m-lord-renkse marked this pull request as ready for review February 7, 2025 15:32
@m-lord-renkse m-lord-renkse requested a review from a team as a code owner February 7, 2025 15:32
Copy link
Contributor

@sunce86 sunce86 left a comment

Choose a reason for hiding this comment

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

I've accepted the suggestion in one of my previous PRs, but I am not sure about this change as it kills readability a bit (at least to me).

@MartinquaXD
Copy link
Contributor

but I am not sure about this change as it kills readability a bit (at least to me).

I agree. The derive_more crate is very useful if your struct has many fields and only need some special debug implementation for few of them. But if there is literally 1 field I think the explicit Debug implementation is just easier to understand.

@m-lord-renkse
Copy link
Contributor Author

@MartinquaXD @sunce86 I understand your point, totally valid. What do you guys think of having a function to do this conversion, and the derive more points to that function (the same as serde_with). We could have the function in a common crate, so we don't need to repeat the implementation all over. WDYT?

@MartinquaXD
Copy link
Contributor

MartinquaXD commented Feb 10, 2025

We could have the function in a common crate, so we don't need to repeat the implementation all over. WDYT?

Not sure this is addressing an issue we have at the moment. I'd prefer to focus our time on actually fixing alerts for now. When we are back to a sustainable state we can discuss how to make logging nicer.

@github-actions github-actions bot locked and limited conversation to collaborators Feb 10, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants