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

Improve error messages when deserializing enums #1217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1873,7 +1873,14 @@ impl<'de, R: Read<'de>> de::Deserializer<'de> for &mut Deserializer<R> {
Some(b'{') => {
check_recursion! {
self.eat_char();
let ret = visitor.visit_enum(VariantAccess::new(self));
let ret = match tri!(self.parse_whitespace()) {
Some(b'}') => Err(self.fix_position(de::Error::invalid_value(
Unexpected::Other("empty map"),
&"enum variant",
))),
Some(_) => visitor.visit_enum(VariantAccess::new(self)),
None => Err(self.error(ErrorCode::EofWhileParsingObject)),
};
}
let value = tri!(ret);

Expand All @@ -1882,12 +1889,11 @@ impl<'de, R: Read<'de>> de::Deserializer<'de> for &mut Deserializer<R> {
self.eat_char();
Ok(value)
}
Some(_) => Err(self.error(ErrorCode::ExpectedSomeValue)),
Some(_) => Err(self.error(ErrorCode::ExpectedObjectCommaOrEnd)),
None => Err(self.error(ErrorCode::EofWhileParsingObject)),
}
}
Some(b'"') => visitor.visit_enum(UnitVariantAccess::new(self)),
Some(_) => Err(self.peek_error(ErrorCode::ExpectedSomeValue)),
Some(_) => visitor.visit_enum(UnitVariantAccess::new(self)),
None => Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
}
}
Expand Down
47 changes: 45 additions & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ enum Animal {
AntHive(Vec<String>),
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
enum BoardGame {
Chess,
Checkers,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct Inner {
a: (),
Expand Down Expand Up @@ -1304,12 +1310,15 @@ fn test_parse_option() {
fn test_parse_enum_errors() {
test_parse_err::<Animal>(
&[
("{}", "expected value at line 1 column 2"),
("[]", "expected value at line 1 column 1"),
("{}", "invalid value: empty map, expected enum variant at line 1 column 1"),
("[]", "invalid type: sequence, expected variant identifier at line 1 column 0"),
("true", "invalid type: boolean `true`, expected variant identifier at line 1 column 4"),
("\"unknown\"",
"unknown variant `unknown`, expected one of `Dog`, `Frog`, `Cat`, `AntHive` at line 1 column 9"),
("{\"unknown\":null}",
"unknown variant `unknown`, expected one of `Dog`, `Frog`, `Cat`, `AntHive` at line 1 column 10"),
("{\"AntHive\": []", "EOF while parsing an object at line 1 column 14"),
("{\"AntHive\": []\"", "expected `,` or `}` at line 1 column 14"),
("{\"Dog\":", "EOF while parsing a value at line 1 column 7"),
("{\"Dog\":}", "expected value at line 1 column 8"),
("{\"Dog\":{}}", "invalid type: map, expected unit at line 1 column 7"),
Expand All @@ -1331,6 +1340,40 @@ fn test_parse_enum_errors() {
);
}

#[test]
fn test_parse_value_less_enum_errors() {
test_parse_err::<BoardGame>(&[
(
"1",
"invalid type: integer `1`, expected variant identifier at line 1 column 1",
),
(
"null",
"invalid type: null, expected variant identifier at line 1 column 4",
),
(
"true",
"invalid type: boolean `true`, expected variant identifier at line 1 column 4",
),
(
"[]",
"invalid type: sequence, expected variant identifier at line 1 column 0",
),
(
"{}",
"invalid value: empty map, expected enum variant at line 1 column 1",
),
(
"{\"unknown\": \"unknown\"}",
"unknown variant `unknown`, expected `Chess` or `Checkers` at line 1 column 10",
),
(
"{\"Chess\": \"unknown\"}",
"invalid type: string \"unknown\", expected unit at line 1 column 19",
),
]);
}

#[test]
fn test_parse_enum() {
test_parse_ok(vec![
Expand Down