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
When a serde(untagged) enum can't deser, show all the reasons why #2376
Closed
cbeck88
wants to merge
2
commits into
serde-rs:master
from
cbeck88:improve-serde-untagged-error-messages
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1674,28 +1674,44 @@ fn deserialize_untagged_enum( | |
)) | ||
}); | ||
|
||
// TODO this message could be better by saving the errors from the failed | ||
// attempts. The heuristic used by TOML was to count the number of fields | ||
// processed before an error, and use the error that happened after the | ||
// largest number of fields. I'm not sure I like that. Maybe it would be | ||
// better to save all the errors and combine them into one message that | ||
// explains why none of the variants matched. | ||
let fallthrough_msg = format!( | ||
"data did not match any variant of untagged enum {}", | ||
params.type_name() | ||
); | ||
let fallthrough_msg = cattrs.expecting().unwrap_or(&fallthrough_msg); | ||
|
||
quote_block! { | ||
// If all variants cannot be deserialized, we will try to write an error | ||
// message explaining why it failed for each one | ||
// The format string we are building will have the following structure: | ||
// "data did not match any variant of untagged enum{}\nvar1: {}\nvar2: {}\nvar3: {}" | ||
let mut err_format_string = fallthrough_msg.to_owned(); | ||
let mut num_variants = 0usize; | ||
for var in variants.iter().filter(|variant| !variant.attrs.skip_deserializing()) { | ||
err_format_string.push_str("\n"); | ||
err_format_string.push_str(&var.ident.to_string()); | ||
err_format_string.push_str(": {}"); | ||
num_variants += 1; | ||
} | ||
|
||
// We need two copies of this iterator | ||
let err_identifiers1 = (0..num_variants).map(|idx| format_ident!("_err{}", idx)); | ||
let err_identifiers2 = err_identifiers1.clone(); | ||
Comment on lines
+1696
to
+1698
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While not strictly necessary, as you could probably do the iteration over the |
||
|
||
quote_block! { | ||
let __content = try!(<_serde::__private::de::Content as _serde::Deserialize>::deserialize(__deserializer)); | ||
|
||
#( | ||
if let _serde::__private::Ok(__ok) = #attempts { | ||
return _serde::__private::Ok(__ok); | ||
} | ||
let #err_identifiers1 = match #attempts { | ||
_serde::__private::Ok(__ok) => { | ||
return _serde::__private::Ok(__ok); | ||
}, | ||
_serde::__private::Err(__err) => { | ||
__err | ||
}, | ||
}; | ||
)* | ||
|
||
_serde::__private::Err(_serde::de::Error::custom(#fallthrough_msg)) | ||
_serde::__private::Err(_serde::de::Error::custom(format_args!(#err_format_string, #(#err_identifiers2),*))) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm not confident this will render well in all the error libraries out there, but we can adjust it once we have seen some of these messages in the wild. The Display/Debug output isn't something we provide back compat guarantees for anyway.