1use crate::serde_ext::de::IntoDeserializer;
2use crate::{stry, Deserializer, Error, ErrorType, Node, Result, StaticNode};
3use serde_ext::de::{self, DeserializeSeed, MapAccess, SeqAccess, Visitor};
4use serde_ext::forward_to_deserialize_any;
5use std::str;
6
7impl<'a, 'de> de::Deserializer<'de> for &'a mut Deserializer<'de>
8where
9 'de: 'a,
10{
11 type Error = Error;
12
13 #[cfg_attr(not(feature = "no-inline"), inline)]
17 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
18 where
19 V: Visitor<'de>,
20 {
21 match stry!(self.next()) {
22 Node::String(s) => visitor.visit_borrowed_str(s),
23 Node::Static(StaticNode::Null) => visitor.visit_unit(),
24 Node::Static(StaticNode::Bool(b)) => visitor.visit_bool(b),
25 #[allow(clippy::useless_conversion)] Node::Static(StaticNode::F64(n)) => visitor.visit_f64(n.into()),
27 Node::Static(StaticNode::I64(n)) => visitor.visit_i64(n),
28 #[cfg(feature = "128bit")]
29 Node::Static(StaticNode::I128(n)) => visitor.visit_i128(n),
30 Node::Static(StaticNode::U64(n)) => visitor.visit_u64(n),
31 #[cfg(feature = "128bit")]
32 Node::Static(StaticNode::U128(n)) => visitor.visit_u128(n),
33 Node::Array { len, count: _ } => visitor.visit_seq(CommaSeparated::new(self, len)),
34 Node::Object { len, count: _ } => visitor.visit_map(CommaSeparated::new(self, len)),
35 }
36 }
37
38 #[cfg_attr(not(feature = "no-inline"), inline)]
53 fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value>
54 where
55 V: Visitor<'de>,
56 {
57 match stry!(self.next()) {
58 Node::Static(StaticNode::Bool(b)) => visitor.visit_bool(b),
59 _c => Err(Deserializer::error(ErrorType::ExpectedBoolean)),
60 }
61 }
62
63 #[cfg_attr(not(feature = "no-inline"), inline)]
66 fn deserialize_str<V>(self, visitor: V) -> Result<V::Value>
67 where
68 V: Visitor<'de>,
69 {
70 if let Ok(Node::String(s)) = self.next() {
71 visitor.visit_borrowed_str(s)
72 } else {
73 Err(Deserializer::error(ErrorType::ExpectedString))
74 }
75 }
76
77 #[cfg_attr(not(feature = "no-inline"), inline)]
78 fn deserialize_string<V>(self, visitor: V) -> Result<V::Value>
79 where
80 V: Visitor<'de>,
81 {
82 if let Ok(Node::String(s)) = self.next() {
83 visitor.visit_str(s)
84 } else {
85 Err(Deserializer::error(ErrorType::ExpectedString))
86 }
87 }
88
89 #[cfg_attr(not(feature = "no-inline"), inline)]
92 #[allow(clippy::cast_possible_truncation)]
93 fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value>
94 where
95 V: Visitor<'de>,
96 {
97 visitor.visit_i8(stry!(self.parse_i8()))
98 }
99
100 #[cfg_attr(not(feature = "no-inline"), inline)]
101 #[allow(clippy::cast_possible_truncation)]
102 fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value>
103 where
104 V: Visitor<'de>,
105 {
106 visitor.visit_i16(stry!(self.parse_i16()))
107 }
108
109 #[cfg_attr(not(feature = "no-inline"), inline)]
110 #[allow(clippy::cast_possible_truncation)]
111 fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value>
112 where
113 V: Visitor<'de>,
114 {
115 visitor.visit_i32(stry!(self.parse_i32()))
116 }
117
118 #[cfg_attr(not(feature = "no-inline"), inline)]
119 fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value>
120 where
121 V: Visitor<'de>,
122 {
123 visitor.visit_i64(stry!(self.parse_i64()))
124 }
125
126 #[cfg_attr(not(feature = "no-inline"), inline)]
127 fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value>
128 where
129 V: Visitor<'de>,
130 {
131 visitor.visit_i128(stry!(self.parse_i128()))
132 }
133
134 #[cfg_attr(not(feature = "no-inline"), inline)]
135 #[allow(clippy::cast_possible_truncation)]
136 fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
137 where
138 V: Visitor<'de>,
139 {
140 visitor.visit_u8(stry!(self.parse_u8()))
141 }
142
143 #[cfg_attr(not(feature = "no-inline"), inline)]
144 #[allow(clippy::cast_possible_truncation)]
145 fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value>
146 where
147 V: Visitor<'de>,
148 {
149 visitor.visit_u16(stry!(self.parse_u16()))
150 }
151
152 #[cfg_attr(not(feature = "no-inline"), inline)]
153 #[allow(clippy::cast_possible_truncation)]
154 fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value>
155 where
156 V: Visitor<'de>,
157 {
158 visitor.visit_u32(stry!(self.parse_u32()))
159 }
160
161 #[cfg_attr(not(feature = "no-inline"), inline)]
162 fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value>
163 where
164 V: Visitor<'de>,
165 {
166 visitor.visit_u64(stry!(self.parse_u64()))
167 }
168
169 #[cfg_attr(not(feature = "no-inline"), inline)]
170 fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value>
171 where
172 V: Visitor<'de>,
173 {
174 visitor.visit_u128(stry!(self.parse_u128()))
175 }
176
177 #[cfg_attr(not(feature = "no-inline"), inline)]
178 #[allow(clippy::cast_possible_truncation)]
179 fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value>
180 where
181 V: Visitor<'de>,
182 {
183 let v: f64 = stry!(self.parse_double());
184 visitor.visit_f32(v as f32)
185 }
186
187 #[cfg_attr(not(feature = "no-inline"), inline)]
188 fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value>
189 where
190 V: Visitor<'de>,
191 {
192 visitor.visit_f64(stry!(self.parse_double()))
193 }
194
195 #[cfg_attr(not(feature = "no-inline"), inline)]
205 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
206 where
207 V: Visitor<'de>,
208 {
209 if stry!(self.peek()) == Node::Static(StaticNode::Null) {
210 self.skip();
211 visitor.visit_unit()
212 } else {
213 visitor.visit_some(self)
214 }
215 }
216
217 #[cfg_attr(not(feature = "no-inline"), inline)]
219 fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value>
220 where
221 V: Visitor<'de>,
222 {
223 if stry!(self.next()) != Node::Static(StaticNode::Null) {
224 return Err(Deserializer::error(ErrorType::ExpectedNull));
225 }
226 visitor.visit_unit()
227 }
228
229 #[cfg_attr(not(feature = "no-inline"), inline)]
233 fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value>
234 where
235 V: Visitor<'de>,
236 {
237 if let Ok(Node::Array { len, count: _ }) = self.next() {
239 visitor.visit_seq(CommaSeparated::new(self, len))
241 } else {
242 Err(Deserializer::error(ErrorType::ExpectedArray))
243 }
244 }
245
246 #[cfg_attr(not(feature = "no-inline"), inline)]
254 fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value>
255 where
256 V: Visitor<'de>,
257 {
258 self.deserialize_seq(visitor)
259 }
263
264 fn deserialize_tuple_struct<V>(
266 self,
267 _name: &'static str,
268 _len: usize,
269 visitor: V,
270 ) -> Result<V::Value>
271 where
272 V: Visitor<'de>,
273 {
274 self.deserialize_seq(visitor)
275 }
276
277 fn deserialize_unit_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
279 where
280 V: Visitor<'de>,
281 {
282 self.deserialize_unit(visitor)
283 }
284
285 fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
289 where
290 V: Visitor<'de>,
291 {
292 visitor.visit_newtype_struct(self)
293 }
294
295 #[cfg_attr(not(feature = "no-inline"), inline)]
296 fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
297 where
298 V: Visitor<'de>,
299 {
300 if let Ok(Node::Object { len, count: _ }) = self.next() {
302 visitor.visit_map(CommaSeparated::new(self, len))
304 } else {
305 Err(Deserializer::error(ErrorType::ExpectedMap))
306 }
307 }
308
309 #[cfg_attr(not(feature = "no-inline"), inline)]
310 fn deserialize_struct<V>(
311 self,
312 _name: &'static str,
313 _fields: &'static [&'static str],
314 visitor: V,
315 ) -> Result<V::Value>
316 where
317 V: Visitor<'de>,
318 {
319 match self.next() {
320 Ok(Node::Object { len, count: _ }) => visitor.visit_map(CommaSeparated::new(self, len)),
322 Ok(Node::Array { len, count: _ }) => visitor.visit_seq(CommaSeparated::new(self, len)),
323 _ => Err(Deserializer::error(ErrorType::ExpectedMap)),
324 }
325 }
326
327 #[cfg_attr(not(feature = "no-inline"), inline)]
328 fn deserialize_enum<V>(
329 self,
330 _name: &'static str,
331 _variants: &'static [&'static str],
332 visitor: V,
333 ) -> Result<V::Value>
334 where
335 V: Visitor<'de>,
336 {
337 match self.next() {
339 Ok(Node::Object { len: 1, .. }) => {
340 visitor.visit_enum(VariantAccess::new(self))
343 }
344 Ok(Node::String(s)) => visitor.visit_enum(s.into_deserializer()),
345 _ => Err(Deserializer::error(ErrorType::ExpectedEnum)),
346 }
347 }
348
349 forward_to_deserialize_any! {
350 char
351 bytes byte_buf
352 identifier ignored_any
353 }
354}
355
356struct VariantAccess<'a, 'de> {
358 de: &'a mut Deserializer<'de>,
359}
360
361impl<'a, 'de> VariantAccess<'a, 'de> {
362 fn new(de: &'a mut Deserializer<'de>) -> Self {
363 VariantAccess { de }
364 }
365}
366
367impl<'de, 'a> de::EnumAccess<'de> for VariantAccess<'a, 'de> {
368 type Error = Error;
369 type Variant = Self;
370
371 fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self)>
372 where
373 V: de::DeserializeSeed<'de>,
374 {
375 let val = stry!(seed.deserialize(&mut *self.de));
376 Ok((val, self))
377 }
378}
379
380impl<'de, 'a> de::VariantAccess<'de> for VariantAccess<'a, 'de> {
381 type Error = Error;
382
383 fn unit_variant(self) -> Result<()> {
384 de::Deserialize::deserialize(self.de)
385 }
386
387 fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
388 where
389 T: de::DeserializeSeed<'de>,
390 {
391 seed.deserialize(self.de)
392 }
393
394 fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value>
395 where
396 V: de::Visitor<'de>,
397 {
398 de::Deserializer::deserialize_seq(self.de, visitor)
399 }
400
401 fn struct_variant<V>(self, fields: &'static [&'static str], visitor: V) -> Result<V::Value>
402 where
403 V: de::Visitor<'de>,
404 {
405 de::Deserializer::deserialize_struct(self.de, "", fields, visitor)
406 }
407}
408
409struct CommaSeparated<'a, 'de: 'a> {
413 de: &'a mut Deserializer<'de>,
414 len: usize,
415}
416impl<'a, 'de> CommaSeparated<'a, 'de> {
417 #[cfg_attr(not(feature = "no-inline"), inline)]
418 fn new(de: &'a mut Deserializer<'de>, len: usize) -> Self {
419 CommaSeparated { de, len }
420 }
421}
422
423impl<'de, 'a> SeqAccess<'de> for CommaSeparated<'a, 'de> {
426 type Error = Error;
427
428 #[cfg_attr(not(feature = "no-inline"), inline)]
429 fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
430 where
431 T: DeserializeSeed<'de>,
432 {
433 if self.len == 0 {
434 Ok(None)
435 } else {
436 self.len -= 1;
437 seed.deserialize(&mut *self.de).map(Some)
438 }
439 }
440 #[cfg_attr(not(feature = "no-inline"), inline)]
441 fn size_hint(&self) -> Option<usize> {
442 Some(self.len)
443 }
444}
445
446impl<'de, 'a> MapAccess<'de> for CommaSeparated<'a, 'de> {
449 type Error = Error;
450
451 #[cfg_attr(not(feature = "no-inline"), inline)]
452 fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
453 where
454 K: DeserializeSeed<'de>,
455 {
456 if self.len == 0 {
457 Ok(None)
458 } else {
459 self.len -= 1;
460 seed.deserialize(MapKey { de: &mut *self.de }).map(Some)
461 }
462 }
463
464 #[cfg_attr(not(feature = "no-inline"), inline)]
465 fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
466 where
467 V: DeserializeSeed<'de>,
468 {
469 seed.deserialize(&mut *self.de)
471 }
472
473 #[cfg_attr(not(feature = "no-inline"), inline)]
474 fn size_hint(&self) -> Option<usize> {
475 Some(self.len)
476 }
477}
478
479struct MapKey<'de: 'a, 'a> {
482 de: &'a mut Deserializer<'de>,
483}
484
485macro_rules! deserialize_integer_key {
486 ($method:ident => $visit:ident; $type:ty) => {
487 fn $method<V>(self, visitor: V) -> Result<V::Value>
488 where
489 V: de::Visitor<'de>,
490 {
491 visitor.$visit(stry!(match unsafe { self.de.next_() } {
492 Node::String(s) => s
493 .parse::<$type>()
494 .map_err(|_| Deserializer::error(ErrorType::InvalidNumber)),
495 _ => Err(Deserializer::error(ErrorType::ExpectedString)),
496 }))
497 }
498 };
499}
500
501impl<'de, 'a> de::Deserializer<'de> for MapKey<'de, 'a> {
502 type Error = Error;
503
504 #[cfg_attr(not(feature = "no-inline"), inline)]
505 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
506 where
507 V: de::Visitor<'de>,
508 {
509 match stry!(self.de.next()) {
510 Node::String(s) => visitor.visit_borrowed_str(s),
511 _ => Err(Deserializer::error(ErrorType::ExpectedString)),
512 }
513 }
514
515 deserialize_integer_key!(deserialize_i8 => visit_i8; i8);
516 deserialize_integer_key!(deserialize_i16 => visit_i16; i16);
517 deserialize_integer_key!(deserialize_i32 => visit_i32; i32);
518 deserialize_integer_key!(deserialize_i64 => visit_i64; i64);
519 deserialize_integer_key!(deserialize_u8 => visit_u8; u8);
520 deserialize_integer_key!(deserialize_u16 => visit_u16; u16);
521 deserialize_integer_key!(deserialize_u32 => visit_u32; u32);
522 deserialize_integer_key!(deserialize_u64 => visit_u64; u64);
523
524 #[cfg(feature = "128bit")]
525 deserialize_integer_key!(deserialize_i128 => visit_i128; i128);
526 #[cfg(feature = "128bit")]
527 deserialize_integer_key!(deserialize_u128 => visit_u128; u128);
528
529 #[cfg_attr(not(feature = "no-inline"), inline)]
530 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
531 where
532 V: de::Visitor<'de>,
533 {
534 visitor.visit_some(self)
536 }
537
538 #[cfg_attr(not(feature = "no-inline"), inline)]
539 fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
540 where
541 V: de::Visitor<'de>,
542 {
543 visitor.visit_newtype_struct(self)
544 }
545
546 #[cfg_attr(not(feature = "no-inline"), inline)]
547 fn deserialize_enum<V>(
548 self,
549 name: &'static str,
550 variants: &'static [&'static str],
551 visitor: V,
552 ) -> Result<V::Value>
553 where
554 V: de::Visitor<'de>,
555 {
556 self.de.deserialize_enum(name, variants, visitor)
557 }
558
559 #[cfg_attr(not(feature = "no-inline"), inline)]
560 fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value>
561 where
562 V: de::Visitor<'de>,
563 {
564 self.de.deserialize_bytes(visitor)
565 }
566
567 #[cfg_attr(not(feature = "no-inline"), inline)]
568 fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value>
569 where
570 V: de::Visitor<'de>,
571 {
572 self.de.deserialize_bytes(visitor)
573 }
574
575 forward_to_deserialize_any! {
576 bool f32 f64 char str string unit unit_struct seq tuple tuple_struct map
577 struct identifier ignored_any
578 }
579}
580
581#[cfg(test)]
582mod tests;