1 | use crate::de::Error; |
2 | |
3 | pub(crate) struct ArrayDeserializer { |
4 | input: Vec<crate::Item>, |
5 | span: Option<std::ops::Range<usize>>, |
6 | } |
7 | |
8 | impl ArrayDeserializer { |
9 | pub(crate) fn new(input: Vec<crate::Item>, span: Option<std::ops::Range<usize>>) -> Self { |
10 | Self { input, span } |
11 | } |
12 | } |
13 | |
14 | // Note: this is wrapped by `ValueDeserializer` and any trait methods |
15 | // implemented here need to be wrapped there |
16 | impl<'de> serde::Deserializer<'de> for ArrayDeserializer { |
17 | type Error = Error; |
18 | |
19 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
20 | where |
21 | V: serde::de::Visitor<'de>, |
22 | { |
23 | visitor.visit_seq(ArraySeqAccess::new(self.input)) |
24 | } |
25 | |
26 | fn deserialize_struct<V>( |
27 | self, |
28 | name: &'static str, |
29 | fields: &'static [&'static str], |
30 | visitor: V, |
31 | ) -> Result<V::Value, Error> |
32 | where |
33 | V: serde::de::Visitor<'de>, |
34 | { |
35 | if serde_spanned::__unstable::is_spanned(name, fields) { |
36 | if let Some(span) = self.span.clone() { |
37 | return visitor.visit_map(super::SpannedDeserializer::new(self, span)); |
38 | } |
39 | } |
40 | |
41 | self.deserialize_any(visitor) |
42 | } |
43 | |
44 | serde::forward_to_deserialize_any! { |
45 | bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq |
46 | bytes byte_buf map option unit newtype_struct |
47 | ignored_any unit_struct tuple_struct tuple enum identifier |
48 | } |
49 | } |
50 | |
51 | impl<'de> serde::de::IntoDeserializer<'de, crate::de::Error> for ArrayDeserializer { |
52 | type Deserializer = Self; |
53 | |
54 | fn into_deserializer(self) -> Self::Deserializer { |
55 | self |
56 | } |
57 | } |
58 | |
59 | impl crate::Array { |
60 | pub(crate) fn into_deserializer(self) -> ArrayDeserializer { |
61 | ArrayDeserializer::new(self.values, self.span) |
62 | } |
63 | } |
64 | |
65 | impl crate::ArrayOfTables { |
66 | pub(crate) fn into_deserializer(self) -> ArrayDeserializer { |
67 | ArrayDeserializer::new(self.values, self.span) |
68 | } |
69 | } |
70 | |
71 | pub(crate) struct ArraySeqAccess { |
72 | iter: std::vec::IntoIter<crate::Item>, |
73 | } |
74 | |
75 | impl ArraySeqAccess { |
76 | pub(crate) fn new(input: Vec<crate::Item>) -> Self { |
77 | Self { |
78 | iter: input.into_iter(), |
79 | } |
80 | } |
81 | } |
82 | |
83 | impl<'de> serde::de::SeqAccess<'de> for ArraySeqAccess { |
84 | type Error = Error; |
85 | |
86 | fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> |
87 | where |
88 | T: serde::de::DeserializeSeed<'de>, |
89 | { |
90 | match self.iter.next() { |
91 | Some(v: Item) => seed |
92 | .deserialize(crate::de::ValueDeserializer::new(v)) |
93 | .map(op:Some), |
94 | None => Ok(None), |
95 | } |
96 | } |
97 | } |
98 | |