-
-
Notifications
You must be signed in to change notification settings - Fork 226
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
Deserializing enums fails at run-time #211
Comments
I don't see a way to do this. The only enum support offered is at the level of an individual field. There is no record level enum support like you want here and I don't really see a way to add it. You'll have to do this translation manually. (In general, Serde and csv don't really fit together well. The existing support is a hodge podge of things with a bunch of arbitrary-seeming limitations. The main problem is that csv has no types and is not a recursive format.) |
That's very understandable but if you could add this as a warning in the docs, that would be great. I spent a hour trying to find what I'm doing wrong and given that internally-tagged enums are quite common place in the serde world, I'm sure I'm not the only one who will hit this. |
@zeenix Could you perhaps propose a wording/location (best through a PR) where this warning would have save you time? |
Sure, I'll add it to my TODO for the week. :) |
@BurntSushi here you go: #231 |
This comment was marked as duplicate.
This comment was marked as duplicate.
It is unfortunate that both [1] and [2] conspire to make this code way worse than it could otherwise be with a saner (de)serialization format. We both need to introduce `TransactionRecord` due to tagged enums not being powerful enough in CSV, and make its `amount` field optional to deal with the varying number of fields for each kind of transaction. [1]: BurntSushi/rust-csv#211 [2]: BurntSushi/rust-csv#172
It's been 2 years since I provided the PR but you didn't review. 😔 |
I have no idea if there's a good way to do something along these lines in |
Thanks for the idea it looks quite simple! I've been previously looking at deserializing it as a map which would allow the discriminant field to be on any position. |
What version of the
csv
crate are you using?1.1.3
Briefly describe the question, bug or feature request.
I can't get csv/serde to deserialize an enum.
I am processing a CSV file whose records store several different variants of data. It has a discriminator field and columns specific to each variant (which are empty when the discriminator shows the other variant), as well as some common columns, for example:
EventType
is the discriminator that discriminates between two variants,A
andB
.C
is the field common to both variants, andA1
andB1
are fields belonging to variantsA
andB
respectively.I would like to deserialize those rows Rust enums, something like:
...but I can't get that to successfully run with serde/csv using the code below (or variants thereof that I tested).
Include a complete program demonstrating a problem.
What is the observed behavior of the code above?
The program outputs errors for both fields, the error message being
"invalid type: string \"A\", expected internally tagged enum"
. Full output:What is the expected or desired behavior of the code above?
It should output the deserialized records, something like:
The text was updated successfully, but these errors were encountered: