|
1 | 1 | use crate::{ |
| 2 | + de::key::QNameDeserializer, |
2 | 3 | de::simple_type::SimpleTypeDeserializer, |
3 | | - de::{deserialize_bool, DeEvent, Deserializer, XmlRead, TEXT_KEY}, |
4 | | - encoding::Decoder, |
| 4 | + de::{DeEvent, Deserializer, XmlRead, TEXT_KEY}, |
5 | 5 | errors::serialize::DeError, |
6 | | - escape::unescape, |
7 | 6 | }; |
8 | 7 | use serde::de::value::StrDeserializer; |
9 | 8 | use serde::de::{self, DeserializeSeed, Deserializer as _, Visitor}; |
10 | | -use serde::{forward_to_deserialize_any, serde_if_integer128}; |
11 | | -use std::borrow::Cow; |
12 | 9 |
|
13 | 10 | /// An enum access |
14 | 11 | pub struct EnumAccess<'de, 'a, R> |
|
41 | 38 | let decoder = self.de.reader.decoder(); |
42 | 39 | let (name, is_text) = match self.de.peek()? { |
43 | 40 | DeEvent::Start(e) => ( |
44 | | - seed.deserialize(VariantDeserializer::new( |
45 | | - e.local_name().into_inner(), |
46 | | - decoder, |
47 | | - false, |
48 | | - ))?, |
| 41 | + seed.deserialize(QNameDeserializer::from_elem(e.name(), decoder)?)?, |
49 | 42 | false, |
50 | 43 | ), |
51 | 44 | DeEvent::Text(_) | DeEvent::CData(_) => ( |
@@ -153,221 +146,3 @@ where |
153 | 146 | } |
154 | 147 | } |
155 | 148 | } |
156 | | - |
157 | | -//////////////////////////////////////////////////////////////////////////////////////////////////// |
158 | | - |
159 | | -/// A deserializer for a xml escaped and encoded value |
160 | | -/// |
161 | | -/// # Note |
162 | | -/// |
163 | | -/// Escaping the value is actually not always necessary, for instance |
164 | | -/// when converting to float, we don't expect any escapable character |
165 | | -/// anyway |
166 | | -#[derive(Clone, Debug)] |
167 | | -struct VariantDeserializer<'a> { |
168 | | - /// Possible escaped value of text/CDATA or tag name value |
169 | | - escaped_value: &'a [u8], |
170 | | - /// If `true`, value requires unescaping before using |
171 | | - escaped: bool, |
172 | | - decoder: Decoder, |
173 | | -} |
174 | | - |
175 | | -impl<'a> VariantDeserializer<'a> { |
176 | | - pub fn new(escaped_value: &'a [u8], decoder: Decoder, escaped: bool) -> Self { |
177 | | - Self { |
178 | | - decoder, |
179 | | - escaped_value, |
180 | | - escaped, |
181 | | - } |
182 | | - } |
183 | | -} |
184 | | - |
185 | | -macro_rules! deserialize_num { |
186 | | - ($method:ident, $visit:ident) => { |
187 | | - fn $method<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
188 | | - where |
189 | | - V: Visitor<'de>, |
190 | | - { |
191 | | - let value = self.decoder.decode(self.escaped_value)?.parse()?; |
192 | | - |
193 | | - visitor.$visit(value) |
194 | | - } |
195 | | - }; |
196 | | -} |
197 | | - |
198 | | -impl<'de, 'a> de::Deserializer<'de> for VariantDeserializer<'a> { |
199 | | - type Error = DeError; |
200 | | - |
201 | | - deserialize_num!(deserialize_i8, visit_i8); |
202 | | - deserialize_num!(deserialize_i16, visit_i16); |
203 | | - deserialize_num!(deserialize_i32, visit_i32); |
204 | | - deserialize_num!(deserialize_i64, visit_i64); |
205 | | - |
206 | | - deserialize_num!(deserialize_u8, visit_u8); |
207 | | - deserialize_num!(deserialize_u16, visit_u16); |
208 | | - deserialize_num!(deserialize_u32, visit_u32); |
209 | | - deserialize_num!(deserialize_u64, visit_u64); |
210 | | - |
211 | | - deserialize_num!(deserialize_f32, visit_f32); |
212 | | - deserialize_num!(deserialize_f64, visit_f64); |
213 | | - |
214 | | - serde_if_integer128! { |
215 | | - deserialize_num!(deserialize_i128, visit_i128); |
216 | | - deserialize_num!(deserialize_u128, visit_u128); |
217 | | - } |
218 | | - |
219 | | - fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
220 | | - where |
221 | | - V: Visitor<'de>, |
222 | | - { |
223 | | - self.deserialize_str(visitor) |
224 | | - } |
225 | | - |
226 | | - fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
227 | | - where |
228 | | - V: Visitor<'de>, |
229 | | - { |
230 | | - let decoded = self.decoder.decode(self.escaped_value)?; |
231 | | - if self.escaped { |
232 | | - match unescape(&decoded)? { |
233 | | - Cow::Borrowed(s) => visitor.visit_str(s), |
234 | | - Cow::Owned(s) => visitor.visit_string(s), |
235 | | - } |
236 | | - } else { |
237 | | - match decoded { |
238 | | - Cow::Borrowed(s) => visitor.visit_str(s), |
239 | | - Cow::Owned(s) => visitor.visit_string(s), |
240 | | - } |
241 | | - } |
242 | | - } |
243 | | - |
244 | | - /// Returns [`DeError::Unsupported`] |
245 | | - fn deserialize_bytes<V>(self, _visitor: V) -> Result<V::Value, Self::Error> |
246 | | - where |
247 | | - V: Visitor<'de>, |
248 | | - { |
249 | | - Err(DeError::Unsupported( |
250 | | - "binary data content is not supported by XML format".into(), |
251 | | - )) |
252 | | - } |
253 | | - |
254 | | - /// Forwards deserialization to the [`deserialize_bytes`](#method.deserialize_bytes) |
255 | | - fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
256 | | - where |
257 | | - V: Visitor<'de>, |
258 | | - { |
259 | | - self.deserialize_bytes(visitor) |
260 | | - } |
261 | | - |
262 | | - fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
263 | | - where |
264 | | - V: Visitor<'de>, |
265 | | - { |
266 | | - self.deserialize_str(visitor) |
267 | | - } |
268 | | - |
269 | | - fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
270 | | - where |
271 | | - V: Visitor<'de>, |
272 | | - { |
273 | | - deserialize_bool(self.escaped_value, self.decoder, visitor) |
274 | | - } |
275 | | - |
276 | | - fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
277 | | - where |
278 | | - V: Visitor<'de>, |
279 | | - { |
280 | | - self.deserialize_str(visitor) |
281 | | - } |
282 | | - |
283 | | - fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
284 | | - where |
285 | | - V: Visitor<'de>, |
286 | | - { |
287 | | - visitor.visit_unit() |
288 | | - } |
289 | | - |
290 | | - fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
291 | | - where |
292 | | - V: Visitor<'de>, |
293 | | - { |
294 | | - if self.escaped_value.is_empty() { |
295 | | - visitor.visit_none() |
296 | | - } else { |
297 | | - visitor.visit_some(self) |
298 | | - } |
299 | | - } |
300 | | - |
301 | | - fn deserialize_enum<V>( |
302 | | - self, |
303 | | - _name: &str, |
304 | | - _variants: &'static [&'static str], |
305 | | - visitor: V, |
306 | | - ) -> Result<V::Value, Self::Error> |
307 | | - where |
308 | | - V: Visitor<'de>, |
309 | | - { |
310 | | - visitor.visit_enum(self) |
311 | | - } |
312 | | - |
313 | | - fn deserialize_newtype_struct<V>( |
314 | | - self, |
315 | | - _name: &'static str, |
316 | | - visitor: V, |
317 | | - ) -> Result<V::Value, Self::Error> |
318 | | - where |
319 | | - V: Visitor<'de>, |
320 | | - { |
321 | | - visitor.visit_newtype_struct(self) |
322 | | - } |
323 | | - |
324 | | - forward_to_deserialize_any! { |
325 | | - unit_struct seq tuple tuple_struct map struct identifier ignored_any |
326 | | - } |
327 | | -} |
328 | | - |
329 | | -impl<'de, 'a> de::EnumAccess<'de> for VariantDeserializer<'a> { |
330 | | - type Error = DeError; |
331 | | - type Variant = Self; |
332 | | - |
333 | | - fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self), Self::Error> |
334 | | - where |
335 | | - V: DeserializeSeed<'de>, |
336 | | - { |
337 | | - let name = seed.deserialize(self.clone())?; |
338 | | - Ok((name, self)) |
339 | | - } |
340 | | -} |
341 | | - |
342 | | -impl<'de, 'a> de::VariantAccess<'de> for VariantDeserializer<'a> { |
343 | | - type Error = DeError; |
344 | | - |
345 | | - fn unit_variant(self) -> Result<(), Self::Error> { |
346 | | - Ok(()) |
347 | | - } |
348 | | - |
349 | | - fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error> |
350 | | - where |
351 | | - T: DeserializeSeed<'de>, |
352 | | - { |
353 | | - seed.deserialize(self) |
354 | | - } |
355 | | - |
356 | | - fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error> |
357 | | - where |
358 | | - V: Visitor<'de>, |
359 | | - { |
360 | | - unimplemented!() |
361 | | - } |
362 | | - |
363 | | - fn struct_variant<V>( |
364 | | - self, |
365 | | - _fields: &'static [&'static str], |
366 | | - _visitor: V, |
367 | | - ) -> Result<V::Value, Self::Error> |
368 | | - where |
369 | | - V: Visitor<'de>, |
370 | | - { |
371 | | - unimplemented!() |
372 | | - } |
373 | | -} |
0 commit comments