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

Add #[serde(rename_all_fields = "foo")] attribute #1695

Merged
merged 2 commits into from Jul 27, 2023

Conversation

jplatte
Copy link
Contributor

@jplatte jplatte commented Dec 15, 2019

fixes #1061

If you don't like the style change in the second commit, I can remove it from this PR (or you can just cherry-pick the first commit).

@jplatte jplatte changed the title Rename all fields Add #[serde(rename_all_fields = "foo")] attribute Dec 15, 2019
@jplatte
Copy link
Contributor Author

jplatte commented Dec 20, 2019

r? @dtolnay

@jplatte
Copy link
Contributor Author

jplatte commented Mar 28, 2020

I'll try again since Triagebot Notifications are now a thing:

r? @dtolnay

I've opened this PR because you wrote over at #1061 (comment) that you'd accept a PR for this. If your opinion has changed, I won't be upset if this is closed without further reasoning.

@kjeremy
Copy link

kjeremy commented Jul 20, 2020

@dtolnay it looks like this was approved a few months back but not merged in.

@jplatte
Copy link
Contributor Author

jplatte commented Jul 20, 2020

@kjeremy The person who left the review doesn't seem to be associated with serde in any way.

@jplatte
Copy link
Contributor Author

jplatte commented Oct 2, 2020

Rebased to trigger new CI runs, the old ones had been stuck for months.

@jplatte
Copy link
Contributor Author

jplatte commented Oct 23, 2020

CI fixed, no merge conflics. Ping @dtolnay

@roberts-ivanovs

This comment has been minimized.

@kaiba42

This comment has been minimized.

@llgerard
Copy link

Would love this!

@pmhpereira
Copy link

Any update on this? Would love to have this merged as well.

@jplatte
Copy link
Contributor Author

jplatte commented Jul 10, 2023

Rebased to fix conflicts with the syn 2.0 upgrade.

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Thanks!

serde_derive/src/internals/attr.rs Outdated Show resolved Hide resolved
Comment on lines 391 to 395
syn::Data::Union(syn::DataUnion { union_token, .. }) => {
let msg = "#[serde(rename_all_fields)] can only be used on enums";
cx.error_spanned_by(union_token, msg);
}
Copy link
Member

Choose a reason for hiding this comment

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

I think using the struct/union token for the span of this error is not a good choice. The problem is not that the user wrote a struct. The problem is that the user wrote a rename_all_fields attribute on their struct. The fix is not for the user to turn their struct into an enum. The fix is to change the attribute. So the error should point to the attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. When I just updated this, I noticed the (container-level) #[serde(default)] does the same thing of pointing to the enum / struct token. Should I send another PR to change that?

Copy link
Member

Choose a reason for hiding this comment

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

Yep, let's fix that too. Good catch.

serde_derive/src/internals/attr.rs Outdated Show resolved Hide resolved
Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you.

@dtolnay dtolnay merged commit e74925b into serde-rs:master Jul 27, 2023
21 checks passed
@jplatte jplatte deleted the rename_all_fields branch July 27, 2023 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Apply rename_all to enum member "fields"
8 participants