serde_hzdata/de/
value.rs

1use std::{collections::HashMap, marker::PhantomData};
2
3use serde::{
4    de::{self, DeserializeSeed, Visitor},
5    Deserialize,
6};
7
8use crate::{
9    de::{value_map_access::HzdataValueMapAccess, value_seq_access::HzdataValueSeqAccess},
10    Error, HzdataValue,
11};
12
13impl<'de> Deserialize<'de> for HzdataValue {
14    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
15    where
16        D: serde::Deserializer<'de>,
17    {
18        struct KeySeed;
19
20        impl<'de> DeserializeSeed<'de> for KeySeed {
21            type Value = String;
22
23            fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
24            where
25                D: serde::Deserializer<'de>,
26            {
27                deserializer.deserialize_identifier(self)
28            }
29        }
30
31        impl<'de> Visitor<'de> for KeySeed {
32            type Value = String;
33
34            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
35                formatter.write_str("any valid Hzdata object key")
36            }
37
38            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
39            where
40                E: serde::de::Error,
41            {
42                Ok(v.to_string())
43            }
44
45            fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
46            where
47                E: serde::de::Error,
48            {
49                Ok(v)
50            }
51        }
52
53        struct ValueVisitor;
54
55        impl<'de> Visitor<'de> for ValueVisitor {
56            type Value = HzdataValue;
57
58            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
59                formatter.write_str("any valid Hzdata value")
60            }
61
62            fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
63            where
64                E: serde::de::Error,
65            {
66                Ok(HzdataValue::Boolean(v))
67            }
68
69            fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
70            where
71                E: serde::de::Error,
72            {
73                Ok(HzdataValue::Integer(v))
74            }
75
76            fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
77            where
78                E: serde::de::Error,
79            {
80                Ok(HzdataValue::Float(v))
81            }
82
83            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
84            where
85                E: serde::de::Error,
86            {
87                Ok(HzdataValue::String(v.to_owned()))
88            }
89
90            fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
91            where
92                E: serde::de::Error,
93            {
94                Ok(HzdataValue::String(v))
95            }
96
97            fn visit_none<E>(self) -> Result<Self::Value, E>
98            where
99                E: serde::de::Error,
100            {
101                Ok(HzdataValue::Nothing)
102            }
103
104            fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
105            where
106                D: serde::Deserializer<'de>,
107            {
108                Deserialize::deserialize(deserializer)
109            }
110
111            fn visit_unit<E>(self) -> Result<Self::Value, E>
112            where
113                E: serde::de::Error,
114            {
115                Ok(HzdataValue::Nothing)
116            }
117
118            fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
119            where
120                D: serde::Deserializer<'de>,
121            {
122                // TODO: handle newtype struct Regex(String)
123                deserializer.deserialize_any(self)
124            }
125
126            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
127            where
128                A: serde::de::SeqAccess<'de>,
129            {
130                let mut array = Vec::new();
131
132                while let Some(val) = seq.next_element()? {
133                    array.push(val);
134                }
135
136                Ok(HzdataValue::Array(array))
137            }
138
139            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
140            where
141                A: serde::de::MapAccess<'de>,
142            {
143                let mut object = HashMap::new();
144
145                while let Some((key, value)) = map.next_entry_seed(KeySeed, PhantomData)? {
146                    object.insert(key, value);
147                }
148
149                Ok(HzdataValue::Object(object))
150            }
151        }
152
153        deserializer.deserialize_any(ValueVisitor)
154    }
155}
156
157impl<'de> de::Deserializer<'de> for HzdataValue {
158    type Error = Error<'de>;
159
160    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
161    where
162        V: Visitor<'de>,
163    {
164        match self {
165            HzdataValue::Boolean(val) => visitor.visit_bool(val),
166            HzdataValue::Integer(val) => visitor.visit_i64(val),
167            HzdataValue::Float(val) => visitor.visit_f64(val),
168            HzdataValue::String(val) => visitor.visit_string(val),
169            HzdataValue::RegexText(_) => self.deserialize_newtype_struct("Regex", visitor),
170            HzdataValue::Unit(_) => todo!(),
171            HzdataValue::Array(val) => visitor.visit_seq(HzdataValueSeqAccess::new(val)),
172            HzdataValue::Object(val) => visitor.visit_map(HzdataValueMapAccess::new(val)),
173            HzdataValue::Nothing => visitor.visit_none(),
174        }
175    }
176
177    serde::forward_to_deserialize_any! {
178        bool i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 /* char */ str string /* bytes byte_buf */
179    }
180
181    fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
182    where
183        V: Visitor<'de>,
184    {
185        todo!()
186    }
187
188    fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
189    where
190        V: Visitor<'de>,
191    {
192        todo!()
193    }
194
195    fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
196    where
197        V: Visitor<'de>,
198    {
199        todo!()
200    }
201
202    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
203    where
204        V: Visitor<'de>,
205    {
206        if self == HzdataValue::Nothing {
207            visitor.visit_none()
208        } else {
209            visitor.visit_some(self)
210        }
211    }
212
213    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
214    where
215        V: Visitor<'de>,
216    {
217        todo!()
218    }
219
220    fn deserialize_unit_struct<V>(
221        self,
222        name: &'static str,
223        visitor: V,
224    ) -> Result<V::Value, Self::Error>
225    where
226        V: Visitor<'de>,
227    {
228        todo!()
229    }
230
231    fn deserialize_newtype_struct<V>(
232        self,
233        name: &'static str,
234        visitor: V,
235    ) -> Result<V::Value, Self::Error>
236    where
237        V: Visitor<'de>,
238    {
239        todo!()
240    }
241
242    fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
243    where
244        V: Visitor<'de>,
245    {
246        self.deserialize_any(visitor)
247    }
248
249    fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
250    where
251        V: Visitor<'de>,
252    {
253        todo!()
254    }
255
256    fn deserialize_tuple_struct<V>(
257        self,
258        name: &'static str,
259        len: usize,
260        visitor: V,
261    ) -> Result<V::Value, Self::Error>
262    where
263        V: Visitor<'de>,
264    {
265        todo!()
266    }
267
268    fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
269    where
270        V: Visitor<'de>,
271    {
272        self.deserialize_any(visitor)
273    }
274
275    fn deserialize_struct<V>(
276        self,
277        _name: &'static str,
278        _fields: &'static [&'static str],
279        visitor: V,
280    ) -> Result<V::Value, Self::Error>
281    where
282        V: Visitor<'de>,
283    {
284        self.deserialize_map(visitor)
285    }
286
287    fn deserialize_enum<V>(
288        self,
289        name: &'static str,
290        variants: &'static [&'static str],
291        visitor: V,
292    ) -> Result<V::Value, Self::Error>
293    where
294        V: Visitor<'de>,
295    {
296        todo!()
297    }
298
299    fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
300    where
301        V: Visitor<'de>,
302    {
303        todo!()
304    }
305
306    fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
307    where
308        V: Visitor<'de>,
309    {
310        todo!()
311    }
312}