You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently this produces a macro error error: #[serde(rename_all_fields)] can only be used on enums.
Allowing use of rename_all_fields would give greater consistency - to me it seems that the operation "rename all fields" applies just as well to plain structs as to struct variants in enums.
This would mean we could use the same attribute for structs and enums to get a reasonable javascript compatible renaming, using PascalCase variant names (no renaming from Rust) and camelCase field names. This would make it easier to explain to other team members new to Serde, and reduce the risk of accidentally renaming enum variants since we wouldn't have to use rename_all anywhere.
One use case that would be very useful for me would be when using prost to generate structs and enums from protobuf. prost allows adding annotations to generated structs and enums, but the same annotation must be used in all cases.
It seems like this wouldn't be a breaking change, since it wouldn't change the behaviour of any existing error-free code, it would just allow code that would previously have produced an error to compile?
One alternative for my use case would be a proc_macro_attribute that operates on data and applies different serde attributes depending on whether the data is an enum or struct, but it seems a shame that's necessary. We'd probably implement the macro to accept a "style" (e.g. a style could represent "PascalCase enum variants, camelCase fields, tag = type" to use one common approach) that could then set other attributes like tag = "type" on enums - not sure whether that's something serde could eventually have to allow for more compact attributes that aim for a common combination of renaming settings?
The text was updated successfully, but these errors were encountered:
It could be useful if the following code worked:
Currently this produces a macro error
error: #[serde(rename_all_fields)] can only be used on enums
.Allowing use of
rename_all_fields
would give greater consistency - to me it seems that the operation "rename all fields" applies just as well to plain structs as to struct variants in enums.This would mean we could use the same attribute for structs and enums to get a reasonable javascript compatible renaming, using PascalCase variant names (no renaming from Rust) and camelCase field names. This would make it easier to explain to other team members new to Serde, and reduce the risk of accidentally renaming enum variants since we wouldn't have to use
rename_all
anywhere.One use case that would be very useful for me would be when using
prost
to generate structs and enums from protobuf.prost
allows adding annotations to generated structs and enums, but the same annotation must be used in all cases.It seems like this wouldn't be a breaking change, since it wouldn't change the behaviour of any existing error-free code, it would just allow code that would previously have produced an error to compile?
One alternative for my use case would be a
proc_macro_attribute
that operates on data and applies differentserde
attributes depending on whether the data is an enum or struct, but it seems a shame that's necessary. We'd probably implement the macro to accept a "style" (e.g. a style could represent "PascalCase enum variants, camelCase fields, tag = type" to use one common approach) that could then set other attributes liketag = "type"
on enums - not sure whether that's something serde could eventually have to allow for more compact attributes that aim for a common combination of renaming settings?The text was updated successfully, but these errors were encountered: