serde_types/parsing/
der.rs

1use serde::{
2    __private::de::EnumDeserializer,
3    de::{
4        value::{Error, MapDeserializer},
5        Error as _,
6    },
7};
8
9use super::*;
10
11impl<'de> ParsableValue<'de> {
12    fn to_deserializer(self) -> ContentDeserializer<'de, Error> {
13        ContentDeserializer::new(self.inner)
14    }
15
16    fn invalid_type<V>(self, v: V) -> Result<V::Value, Error>
17    where
18        V: Visitor<'de>,
19    {
20        Err(serde::de::Error::invalid_type(self.unexpected(), &v))
21    }
22    // fn custom_error<V>(self, v: impl Into<String>) -> Result<V::Value, Error>
23    // where
24    //     V: Visitor<'de>,
25    // {
26    //     Err(serde::de::Error::custom(v.into()))
27    // }
28
29    fn unexpected(&'de self) -> Unexpected<'de> {
30        match &self.inner {
31            Content::Bool(b) => Unexpected::Bool(*b),
32            Content::U8(n) => Unexpected::Unsigned(*n as u64),
33            Content::U16(n) => Unexpected::Unsigned(*n as u64),
34            Content::U32(n) => Unexpected::Unsigned(*n as u64),
35            Content::U64(n) => Unexpected::Unsigned(*n),
36            Content::I8(n) => Unexpected::Signed(*n as i64),
37            Content::I16(n) => Unexpected::Signed(*n as i64),
38            Content::I32(n) => Unexpected::Signed(*n as i64),
39            Content::I64(n) => Unexpected::Signed(*n),
40            Content::F32(f) => Unexpected::Float(*f as f64),
41            Content::F64(f) => Unexpected::Float(*f),
42            Content::Char(c) => Unexpected::Char(*c),
43            Content::String(s) => Unexpected::Str(s.as_str()),
44            Content::Str(s) => Unexpected::Str(s),
45            Content::ByteBuf(b) => Unexpected::Bytes(b.as_slice()),
46            Content::Bytes(b) => Unexpected::Bytes(b),
47            Content::None | Content::Some(_) => Unexpected::Option,
48            Content::Unit => Unexpected::Unit,
49            Content::Newtype(_) => Unexpected::NewtypeStruct,
50            Content::Seq(_) => Unexpected::Seq,
51            Content::Map(_) => Unexpected::Map,
52        }
53    }
54    fn parse_bool<V>(s: impl AsRef<str>, v: V) -> Result<V::Value, Error>
55    where
56        V: Visitor<'de>,
57    {
58        if s.as_ref().eq_ignore_ascii_case("true") {
59            v.visit_bool(true)
60        }
61        else if s.as_ref().eq_ignore_ascii_case("false") {
62            v.visit_bool(false)
63        }
64        else {
65            Err(Error::invalid_value(Unexpected::Str(s.as_ref()), &v))
66        }
67    }
68    fn parse_u8<V>(s: &str, v: V) -> Result<V::Value, Error>
69    where
70        V: Visitor<'de>,
71    {
72        match u8::from_str(s) {
73            Ok(o) => v.visit_u8(o),
74            Err(_) => Err(Error::invalid_value(Unexpected::Str(s), &v)),
75        }
76    }
77    fn parse_u16<V>(s: &str, v: V) -> Result<V::Value, Error>
78    where
79        V: Visitor<'de>,
80    {
81        match u16::from_str(s) {
82            Ok(o) => v.visit_u16(o),
83            Err(_) => Err(Error::invalid_value(Unexpected::Str(s), &v)),
84        }
85    }
86    fn parse_u32<V>(s: &str, v: V) -> Result<V::Value, Error>
87    where
88        V: Visitor<'de>,
89    {
90        match u32::from_str(s) {
91            Ok(o) => v.visit_u32(o),
92            Err(_) => Err(Error::invalid_value(Unexpected::Str(s), &v)),
93        }
94    }
95    fn parse_u64<V>(s: &str, v: V) -> Result<V::Value, Error>
96    where
97        V: Visitor<'de>,
98    {
99        match u64::from_str(s) {
100            Ok(o) => v.visit_u64(o),
101            Err(_) => Err(Error::invalid_value(Unexpected::Str(s), &v)),
102        }
103    }
104    fn parse_u128<V>(s: &str, v: V) -> Result<V::Value, Error>
105    where
106        V: Visitor<'de>,
107    {
108        match u128::from_str(s) {
109            Ok(o) => v.visit_u128(o),
110            Err(_) => Err(Error::invalid_value(Unexpected::Str(s), &v)),
111        }
112    }
113    fn parse_i8<V>(s: &str, v: V) -> Result<V::Value, Error>
114    where
115        V: Visitor<'de>,
116    {
117        match i8::from_str(s) {
118            Ok(o) => v.visit_i8(o),
119            Err(_) => Err(Error::invalid_value(Unexpected::Str(s), &v)),
120        }
121    }
122    fn parse_i16<V>(s: &str, v: V) -> Result<V::Value, Error>
123    where
124        V: Visitor<'de>,
125    {
126        match i16::from_str(s) {
127            Ok(o) => v.visit_i16(o),
128            Err(_) => Err(Error::invalid_value(Unexpected::Str(s), &v)),
129        }
130    }
131    fn parse_i32<V>(s: &str, v: V) -> Result<V::Value, Error>
132    where
133        V: Visitor<'de>,
134    {
135        match i32::from_str(s) {
136            Ok(o) => v.visit_i32(o),
137            Err(_) => Err(Error::invalid_value(Unexpected::Str(s), &v)),
138        }
139    }
140    fn parse_i64<V>(s: &str, v: V) -> Result<V::Value, Error>
141    where
142        V: Visitor<'de>,
143    {
144        match i64::from_str(s) {
145            Ok(o) => v.visit_i64(o),
146            Err(_) => Err(Error::invalid_value(Unexpected::Str(s), &v)),
147        }
148    }
149    fn parse_i128<V>(s: &str, v: V) -> Result<V::Value, Error>
150    where
151        V: Visitor<'de>,
152    {
153        match i128::from_str(s) {
154            Ok(o) => v.visit_i128(o),
155            Err(_) => Err(Error::invalid_value(Unexpected::Str(s), &v)),
156        }
157    }
158}
159
160impl<'de> Deserializer<'de> for ParsableValue<'de> {
161    type Error = Error;
162
163    fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
164    where
165        V: Visitor<'de>,
166    {
167        self.to_deserializer().deserialize_any(visitor)
168    }
169
170    fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
171    where
172        V: Visitor<'de>,
173    {
174        match self.inner {
175            Content::Bool(v) => visitor.visit_bool(v),
176            Content::Str(v) => ParsableValue::parse_bool(v, visitor),
177            Content::String(v) => ParsableValue::parse_bool(v, visitor),
178            _ => self.invalid_type(visitor),
179        }
180    }
181
182    fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
183    where
184        V: Visitor<'de>,
185    {
186        parse_integer(self.inner, visitor, ParsableValue::parse_i8)
187    }
188
189    fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
190    where
191        V: Visitor<'de>,
192    {
193        parse_integer(self.inner, visitor, ParsableValue::parse_i16)
194    }
195
196    fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
197    where
198        V: Visitor<'de>,
199    {
200        parse_integer(self.inner, visitor, ParsableValue::parse_i32)
201    }
202
203    fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
204    where
205        V: Visitor<'de>,
206    {
207        parse_integer(self.inner, visitor, ParsableValue::parse_i64)
208    }
209
210    fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
211    where
212        V: Visitor<'de>,
213    {
214        parse_integer(self.inner, visitor, ParsableValue::parse_i128)
215    }
216
217    fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
218    where
219        V: Visitor<'de>,
220    {
221        parse_integer(self.inner, visitor, ParsableValue::parse_u8)
222    }
223
224    fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
225    where
226        V: Visitor<'de>,
227    {
228        parse_integer(self.inner, visitor, ParsableValue::parse_u16)
229    }
230
231    fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
232    where
233        V: Visitor<'de>,
234    {
235        parse_integer(self.inner, visitor, ParsableValue::parse_u32)
236    }
237
238    fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
239    where
240        V: Visitor<'de>,
241    {
242        parse_integer(self.inner, visitor, ParsableValue::parse_u64)
243    }
244
245    fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
246    where
247        V: Visitor<'de>,
248    {
249        parse_integer(self.inner, visitor, ParsableValue::parse_u128)
250    }
251
252    fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
253    where
254        V: Visitor<'de>,
255    {
256        self.deserialize_f64(visitor)
257    }
258
259    fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
260    where
261        V: Visitor<'de>,
262    {
263        match self.inner {
264            Content::F32(v) => visitor.visit_f32(v),
265            Content::F64(v) => visitor.visit_f64(v),
266            Content::U8(v) => visitor.visit_u8(v),
267            Content::U16(v) => visitor.visit_u16(v),
268            Content::U32(v) => visitor.visit_u32(v),
269            Content::U64(v) => visitor.visit_u64(v),
270            Content::I8(v) => visitor.visit_i8(v),
271            Content::I16(v) => visitor.visit_i16(v),
272            Content::I32(v) => visitor.visit_i32(v),
273            Content::I64(v) => visitor.visit_i64(v),
274            _ => self.invalid_type(visitor),
275        }
276    }
277
278    fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
279    where
280        V: Visitor<'de>,
281    {
282        self.deserialize_string(visitor)
283    }
284
285    fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
286    where
287        V: Visitor<'de>,
288    {
289        self.deserialize_string(visitor)
290    }
291
292    fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
293    where
294        V: Visitor<'de>,
295    {
296        match self.inner {
297            Content::Char(v) => visitor.visit_char(v),
298            Content::String(v) => visitor.visit_string(v),
299            Content::Str(v) => visitor.visit_borrowed_str(v),
300            Content::ByteBuf(v) => visitor.visit_byte_buf(v),
301            Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
302            _ => self.invalid_type(visitor),
303        }
304    }
305
306    fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
307    where
308        V: Visitor<'de>,
309    {
310        self.deserialize_byte_buf(visitor)
311    }
312
313    fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
314    where
315        V: Visitor<'de>,
316    {
317        match self.inner {
318            Content::String(v) => visitor.visit_string(v),
319            Content::Str(v) => visitor.visit_borrowed_str(v),
320            Content::ByteBuf(v) => visitor.visit_byte_buf(v),
321            Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
322            Content::Seq(_) => todo!(),
323            _ => self.invalid_type(visitor),
324        }
325    }
326
327    fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
328    where
329        V: Visitor<'de>,
330    {
331        match self.inner {
332            Content::None => visitor.visit_none(),
333            Content::Some(v) => visitor.visit_some(ParsableValue::new(*v)),
334            Content::Unit => visitor.visit_unit(),
335            _ => visitor.visit_some(self),
336        }
337    }
338
339    fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
340    where
341        V: Visitor<'de>,
342    {
343        match self.inner {
344            Content::Unit => visitor.visit_unit(),
345            Content::Map(v) if v.is_empty() => visitor.visit_unit(),
346            Content::Seq(v) if v.is_empty() => visitor.visit_unit(),
347            _ => self.deserialize_any(visitor),
348        }
349    }
350
351    #[allow(unused_variables)]
352    fn deserialize_unit_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value, Self::Error>
353    where
354        V: Visitor<'de>,
355    {
356        self.deserialize_unit(visitor)
357    }
358
359    #[allow(unused_variables)]
360    fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value, Self::Error>
361    where
362        V: Visitor<'de>,
363    {
364        match self.inner {
365            Content::Newtype(v) => visitor.visit_newtype_struct(ParsableValue::from(*v)),
366            _ => visitor.visit_newtype_struct(self),
367        }
368    }
369
370    fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
371    where
372        V: Visitor<'de>,
373    {
374        match self.inner {
375            Content::Seq(s) => {
376                let seq = s.into_iter().map(ParsableValue::from);
377                let mut seq_visitor = SeqDeserializer::new(seq);
378                let value = visitor.visit_seq(&mut seq_visitor)?;
379                seq_visitor.end()?;
380                Ok(value)
381            }
382            _ => self.invalid_type(visitor),
383        }
384    }
385
386    #[allow(unused_variables)]
387    fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error>
388    where
389        V: Visitor<'de>,
390    {
391        self.deserialize_seq(visitor)
392    }
393
394    #[allow(unused_variables)]
395    fn deserialize_tuple_struct<V>(self, name: &'static str, len: usize, visitor: V) -> Result<V::Value, Self::Error>
396    where
397        V: Visitor<'de>,
398    {
399        self.deserialize_seq(visitor)
400    }
401
402    fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
403    where
404        V: Visitor<'de>,
405    {
406        match self.inner {
407            Content::Map(content) => {
408                let map = content.into_iter().map(|(k, v)| (ContentDeserializer::new(k), ParsableValue::from(v)));
409                let mut map_visitor = MapDeserializer::new(map);
410                let value = visitor.visit_map(&mut map_visitor)?;
411                map_visitor.end()?;
412                Ok(value)
413            }
414            _ => self.invalid_type(visitor),
415        }
416    }
417
418    #[allow(unused_variables)]
419    fn deserialize_struct<V>(
420        self,
421        name: &'static str,
422        fields: &'static [&'static str],
423        visitor: V,
424    ) -> Result<V::Value, Self::Error>
425    where
426        V: Visitor<'de>,
427    {
428        self.deserialize_map(visitor)
429    }
430
431    #[allow(unused_variables)]
432    fn deserialize_enum<V>(
433        self,
434        name: &'static str,
435        variants: &'static [&'static str],
436        visitor: V,
437    ) -> Result<V::Value, Self::Error>
438    where
439        V: Visitor<'de>,
440    {
441        match self.inner {
442            Content::Bool(_) => {
443                todo!()
444            }
445            Content::U8(_) => {
446                todo!()
447            }
448            Content::U16(_) => {
449                todo!()
450            }
451            Content::U32(_) => {
452                todo!()
453            }
454            s @ Content::U64(_) => visitor.visit_enum(EnumDeserializer::new(s, None)),
455            Content::I8(_) => {
456                todo!()
457            }
458            Content::I16(_) => {
459                todo!()
460            }
461            Content::I32(_) => {
462                todo!()
463            }
464            Content::I64(_) => {
465                todo!()
466            }
467            Content::F32(_) => {
468                todo!()
469            }
470            Content::F64(_) => {
471                todo!()
472            }
473            Content::Char(_) => {
474                todo!()
475            }
476            Content::String(s) => visitor.visit_enum(EnumDeserializer::new(Content::String(s), None)),
477            Content::Str(s) => visitor.visit_enum(EnumDeserializer::new(Content::Str(s), None)),
478            Content::ByteBuf(_) => {
479                todo!()
480            }
481            Content::Bytes(_) => {
482                todo!()
483            }
484            Content::None => {
485                todo!()
486            }
487            Content::Some(_) => {
488                todo!()
489            }
490            Content::Unit => {
491                todo!()
492            }
493            _ => self.invalid_type(visitor),
494        }
495    }
496
497    fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
498    where
499        V: Visitor<'de>,
500    {
501        match self.inner {
502            Content::String(v) => visitor.visit_string(v),
503            Content::Str(v) => visitor.visit_borrowed_str(v),
504            Content::ByteBuf(v) => visitor.visit_byte_buf(v),
505            Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
506            Content::U8(v) => visitor.visit_u8(v),
507            Content::U64(v) => visitor.visit_u64(v),
508            _ => self.invalid_type(visitor),
509        }
510    }
511
512    fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
513    where
514        V: Visitor<'de>,
515    {
516        visitor.visit_unit()
517    }
518}
519
520impl<'de> IntoDeserializer<'de> for ParsableValue<'de> {
521    type Deserializer = ParsableValue<'de>;
522
523    fn into_deserializer(self) -> Self::Deserializer {
524        self
525    }
526}
527
528fn parse_integer<'de, V, F>(inner: Content<'de>, visitor: V, parser: F) -> Result<V::Value, Error>
529where
530    V: Visitor<'de>,
531    F: Fn(&str, V) -> Result<V::Value, Error>,
532{
533    match inner {
534        Content::U8(v) => visitor.visit_u8(v),
535        Content::U16(v) => visitor.visit_u16(v),
536        Content::U32(v) => visitor.visit_u32(v),
537        Content::U64(v) => visitor.visit_u64(v),
538        Content::I8(v) => visitor.visit_i8(v),
539        Content::I16(v) => visitor.visit_i16(v),
540        Content::I32(v) => visitor.visit_i32(v),
541        Content::I64(v) => visitor.visit_i64(v),
542        Content::String(s) => parser(&s, visitor),
543        Content::Str(s) => parser(s, visitor),
544        Content::None => {
545            todo!()
546        }
547        Content::Some(s) => parse_integer(*s, visitor, parser),
548        Content::Unit => parser("0", visitor),
549        _ => ParsableValue::from(inner).invalid_type(visitor),
550    }
551}