From 1b8290b3185e0ffff0769cbca2abe695aafed153 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 4 May 2023 16:30:10 -0700 Subject: [PATCH] Convert serde_test's unexpected from macro to function --- serde_test/src/de.rs | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/serde_test/src/de.rs b/serde_test/src/de.rs index 21d0764da..0cde33ca9 100644 --- a/serde_test/src/de.rs +++ b/serde_test/src/de.rs @@ -32,13 +32,11 @@ macro_rules! assert_next_token { }; } -macro_rules! unexpected { - ($token:expr) => { - Err(de::Error::custom(format!( - "deserialization did not expect this token: {}", - $token - ))) - }; +fn unexpected(token: Token) -> Error { + de::Error::custom(format!( + "deserialization did not expect this token: {}", + token, + )) } macro_rules! end_of_tokens { @@ -211,7 +209,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { self.next_token(); visitor.visit_u64(variant) } - (variant, Token::Unit) => unexpected!(variant), + (variant, Token::Unit) => Err(unexpected(variant)), (variant, _) => { visitor.visit_map(EnumMapVisitor::new(self, variant, EnumFormat::Any)) } @@ -239,9 +237,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { | Token::MapEnd | Token::StructEnd | Token::TupleVariantEnd - | Token::StructVariantEnd => { - unexpected!(token) - } + | Token::StructVariantEnd => Err(unexpected(token)), } } @@ -538,7 +534,7 @@ impl<'de, 'a> VariantAccess<'de> for DeserializerEnumVisitor<'a, 'de> { self.de .visit_seq(Some(len), Token::TupleVariantEnd, visitor) } else { - unexpected!(token) + Err(unexpected(token)) } } Token::Seq { @@ -549,7 +545,7 @@ impl<'de, 'a> VariantAccess<'de> for DeserializerEnumVisitor<'a, 'de> { if len == enum_len { self.de.visit_seq(Some(len), Token::SeqEnd, visitor) } else { - unexpected!(token) + Err(unexpected(token)) } } _ => de::Deserializer::deserialize_any(self.de, visitor), @@ -572,7 +568,7 @@ impl<'de, 'a> VariantAccess<'de> for DeserializerEnumVisitor<'a, 'de> { self.de .visit_map(Some(fields.len()), Token::StructVariantEnd, visitor) } else { - unexpected!(token) + Err(unexpected(token)) } } Token::Map { @@ -584,7 +580,7 @@ impl<'de, 'a> VariantAccess<'de> for DeserializerEnumVisitor<'a, 'de> { self.de .visit_map(Some(fields.len()), Token::MapEnd, visitor) } else { - unexpected!(token) + Err(unexpected(token)) } } _ => de::Deserializer::deserialize_any(self.de, visitor), @@ -629,7 +625,7 @@ impl<'de, 'a> MapAccess<'de> for EnumMapVisitor<'a, 'de> { .deserialize(BytesDeserializer { value: variant }) .map(Some), Some(Token::U32(variant)) => seed.deserialize(variant.into_deserializer()).map(Some), - Some(other) => unexpected!(other), + Some(other) => Err(unexpected(other)), None => Ok(None), } }