taos_query/de/
mod.rs

1use std::marker::PhantomData;
2
3use serde::de::{DeserializeSeed, IntoDeserializer, MapAccess, SeqAccess, Visitor};
4use serde::Deserializer;
5
6use serde::de::value::Error;
7
8use crate::common::BorrowedValue;
9use crate::Field;
10
11/// Row-based deserializer helper.
12///
13/// 'b: field lifetime may go across the whole query.
14pub(crate) struct RecordDeserializer<'b, R>
15where
16    R: IntoIterator<Item = (&'b Field, BorrowedValue<'b>)>,
17{
18    inner: <R as IntoIterator>::IntoIter,
19    value: Option<BorrowedValue<'b>>,
20    _marker: PhantomData<&'b u8>,
21}
22
23impl<'b, R> From<R> for RecordDeserializer<'b, R>
24where
25    R: IntoIterator<Item = (&'b Field, BorrowedValue<'b>)>,
26{
27    fn from(input: R) -> Self {
28        Self {
29            inner: input.into_iter(),
30            value: None,
31            _marker: PhantomData,
32        }
33    }
34}
35
36impl<'b, R> RecordDeserializer<'b, R>
37where
38    R: IntoIterator<Item = (&'b Field, BorrowedValue<'b>)>,
39{
40    fn next_value(&mut self) -> Option<BorrowedValue<'b>> {
41        self.inner.next().map(|(_, v)| v)
42    }
43}
44
45impl<'de, 'b: 'de, R> MapAccess<'de> for RecordDeserializer<'b, R>
46where
47    R: IntoIterator<Item = (&'b Field, BorrowedValue<'b>)>,
48{
49    type Error = Error;
50
51    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Self::Error>
52    where
53        K: DeserializeSeed<'de>,
54    {
55        match self.inner.next() {
56            Some((field, value)) => {
57                self.value = Some(value);
58                let field = field;
59                seed.deserialize(field.name().into_deserializer()).map(Some)
60            }
61            _ => Ok(None),
62        }
63    }
64
65    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Self::Error>
66    where
67        V: DeserializeSeed<'de>,
68    {
69        let value = self.value.take().unwrap(); // always be here, so it's safe to unwrap
70
71        seed.deserialize(value)
72            .map_err(<Self::Error as serde::de::Error>::custom)
73    }
74}
75
76impl<'de, 'b: 'de, R> SeqAccess<'de> for RecordDeserializer<'b, R>
77where
78    R: IntoIterator<Item = (&'b Field, BorrowedValue<'b>)>,
79{
80    type Error = Error;
81
82    fn next_element_seed<S>(&mut self, seed: S) -> Result<Option<S::Value>, Self::Error>
83    where
84        S: DeserializeSeed<'de>,
85    {
86        match self.inner.next() {
87            Some((_, v)) => seed
88                .deserialize(v)
89                .map_err(<Self::Error as serde::de::Error>::custom)
90                .map(Some),
91            None => Ok(None),
92        }
93    }
94}
95
96impl<'de, 'b: 'de, R> Deserializer<'de> for RecordDeserializer<'b, R>
97where
98    R: IntoIterator<Item = (&'b Field, BorrowedValue<'b>)>,
99{
100    type Error = Error;
101
102    // Look at the input data to decide what Serde data model type to
103    // deserialize as. Not all data formats are able to support this operation.
104    // Formats that support `deserialize_any` are known as self-describing.
105    fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
106    where
107        V: Visitor<'de>,
108    {
109        match self.next_value() {
110            Some(v) => v
111                .deserialize_any(visitor)
112                .map_err(<Self::Error as serde::de::Error>::custom),
113            None => Err(<Self::Error as serde::de::Error>::custom(
114                "expect value, not none",
115            )),
116        }
117    }
118
119    serde::forward_to_deserialize_any! {
120        bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char bytes byte_buf enum
121        identifier ignored_any
122    }
123
124    // Refer to the "Understanding deserializer lifetimes" page for information
125    // about the three deserialization flavors of strings in Serde.
126    fn deserialize_str<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
127    where
128        V: Visitor<'de>,
129    {
130        match self.next_value() {
131            Some(v) => v
132                .deserialize_str(visitor)
133                .map_err(<Self::Error as serde::de::Error>::custom),
134            None => Err(<Self::Error as serde::de::Error>::custom(
135                "expect value, not none",
136            )),
137        }
138    }
139
140    fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
141    where
142        V: Visitor<'de>,
143    {
144        self.deserialize_str(visitor)
145    }
146
147    fn deserialize_option<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
148    where
149        V: Visitor<'de>,
150    {
151        match self.next_value() {
152            Some(v) => {
153                if v.is_null() {
154                    visitor.visit_none()
155                } else {
156                    visitor
157                        .visit_some(v)
158                        .map_err(<Self::Error as serde::de::Error>::custom)
159                }
160            }
161            _ => Err(<Self::Error as serde::de::Error>::custom(
162                "expect next value",
163            )),
164        }
165    }
166
167    // In Serde, unit means an anonymous value containing no data.
168    fn deserialize_unit<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
169    where
170        V: Visitor<'de>,
171    {
172        match self.next_value() {
173            Some(_v) => visitor.visit_unit(),
174            _ => Err(<Self::Error as serde::de::Error>::custom(
175                "there's no enough value",
176            )),
177        }
178    }
179
180    // Unit struct means a named value containing no data.
181    fn deserialize_unit_struct<V>(
182        self,
183        _name: &'static str,
184        visitor: V,
185    ) -> Result<V::Value, Self::Error>
186    where
187        V: Visitor<'de>,
188    {
189        self.deserialize_unit(visitor)
190    }
191
192    // As is done here, serializers are encouraged to treat newtype structs as
193    // insignificant wrappers around the data they contain. That means not
194    // parsing anything other than the contained value.
195    fn deserialize_newtype_struct<V>(
196        self,
197        _name: &'static str,
198        visitor: V,
199    ) -> Result<V::Value, Self::Error>
200    where
201        V: Visitor<'de>,
202    {
203        visitor.visit_newtype_struct(self)
204    }
205
206    // Deserialization of compound types like sequences and maps happens by
207    // passing the visitor an "Access" object that gives it the ability to
208    // iterate through the data contained in the sequence.
209    fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
210    where
211        V: Visitor<'de>,
212    {
213        visitor.visit_seq(self)
214    }
215
216    // Tuples look just like sequences.
217    fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
218    where
219        V: Visitor<'de>,
220    {
221        // self.deserialize_any(visitor)
222        visitor.visit_seq(self)
223    }
224
225    // Tuple structs look just like sequences.
226    fn deserialize_tuple_struct<V>(
227        self,
228        _name: &'static str,
229        _len: usize,
230        visitor: V,
231    ) -> Result<V::Value, Self::Error>
232    where
233        V: Visitor<'de>,
234    {
235        self.deserialize_seq(visitor)
236    }
237
238    // Much like `deserialize_seq` but calls the visitors `visit_map` method
239    // with a `MapAccess` implementation, rather than the visitor's `visit_seq`
240    // method with a `SeqAccess` implementation.
241    fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
242    where
243        V: Visitor<'de>,
244    {
245        // let value = visitor.visit_map(self);
246        // unimplemented!();
247        visitor.visit_map(self)
248    }
249
250    // Structs look just like maps in JSON.
251    //
252    // Notice the `fields` parameter - a "struct" in the Serde data model means
253    // that the `Deserialize` implementation is required to know what the fields
254    // are before even looking at the input data. Any key-value pairing in which
255    // the fields cannot be known ahead of time is probably a map.
256    fn deserialize_struct<V>(
257        self,
258        _name: &'static str,
259        _fields: &'static [&'static str],
260        visitor: V,
261    ) -> Result<V::Value, Self::Error>
262    where
263        V: Visitor<'de>,
264    {
265        self.deserialize_map(visitor)
266    }
267}