Skip to content

Commit

Permalink
Handle early return sooner on eof in seq or map
Browse files Browse the repository at this point in the history
This matches how peeking is done within deserialize_any and other
Deserializer methods
  • Loading branch information
dtolnay committed Oct 19, 2024
1 parent 2a4cb44 commit 0f54a1a
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1929,25 +1929,31 @@ impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> {
fn has_next_element<'de, 'a, R: Read<'de> + 'a>(
seq: &mut SeqAccess<'a, R>,
) -> Result<bool> {
match tri!(seq.de.parse_whitespace()) {
Some(b']') => Ok(false),
Some(b',') if !seq.first => {
let peek = match tri!(seq.de.parse_whitespace()) {
Some(b) => b,
None => {
return Err(seq.de.peek_error(ErrorCode::EofWhileParsingList));
}
};

match peek {
b']' => Ok(false),
b',' if !seq.first => {
seq.de.eat_char();
match tri!(seq.de.parse_whitespace()) {
Some(b']') => Err(seq.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Ok(true),
None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingValue)),
}
}
Some(_) => {
_ => {
if seq.first {
seq.first = false;
Ok(true)
} else {
Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd))
}
}
None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingList)),
}
}

Expand Down Expand Up @@ -1978,9 +1984,16 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
K: de::DeserializeSeed<'de>,
{
fn has_next_key<'de, 'a, R: Read<'de> + 'a>(map: &mut MapAccess<'a, R>) -> Result<bool> {
match tri!(map.de.parse_whitespace()) {
Some(b'}') => Ok(false),
Some(b',') if !map.first => {
let peek = match tri!(map.de.parse_whitespace()) {
Some(b) => b,
None => {
return Err(map.de.peek_error(ErrorCode::EofWhileParsingObject));
}
};

match peek {
b'}' => Ok(false),
b',' if !map.first => {
map.de.eat_char();
match tri!(map.de.parse_whitespace()) {
Some(b'"') => Ok(true),
Expand All @@ -1989,10 +2002,10 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
None => Err(map.de.peek_error(ErrorCode::EofWhileParsingValue)),
}
}
Some(b) => {
_ => {
if map.first {
map.first = false;
if b == b'"' {
if peek == b'"' {
Ok(true)
} else {
Err(map.de.peek_error(ErrorCode::KeyMustBeAString))
Expand All @@ -2001,7 +2014,6 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd))
}
}
None => Err(map.de.peek_error(ErrorCode::EofWhileParsingObject)),
}
}

Expand Down

0 comments on commit 0f54a1a

Please sign in to comment.