• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![allow(clippy::derive_partial_eq_without_eq)]
2 
3 use serde::de::value::{Error, MapDeserializer, SeqDeserializer};
4 use serde::de::{
5     Deserialize, DeserializeSeed, Deserializer, EnumAccess, IgnoredAny, IntoDeserializer,
6     VariantAccess, Visitor,
7 };
8 use serde::forward_to_deserialize_any;
9 use serde_derive::Deserialize;
10 
11 #[derive(PartialEq, Debug, Deserialize)]
12 enum Target {
13     Unit,
14     Newtype(i32),
15     Tuple(i32, i32),
16     Struct { a: i32 },
17 }
18 
19 struct Enum(&'static str);
20 
21 impl<'de> Deserializer<'de> for Enum {
22     type Error = Error;
23 
deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,24     fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
25     where
26         V: Visitor<'de>,
27     {
28         visitor.visit_enum(self)
29     }
30 
31     forward_to_deserialize_any! {
32         bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
33         bytes byte_buf option unit unit_struct newtype_struct seq tuple
34         tuple_struct map struct enum identifier ignored_any
35     }
36 }
37 
38 impl<'de> EnumAccess<'de> for Enum {
39     type Error = Error;
40     type Variant = Self;
41 
variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> where V: DeserializeSeed<'de>,42     fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
43     where
44         V: DeserializeSeed<'de>,
45     {
46         let v = seed.deserialize(self.0.into_deserializer())?;
47         Ok((v, self))
48     }
49 }
50 
51 impl<'de> VariantAccess<'de> for Enum {
52     type Error = Error;
53 
unit_variant(self) -> Result<(), Self::Error>54     fn unit_variant(self) -> Result<(), Self::Error> {
55         Ok(())
56     }
57 
newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error> where T: DeserializeSeed<'de>,58     fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, Self::Error>
59     where
60         T: DeserializeSeed<'de>,
61     {
62         seed.deserialize(10i32.into_deserializer())
63     }
64 
tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error> where V: Visitor<'de>,65     fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
66     where
67         V: Visitor<'de>,
68     {
69         let seq = SeqDeserializer::new(vec![1i32, 2].into_iter());
70         visitor.visit_seq(seq)
71     }
72 
struct_variant<V>( self, _fields: &'static [&'static str], visitor: V, ) -> Result<V::Value, Self::Error> where V: Visitor<'de>,73     fn struct_variant<V>(
74         self,
75         _fields: &'static [&'static str],
76         visitor: V,
77     ) -> Result<V::Value, Self::Error>
78     where
79         V: Visitor<'de>,
80     {
81         let map = MapDeserializer::new(vec![("a", 10i32)].into_iter());
82         visitor.visit_map(map)
83     }
84 }
85 
86 #[test]
test_deserialize_enum()87 fn test_deserialize_enum() {
88     // First just make sure the Deserializer impl works
89     assert_eq!(Target::Unit, Target::deserialize(Enum("Unit")).unwrap());
90     assert_eq!(
91         Target::Newtype(10),
92         Target::deserialize(Enum("Newtype")).unwrap()
93     );
94     assert_eq!(
95         Target::Tuple(1, 2),
96         Target::deserialize(Enum("Tuple")).unwrap()
97     );
98     assert_eq!(
99         Target::Struct { a: 10 },
100         Target::deserialize(Enum("Struct")).unwrap()
101     );
102 
103     // Now try IgnoredAny
104     IgnoredAny::deserialize(Enum("Unit")).unwrap();
105     IgnoredAny::deserialize(Enum("Newtype")).unwrap();
106     IgnoredAny::deserialize(Enum("Tuple")).unwrap();
107     IgnoredAny::deserialize(Enum("Struct")).unwrap();
108 }
109