Skip to content

Commit

Permalink
Convert serde_test's assert_next_token from macro to function
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed May 4, 2023
1 parent 1b8290b commit 8f4d37c
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions serde_test/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,18 @@ pub struct Deserializer<'de> {
tokens: &'de [Token],
}

macro_rules! assert_next_token {
($de:expr, $expected:expr) => {
match $de.next_token_opt() {
Some(token) if token == $expected => {}
Some(other) => {
return Err(de::Error::custom(format!(
"expected Token::{} but deserialization wants Token::{}",
other, $expected
)))
}
None => {
return Err(de::Error::custom(format!(
"end of tokens but deserialization wants Token::{}",
$expected
)))
}
}
};
fn assert_next_token(de: &mut Deserializer, expected: Token) -> Result<(), Error> {
match de.next_token_opt() {
Some(token) if token == expected => Ok(()),
Some(other) => Err(de::Error::custom(format!(
"expected Token::{} but deserialization wants Token::{}",
other, expected,
))),
None => Err(de::Error::custom(format!(
"end of tokens but deserialization wants Token::{}",
expected,
))),
}
}

fn unexpected(token: Token) -> Error {
Expand Down Expand Up @@ -99,7 +93,7 @@ impl<'de> Deserializer<'de> {
len: len,
end: end,
})?;
assert_next_token!(self, end);
assert_next_token(self, end)?;
Ok(value)
}

Expand All @@ -117,7 +111,7 @@ impl<'de> Deserializer<'de> {
len: len,
end: end,
})?;
assert_next_token!(self, end);
assert_next_token(self, end)?;
Ok(value)
}
}
Expand Down Expand Up @@ -291,7 +285,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
{
match self.peek_token() {
Token::UnitStruct { .. } => {
assert_next_token!(self, Token::UnitStruct { name: name });
assert_next_token(self, Token::UnitStruct { name: name })?;
visitor.visit_unit()
}
_ => self.deserialize_any(visitor),
Expand All @@ -308,7 +302,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
{
match self.peek_token() {
Token::NewtypeStruct { .. } => {
assert_next_token!(self, Token::NewtypeStruct { name: name });
assert_next_token(self, Token::NewtypeStruct { name: name })?;
visitor.visit_newtype_struct(self)
}
_ => self.deserialize_any(visitor),
Expand Down Expand Up @@ -355,7 +349,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
visitor.visit_unit()
}
Token::UnitStruct { .. } => {
assert_next_token!(self, Token::UnitStruct { name: name });
assert_next_token(self, Token::UnitStruct { name: name })?;
visitor.visit_unit()
}
Token::Seq { .. } => {
Expand All @@ -367,7 +361,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
self.visit_seq(Some(len), Token::TupleEnd, visitor)
}
Token::TupleStruct { len: n, .. } => {
assert_next_token!(self, Token::TupleStruct { name: name, len: n });
assert_next_token(self, Token::TupleStruct { name: name, len: n })?;
self.visit_seq(Some(len), Token::TupleStructEnd, visitor)
}
_ => self.deserialize_any(visitor),
Expand All @@ -385,7 +379,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
{
match self.peek_token() {
Token::Struct { len: n, .. } => {
assert_next_token!(self, Token::Struct { name: name, len: n });
assert_next_token(self, Token::Struct { name: name, len: n })?;
self.visit_map(Some(fields.len()), Token::StructEnd, visitor)
}
Token::Map { .. } => {
Expand Down Expand Up @@ -644,7 +638,7 @@ impl<'de, 'a> MapAccess<'de> for EnumMapVisitor<'a, 'de> {
};
seed.deserialize(SeqAccessDeserializer::new(visitor))?
};
assert_next_token!(self.de, Token::TupleVariantEnd);
assert_next_token(self.de, Token::TupleVariantEnd)?;
Ok(value)
}
EnumFormat::Map => {
Expand All @@ -656,7 +650,7 @@ impl<'de, 'a> MapAccess<'de> for EnumMapVisitor<'a, 'de> {
};
seed.deserialize(MapAccessDeserializer::new(visitor))?
};
assert_next_token!(self.de, Token::StructVariantEnd);
assert_next_token(self.de, Token::StructVariantEnd)?;
Ok(value)
}
EnumFormat::Any => seed.deserialize(&mut *self.de),
Expand Down

0 comments on commit 8f4d37c

Please sign in to comment.