| 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::InternalString, |
| 8 | } |
| 9 | |
| 10 | impl KeyDeserializer { |
| 11 | pub(crate) fn new(key: crate::InternalString, span: Option<std::ops::Range<usize>>) -> Self { |
| 12 | KeyDeserializer { span, key } |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | impl<'de> serde::de::IntoDeserializer<'de, 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.as_str(), span)); |
| 60 | } |
| 61 | } |
| 62 | self.deserialize_any(visitor) |
| 63 | } |
| 64 | |
| 65 | serde::forward_to_deserialize_any! { |
| 66 | bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
| 67 | bytes byte_buf map option unit newtype_struct |
| 68 | ignored_any unit_struct tuple_struct tuple identifier |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl<'de> serde::de::EnumAccess<'de> for KeyDeserializer { |
| 73 | type Error = super::Error; |
| 74 | type Variant = UnitOnly<Self::Error>; |
| 75 | |
| 76 | fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> |
| 77 | where |
| 78 | T: serde::de::DeserializeSeed<'de>, |
| 79 | { |
| 80 | seed.deserialize(self).map(op:unit_only) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | pub(crate) struct UnitOnly<E> { |
| 85 | marker: std::marker::PhantomData<E>, |
| 86 | } |
| 87 | |
| 88 | fn unit_only<T, E>(t: T) -> (T, UnitOnly<E>) { |
| 89 | ( |
| 90 | t, |
| 91 | UnitOnly { |
| 92 | marker: std::marker::PhantomData, |
| 93 | }, |
| 94 | ) |
| 95 | } |
| 96 | |
| 97 | impl<'de, E> serde::de::VariantAccess<'de> for UnitOnly<E> |
| 98 | where |
| 99 | E: serde::de::Error, |
| 100 | { |
| 101 | type Error = E; |
| 102 | |
| 103 | fn unit_variant(self) -> Result<(), Self::Error> { |
| 104 | Ok(()) |
| 105 | } |
| 106 | |
| 107 | fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error> |
| 108 | where |
| 109 | T: serde::de::DeserializeSeed<'de>, |
| 110 | { |
| 111 | Err(serde::de::Error::invalid_type( |
| 112 | serde::de::Unexpected::UnitVariant, |
| 113 | &"newtype variant" , |
| 114 | )) |
| 115 | } |
| 116 | |
| 117 | fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error> |
| 118 | where |
| 119 | V: serde::de::Visitor<'de>, |
| 120 | { |
| 121 | Err(serde::de::Error::invalid_type( |
| 122 | serde::de::Unexpected::UnitVariant, |
| 123 | &"tuple variant" , |
| 124 | )) |
| 125 | } |
| 126 | |
| 127 | fn struct_variant<V>( |
| 128 | self, |
| 129 | _fields: &'static [&'static str], |
| 130 | _visitor: V, |
| 131 | ) -> Result<V::Value, Self::Error> |
| 132 | where |
| 133 | V: serde::de::Visitor<'de>, |
| 134 | { |
| 135 | Err(serde::de::Error::invalid_type( |
| 136 | serde::de::Unexpected::UnitVariant, |
| 137 | &"struct variant" , |
| 138 | )) |
| 139 | } |
| 140 | } |
| 141 | |