1 | use serde::de::IntoDeserializer; |
2 | |
3 | use crate::de::Error; |
4 | |
5 | pub(crate) struct TableDeserializer { |
6 | span: Option<std::ops::Range<usize>>, |
7 | items: crate::table::KeyValuePairs, |
8 | } |
9 | |
10 | // Note: this is wrapped by `Deserializer` and `ValueDeserializer` and any trait methods |
11 | // implemented here need to be wrapped there |
12 | impl<'de> serde::Deserializer<'de> for TableDeserializer { |
13 | type Error = Error; |
14 | |
15 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
16 | where |
17 | V: serde::de::Visitor<'de>, |
18 | { |
19 | visitor.visit_map(crate::de::TableMapAccess::new(self)) |
20 | } |
21 | |
22 | // `None` is interpreted as a missing field so be sure to implement `Some` |
23 | // as a present field. |
24 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Error> |
25 | where |
26 | V: serde::de::Visitor<'de>, |
27 | { |
28 | visitor.visit_some(self) |
29 | } |
30 | |
31 | fn deserialize_newtype_struct<V>( |
32 | self, |
33 | _name: &'static str, |
34 | visitor: V, |
35 | ) -> Result<V::Value, Error> |
36 | where |
37 | V: serde::de::Visitor<'de>, |
38 | { |
39 | visitor.visit_newtype_struct(self) |
40 | } |
41 | |
42 | fn deserialize_struct<V>( |
43 | self, |
44 | name: &'static str, |
45 | fields: &'static [&'static str], |
46 | visitor: V, |
47 | ) -> Result<V::Value, Error> |
48 | where |
49 | V: serde::de::Visitor<'de>, |
50 | { |
51 | if serde_spanned::__unstable::is_spanned(name, fields) { |
52 | if let Some(span) = self.span.clone() { |
53 | return visitor.visit_map(super::SpannedDeserializer::new(self, span)); |
54 | } |
55 | } |
56 | |
57 | self.deserialize_any(visitor) |
58 | } |
59 | |
60 | // Called when the type to deserialize is an enum, as opposed to a field in the type. |
61 | fn deserialize_enum<V>( |
62 | self, |
63 | _name: &'static str, |
64 | _variants: &'static [&'static str], |
65 | visitor: V, |
66 | ) -> Result<V::Value, Error> |
67 | where |
68 | V: serde::de::Visitor<'de>, |
69 | { |
70 | if self.items.is_empty() { |
71 | Err(crate::de::Error::custom( |
72 | "wanted exactly 1 element, found 0 elements" , |
73 | self.span, |
74 | )) |
75 | } else if self.items.len() != 1 { |
76 | Err(crate::de::Error::custom( |
77 | "wanted exactly 1 element, more than 1 element" , |
78 | self.span, |
79 | )) |
80 | } else { |
81 | visitor.visit_enum(crate::de::TableMapAccess::new(self)) |
82 | } |
83 | } |
84 | |
85 | serde::forward_to_deserialize_any! { |
86 | bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
87 | bytes byte_buf map unit |
88 | ignored_any unit_struct tuple_struct tuple identifier |
89 | } |
90 | } |
91 | |
92 | impl<'de> serde::de::IntoDeserializer<'de, crate::de::Error> for TableDeserializer { |
93 | type Deserializer = TableDeserializer; |
94 | |
95 | fn into_deserializer(self) -> Self::Deserializer { |
96 | self |
97 | } |
98 | } |
99 | |
100 | impl crate::Table { |
101 | pub(crate) fn into_deserializer(self) -> TableDeserializer { |
102 | TableDeserializer { |
103 | span: self.span(), |
104 | items: self.items, |
105 | } |
106 | } |
107 | } |
108 | |
109 | impl crate::InlineTable { |
110 | pub(crate) fn into_deserializer(self) -> TableDeserializer { |
111 | TableDeserializer { |
112 | span: self.span(), |
113 | items: self.items, |
114 | } |
115 | } |
116 | } |
117 | |
118 | pub(crate) struct TableMapAccess { |
119 | iter: indexmap::map::IntoIter<crate::InternalString, crate::table::TableKeyValue>, |
120 | span: Option<std::ops::Range<usize>>, |
121 | value: Option<(crate::Key, crate::Item)>, |
122 | } |
123 | |
124 | impl TableMapAccess { |
125 | pub(crate) fn new(input: TableDeserializer) -> Self { |
126 | Self { |
127 | iter: input.items.into_iter(), |
128 | span: input.span, |
129 | value: None, |
130 | } |
131 | } |
132 | } |
133 | |
134 | impl<'de> serde::de::MapAccess<'de> for TableMapAccess { |
135 | type Error = Error; |
136 | |
137 | fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error> |
138 | where |
139 | K: serde::de::DeserializeSeed<'de>, |
140 | { |
141 | match self.iter.next() { |
142 | Some((k, v)) => { |
143 | let ret = seed |
144 | .deserialize(super::KeyDeserializer::new(k, v.key.span())) |
145 | .map(Some) |
146 | .map_err(|mut e: Self::Error| { |
147 | if e.span().is_none() { |
148 | e.set_span(v.key.span()); |
149 | } |
150 | e |
151 | }); |
152 | self.value = Some((v.key, v.value)); |
153 | ret |
154 | } |
155 | None => Ok(None), |
156 | } |
157 | } |
158 | |
159 | fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error> |
160 | where |
161 | V: serde::de::DeserializeSeed<'de>, |
162 | { |
163 | match self.value.take() { |
164 | Some((k, v)) => { |
165 | let span = v.span().or_else(|| k.span()); |
166 | seed.deserialize(crate::de::ValueDeserializer::new(v)) |
167 | .map_err(|mut e: Self::Error| { |
168 | if e.span().is_none() { |
169 | e.set_span(span); |
170 | } |
171 | e.add_key(k.get().to_owned()); |
172 | e |
173 | }) |
174 | } |
175 | None => { |
176 | panic!("no more values in next_value_seed, internal error in ValueDeserializer" ) |
177 | } |
178 | } |
179 | } |
180 | } |
181 | |
182 | impl<'de> serde::de::EnumAccess<'de> for TableMapAccess { |
183 | type Error = Error; |
184 | type Variant = super::TableEnumDeserializer; |
185 | |
186 | fn variant_seed<V>(mut self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> |
187 | where |
188 | V: serde::de::DeserializeSeed<'de>, |
189 | { |
190 | let (key, value) = match self.iter.next() { |
191 | Some(pair) => pair, |
192 | None => { |
193 | return Err(Error::custom( |
194 | "expected table with exactly 1 entry, found empty table" , |
195 | self.span, |
196 | )); |
197 | } |
198 | }; |
199 | |
200 | let val = seed |
201 | .deserialize(key.into_deserializer()) |
202 | .map_err(|mut e: Self::Error| { |
203 | if e.span().is_none() { |
204 | e.set_span(value.key.span()); |
205 | } |
206 | e |
207 | })?; |
208 | |
209 | let variant = super::TableEnumDeserializer::new(value.value); |
210 | |
211 | Ok((val, variant)) |
212 | } |
213 | } |
214 | |