Skip to content

Commit 0f54a1a

Browse files
committed
Handle early return sooner on eof in seq or map
This matches how peeking is done within deserialize_any and other Deserializer methods
1 parent 2a4cb44 commit 0f54a1a

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/de.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -1929,25 +1929,31 @@ impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> {
19291929
fn has_next_element<'de, 'a, R: Read<'de> + 'a>(
19301930
seq: &mut SeqAccess<'a, R>,
19311931
) -> Result<bool> {
1932-
match tri!(seq.de.parse_whitespace()) {
1933-
Some(b']') => Ok(false),
1934-
Some(b',') if !seq.first => {
1932+
let peek = match tri!(seq.de.parse_whitespace()) {
1933+
Some(b) => b,
1934+
None => {
1935+
return Err(seq.de.peek_error(ErrorCode::EofWhileParsingList));
1936+
}
1937+
};
1938+
1939+
match peek {
1940+
b']' => Ok(false),
1941+
b',' if !seq.first => {
19351942
seq.de.eat_char();
19361943
match tri!(seq.de.parse_whitespace()) {
19371944
Some(b']') => Err(seq.de.peek_error(ErrorCode::TrailingComma)),
19381945
Some(_) => Ok(true),
19391946
None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingValue)),
19401947
}
19411948
}
1942-
Some(_) => {
1949+
_ => {
19431950
if seq.first {
19441951
seq.first = false;
19451952
Ok(true)
19461953
} else {
19471954
Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd))
19481955
}
19491956
}
1950-
None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingList)),
19511957
}
19521958
}
19531959

@@ -1978,9 +1984,16 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
19781984
K: de::DeserializeSeed<'de>,
19791985
{
19801986
fn has_next_key<'de, 'a, R: Read<'de> + 'a>(map: &mut MapAccess<'a, R>) -> Result<bool> {
1981-
match tri!(map.de.parse_whitespace()) {
1982-
Some(b'}') => Ok(false),
1983-
Some(b',') if !map.first => {
1987+
let peek = match tri!(map.de.parse_whitespace()) {
1988+
Some(b) => b,
1989+
None => {
1990+
return Err(map.de.peek_error(ErrorCode::EofWhileParsingObject));
1991+
}
1992+
};
1993+
1994+
match peek {
1995+
b'}' => Ok(false),
1996+
b',' if !map.first => {
19841997
map.de.eat_char();
19851998
match tri!(map.de.parse_whitespace()) {
19861999
Some(b'"') => Ok(true),
@@ -1989,10 +2002,10 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
19892002
None => Err(map.de.peek_error(ErrorCode::EofWhileParsingValue)),
19902003
}
19912004
}
1992-
Some(b) => {
2005+
_ => {
19932006
if map.first {
19942007
map.first = false;
1995-
if b == b'"' {
2008+
if peek == b'"' {
19962009
Ok(true)
19972010
} else {
19982011
Err(map.de.peek_error(ErrorCode::KeyMustBeAString))
@@ -2001,7 +2014,6 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
20012014
Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd))
20022015
}
20032016
}
2004-
None => Err(map.de.peek_error(ErrorCode::EofWhileParsingObject)),
20052017
}
20062018
}
20072019

0 commit comments

Comments
 (0)