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

added support for "name" attribute for enum variants #13

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

twop
Copy link

@twop twop commented Nov 8, 2024

Upd: added support for name="something" for child node too

fixes #12

done:

  • reused 'name' attribute
    • although now it can be top level, instead being an inner part of attribute or argument
    • like so #[knus(name="PrintString")]
    • it doesn't seem great to not reuse the established pattern with other renames (like inside child), but seems clean enough (skip is already top level and only applicable for enum variants)
  • used name value if present for code generation
  • changed test in derive/tests/normal.rs
  • added changes to derive_decode.md, but not inside README.md for consistency
  • added support for child(name="something") too

so now this works as expected:

enum Action {
    #[knus(name="Create")]
    Create(#[knus(argument)] String),

    #[knus(name="PrintString")]
    PrintString(PrintString),
}
Create "xxx"
PrintString "yyy" line=2

and this:

#[derive(knus::Decode)]
struct Version {
    #[knus(argument)]
    number: u32
}
#[derive(knus::Decode)]
struct MyNode {
    #[knus(child(name="ver"))]
    version: Version,
}
ver 1

@codecov-commenter
Copy link

codecov-commenter commented Nov 8, 2024

Codecov Report

Attention: Patch coverage is 71.42857% with 10 lines in your changes missing coverage. Please review.

Project coverage is 76.27%. Comparing base (64b8133) to head (0bcbd44).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
derive/src/definition.rs 71.42% 1 Missing and 9 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #13      +/-   ##
==========================================
- Coverage   76.27%   76.27%   -0.01%     
==========================================
  Files          15       15              
  Lines        4274     4282       +8     
  Branches     4274     4282       +8     
==========================================
+ Hits         3260     3266       +6     
+ Misses        872      871       -1     
- Partials      142      145       +3     

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

@TheLostLambda
Copy link
Owner

Hi @twop ! I should have the chance to look at this in more detail over the weekend, but on a design note (and sorry if this might add a bit of additional work), what do you think about also adding, for completeness, something like serdes rename_all attribute?

https://serde.rs/container-attrs.html#rename_all

That might be an even more elegant way to address your particular case where you want everything in PascalCase!

@twop
Copy link
Author

twop commented Nov 8, 2024

I did think about it, but decided not to tackle it in this PR:

  1. I personally prefer explicit field renames with serde(although, that is a preference).
  2. I think if people don't ask for it => don't build it. Even if it makes sense, I would probably still prefer to introduce it in a separate PR.

That said, it is my preference, and not a belief.

@twop
Copy link
Author

twop commented Nov 9, 2024

Upd: added explicit naming for child too

@TheLostLambda
Copy link
Owner

I did think about it, but decided not to tackle it in this PR:

As for this, my personal preference likely runs counter to this — I think I'd be frustrated by having that "boilerplate" for every field, but, with that being said, it's not a feature I personally need right now.

Ideally we'd also have that "rename all" support (also means more intuitive coming from serde), but I don't mind merging this on its own either!

@twop
Copy link
Author

twop commented Nov 15, 2024

rename all, seems nice, but I haven't looked into it's implementation

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.

add ability to manually name enum variants, currently it is bound to kebab-case
3 participants