From 10033cca54a651d8da3d408090fcfd279d762230 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 23 Oct 2023 18:52:00 +0100 Subject: impl Deserializer for Config: forward everything to cache This had open-coded copies of the impl for Value. Replace them with calls to the impl for Value. This reduces duplication. It would allow us to change the impl for Value. Use a macro for the many very-similar functions. Signed-off-by: Ian Jackson --- src/de.rs | 160 ++++++++++++++++++-------------------------------------------- 1 file changed, 47 insertions(+), 113 deletions(-) diff --git a/src/de.rs b/src/de.rs index 0325ce6..0e2b8de 100644 --- a/src/de.rs +++ b/src/de.rs @@ -363,122 +363,56 @@ impl<'de> de::VariantAccess<'de> for EnumAccess { } } -impl<'de> de::Deserializer<'de> for Config { - type Error = ConfigError; - +/// Define `$method`s, `deserialize_foo`, by forwarding to `Value` +/// +/// `($arg: $argtype, ...)`, if supplied, are the formal arguments +macro_rules! config_deserialize_via_value { { $( + $method:ident $( ( $( $arg:ident: $argtype:ty ),* ) )? ; +)* } => { $( #[inline] - fn deserialize_any(self, visitor: V) -> Result - where - V: de::Visitor<'de>, - { - // Delegate deserialization to Value - de::Deserializer::deserialize_any(self.cache, visitor) + fn $method>( + self, + $( $( $arg: $argtype, )* )? + visitor: V, + ) -> Result { + self.cache.$method( $( $( $arg, )* )? visitor) } +)* } } - #[inline] - fn deserialize_bool>(self, visitor: V) -> Result { - visitor.visit_bool(self.cache.into_bool()?) - } - - #[inline] - fn deserialize_i8>(self, visitor: V) -> Result { - let num = try_convert_number!(signed, self.cache, "8"); - visitor.visit_i8(num) - } - - #[inline] - fn deserialize_i16>(self, visitor: V) -> Result { - let num = try_convert_number!(signed, self.cache, "16"); - visitor.visit_i16(num) - } - - #[inline] - fn deserialize_i32>(self, visitor: V) -> Result { - let num = try_convert_number!(signed, self.cache, "32"); - visitor.visit_i32(num) - } - - #[inline] - fn deserialize_i64>(self, visitor: V) -> Result { - let num = try_convert_number!(signed, self.cache, "64"); - visitor.visit_i64(num) - } - - #[inline] - fn deserialize_u8>(self, visitor: V) -> Result { - let num = try_convert_number!(unsigned, self.cache, "8"); - visitor.visit_u8(num) - } - - #[inline] - fn deserialize_u16>(self, visitor: V) -> Result { - let num = try_convert_number!(unsigned, self.cache, "16"); - visitor.visit_u16(num) - } - - #[inline] - fn deserialize_u32>(self, visitor: V) -> Result { - let num = try_convert_number!(unsigned, self.cache, "32"); - visitor.visit_u32(num) - } - - #[inline] - fn deserialize_u64>(self, visitor: V) -> Result { - let num = try_convert_number!(unsigned, self.cache, "64"); - visitor.visit_u64(num) - } - - #[inline] - fn deserialize_f32>(self, visitor: V) -> Result { - visitor.visit_f32(self.cache.into_float()? as f32) - } - - #[inline] - fn deserialize_f64>(self, visitor: V) -> Result { - visitor.visit_f64(self.cache.into_float()?) - } - - #[inline] - fn deserialize_str>(self, visitor: V) -> Result { - visitor.visit_string(self.cache.into_string()?) - } - - #[inline] - fn deserialize_string>(self, visitor: V) -> Result { - visitor.visit_string(self.cache.into_string()?) - } - - #[inline] - fn deserialize_option(self, visitor: V) -> Result - where - V: de::Visitor<'de>, - { - // Match an explicit nil as None and everything else as Some - match self.cache.kind { - ValueKind::Nil => visitor.visit_none(), - _ => visitor.visit_some(self), - } - } - - fn deserialize_enum( - self, - name: &'static str, - variants: &'static [&'static str], - visitor: V, - ) -> Result - where - V: de::Visitor<'de>, - { - visitor.visit_enum(EnumAccess { - value: self.cache, - name, - variants, - }) - } +impl<'de> de::Deserializer<'de> for Config { + type Error = ConfigError; - serde::forward_to_deserialize_any! { - char seq - bytes byte_buf map struct unit newtype_struct - identifier ignored_any unit_struct tuple_struct tuple + config_deserialize_via_value! { + deserialize_any; + deserialize_bool; + deserialize_i8; + deserialize_i16; + deserialize_i32; + deserialize_i64; + deserialize_u8; + deserialize_u16; + deserialize_u32; + deserialize_u64; + deserialize_f32; + deserialize_f64; + deserialize_str; + deserialize_string; + deserialize_option; + + deserialize_char; + deserialize_seq; + deserialize_bytes; + deserialize_byte_buf; + deserialize_map; + deserialize_unit; + deserialize_identifier; + deserialize_ignored_any; + + deserialize_enum(name: &'static str, variants: &'static [&'static str]); + deserialize_unit_struct(name: &'static str); + deserialize_newtype_struct(name: &'static str); + deserialize_tuple(n: usize); + deserialize_tuple_struct(name: &'static str, n: usize); + deserialize_struct(name: &'static str, fields: &'static [&'static str]); } } -- cgit v1.2.3