Skip to content

Commit

Permalink
Rearrange 'match peek'
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 19, 2024
1 parent 4cb90ce commit 2a4cb44
Showing 1 changed file with 26 additions and 34 deletions.
60 changes: 26 additions & 34 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1929,31 +1929,25 @@ 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> {
let peek = match tri!(seq.de.parse_whitespace()) {
Some(b']') => {
return Ok(false);
}
match tri!(seq.de.parse_whitespace()) {
Some(b']') => Ok(false),
Some(b',') if !seq.first => {
seq.de.eat_char();
tri!(seq.de.parse_whitespace())
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(b) => {
Some(_) => {
if seq.first {
seq.first = false;
Some(b)
Ok(true)
} else {
return Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd));
Err(seq.de.peek_error(ErrorCode::ExpectedListCommaOrEnd))
}
}
None => {
return Err(seq.de.peek_error(ErrorCode::EofWhileParsingList));
}
};

match peek {
Some(b']') => Err(seq.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Ok(true),
None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingValue)),
None => Err(seq.de.peek_error(ErrorCode::EofWhileParsingList)),
}
}

Expand Down Expand Up @@ -1984,32 +1978,30 @@ 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> {
let peek = match tri!(map.de.parse_whitespace()) {
Some(b'}') => {
return Ok(false);
}
match tri!(map.de.parse_whitespace()) {
Some(b'}') => Ok(false),
Some(b',') if !map.first => {
map.de.eat_char();
tri!(map.de.parse_whitespace())
match tri!(map.de.parse_whitespace()) {
Some(b'"') => Ok(true),
Some(b'}') => Err(map.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Err(map.de.peek_error(ErrorCode::KeyMustBeAString)),
None => Err(map.de.peek_error(ErrorCode::EofWhileParsingValue)),
}
}
Some(b) => {
if map.first {
map.first = false;
Some(b)
if b == b'"' {
Ok(true)
} else {
Err(map.de.peek_error(ErrorCode::KeyMustBeAString))
}
} else {
return Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd));
Err(map.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd))
}
}
None => {
return Err(map.de.peek_error(ErrorCode::EofWhileParsingObject));
}
};

match peek {
Some(b'"') => Ok(true),
Some(b'}') => Err(map.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Err(map.de.peek_error(ErrorCode::KeyMustBeAString)),
None => Err(map.de.peek_error(ErrorCode::EofWhileParsingValue)),
None => Err(map.de.peek_error(ErrorCode::EofWhileParsingObject)),
}
}

Expand Down

0 comments on commit 2a4cb44

Please sign in to comment.