serde_structuredqs/de/
deserializer.rs

1use crate::{
2    de::{
3        key::KeyDeserializer,
4        level::{Level, LevelDeserializer},
5        parser::Parser,
6    },
7    error::{Error, Result},
8};
9
10use serde::de::{self, Error as _};
11use serde::forward_to_deserialize_any;
12
13use std::borrow::Cow;
14use std::collections::btree_map::{BTreeMap, IntoIter};
15use std::iter::Iterator;
16use std::str;
17
18/// A deserializer for the querystring format.
19///
20/// Supported top-level outputs are structs and maps.
21pub(crate) struct Deserializer<'a> {
22    pub(crate) iter: IntoIter<Cow<'a, str>, Level<'a>>,
23    pub(crate) value: Option<Level<'a>>,
24}
25
26impl<'a> Deserializer<'a> {
27    pub(crate) fn with_map(map: BTreeMap<Cow<'a, str>, Level<'a>>) -> Self {
28        Deserializer {
29            iter: map.into_iter(),
30            value: None,
31        }
32    }
33
34    /// Returns a new `Deserializer<'a>`.
35    pub(crate) fn with_bytes(input: &'a [u8]) -> Result<Self> {
36        Parser::new(input).as_deserializer()
37    }
38}
39
40impl<'de> de::Deserializer<'de> for Deserializer<'de> {
41    type Error = Error;
42
43    fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value>
44    where
45        V: de::Visitor<'de>,
46    {
47        if self.iter.next().is_none() {
48            return visitor.visit_unit();
49        }
50
51        Err(Error::custom("primitive"))
52    }
53
54    fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
55    where
56        V: de::Visitor<'de>,
57    {
58        visitor.visit_map(self)
59    }
60
61    fn deserialize_struct<V>(
62        self,
63        _name: &'static str,
64        _fields: &'static [&'static str],
65        visitor: V,
66    ) -> Result<V::Value>
67    where
68        V: de::Visitor<'de>,
69    {
70        self.deserialize_map(visitor)
71    }
72
73    /// Throws an error.
74    ///
75    /// Sequences are not supported at the top level.
76    fn deserialize_seq<V>(self, _visitor: V) -> Result<V::Value>
77    where
78        V: de::Visitor<'de>,
79    {
80        Err(Error::custom("sequence"))
81    }
82
83    fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value>
84    where
85        V: de::Visitor<'de>,
86    {
87        self.deserialize_map(visitor)
88    }
89
90    /// Throws an error.
91    ///
92    /// Tuples are not supported at the top level.
93    fn deserialize_tuple<V>(self, _len: usize, _visitor: V) -> Result<V::Value>
94    where
95        V: de::Visitor<'de>,
96    {
97        Err(Error::custom("tuple"))
98    }
99
100    /// Throws an error.
101    ///
102    /// TupleStructs are not supported at the top level.
103    fn deserialize_tuple_struct<V>(
104        self,
105        _name: &'static str,
106        _len: usize,
107        _visitor: V,
108    ) -> Result<V::Value>
109    where
110        V: de::Visitor<'de>,
111    {
112        Err(Error::custom("tuple struct"))
113    }
114
115    fn deserialize_enum<V>(
116        self,
117        _name: &'static str,
118        _variants: &'static [&'static str],
119        _visitor: V,
120    ) -> Result<V::Value>
121    where
122        V: de::Visitor<'de>,
123    {
124        Err(Error::custom("enum"))
125    }
126
127    forward_to_deserialize_any! {
128        bool
129        u8
130        u16
131        u32
132        u64
133        i8
134        i16
135        i32
136        i64
137        f32
138        f64
139        char
140        str
141        string
142        unit
143        option
144        bytes
145        byte_buf
146        unit_struct
147        identifier
148        ignored_any
149    }
150}
151
152impl<'de> de::MapAccess<'de> for Deserializer<'de> {
153    type Error = Error;
154
155    fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
156    where
157        K: de::DeserializeSeed<'de>,
158    {
159        if let Some((key, value)) = self.iter.next() {
160            self.value = Some(value);
161            seed.deserialize(KeyDeserializer(key)).map(Some)
162        } else {
163            Ok(None)
164        }
165    }
166
167    fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
168    where
169        V: de::DeserializeSeed<'de>,
170    {
171        if let Some(v) = self.value.take() {
172            seed.deserialize(LevelDeserializer(v))
173        } else {
174            Err(Error::custom(
175                "Somehow the map was empty after a non-empty key was returned",
176            ))
177        }
178    }
179}