| 1 | use serde::de::IntoDeserializer; |
| 2 | |
| 3 | use super::Error; |
| 4 | |
| 5 | pub(crate) struct KeyDeserializer { |
| 6 | span: Option<std::ops::Range<usize>>, |
| 7 | key: crate::Key, |
| 8 | } |
| 9 | |
| 10 | impl KeyDeserializer { |
| 11 | pub(crate) fn new(key: crate::Key, span: Option<std::ops::Range<usize>>) -> Self { |
| 12 | KeyDeserializer { span, key } |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | impl IntoDeserializer<'_, Error> for KeyDeserializer { |
| 17 | type Deserializer = Self; |
| 18 | |
| 19 | fn into_deserializer(self) -> Self::Deserializer { |
| 20 | self |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | impl<'de> serde::de::Deserializer<'de> for KeyDeserializer { |
| 25 | type Error = Error; |
| 26 | |
| 27 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error> |
| 28 | where |
| 29 | V: serde::de::Visitor<'de>, |
| 30 | { |
| 31 | self.key.into_deserializer().deserialize_any(visitor) |
| 32 | } |
| 33 | |
| 34 | fn deserialize_enum<V>( |
| 35 | self, |
| 36 | name: &str, |
| 37 | variants: &'static [&'static str], |
| 38 | visitor: V, |
| 39 | ) -> Result<V::Value, Self::Error> |
| 40 | where |
| 41 | V: serde::de::Visitor<'de>, |
| 42 | { |
| 43 | let _ = name; |
| 44 | let _ = variants; |
| 45 | visitor.visit_enum(self) |
| 46 | } |
| 47 | |
| 48 | fn deserialize_struct<V>( |
| 49 | self, |
| 50 | name: &'static str, |
| 51 | fields: &'static [&'static str], |
| 52 | visitor: V, |
| 53 | ) -> Result<V::Value, Error> |
| 54 | where |
| 55 | V: serde::de::Visitor<'de>, |
| 56 | { |
| 57 | if serde_spanned::__unstable::is_spanned(name, fields) { |
| 58 | if let Some(span) = self.span.clone() { |
| 59 | return visitor.visit_map(super::SpannedDeserializer::new(self.key.get(), span)); |
| 60 | } |
| 61 | } |
| 62 | self.deserialize_any(visitor) |
| 63 | } |
| 64 | |
| 65 | fn deserialize_newtype_struct<V>( |
| 66 | self, |
| 67 | _name: &'static str, |
| 68 | visitor: V, |
| 69 | ) -> Result<V::Value, Error> |
| 70 | where |
| 71 | V: serde::de::Visitor<'de>, |
| 72 | { |
| 73 | visitor.visit_newtype_struct(self) |
| 74 | } |
| 75 | |
| 76 | serde::forward_to_deserialize_any! { |
| 77 | bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 78 | bytes byte_buf map option unit |
| 79 | ignored_any unit_struct tuple_struct tuple identifier |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl<'de> serde::de::EnumAccess<'de> for KeyDeserializer { |
| 84 | type Error = Error; |
| 85 | type Variant = UnitOnly<Self::Error>; |
| 86 | |
| 87 | fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> |
| 88 | where |
| 89 | T: serde::de::DeserializeSeed<'de>, |
| 90 | { |
| 91 | seed.deserialize(self).map(op:unit_only) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | pub(crate) struct UnitOnly<E> { |
| 96 | marker: std::marker::PhantomData<E>, |
| 97 | } |
| 98 | |
| 99 | fn unit_only<T, E>(t: T) -> (T, UnitOnly<E>) { |
| 100 | ( |
| 101 | t, |
| 102 | UnitOnly { |
| 103 | marker: std::marker::PhantomData, |
| 104 | }, |
| 105 | ) |
| 106 | } |
| 107 | |
| 108 | impl<'de, E> serde::de::VariantAccess<'de> for UnitOnly<E> |
| 109 | where |
| 110 | E: serde::de::Error, |
| 111 | { |
| 112 | type Error = E; |
| 113 | |
| 114 | fn unit_variant(self) -> Result<(), Self::Error> { |
| 115 | Ok(()) |
| 116 | } |
| 117 | |
| 118 | fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error> |
| 119 | where |
| 120 | T: serde::de::DeserializeSeed<'de>, |
| 121 | { |
| 122 | Err(serde::de::Error::invalid_type( |
| 123 | serde::de::Unexpected::UnitVariant, |
| 124 | &"newtype variant" , |
| 125 | )) |
| 126 | } |
| 127 | |
| 128 | fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error> |
| 129 | where |
| 130 | V: serde::de::Visitor<'de>, |
| 131 | { |
| 132 | Err(serde::de::Error::invalid_type( |
| 133 | serde::de::Unexpected::UnitVariant, |
| 134 | &"tuple variant" , |
| 135 | )) |
| 136 | } |
| 137 | |
| 138 | fn struct_variant<V>( |
| 139 | self, |
| 140 | _fields: &'static [&'static str], |
| 141 | _visitor: V, |
| 142 | ) -> Result<V::Value, Self::Error> |
| 143 | where |
| 144 | V: serde::de::Visitor<'de>, |
| 145 | { |
| 146 | Err(serde::de::Error::invalid_type( |
| 147 | serde::de::Unexpected::UnitVariant, |
| 148 | &"struct variant" , |
| 149 | )) |
| 150 | } |
| 151 | } |
| 152 | |