-
-
Notifications
You must be signed in to change notification settings - Fork 777
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
Integer/boolean tags for internally/adjacently tagged enums #2056
Integer/boolean tags for internally/adjacently tagged enums #2056
Conversation
@dtolnay I think this is ready for review now. |
Is there an example of a workaround until this lands? I need to deserialize something like:
Where the discriminators are 0, 1, 2, 5, 7, and 12, respectively. I cannot change the data format. I believe that I need to use a custom |
Hey! Any news on this? I'd really need that for a project I'm working on. |
This comment was marked as spam.
This comment was marked as spam.
I think @AmaranthineCodices needs to fix the CI / Test suite before @dtolnay is going to look at this. |
I have rebased this PR to HEAD. With the exception of a few minor clippy issues it passes the CI, see https://github.com/MForster/serde/pull/1. @dtolnay, can you take look at this PR? If it is basically good to go and just needs a few minor fixes, I can take care of that. |
This being integrated would be helpful for me too; It's needed to describe the scripting commands in RPG Maker MV projects, which are saved in JSON with the following shape: {
"code": <integer>, // tag
"indent": <integer>, // shared field, in an outer struct that the enum is flattened into
"parameters": [<values>] // types vary by variant
} Currently, the most effective way to proceed there for me is to deserialise the partial object of non-shared fields into a dynamically typed This particular enum likely has upwards of 100 variants, so being able to generate an efficient implementation there would be very welcome. For now I'll see how well I can bodge it with an unhygienic utility macro. Update: I moved to a lazy typing system that doesn't use Serde (weird use case), so I currently don't need this anymore. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as spam.
This comment was marked as spam.
This would be amazing for Discord's GatewayEvent enum, specifically replacing the custom deserialization of https://github.com/serenity-rs/serenity/blob/f97322475eadf2dc78ea3e6d31b55811b3eb50ba/src/model/event.rs#L624-L711 |
This comment was marked as spam.
This comment was marked as spam.
This is an awesome feature that will be helpful to many of my projects. |
This comment was marked as spam.
This comment was marked as spam.
whats missing for this to merge? 1 year 😭 |
@dtolnay Can we please get an information what is with this PR? There's lots of people waiting for this particular feature and the workaround to compile AmranthineCodices branch manually is getting really annoying, because we have to compile everything which uses serde with our custom branch as dependency. |
This doesn't work because this PR isn't merged: serde-rs/serde#2056
This doesn't work because this PR isn't merged: serde-rs/serde#2056
Any updates regarding this? This seems to be the last piece preventing some of the private constructs of serde to be made public. Really looking forward to reusing some of the components for custom deserializations. |
This comment was marked as spam.
This comment was marked as spam.
In the meantime I found #745 (comment) to be a reasonable workaround |
@ maintainers It'd be great to get this into a release... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of the comments in #2525 (review) apply to this PR.
I am going to close this, as I don't have the personal bandwidth to push this through. Sorry for the delay, but I've lost all context for this and I can't drive it right now. |
This doesn't work because this PR isn't merged: serde-rs/serde#2056
Closes #745.
Implements integer/boolean renames / aliases for internally and adjacently tagged enum variants:
UI
In
attr.rs
, I introduced a newName
variant,VariantName
. To cut down on code duplication, I madeName
generic over its internal name representation;Name
is nowNames<String>
. I added a step tocheck.rs
to ensure that non-string renames are only used on internally/adjacently tagged enums. It's not necessary to check this for structs or the container name; the types of those names are stillString
.Deserialization
Deserialization was relatively straightforward to implement: rather than unconditionally generating constructors/fields for strings/bytes, we only do this if there are string-named variants. We do the same for ints/bools. One concern is that
VARIANTS
is still&'static [&'static str]
, but I think this is pretty low-impact, as it's used only for stringifying error messages. Changing it would be a breaking change as it would be visible in the signatures ofError::unknown_variant
andError::unknown_field
.Serialization
For serialization, I had to add a
VariantName
enum toserde
itself, to be used in places where an integer/boolean can appear where previously only aString
existed. We generate code inserde_derive
for this enum wherever we invoke these methods.The one part of this change that I'm not happy about is here:
https://github.com/AmaranthineCodices/serde/blob/37b4c68c0a9aa7aba8c6ee148c9c5e64c6f3f28c/serde_derive/src/ser.rs#L700
I am not fond of this
to_string
call, but getting rid of it would be a breaking change as far as I can tell - the change would be visible in the public-facing signatures ofSerializer::serialize_struct
andSerializer::serialize_struct_variant
. This would be a good change to include in a new major serde version, though.TODO
collect_other_fields
in deserializationdeserialize_generated_identifier
and see if I can remove the code duplication between it anddeserialize_identifier
main_constructors
indeserialize_identifier