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

refactor: Factor out whitespace skipping into helpers #690

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
189 changes: 67 additions & 122 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,24 @@ impl<'de, R: Read<'de>> Deserializer<R> {
Error::syntax(reason, position.line, position.column)
}

/// Returns the first non-whitespace byte without consuming it, or `Err` if
/// EOF is encountered.
fn parse_whitespace_in_value(&mut self) -> Result<u8> {
match tri!(self.parse_whitespace()) {
Some(b) => Ok(b),
None => Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
}
}

/// Returns the first non-whitespace byte without consuming it, or `Err` if
/// EOF is encountered.
fn parse_whitespace_in_object(&mut self) -> Result<u8> {
match tri!(self.parse_whitespace()) {
Some(b) => Ok(b),
None => Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
}
}

/// Returns the first non-whitespace byte without consuming it, or `None` if
/// EOF is encountered.
fn parse_whitespace(&mut self) -> Result<Option<u8>> {
Expand Down Expand Up @@ -304,12 +322,7 @@ impl<'de, R: Read<'de>> Deserializer<R> {
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let peek = tri!(self.parse_whitespace_in_value());

let value = match peek {
b'-' => {
Expand Down Expand Up @@ -961,13 +974,12 @@ impl<'de, R: Read<'de>> Deserializer<R> {
}

fn parse_object_colon(&mut self) -> Result<()> {
match tri!(self.parse_whitespace()) {
Some(b':') => {
match tri!(self.parse_whitespace_in_object()) {
b':' => {
self.eat_char();
Ok(())
}
Some(_) => Err(self.peek_error(ErrorCode::ExpectedColon)),
None => Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
_ => Err(self.peek_error(ErrorCode::ExpectedColon)),
}
}

Expand All @@ -990,14 +1002,13 @@ impl<'de, R: Read<'de>> Deserializer<R> {
}

fn end_map(&mut self) -> Result<()> {
match tri!(self.parse_whitespace()) {
Some(b'}') => {
match tri!(self.parse_whitespace_in_object()) {
b'}' => {
self.eat_char();
Ok(())
}
Some(b',') => Err(self.peek_error(ErrorCode::TrailingComma)),
Some(_) => Err(self.peek_error(ErrorCode::TrailingCharacters)),
None => Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
b',' => Err(self.peek_error(ErrorCode::TrailingComma)),
_ => Err(self.peek_error(ErrorCode::TrailingCharacters)),
}
}

Expand All @@ -1006,12 +1017,7 @@ impl<'de, R: Read<'de>> Deserializer<R> {
let mut enclosing = None;

loop {
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let peek = tri!(self.parse_whitespace_in_value());

let frame = match peek {
b'n' => {
Expand Down Expand Up @@ -1099,16 +1105,14 @@ impl<'de, R: Read<'de>> Deserializer<R> {
}

if frame == b'{' {
match tri!(self.parse_whitespace()) {
Some(b'"') => self.eat_char(),
Some(_) => return Err(self.peek_error(ErrorCode::KeyMustBeAString)),
None => return Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
match tri!(self.parse_whitespace_in_object()) {
b'"' => self.eat_char(),
_ => return Err(self.peek_error(ErrorCode::KeyMustBeAString)),
}
tri!(self.read.ignore_str());
match tri!(self.parse_whitespace()) {
Some(b':') => self.eat_char(),
Some(_) => return Err(self.peek_error(ErrorCode::ExpectedColon)),
None => return Err(self.peek_error(ErrorCode::EofWhileParsingObject)),
match tri!(self.parse_whitespace_in_object()) {
b':' => self.eat_char(),
_ => return Err(self.peek_error(ErrorCode::ExpectedColon)),
}
}

Expand Down Expand Up @@ -1292,12 +1296,7 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let peek = tri!(self.parse_whitespace_in_value());

let value = match peek {
b'n' => {
Expand Down Expand Up @@ -1368,12 +1367,7 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let peek = tri!(self.parse_whitespace_in_value());

let value = match peek {
b't' => {
Expand Down Expand Up @@ -1425,16 +1419,10 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
{
let mut buf = String::new();

match tri!(self.parse_whitespace()) {
Some(b'-') => {
self.eat_char();
buf.push('-');
}
Some(_) => {}
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
if tri!(self.parse_whitespace_in_value()) == b'-' {
self.eat_char();
buf.push('-');
}

tri!(self.scan_integer128(&mut buf));

Expand All @@ -1455,14 +1443,8 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
where
V: de::Visitor<'de>,
{
match tri!(self.parse_whitespace()) {
Some(b'-') => {
return Err(self.peek_error(ErrorCode::NumberOutOfRange));
}
Some(_) => {}
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
if tri!(self.parse_whitespace_in_value()) == b'-' {
return Err(self.peek_error(ErrorCode::NumberOutOfRange));
}

let mut buf = String::new();
Expand Down Expand Up @@ -1493,12 +1475,7 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let peek = tri!(self.parse_whitespace_in_value());

let value = match peek {
b'"' => {
Expand Down Expand Up @@ -1600,12 +1577,7 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let peek = tri!(self.parse_whitespace_in_value());

let value = match peek {
b'"' => {
Expand Down Expand Up @@ -1654,12 +1626,7 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let peek = tri!(self.parse_whitespace_in_value());

let value = match peek {
b'n' => {
Expand Down Expand Up @@ -1704,12 +1671,7 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let peek = tri!(self.parse_whitespace_in_value());

let value = match peek {
b'[' => {
Expand Down Expand Up @@ -1755,12 +1717,7 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let peek = tri!(self.parse_whitespace_in_value());

let value = match peek {
b'{' => {
Expand Down Expand Up @@ -1792,12 +1749,7 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
where
V: de::Visitor<'de>,
{
let peek = match tri!(self.parse_whitespace()) {
Some(b) => b,
None => {
return Err(self.peek_error(ErrorCode::EofWhileParsingValue));
}
};
let peek = tri!(self.parse_whitespace_in_value());

let value = match peek {
b'[' => {
Expand Down Expand Up @@ -1843,25 +1795,23 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
where
V: de::Visitor<'de>,
{
match tri!(self.parse_whitespace()) {
Some(b'{') => {
match tri!(self.parse_whitespace_in_value()) {
b'{' => {
check_recursion! {
self.eat_char();
let value = tri!(visitor.visit_enum(VariantAccess::new(self)));
}

match tri!(self.parse_whitespace()) {
Some(b'}') => {
match tri!(self.parse_whitespace_in_object()) {
b'}' => {
self.eat_char();
Ok(value)
}
Some(_) => Err(self.error(ErrorCode::ExpectedSomeValue)),
None => Err(self.error(ErrorCode::EofWhileParsingObject)),
_ => Err(self.error(ErrorCode::ExpectedSomeValue)),
}
}
Some(b'"') => visitor.visit_enum(UnitVariantAccess::new(self)),
Some(_) => Err(self.peek_error(ErrorCode::ExpectedSomeValue)),
None => Err(self.peek_error(ErrorCode::EofWhileParsingValue)),
b'"' => visitor.visit_enum(UnitVariantAccess::new(self)),
_ => Err(self.peek_error(ErrorCode::ExpectedSomeValue)),
}
}

Expand Down Expand Up @@ -1905,12 +1855,12 @@ impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> {
}
Some(b',') if !self.first => {
self.de.eat_char();
tri!(self.de.parse_whitespace())
tri!(self.de.parse_whitespace_in_value())
}
Some(b) => {
if self.first {
self.first = false;
Some(b)
b
} else {
return Err(self.de.peek_error(ErrorCode::ExpectedListCommaOrEnd));
}
Expand All @@ -1921,9 +1871,8 @@ impl<'de, 'a, R: Read<'de> + 'a> de::SeqAccess<'de> for SeqAccess<'a, R> {
};

match peek {
Some(b']') => Err(self.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Ok(Some(tri!(seed.deserialize(&mut *self.de)))),
None => Err(self.de.peek_error(ErrorCode::EofWhileParsingValue)),
b']' => Err(self.de.peek_error(ErrorCode::TrailingComma)),
_ => Ok(Some(tri!(seed.deserialize(&mut *self.de)))),
}
}
}
Expand All @@ -1946,32 +1895,28 @@ impl<'de, 'a, R: Read<'de> + 'a> de::MapAccess<'de> for MapAccess<'a, R> {
where
K: de::DeserializeSeed<'de>,
{
let peek = match tri!(self.de.parse_whitespace()) {
Some(b'}') => {
let peek = match tri!(self.de.parse_whitespace_in_object()) {
b'}' => {
return Ok(None);
}
Some(b',') if !self.first => {
b',' if !self.first => {
self.de.eat_char();
tri!(self.de.parse_whitespace())
tri!(self.de.parse_whitespace_in_value())
}
Some(b) => {
b => {
if self.first {
self.first = false;
Some(b)
b
} else {
return Err(self.de.peek_error(ErrorCode::ExpectedObjectCommaOrEnd));
}
}
None => {
return Err(self.de.peek_error(ErrorCode::EofWhileParsingObject));
}
};

match peek {
Some(b'"') => seed.deserialize(MapKey { de: &mut *self.de }).map(Some),
Some(b'}') => Err(self.de.peek_error(ErrorCode::TrailingComma)),
Some(_) => Err(self.de.peek_error(ErrorCode::KeyMustBeAString)),
None => Err(self.de.peek_error(ErrorCode::EofWhileParsingValue)),
b'"' => seed.deserialize(MapKey { de: &mut *self.de }).map(Some),
b'}' => Err(self.de.peek_error(ErrorCode::TrailingComma)),
_ => Err(self.de.peek_error(ErrorCode::KeyMustBeAString)),
}
}

Expand Down