/rust/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.152/src/de/value.rs
Line | Count | Source (jump to first uncovered line) |
1 | | //! Building blocks for deserializing basic values using the `IntoDeserializer` |
2 | | //! trait. |
3 | | //! |
4 | | //! ```edition2018 |
5 | | //! use std::str::FromStr; |
6 | | //! use serde::Deserialize; |
7 | | //! use serde::de::{value, IntoDeserializer}; |
8 | | //! |
9 | | //! #[derive(Deserialize)] |
10 | | //! enum Setting { |
11 | | //! On, |
12 | | //! Off, |
13 | | //! } |
14 | | //! |
15 | | //! impl FromStr for Setting { |
16 | | //! type Err = value::Error; |
17 | | //! |
18 | | //! fn from_str(s: &str) -> Result<Self, Self::Err> { |
19 | | //! Self::deserialize(s.into_deserializer()) |
20 | | //! } |
21 | | //! } |
22 | | //! ``` |
23 | | |
24 | | use lib::*; |
25 | | |
26 | | use self::private::{First, Second}; |
27 | | use __private::size_hint; |
28 | | use de::{self, Deserializer, Expected, IntoDeserializer, SeqAccess, Visitor}; |
29 | | use ser; |
30 | | |
31 | | //////////////////////////////////////////////////////////////////////////////// |
32 | | |
33 | | // For structs that contain a PhantomData. We do not want the trait |
34 | | // bound `E: Clone` inferred by derive(Clone). |
35 | | macro_rules! impl_copy_clone { |
36 | | ($ty:ident $(<$lifetime:tt>)*) => { |
37 | | impl<$($lifetime,)* E> Copy for $ty<$($lifetime,)* E> {} |
38 | | |
39 | | impl<$($lifetime,)* E> Clone for $ty<$($lifetime,)* E> { |
40 | 0 | fn clone(&self) -> Self { |
41 | 0 | *self |
42 | 0 | } Unexecuted instantiation: <serde::de::value::BorrowedBytesDeserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::U64Deserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::U16Deserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::BytesDeserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::U128Deserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::I16Deserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::I64Deserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::StrDeserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::UnitDeserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::BorrowedStrDeserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::F32Deserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::UsizeDeserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::U32Deserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::I32Deserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::U8Deserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::F64Deserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::I8Deserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::IsizeDeserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::I128Deserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::CharDeserializer<_> as core::clone::Clone>::clone Unexecuted instantiation: <serde::de::value::BoolDeserializer<_> as core::clone::Clone>::clone |
43 | | } |
44 | | }; |
45 | | } |
46 | | |
47 | | //////////////////////////////////////////////////////////////////////////////// |
48 | | |
49 | | /// A minimal representation of all possible errors that can occur using the |
50 | | /// `IntoDeserializer` trait. |
51 | 0 | #[derive(Clone, PartialEq)] |
52 | | pub struct Error { |
53 | | err: ErrorImpl, |
54 | | } |
55 | | |
56 | | #[cfg(any(feature = "std", feature = "alloc"))] |
57 | | type ErrorImpl = Box<str>; |
58 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
59 | | type ErrorImpl = (); |
60 | | |
61 | | impl de::Error for Error { |
62 | | #[cfg(any(feature = "std", feature = "alloc"))] |
63 | | #[cold] |
64 | 0 | fn custom<T>(msg: T) -> Self |
65 | 0 | where |
66 | 0 | T: Display, |
67 | 0 | { |
68 | 0 | Error { |
69 | 0 | err: msg.to_string().into_boxed_str(), |
70 | 0 | } |
71 | 0 | } |
72 | | |
73 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
74 | | #[cold] |
75 | | fn custom<T>(msg: T) -> Self |
76 | | where |
77 | | T: Display, |
78 | | { |
79 | | let _ = msg; |
80 | | Error { err: () } |
81 | | } |
82 | | } |
83 | | |
84 | | impl ser::Error for Error { |
85 | | #[cold] |
86 | 0 | fn custom<T>(msg: T) -> Self |
87 | 0 | where |
88 | 0 | T: Display, |
89 | 0 | { |
90 | 0 | de::Error::custom(msg) |
91 | 0 | } |
92 | | } |
93 | | |
94 | | impl Display for Error { |
95 | | #[cfg(any(feature = "std", feature = "alloc"))] |
96 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
97 | 0 | formatter.write_str(&self.err) |
98 | 0 | } |
99 | | |
100 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
101 | | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
102 | | formatter.write_str("Serde deserialization error") |
103 | | } |
104 | | } |
105 | | |
106 | | impl Debug for Error { |
107 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
108 | 0 | let mut debug = formatter.debug_tuple("Error"); |
109 | 0 | #[cfg(any(feature = "std", feature = "alloc"))] |
110 | 0 | debug.field(&self.err); |
111 | 0 | debug.finish() |
112 | 0 | } |
113 | | } |
114 | | |
115 | | #[cfg(feature = "std")] |
116 | | impl error::Error for Error { |
117 | 0 | fn description(&self) -> &str { |
118 | 0 | &self.err |
119 | 0 | } |
120 | | } |
121 | | |
122 | | //////////////////////////////////////////////////////////////////////////////// |
123 | | |
124 | | impl<'de, E> IntoDeserializer<'de, E> for () |
125 | | where |
126 | | E: de::Error, |
127 | | { |
128 | | type Deserializer = UnitDeserializer<E>; |
129 | | |
130 | 0 | fn into_deserializer(self) -> UnitDeserializer<E> { |
131 | 0 | UnitDeserializer::new() |
132 | 0 | } |
133 | | } |
134 | | |
135 | | /// A deserializer holding a `()`. |
136 | | pub struct UnitDeserializer<E> { |
137 | | marker: PhantomData<E>, |
138 | | } |
139 | | |
140 | | impl_copy_clone!(UnitDeserializer); |
141 | | |
142 | | impl<E> UnitDeserializer<E> { |
143 | | #[allow(missing_docs)] |
144 | 0 | pub fn new() -> Self { |
145 | 0 | UnitDeserializer { |
146 | 0 | marker: PhantomData, |
147 | 0 | } |
148 | 0 | } |
149 | | } |
150 | | |
151 | | impl<'de, E> de::Deserializer<'de> for UnitDeserializer<E> |
152 | | where |
153 | | E: de::Error, |
154 | | { |
155 | | type Error = E; |
156 | | |
157 | | forward_to_deserialize_any! { |
158 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
159 | | bytes byte_buf unit unit_struct newtype_struct seq tuple tuple_struct |
160 | | map struct enum identifier ignored_any |
161 | | } |
162 | | |
163 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
164 | 0 | where |
165 | 0 | V: de::Visitor<'de>, |
166 | 0 | { |
167 | 0 | visitor.visit_unit() |
168 | 0 | } |
169 | | |
170 | 0 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
171 | 0 | where |
172 | 0 | V: de::Visitor<'de>, |
173 | 0 | { |
174 | 0 | visitor.visit_none() |
175 | 0 | } |
176 | | } |
177 | | |
178 | | impl<E> Debug for UnitDeserializer<E> { |
179 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
180 | 0 | formatter.debug_struct("UnitDeserializer").finish() |
181 | 0 | } |
182 | | } |
183 | | |
184 | | //////////////////////////////////////////////////////////////////////////////// |
185 | | |
186 | | /// A deserializer that cannot be instantiated. |
187 | | #[cfg(feature = "unstable")] |
188 | | pub struct NeverDeserializer<E> { |
189 | | never: !, |
190 | | marker: PhantomData<E>, |
191 | | } |
192 | | |
193 | | #[cfg(feature = "unstable")] |
194 | | impl<'de, E> IntoDeserializer<'de, E> for ! |
195 | | where |
196 | | E: de::Error, |
197 | | { |
198 | | type Deserializer = NeverDeserializer<E>; |
199 | | |
200 | | fn into_deserializer(self) -> Self::Deserializer { |
201 | | self |
202 | | } |
203 | | } |
204 | | |
205 | | #[cfg(feature = "unstable")] |
206 | | impl<'de, E> de::Deserializer<'de> for NeverDeserializer<E> |
207 | | where |
208 | | E: de::Error, |
209 | | { |
210 | | type Error = E; |
211 | | |
212 | | fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, Self::Error> |
213 | | where |
214 | | V: de::Visitor<'de>, |
215 | | { |
216 | | self.never |
217 | | } |
218 | | |
219 | | forward_to_deserialize_any! { |
220 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
221 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
222 | | tuple_struct map struct enum identifier ignored_any |
223 | | } |
224 | | } |
225 | | |
226 | | //////////////////////////////////////////////////////////////////////////////// |
227 | | |
228 | | macro_rules! primitive_deserializer { |
229 | | ($ty:ty, $doc:tt, $name:ident, $method:ident $($cast:tt)*) => { |
230 | | #[doc = "A deserializer holding"] |
231 | | #[doc = $doc] |
232 | | pub struct $name<E> { |
233 | | value: $ty, |
234 | | marker: PhantomData<E> |
235 | | } |
236 | | |
237 | | impl_copy_clone!($name); |
238 | | |
239 | | impl<'de, E> IntoDeserializer<'de, E> for $ty |
240 | | where |
241 | | E: de::Error, |
242 | | { |
243 | | type Deserializer = $name<E>; |
244 | | |
245 | 0 | fn into_deserializer(self) -> $name<E> { |
246 | 0 | $name::new(self) |
247 | 0 | } Unexecuted instantiation: <usize as serde::de::IntoDeserializer<_>>::into_deserializer Unexecuted instantiation: <i16 as serde::de::IntoDeserializer<_>>::into_deserializer Unexecuted instantiation: <f64 as serde::de::IntoDeserializer<_>>::into_deserializer Unexecuted instantiation: <i64 as serde::de::IntoDeserializer<_>>::into_deserializer Unexecuted instantiation: <isize as serde::de::IntoDeserializer<_>>::into_deserializer Unexecuted instantiation: <u128 as serde::de::IntoDeserializer<_>>::into_deserializer Unexecuted instantiation: <u16 as serde::de::IntoDeserializer<_>>::into_deserializer Unexecuted instantiation: <bool as serde::de::IntoDeserializer<_>>::into_deserializer Unexecuted instantiation: <i8 as serde::de::IntoDeserializer<_>>::into_deserializer Unexecuted instantiation: <i32 as serde::de::IntoDeserializer<_>>::into_deserializer Unexecuted instantiation: <u8 as serde::de::IntoDeserializer<_>>::into_deserializer Unexecuted instantiation: <f32 as serde::de::IntoDeserializer<_>>::into_deserializer Unexecuted instantiation: <char as serde::de::IntoDeserializer<_>>::into_deserializer Unexecuted instantiation: <i128 as serde::de::IntoDeserializer<_>>::into_deserializer Unexecuted instantiation: <u64 as serde::de::IntoDeserializer<_>>::into_deserializer |
248 | | } |
249 | | |
250 | | impl<E> $name<E> { |
251 | | #[allow(missing_docs)] |
252 | 0 | pub fn new(value: $ty) -> Self { |
253 | 0 | $name { |
254 | 0 | value: value, |
255 | 0 | marker: PhantomData, |
256 | 0 | } |
257 | 0 | } Unexecuted instantiation: <serde::de::value::I64Deserializer<_>>::new Unexecuted instantiation: <serde::de::value::I32Deserializer<_>>::new Unexecuted instantiation: <serde::de::value::F64Deserializer<_>>::new Unexecuted instantiation: <serde::de::value::F32Deserializer<_>>::new Unexecuted instantiation: <serde::de::value::BoolDeserializer<_>>::new Unexecuted instantiation: <serde::de::value::U128Deserializer<_>>::new Unexecuted instantiation: <serde::de::value::U8Deserializer<_>>::new Unexecuted instantiation: <serde::de::value::IsizeDeserializer<_>>::new Unexecuted instantiation: <serde::de::value::I16Deserializer<_>>::new Unexecuted instantiation: <serde::de::value::CharDeserializer<_>>::new Unexecuted instantiation: <serde::de::value::I8Deserializer<_>>::new Unexecuted instantiation: <serde::de::value::I128Deserializer<_>>::new Unexecuted instantiation: <serde::de::value::U16Deserializer<_>>::new Unexecuted instantiation: <serde::de::value::UsizeDeserializer<_>>::new Unexecuted instantiation: <serde::de::value::U64Deserializer<_>>::new |
258 | | } |
259 | | |
260 | | impl<'de, E> de::Deserializer<'de> for $name<E> |
261 | | where |
262 | | E: de::Error, |
263 | | { |
264 | | type Error = E; |
265 | | |
266 | | forward_to_deserialize_any! { |
267 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str |
268 | | string bytes byte_buf option unit unit_struct newtype_struct seq |
269 | | tuple tuple_struct map struct enum identifier ignored_any |
270 | | } |
271 | | |
272 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
273 | 0 | where |
274 | 0 | V: de::Visitor<'de>, |
275 | 0 | { |
276 | 0 | visitor.$method(self.value $($cast)*) |
277 | 0 | } Unexecuted instantiation: <serde::de::value::CharDeserializer<_> as serde::de::Deserializer>::deserialize_any::<_> Unexecuted instantiation: <serde::de::value::IsizeDeserializer<_> as serde::de::Deserializer>::deserialize_any::<_> Unexecuted instantiation: <serde::de::value::F32Deserializer<_> as serde::de::Deserializer>::deserialize_any::<_> Unexecuted instantiation: <serde::de::value::BoolDeserializer<_> as serde::de::Deserializer>::deserialize_any::<_> Unexecuted instantiation: <serde::de::value::U16Deserializer<_> as serde::de::Deserializer>::deserialize_any::<_> Unexecuted instantiation: <serde::de::value::U128Deserializer<_> as serde::de::Deserializer>::deserialize_any::<_> Unexecuted instantiation: <serde::de::value::I64Deserializer<_> as serde::de::Deserializer>::deserialize_any::<_> Unexecuted instantiation: <serde::de::value::F64Deserializer<_> as serde::de::Deserializer>::deserialize_any::<_> Unexecuted instantiation: <serde::de::value::I128Deserializer<_> as serde::de::Deserializer>::deserialize_any::<_> Unexecuted instantiation: <serde::de::value::I32Deserializer<_> as serde::de::Deserializer>::deserialize_any::<_> Unexecuted instantiation: <serde::de::value::UsizeDeserializer<_> as serde::de::Deserializer>::deserialize_any::<_> Unexecuted instantiation: <serde::de::value::I8Deserializer<_> as serde::de::Deserializer>::deserialize_any::<_> Unexecuted instantiation: <serde::de::value::U64Deserializer<_> as serde::de::Deserializer>::deserialize_any::<_> Unexecuted instantiation: <serde::de::value::I16Deserializer<_> as serde::de::Deserializer>::deserialize_any::<_> Unexecuted instantiation: <serde::de::value::U8Deserializer<_> as serde::de::Deserializer>::deserialize_any::<_> |
278 | | } |
279 | | |
280 | | impl<E> Debug for $name<E> { |
281 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
282 | 0 | formatter |
283 | 0 | .debug_struct(stringify!($name)) |
284 | 0 | .field("value", &self.value) |
285 | 0 | .finish() |
286 | 0 | } Unexecuted instantiation: <serde::de::value::U8Deserializer<_> as core::fmt::Debug>::fmt Unexecuted instantiation: <serde::de::value::IsizeDeserializer<_> as core::fmt::Debug>::fmt Unexecuted instantiation: <serde::de::value::F32Deserializer<_> as core::fmt::Debug>::fmt Unexecuted instantiation: <serde::de::value::I128Deserializer<_> as core::fmt::Debug>::fmt Unexecuted instantiation: <serde::de::value::U16Deserializer<_> as core::fmt::Debug>::fmt Unexecuted instantiation: <serde::de::value::CharDeserializer<_> as core::fmt::Debug>::fmt Unexecuted instantiation: <serde::de::value::I16Deserializer<_> as core::fmt::Debug>::fmt Unexecuted instantiation: <serde::de::value::I8Deserializer<_> as core::fmt::Debug>::fmt Unexecuted instantiation: <serde::de::value::UsizeDeserializer<_> as core::fmt::Debug>::fmt Unexecuted instantiation: <serde::de::value::U64Deserializer<_> as core::fmt::Debug>::fmt Unexecuted instantiation: <serde::de::value::U128Deserializer<_> as core::fmt::Debug>::fmt Unexecuted instantiation: <serde::de::value::I64Deserializer<_> as core::fmt::Debug>::fmt Unexecuted instantiation: <serde::de::value::I32Deserializer<_> as core::fmt::Debug>::fmt Unexecuted instantiation: <serde::de::value::F64Deserializer<_> as core::fmt::Debug>::fmt Unexecuted instantiation: <serde::de::value::BoolDeserializer<_> as core::fmt::Debug>::fmt |
287 | | } |
288 | | } |
289 | | } |
290 | | |
291 | | primitive_deserializer!(bool, "a `bool`.", BoolDeserializer, visit_bool); |
292 | | primitive_deserializer!(i8, "an `i8`.", I8Deserializer, visit_i8); |
293 | | primitive_deserializer!(i16, "an `i16`.", I16Deserializer, visit_i16); |
294 | | primitive_deserializer!(i32, "an `i32`.", I32Deserializer, visit_i32); |
295 | | primitive_deserializer!(i64, "an `i64`.", I64Deserializer, visit_i64); |
296 | | primitive_deserializer!(isize, "an `isize`.", IsizeDeserializer, visit_i64 as i64); |
297 | | primitive_deserializer!(u8, "a `u8`.", U8Deserializer, visit_u8); |
298 | | primitive_deserializer!(u16, "a `u16`.", U16Deserializer, visit_u16); |
299 | | primitive_deserializer!(u64, "a `u64`.", U64Deserializer, visit_u64); |
300 | | primitive_deserializer!(usize, "a `usize`.", UsizeDeserializer, visit_u64 as u64); |
301 | | primitive_deserializer!(f32, "an `f32`.", F32Deserializer, visit_f32); |
302 | | primitive_deserializer!(f64, "an `f64`.", F64Deserializer, visit_f64); |
303 | | primitive_deserializer!(char, "a `char`.", CharDeserializer, visit_char); |
304 | | |
305 | | serde_if_integer128! { |
306 | | primitive_deserializer!(i128, "an `i128`.", I128Deserializer, visit_i128); |
307 | | primitive_deserializer!(u128, "a `u128`.", U128Deserializer, visit_u128); |
308 | | } |
309 | | |
310 | | /// A deserializer holding a `u32`. |
311 | | pub struct U32Deserializer<E> { |
312 | | value: u32, |
313 | | marker: PhantomData<E>, |
314 | | } |
315 | | |
316 | | impl_copy_clone!(U32Deserializer); |
317 | | |
318 | | impl<'de, E> IntoDeserializer<'de, E> for u32 |
319 | | where |
320 | | E: de::Error, |
321 | | { |
322 | | type Deserializer = U32Deserializer<E>; |
323 | | |
324 | 0 | fn into_deserializer(self) -> U32Deserializer<E> { |
325 | 0 | U32Deserializer::new(self) |
326 | 0 | } |
327 | | } |
328 | | |
329 | | impl<E> U32Deserializer<E> { |
330 | | #[allow(missing_docs)] |
331 | 0 | pub fn new(value: u32) -> Self { |
332 | 0 | U32Deserializer { |
333 | 0 | value: value, |
334 | 0 | marker: PhantomData, |
335 | 0 | } |
336 | 0 | } |
337 | | } |
338 | | |
339 | | impl<'de, E> de::Deserializer<'de> for U32Deserializer<E> |
340 | | where |
341 | | E: de::Error, |
342 | | { |
343 | | type Error = E; |
344 | | |
345 | | forward_to_deserialize_any! { |
346 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
347 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
348 | | tuple_struct map struct identifier ignored_any |
349 | | } |
350 | | |
351 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
352 | 0 | where |
353 | 0 | V: de::Visitor<'de>, |
354 | 0 | { |
355 | 0 | visitor.visit_u32(self.value) |
356 | 0 | } |
357 | | |
358 | 0 | fn deserialize_enum<V>( |
359 | 0 | self, |
360 | 0 | name: &str, |
361 | 0 | variants: &'static [&'static str], |
362 | 0 | visitor: V, |
363 | 0 | ) -> Result<V::Value, Self::Error> |
364 | 0 | where |
365 | 0 | V: de::Visitor<'de>, |
366 | 0 | { |
367 | 0 | let _ = name; |
368 | 0 | let _ = variants; |
369 | 0 | visitor.visit_enum(self) |
370 | 0 | } |
371 | | } |
372 | | |
373 | | impl<'de, E> de::EnumAccess<'de> for U32Deserializer<E> |
374 | | where |
375 | | E: de::Error, |
376 | | { |
377 | | type Error = E; |
378 | | type Variant = private::UnitOnly<E>; |
379 | | |
380 | 0 | fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> |
381 | 0 | where |
382 | 0 | T: de::DeserializeSeed<'de>, |
383 | 0 | { |
384 | 0 | seed.deserialize(self).map(private::unit_only) |
385 | 0 | } |
386 | | } |
387 | | |
388 | | impl<E> Debug for U32Deserializer<E> { |
389 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
390 | 0 | formatter |
391 | 0 | .debug_struct("U32Deserializer") |
392 | 0 | .field("value", &self.value) |
393 | 0 | .finish() |
394 | 0 | } |
395 | | } |
396 | | |
397 | | //////////////////////////////////////////////////////////////////////////////// |
398 | | |
399 | | /// A deserializer holding a `&str`. |
400 | | pub struct StrDeserializer<'a, E> { |
401 | | value: &'a str, |
402 | | marker: PhantomData<E>, |
403 | | } |
404 | | |
405 | | impl_copy_clone!(StrDeserializer<'de>); |
406 | | |
407 | | impl<'de, 'a, E> IntoDeserializer<'de, E> for &'a str |
408 | | where |
409 | | E: de::Error, |
410 | | { |
411 | | type Deserializer = StrDeserializer<'a, E>; |
412 | | |
413 | 0 | fn into_deserializer(self) -> StrDeserializer<'a, E> { |
414 | 0 | StrDeserializer::new(self) |
415 | 0 | } |
416 | | } |
417 | | |
418 | | impl<'a, E> StrDeserializer<'a, E> { |
419 | | #[allow(missing_docs)] |
420 | 0 | pub fn new(value: &'a str) -> Self { |
421 | 0 | StrDeserializer { |
422 | 0 | value: value, |
423 | 0 | marker: PhantomData, |
424 | 0 | } |
425 | 0 | } |
426 | | } |
427 | | |
428 | | impl<'de, 'a, E> de::Deserializer<'de> for StrDeserializer<'a, E> |
429 | | where |
430 | | E: de::Error, |
431 | | { |
432 | | type Error = E; |
433 | | |
434 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
435 | 0 | where |
436 | 0 | V: de::Visitor<'de>, |
437 | 0 | { |
438 | 0 | visitor.visit_str(self.value) |
439 | 0 | } |
440 | | |
441 | 0 | fn deserialize_enum<V>( |
442 | 0 | self, |
443 | 0 | name: &str, |
444 | 0 | variants: &'static [&'static str], |
445 | 0 | visitor: V, |
446 | 0 | ) -> Result<V::Value, Self::Error> |
447 | 0 | where |
448 | 0 | V: de::Visitor<'de>, |
449 | 0 | { |
450 | 0 | let _ = name; |
451 | 0 | let _ = variants; |
452 | 0 | visitor.visit_enum(self) |
453 | 0 | } |
454 | | |
455 | | forward_to_deserialize_any! { |
456 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
457 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
458 | | tuple_struct map struct identifier ignored_any |
459 | | } |
460 | | } |
461 | | |
462 | | impl<'de, 'a, E> de::EnumAccess<'de> for StrDeserializer<'a, E> |
463 | | where |
464 | | E: de::Error, |
465 | | { |
466 | | type Error = E; |
467 | | type Variant = private::UnitOnly<E>; |
468 | | |
469 | 0 | fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> |
470 | 0 | where |
471 | 0 | T: de::DeserializeSeed<'de>, |
472 | 0 | { |
473 | 0 | seed.deserialize(self).map(private::unit_only) |
474 | 0 | } |
475 | | } |
476 | | |
477 | | impl<'a, E> Debug for StrDeserializer<'a, E> { |
478 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
479 | 0 | formatter |
480 | 0 | .debug_struct("StrDeserializer") |
481 | 0 | .field("value", &self.value) |
482 | 0 | .finish() |
483 | 0 | } |
484 | | } |
485 | | |
486 | | //////////////////////////////////////////////////////////////////////////////// |
487 | | |
488 | | /// A deserializer holding a `&str` with a lifetime tied to another |
489 | | /// deserializer. |
490 | | pub struct BorrowedStrDeserializer<'de, E> { |
491 | | value: &'de str, |
492 | | marker: PhantomData<E>, |
493 | | } |
494 | | |
495 | | impl_copy_clone!(BorrowedStrDeserializer<'de>); |
496 | | |
497 | | impl<'de, E> BorrowedStrDeserializer<'de, E> { |
498 | | /// Create a new borrowed deserializer from the given string. |
499 | 0 | pub fn new(value: &'de str) -> BorrowedStrDeserializer<'de, E> { |
500 | 0 | BorrowedStrDeserializer { |
501 | 0 | value: value, |
502 | 0 | marker: PhantomData, |
503 | 0 | } |
504 | 0 | } |
505 | | } |
506 | | |
507 | | impl<'de, E> de::Deserializer<'de> for BorrowedStrDeserializer<'de, E> |
508 | | where |
509 | | E: de::Error, |
510 | | { |
511 | | type Error = E; |
512 | | |
513 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
514 | 0 | where |
515 | 0 | V: de::Visitor<'de>, |
516 | 0 | { |
517 | 0 | visitor.visit_borrowed_str(self.value) |
518 | 0 | } |
519 | | |
520 | 0 | fn deserialize_enum<V>( |
521 | 0 | self, |
522 | 0 | name: &str, |
523 | 0 | variants: &'static [&'static str], |
524 | 0 | visitor: V, |
525 | 0 | ) -> Result<V::Value, Self::Error> |
526 | 0 | where |
527 | 0 | V: de::Visitor<'de>, |
528 | 0 | { |
529 | 0 | let _ = name; |
530 | 0 | let _ = variants; |
531 | 0 | visitor.visit_enum(self) |
532 | 0 | } |
533 | | |
534 | | forward_to_deserialize_any! { |
535 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
536 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
537 | | tuple_struct map struct identifier ignored_any |
538 | | } |
539 | | } |
540 | | |
541 | | impl<'de, E> de::EnumAccess<'de> for BorrowedStrDeserializer<'de, E> |
542 | | where |
543 | | E: de::Error, |
544 | | { |
545 | | type Error = E; |
546 | | type Variant = private::UnitOnly<E>; |
547 | | |
548 | 0 | fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> |
549 | 0 | where |
550 | 0 | T: de::DeserializeSeed<'de>, |
551 | 0 | { |
552 | 0 | seed.deserialize(self).map(private::unit_only) |
553 | 0 | } |
554 | | } |
555 | | |
556 | | impl<'de, E> Debug for BorrowedStrDeserializer<'de, E> { |
557 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
558 | 0 | formatter |
559 | 0 | .debug_struct("BorrowedStrDeserializer") |
560 | 0 | .field("value", &self.value) |
561 | 0 | .finish() |
562 | 0 | } |
563 | | } |
564 | | |
565 | | //////////////////////////////////////////////////////////////////////////////// |
566 | | |
567 | | /// A deserializer holding a `String`. |
568 | | #[cfg(any(feature = "std", feature = "alloc"))] |
569 | | pub struct StringDeserializer<E> { |
570 | | value: String, |
571 | | marker: PhantomData<E>, |
572 | | } |
573 | | |
574 | | #[cfg(any(feature = "std", feature = "alloc"))] |
575 | | impl<E> Clone for StringDeserializer<E> { |
576 | 0 | fn clone(&self) -> Self { |
577 | 0 | StringDeserializer { |
578 | 0 | value: self.value.clone(), |
579 | 0 | marker: PhantomData, |
580 | 0 | } |
581 | 0 | } |
582 | | } |
583 | | |
584 | | #[cfg(any(feature = "std", feature = "alloc"))] |
585 | | impl<'de, E> IntoDeserializer<'de, E> for String |
586 | | where |
587 | | E: de::Error, |
588 | | { |
589 | | type Deserializer = StringDeserializer<E>; |
590 | | |
591 | 0 | fn into_deserializer(self) -> StringDeserializer<E> { |
592 | 0 | StringDeserializer::new(self) |
593 | 0 | } |
594 | | } |
595 | | |
596 | | #[cfg(any(feature = "std", feature = "alloc"))] |
597 | | impl<E> StringDeserializer<E> { |
598 | | #[allow(missing_docs)] |
599 | 0 | pub fn new(value: String) -> Self { |
600 | 0 | StringDeserializer { |
601 | 0 | value: value, |
602 | 0 | marker: PhantomData, |
603 | 0 | } |
604 | 0 | } |
605 | | } |
606 | | |
607 | | #[cfg(any(feature = "std", feature = "alloc"))] |
608 | | impl<'de, E> de::Deserializer<'de> for StringDeserializer<E> |
609 | | where |
610 | | E: de::Error, |
611 | | { |
612 | | type Error = E; |
613 | | |
614 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
615 | 0 | where |
616 | 0 | V: de::Visitor<'de>, |
617 | 0 | { |
618 | 0 | visitor.visit_string(self.value) |
619 | 0 | } |
620 | | |
621 | 0 | fn deserialize_enum<V>( |
622 | 0 | self, |
623 | 0 | name: &str, |
624 | 0 | variants: &'static [&'static str], |
625 | 0 | visitor: V, |
626 | 0 | ) -> Result<V::Value, Self::Error> |
627 | 0 | where |
628 | 0 | V: de::Visitor<'de>, |
629 | 0 | { |
630 | 0 | let _ = name; |
631 | 0 | let _ = variants; |
632 | 0 | visitor.visit_enum(self) |
633 | 0 | } |
634 | | |
635 | | forward_to_deserialize_any! { |
636 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
637 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
638 | | tuple_struct map struct identifier ignored_any |
639 | | } |
640 | | } |
641 | | |
642 | | #[cfg(any(feature = "std", feature = "alloc"))] |
643 | | impl<'de, E> de::EnumAccess<'de> for StringDeserializer<E> |
644 | | where |
645 | | E: de::Error, |
646 | | { |
647 | | type Error = E; |
648 | | type Variant = private::UnitOnly<E>; |
649 | | |
650 | 0 | fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> |
651 | 0 | where |
652 | 0 | T: de::DeserializeSeed<'de>, |
653 | 0 | { |
654 | 0 | seed.deserialize(self).map(private::unit_only) |
655 | 0 | } |
656 | | } |
657 | | |
658 | | #[cfg(any(feature = "std", feature = "alloc"))] |
659 | | impl<E> Debug for StringDeserializer<E> { |
660 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
661 | 0 | formatter |
662 | 0 | .debug_struct("StringDeserializer") |
663 | 0 | .field("value", &self.value) |
664 | 0 | .finish() |
665 | 0 | } |
666 | | } |
667 | | |
668 | | //////////////////////////////////////////////////////////////////////////////// |
669 | | |
670 | | /// A deserializer holding a `Cow<str>`. |
671 | | #[cfg(any(feature = "std", feature = "alloc"))] |
672 | | pub struct CowStrDeserializer<'a, E> { |
673 | | value: Cow<'a, str>, |
674 | | marker: PhantomData<E>, |
675 | | } |
676 | | |
677 | | #[cfg(any(feature = "std", feature = "alloc"))] |
678 | | impl<'a, E> Clone for CowStrDeserializer<'a, E> { |
679 | 0 | fn clone(&self) -> Self { |
680 | 0 | CowStrDeserializer { |
681 | 0 | value: self.value.clone(), |
682 | 0 | marker: PhantomData, |
683 | 0 | } |
684 | 0 | } |
685 | | } |
686 | | |
687 | | #[cfg(any(feature = "std", feature = "alloc"))] |
688 | | impl<'de, 'a, E> IntoDeserializer<'de, E> for Cow<'a, str> |
689 | | where |
690 | | E: de::Error, |
691 | | { |
692 | | type Deserializer = CowStrDeserializer<'a, E>; |
693 | | |
694 | 0 | fn into_deserializer(self) -> CowStrDeserializer<'a, E> { |
695 | 0 | CowStrDeserializer::new(self) |
696 | 0 | } |
697 | | } |
698 | | |
699 | | #[cfg(any(feature = "std", feature = "alloc"))] |
700 | | impl<'a, E> CowStrDeserializer<'a, E> { |
701 | | #[allow(missing_docs)] |
702 | 0 | pub fn new(value: Cow<'a, str>) -> Self { |
703 | 0 | CowStrDeserializer { |
704 | 0 | value: value, |
705 | 0 | marker: PhantomData, |
706 | 0 | } |
707 | 0 | } |
708 | | } |
709 | | |
710 | | #[cfg(any(feature = "std", feature = "alloc"))] |
711 | | impl<'de, 'a, E> de::Deserializer<'de> for CowStrDeserializer<'a, E> |
712 | | where |
713 | | E: de::Error, |
714 | | { |
715 | | type Error = E; |
716 | | |
717 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
718 | 0 | where |
719 | 0 | V: de::Visitor<'de>, |
720 | 0 | { |
721 | 0 | match self.value { |
722 | 0 | Cow::Borrowed(string) => visitor.visit_str(string), |
723 | 0 | Cow::Owned(string) => visitor.visit_string(string), |
724 | | } |
725 | 0 | } |
726 | | |
727 | 0 | fn deserialize_enum<V>( |
728 | 0 | self, |
729 | 0 | name: &str, |
730 | 0 | variants: &'static [&'static str], |
731 | 0 | visitor: V, |
732 | 0 | ) -> Result<V::Value, Self::Error> |
733 | 0 | where |
734 | 0 | V: de::Visitor<'de>, |
735 | 0 | { |
736 | 0 | let _ = name; |
737 | 0 | let _ = variants; |
738 | 0 | visitor.visit_enum(self) |
739 | 0 | } |
740 | | |
741 | | forward_to_deserialize_any! { |
742 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
743 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
744 | | tuple_struct map struct identifier ignored_any |
745 | | } |
746 | | } |
747 | | |
748 | | #[cfg(any(feature = "std", feature = "alloc"))] |
749 | | impl<'de, 'a, E> de::EnumAccess<'de> for CowStrDeserializer<'a, E> |
750 | | where |
751 | | E: de::Error, |
752 | | { |
753 | | type Error = E; |
754 | | type Variant = private::UnitOnly<E>; |
755 | | |
756 | 0 | fn variant_seed<T>(self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> |
757 | 0 | where |
758 | 0 | T: de::DeserializeSeed<'de>, |
759 | 0 | { |
760 | 0 | seed.deserialize(self).map(private::unit_only) |
761 | 0 | } |
762 | | } |
763 | | |
764 | | #[cfg(any(feature = "std", feature = "alloc"))] |
765 | | impl<'a, E> Debug for CowStrDeserializer<'a, E> { |
766 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
767 | 0 | formatter |
768 | 0 | .debug_struct("CowStrDeserializer") |
769 | 0 | .field("value", &self.value) |
770 | 0 | .finish() |
771 | 0 | } |
772 | | } |
773 | | |
774 | | //////////////////////////////////////////////////////////////////////////////// |
775 | | |
776 | | /// A deserializer holding a `&[u8]`. Always calls [`Visitor::visit_bytes`]. |
777 | | pub struct BytesDeserializer<'a, E> { |
778 | | value: &'a [u8], |
779 | | marker: PhantomData<E>, |
780 | | } |
781 | | |
782 | | impl<'a, E> BytesDeserializer<'a, E> { |
783 | | /// Create a new deserializer from the given bytes. |
784 | 0 | pub fn new(value: &'a [u8]) -> Self { |
785 | 0 | BytesDeserializer { |
786 | 0 | value: value, |
787 | 0 | marker: PhantomData, |
788 | 0 | } |
789 | 0 | } |
790 | | } |
791 | | |
792 | | impl_copy_clone!(BytesDeserializer<'a>); |
793 | | |
794 | | impl<'de, 'a, E> IntoDeserializer<'de, E> for &'a [u8] |
795 | | where |
796 | | E: de::Error, |
797 | | { |
798 | | type Deserializer = BytesDeserializer<'a, E>; |
799 | | |
800 | 0 | fn into_deserializer(self) -> BytesDeserializer<'a, E> { |
801 | 0 | BytesDeserializer::new(self) |
802 | 0 | } |
803 | | } |
804 | | |
805 | | impl<'de, 'a, E> Deserializer<'de> for BytesDeserializer<'a, E> |
806 | | where |
807 | | E: de::Error, |
808 | | { |
809 | | type Error = E; |
810 | | |
811 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
812 | 0 | where |
813 | 0 | V: Visitor<'de>, |
814 | 0 | { |
815 | 0 | visitor.visit_bytes(self.value) |
816 | 0 | } |
817 | | |
818 | | forward_to_deserialize_any! { |
819 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
820 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
821 | | tuple_struct map struct enum identifier ignored_any |
822 | | } |
823 | | } |
824 | | |
825 | | impl<'a, E> Debug for BytesDeserializer<'a, E> { |
826 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
827 | 0 | formatter |
828 | 0 | .debug_struct("BytesDeserializer") |
829 | 0 | .field("value", &self.value) |
830 | 0 | .finish() |
831 | 0 | } |
832 | | } |
833 | | |
834 | | /// A deserializer holding a `&[u8]` with a lifetime tied to another |
835 | | /// deserializer. Always calls [`Visitor::visit_borrowed_bytes`]. |
836 | | pub struct BorrowedBytesDeserializer<'de, E> { |
837 | | value: &'de [u8], |
838 | | marker: PhantomData<E>, |
839 | | } |
840 | | |
841 | | impl<'de, E> BorrowedBytesDeserializer<'de, E> { |
842 | | /// Create a new borrowed deserializer from the given borrowed bytes. |
843 | 0 | pub fn new(value: &'de [u8]) -> Self { |
844 | 0 | BorrowedBytesDeserializer { |
845 | 0 | value: value, |
846 | 0 | marker: PhantomData, |
847 | 0 | } |
848 | 0 | } |
849 | | } |
850 | | |
851 | | impl_copy_clone!(BorrowedBytesDeserializer<'de>); |
852 | | |
853 | | impl<'de, E> Deserializer<'de> for BorrowedBytesDeserializer<'de, E> |
854 | | where |
855 | | E: de::Error, |
856 | | { |
857 | | type Error = E; |
858 | | |
859 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
860 | 0 | where |
861 | 0 | V: Visitor<'de>, |
862 | 0 | { |
863 | 0 | visitor.visit_borrowed_bytes(self.value) |
864 | 0 | } |
865 | | |
866 | | forward_to_deserialize_any! { |
867 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
868 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
869 | | tuple_struct map struct enum identifier ignored_any |
870 | | } |
871 | | } |
872 | | |
873 | | impl<'de, E> Debug for BorrowedBytesDeserializer<'de, E> { |
874 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
875 | 0 | formatter |
876 | 0 | .debug_struct("BorrowedBytesDeserializer") |
877 | 0 | .field("value", &self.value) |
878 | 0 | .finish() |
879 | 0 | } |
880 | | } |
881 | | |
882 | | //////////////////////////////////////////////////////////////////////////////// |
883 | | |
884 | | /// A deserializer that iterates over a sequence. |
885 | 0 | #[derive(Clone)] |
886 | | pub struct SeqDeserializer<I, E> { |
887 | | iter: iter::Fuse<I>, |
888 | | count: usize, |
889 | | marker: PhantomData<E>, |
890 | | } |
891 | | |
892 | | impl<I, E> SeqDeserializer<I, E> |
893 | | where |
894 | | I: Iterator, |
895 | | { |
896 | | /// Construct a new `SeqDeserializer<I, E>`. |
897 | 0 | pub fn new(iter: I) -> Self { |
898 | 0 | SeqDeserializer { |
899 | 0 | iter: iter.fuse(), |
900 | 0 | count: 0, |
901 | 0 | marker: PhantomData, |
902 | 0 | } |
903 | 0 | } |
904 | | } |
905 | | |
906 | | impl<I, E> SeqDeserializer<I, E> |
907 | | where |
908 | | I: Iterator, |
909 | | E: de::Error, |
910 | | { |
911 | | /// Check for remaining elements after passing a `SeqDeserializer` to |
912 | | /// `Visitor::visit_seq`. |
913 | 0 | pub fn end(self) -> Result<(), E> { |
914 | 0 | let remaining = self.iter.count(); |
915 | 0 | if remaining == 0 { |
916 | 0 | Ok(()) |
917 | | } else { |
918 | | // First argument is the number of elements in the data, second |
919 | | // argument is the number of elements expected by the Deserialize. |
920 | 0 | Err(de::Error::invalid_length( |
921 | 0 | self.count + remaining, |
922 | 0 | &ExpectedInSeq(self.count), |
923 | 0 | )) |
924 | | } |
925 | 0 | } |
926 | | } |
927 | | |
928 | | impl<'de, I, T, E> de::Deserializer<'de> for SeqDeserializer<I, E> |
929 | | where |
930 | | I: Iterator<Item = T>, |
931 | | T: IntoDeserializer<'de, E>, |
932 | | E: de::Error, |
933 | | { |
934 | | type Error = E; |
935 | | |
936 | 0 | fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error> |
937 | 0 | where |
938 | 0 | V: de::Visitor<'de>, |
939 | 0 | { |
940 | 0 | let v = try!(visitor.visit_seq(&mut self)); |
941 | 0 | try!(self.end()); |
942 | 0 | Ok(v) |
943 | 0 | } |
944 | | |
945 | | forward_to_deserialize_any! { |
946 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
947 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
948 | | tuple_struct map struct enum identifier ignored_any |
949 | | } |
950 | | } |
951 | | |
952 | | impl<'de, I, T, E> de::SeqAccess<'de> for SeqDeserializer<I, E> |
953 | | where |
954 | | I: Iterator<Item = T>, |
955 | | T: IntoDeserializer<'de, E>, |
956 | | E: de::Error, |
957 | | { |
958 | | type Error = E; |
959 | | |
960 | 0 | fn next_element_seed<V>(&mut self, seed: V) -> Result<Option<V::Value>, Self::Error> |
961 | 0 | where |
962 | 0 | V: de::DeserializeSeed<'de>, |
963 | 0 | { |
964 | 0 | match self.iter.next() { |
965 | 0 | Some(value) => { |
966 | 0 | self.count += 1; |
967 | 0 | seed.deserialize(value.into_deserializer()).map(Some) |
968 | | } |
969 | 0 | None => Ok(None), |
970 | | } |
971 | 0 | } |
972 | | |
973 | 0 | fn size_hint(&self) -> Option<usize> { |
974 | 0 | size_hint::from_bounds(&self.iter) |
975 | 0 | } |
976 | | } |
977 | | |
978 | | struct ExpectedInSeq(usize); |
979 | | |
980 | | impl Expected for ExpectedInSeq { |
981 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
982 | 0 | if self.0 == 1 { |
983 | 0 | write!(formatter, "1 element in sequence") |
984 | | } else { |
985 | 0 | write!(formatter, "{} elements in sequence", self.0) |
986 | | } |
987 | 0 | } |
988 | | } |
989 | | |
990 | | impl<I, E> Debug for SeqDeserializer<I, E> |
991 | | where |
992 | | I: Debug, |
993 | | { |
994 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
995 | 0 | formatter |
996 | 0 | .debug_struct("SeqDeserializer") |
997 | 0 | .field("iter", &self.iter) |
998 | 0 | .field("count", &self.count) |
999 | 0 | .finish() |
1000 | 0 | } |
1001 | | } |
1002 | | |
1003 | | //////////////////////////////////////////////////////////////////////////////// |
1004 | | |
1005 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1006 | | impl<'de, T, E> IntoDeserializer<'de, E> for Vec<T> |
1007 | | where |
1008 | | T: IntoDeserializer<'de, E>, |
1009 | | E: de::Error, |
1010 | | { |
1011 | | type Deserializer = SeqDeserializer<<Self as IntoIterator>::IntoIter, E>; |
1012 | | |
1013 | 0 | fn into_deserializer(self) -> Self::Deserializer { |
1014 | 0 | SeqDeserializer::new(self.into_iter()) |
1015 | 0 | } |
1016 | | } |
1017 | | |
1018 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1019 | | impl<'de, T, E> IntoDeserializer<'de, E> for BTreeSet<T> |
1020 | | where |
1021 | | T: IntoDeserializer<'de, E> + Eq + Ord, |
1022 | | E: de::Error, |
1023 | | { |
1024 | | type Deserializer = SeqDeserializer<<Self as IntoIterator>::IntoIter, E>; |
1025 | | |
1026 | 0 | fn into_deserializer(self) -> Self::Deserializer { |
1027 | 0 | SeqDeserializer::new(self.into_iter()) |
1028 | 0 | } |
1029 | | } |
1030 | | |
1031 | | #[cfg(feature = "std")] |
1032 | | impl<'de, T, S, E> IntoDeserializer<'de, E> for HashSet<T, S> |
1033 | | where |
1034 | | T: IntoDeserializer<'de, E> + Eq + Hash, |
1035 | | S: BuildHasher, |
1036 | | E: de::Error, |
1037 | | { |
1038 | | type Deserializer = SeqDeserializer<<Self as IntoIterator>::IntoIter, E>; |
1039 | | |
1040 | 0 | fn into_deserializer(self) -> Self::Deserializer { |
1041 | 0 | SeqDeserializer::new(self.into_iter()) |
1042 | 0 | } |
1043 | | } |
1044 | | |
1045 | | //////////////////////////////////////////////////////////////////////////////// |
1046 | | |
1047 | | /// A deserializer holding a `SeqAccess`. |
1048 | 0 | #[derive(Clone, Debug)] |
1049 | | pub struct SeqAccessDeserializer<A> { |
1050 | | seq: A, |
1051 | | } |
1052 | | |
1053 | | impl<A> SeqAccessDeserializer<A> { |
1054 | | /// Construct a new `SeqAccessDeserializer<A>`. |
1055 | 0 | pub fn new(seq: A) -> Self { |
1056 | 0 | SeqAccessDeserializer { seq: seq } |
1057 | 0 | } |
1058 | | } |
1059 | | |
1060 | | impl<'de, A> de::Deserializer<'de> for SeqAccessDeserializer<A> |
1061 | | where |
1062 | | A: de::SeqAccess<'de>, |
1063 | | { |
1064 | | type Error = A::Error; |
1065 | | |
1066 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1067 | 0 | where |
1068 | 0 | V: de::Visitor<'de>, |
1069 | 0 | { |
1070 | 0 | visitor.visit_seq(self.seq) |
1071 | 0 | } |
1072 | | |
1073 | | forward_to_deserialize_any! { |
1074 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
1075 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
1076 | | tuple_struct map struct enum identifier ignored_any |
1077 | | } |
1078 | | } |
1079 | | |
1080 | | //////////////////////////////////////////////////////////////////////////////// |
1081 | | |
1082 | | /// A deserializer that iterates over a map. |
1083 | | pub struct MapDeserializer<'de, I, E> |
1084 | | where |
1085 | | I: Iterator, |
1086 | | I::Item: private::Pair, |
1087 | | { |
1088 | | iter: iter::Fuse<I>, |
1089 | | value: Option<Second<I::Item>>, |
1090 | | count: usize, |
1091 | | lifetime: PhantomData<&'de ()>, |
1092 | | error: PhantomData<E>, |
1093 | | } |
1094 | | |
1095 | | impl<'de, I, E> MapDeserializer<'de, I, E> |
1096 | | where |
1097 | | I: Iterator, |
1098 | | I::Item: private::Pair, |
1099 | | { |
1100 | | /// Construct a new `MapDeserializer<I, E>`. |
1101 | 0 | pub fn new(iter: I) -> Self { |
1102 | 0 | MapDeserializer { |
1103 | 0 | iter: iter.fuse(), |
1104 | 0 | value: None, |
1105 | 0 | count: 0, |
1106 | 0 | lifetime: PhantomData, |
1107 | 0 | error: PhantomData, |
1108 | 0 | } |
1109 | 0 | } |
1110 | | } |
1111 | | |
1112 | | impl<'de, I, E> MapDeserializer<'de, I, E> |
1113 | | where |
1114 | | I: Iterator, |
1115 | | I::Item: private::Pair, |
1116 | | E: de::Error, |
1117 | | { |
1118 | | /// Check for remaining elements after passing a `MapDeserializer` to |
1119 | | /// `Visitor::visit_map`. |
1120 | 0 | pub fn end(self) -> Result<(), E> { |
1121 | 0 | let remaining = self.iter.count(); |
1122 | 0 | if remaining == 0 { |
1123 | 0 | Ok(()) |
1124 | | } else { |
1125 | | // First argument is the number of elements in the data, second |
1126 | | // argument is the number of elements expected by the Deserialize. |
1127 | 0 | Err(de::Error::invalid_length( |
1128 | 0 | self.count + remaining, |
1129 | 0 | &ExpectedInMap(self.count), |
1130 | 0 | )) |
1131 | | } |
1132 | 0 | } |
1133 | | } |
1134 | | |
1135 | | impl<'de, I, E> MapDeserializer<'de, I, E> |
1136 | | where |
1137 | | I: Iterator, |
1138 | | I::Item: private::Pair, |
1139 | | { |
1140 | 0 | fn next_pair(&mut self) -> Option<(First<I::Item>, Second<I::Item>)> { |
1141 | 0 | match self.iter.next() { |
1142 | 0 | Some(kv) => { |
1143 | 0 | self.count += 1; |
1144 | 0 | Some(private::Pair::split(kv)) |
1145 | | } |
1146 | 0 | None => None, |
1147 | | } |
1148 | 0 | } |
1149 | | } |
1150 | | |
1151 | | impl<'de, I, E> de::Deserializer<'de> for MapDeserializer<'de, I, E> |
1152 | | where |
1153 | | I: Iterator, |
1154 | | I::Item: private::Pair, |
1155 | | First<I::Item>: IntoDeserializer<'de, E>, |
1156 | | Second<I::Item>: IntoDeserializer<'de, E>, |
1157 | | E: de::Error, |
1158 | | { |
1159 | | type Error = E; |
1160 | | |
1161 | 0 | fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error> |
1162 | 0 | where |
1163 | 0 | V: de::Visitor<'de>, |
1164 | 0 | { |
1165 | 0 | let value = try!(visitor.visit_map(&mut self)); |
1166 | 0 | try!(self.end()); |
1167 | 0 | Ok(value) |
1168 | 0 | } |
1169 | | |
1170 | 0 | fn deserialize_seq<V>(mut self, visitor: V) -> Result<V::Value, Self::Error> |
1171 | 0 | where |
1172 | 0 | V: de::Visitor<'de>, |
1173 | 0 | { |
1174 | 0 | let value = try!(visitor.visit_seq(&mut self)); |
1175 | 0 | try!(self.end()); |
1176 | 0 | Ok(value) |
1177 | 0 | } |
1178 | | |
1179 | 0 | fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error> |
1180 | 0 | where |
1181 | 0 | V: de::Visitor<'de>, |
1182 | 0 | { |
1183 | 0 | let _ = len; |
1184 | 0 | self.deserialize_seq(visitor) |
1185 | 0 | } |
1186 | | |
1187 | | forward_to_deserialize_any! { |
1188 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
1189 | | bytes byte_buf option unit unit_struct newtype_struct tuple_struct map |
1190 | | struct enum identifier ignored_any |
1191 | | } |
1192 | | } |
1193 | | |
1194 | | impl<'de, I, E> de::MapAccess<'de> for MapDeserializer<'de, I, E> |
1195 | | where |
1196 | | I: Iterator, |
1197 | | I::Item: private::Pair, |
1198 | | First<I::Item>: IntoDeserializer<'de, E>, |
1199 | | Second<I::Item>: IntoDeserializer<'de, E>, |
1200 | | E: de::Error, |
1201 | | { |
1202 | | type Error = E; |
1203 | | |
1204 | 0 | fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> |
1205 | 0 | where |
1206 | 0 | T: de::DeserializeSeed<'de>, |
1207 | 0 | { |
1208 | 0 | match self.next_pair() { |
1209 | 0 | Some((key, value)) => { |
1210 | 0 | self.value = Some(value); |
1211 | 0 | seed.deserialize(key.into_deserializer()).map(Some) |
1212 | | } |
1213 | 0 | None => Ok(None), |
1214 | | } |
1215 | 0 | } |
1216 | | |
1217 | 0 | fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error> |
1218 | 0 | where |
1219 | 0 | T: de::DeserializeSeed<'de>, |
1220 | 0 | { |
1221 | 0 | let value = self.value.take(); |
1222 | 0 | // Panic because this indicates a bug in the program rather than an |
1223 | 0 | // expected failure. |
1224 | 0 | let value = value.expect("MapAccess::next_value called before next_key"); |
1225 | 0 | seed.deserialize(value.into_deserializer()) |
1226 | 0 | } |
1227 | | |
1228 | 0 | fn next_entry_seed<TK, TV>( |
1229 | 0 | &mut self, |
1230 | 0 | kseed: TK, |
1231 | 0 | vseed: TV, |
1232 | 0 | ) -> Result<Option<(TK::Value, TV::Value)>, Self::Error> |
1233 | 0 | where |
1234 | 0 | TK: de::DeserializeSeed<'de>, |
1235 | 0 | TV: de::DeserializeSeed<'de>, |
1236 | 0 | { |
1237 | 0 | match self.next_pair() { |
1238 | 0 | Some((key, value)) => { |
1239 | 0 | let key = try!(kseed.deserialize(key.into_deserializer())); |
1240 | 0 | let value = try!(vseed.deserialize(value.into_deserializer())); |
1241 | 0 | Ok(Some((key, value))) |
1242 | | } |
1243 | 0 | None => Ok(None), |
1244 | | } |
1245 | 0 | } |
1246 | | |
1247 | 0 | fn size_hint(&self) -> Option<usize> { |
1248 | 0 | size_hint::from_bounds(&self.iter) |
1249 | 0 | } |
1250 | | } |
1251 | | |
1252 | | impl<'de, I, E> de::SeqAccess<'de> for MapDeserializer<'de, I, E> |
1253 | | where |
1254 | | I: Iterator, |
1255 | | I::Item: private::Pair, |
1256 | | First<I::Item>: IntoDeserializer<'de, E>, |
1257 | | Second<I::Item>: IntoDeserializer<'de, E>, |
1258 | | E: de::Error, |
1259 | | { |
1260 | | type Error = E; |
1261 | | |
1262 | 0 | fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> |
1263 | 0 | where |
1264 | 0 | T: de::DeserializeSeed<'de>, |
1265 | 0 | { |
1266 | 0 | match self.next_pair() { |
1267 | 0 | Some((k, v)) => { |
1268 | 0 | let de = PairDeserializer(k, v, PhantomData); |
1269 | 0 | seed.deserialize(de).map(Some) |
1270 | | } |
1271 | 0 | None => Ok(None), |
1272 | | } |
1273 | 0 | } |
1274 | | |
1275 | 0 | fn size_hint(&self) -> Option<usize> { |
1276 | 0 | size_hint::from_bounds(&self.iter) |
1277 | 0 | } |
1278 | | } |
1279 | | |
1280 | | // Cannot #[derive(Clone)] because of the bound `Second<I::Item>: Clone`. |
1281 | | impl<'de, I, E> Clone for MapDeserializer<'de, I, E> |
1282 | | where |
1283 | | I: Iterator + Clone, |
1284 | | I::Item: private::Pair, |
1285 | | Second<I::Item>: Clone, |
1286 | | { |
1287 | 0 | fn clone(&self) -> Self { |
1288 | 0 | MapDeserializer { |
1289 | 0 | iter: self.iter.clone(), |
1290 | 0 | value: self.value.clone(), |
1291 | 0 | count: self.count, |
1292 | 0 | lifetime: self.lifetime, |
1293 | 0 | error: self.error, |
1294 | 0 | } |
1295 | 0 | } |
1296 | | } |
1297 | | |
1298 | | impl<'de, I, E> Debug for MapDeserializer<'de, I, E> |
1299 | | where |
1300 | | I: Iterator + Debug, |
1301 | | I::Item: private::Pair, |
1302 | | Second<I::Item>: Debug, |
1303 | | { |
1304 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
1305 | 0 | formatter |
1306 | 0 | .debug_struct("MapDeserializer") |
1307 | 0 | .field("iter", &self.iter) |
1308 | 0 | .field("value", &self.value) |
1309 | 0 | .field("count", &self.count) |
1310 | 0 | .finish() |
1311 | 0 | } |
1312 | | } |
1313 | | |
1314 | | // Used in the `impl SeqAccess for MapDeserializer` to visit the map as a |
1315 | | // sequence of pairs. |
1316 | | struct PairDeserializer<A, B, E>(A, B, PhantomData<E>); |
1317 | | |
1318 | | impl<'de, A, B, E> de::Deserializer<'de> for PairDeserializer<A, B, E> |
1319 | | where |
1320 | | A: IntoDeserializer<'de, E>, |
1321 | | B: IntoDeserializer<'de, E>, |
1322 | | E: de::Error, |
1323 | | { |
1324 | | type Error = E; |
1325 | | |
1326 | | forward_to_deserialize_any! { |
1327 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
1328 | | bytes byte_buf option unit unit_struct newtype_struct tuple_struct map |
1329 | | struct enum identifier ignored_any |
1330 | | } |
1331 | | |
1332 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1333 | 0 | where |
1334 | 0 | V: de::Visitor<'de>, |
1335 | 0 | { |
1336 | 0 | self.deserialize_seq(visitor) |
1337 | 0 | } |
1338 | | |
1339 | 0 | fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1340 | 0 | where |
1341 | 0 | V: de::Visitor<'de>, |
1342 | 0 | { |
1343 | 0 | let mut pair_visitor = PairVisitor(Some(self.0), Some(self.1), PhantomData); |
1344 | 0 | let pair = try!(visitor.visit_seq(&mut pair_visitor)); |
1345 | 0 | if pair_visitor.1.is_none() { |
1346 | 0 | Ok(pair) |
1347 | | } else { |
1348 | 0 | let remaining = pair_visitor.size_hint().unwrap(); |
1349 | 0 | // First argument is the number of elements in the data, second |
1350 | 0 | // argument is the number of elements expected by the Deserialize. |
1351 | 0 | Err(de::Error::invalid_length(2, &ExpectedInSeq(2 - remaining))) |
1352 | | } |
1353 | 0 | } |
1354 | | |
1355 | 0 | fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, Self::Error> |
1356 | 0 | where |
1357 | 0 | V: de::Visitor<'de>, |
1358 | 0 | { |
1359 | 0 | if len == 2 { |
1360 | 0 | self.deserialize_seq(visitor) |
1361 | | } else { |
1362 | | // First argument is the number of elements in the data, second |
1363 | | // argument is the number of elements expected by the Deserialize. |
1364 | 0 | Err(de::Error::invalid_length(2, &ExpectedInSeq(len))) |
1365 | | } |
1366 | 0 | } |
1367 | | } |
1368 | | |
1369 | | struct PairVisitor<A, B, E>(Option<A>, Option<B>, PhantomData<E>); |
1370 | | |
1371 | | impl<'de, A, B, E> de::SeqAccess<'de> for PairVisitor<A, B, E> |
1372 | | where |
1373 | | A: IntoDeserializer<'de, E>, |
1374 | | B: IntoDeserializer<'de, E>, |
1375 | | E: de::Error, |
1376 | | { |
1377 | | type Error = E; |
1378 | | |
1379 | 0 | fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> |
1380 | 0 | where |
1381 | 0 | T: de::DeserializeSeed<'de>, |
1382 | 0 | { |
1383 | 0 | if let Some(k) = self.0.take() { |
1384 | 0 | seed.deserialize(k.into_deserializer()).map(Some) |
1385 | 0 | } else if let Some(v) = self.1.take() { |
1386 | 0 | seed.deserialize(v.into_deserializer()).map(Some) |
1387 | | } else { |
1388 | 0 | Ok(None) |
1389 | | } |
1390 | 0 | } |
1391 | | |
1392 | 0 | fn size_hint(&self) -> Option<usize> { |
1393 | 0 | if self.0.is_some() { |
1394 | 0 | Some(2) |
1395 | 0 | } else if self.1.is_some() { |
1396 | 0 | Some(1) |
1397 | | } else { |
1398 | 0 | Some(0) |
1399 | | } |
1400 | 0 | } |
1401 | | } |
1402 | | |
1403 | | struct ExpectedInMap(usize); |
1404 | | |
1405 | | impl Expected for ExpectedInMap { |
1406 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
1407 | 0 | if self.0 == 1 { |
1408 | 0 | write!(formatter, "1 element in map") |
1409 | | } else { |
1410 | 0 | write!(formatter, "{} elements in map", self.0) |
1411 | | } |
1412 | 0 | } |
1413 | | } |
1414 | | |
1415 | | //////////////////////////////////////////////////////////////////////////////// |
1416 | | |
1417 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1418 | | impl<'de, K, V, E> IntoDeserializer<'de, E> for BTreeMap<K, V> |
1419 | | where |
1420 | | K: IntoDeserializer<'de, E> + Eq + Ord, |
1421 | | V: IntoDeserializer<'de, E>, |
1422 | | E: de::Error, |
1423 | | { |
1424 | | type Deserializer = MapDeserializer<'de, <Self as IntoIterator>::IntoIter, E>; |
1425 | | |
1426 | 0 | fn into_deserializer(self) -> Self::Deserializer { |
1427 | 0 | MapDeserializer::new(self.into_iter()) |
1428 | 0 | } |
1429 | | } |
1430 | | |
1431 | | #[cfg(feature = "std")] |
1432 | | impl<'de, K, V, S, E> IntoDeserializer<'de, E> for HashMap<K, V, S> |
1433 | | where |
1434 | | K: IntoDeserializer<'de, E> + Eq + Hash, |
1435 | | V: IntoDeserializer<'de, E>, |
1436 | | S: BuildHasher, |
1437 | | E: de::Error, |
1438 | | { |
1439 | | type Deserializer = MapDeserializer<'de, <Self as IntoIterator>::IntoIter, E>; |
1440 | | |
1441 | 0 | fn into_deserializer(self) -> Self::Deserializer { |
1442 | 0 | MapDeserializer::new(self.into_iter()) |
1443 | 0 | } |
1444 | | } |
1445 | | |
1446 | | //////////////////////////////////////////////////////////////////////////////// |
1447 | | |
1448 | | /// A deserializer holding a `MapAccess`. |
1449 | 0 | #[derive(Clone, Debug)] |
1450 | | pub struct MapAccessDeserializer<A> { |
1451 | | map: A, |
1452 | | } |
1453 | | |
1454 | | impl<A> MapAccessDeserializer<A> { |
1455 | | /// Construct a new `MapAccessDeserializer<A>`. |
1456 | 0 | pub fn new(map: A) -> Self { |
1457 | 0 | MapAccessDeserializer { map: map } |
1458 | 0 | } |
1459 | | } |
1460 | | |
1461 | | impl<'de, A> de::Deserializer<'de> for MapAccessDeserializer<A> |
1462 | | where |
1463 | | A: de::MapAccess<'de>, |
1464 | | { |
1465 | | type Error = A::Error; |
1466 | | |
1467 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1468 | 0 | where |
1469 | 0 | V: de::Visitor<'de>, |
1470 | 0 | { |
1471 | 0 | visitor.visit_map(self.map) |
1472 | 0 | } |
1473 | | |
1474 | 0 | fn deserialize_enum<V>( |
1475 | 0 | self, |
1476 | 0 | _name: &str, |
1477 | 0 | _variants: &'static [&'static str], |
1478 | 0 | visitor: V, |
1479 | 0 | ) -> Result<V::Value, Self::Error> |
1480 | 0 | where |
1481 | 0 | V: de::Visitor<'de>, |
1482 | 0 | { |
1483 | 0 | visitor.visit_enum(self) |
1484 | 0 | } |
1485 | | |
1486 | | forward_to_deserialize_any! { |
1487 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
1488 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
1489 | | tuple_struct map struct identifier ignored_any |
1490 | | } |
1491 | | } |
1492 | | |
1493 | | impl<'de, A> de::EnumAccess<'de> for MapAccessDeserializer<A> |
1494 | | where |
1495 | | A: de::MapAccess<'de>, |
1496 | | { |
1497 | | type Error = A::Error; |
1498 | | type Variant = private::MapAsEnum<A>; |
1499 | | |
1500 | 0 | fn variant_seed<T>(mut self, seed: T) -> Result<(T::Value, Self::Variant), Self::Error> |
1501 | 0 | where |
1502 | 0 | T: de::DeserializeSeed<'de>, |
1503 | 0 | { |
1504 | 0 | match try!(self.map.next_key_seed(seed)) { |
1505 | 0 | Some(key) => Ok((key, private::map_as_enum(self.map))), |
1506 | 0 | None => Err(de::Error::invalid_type(de::Unexpected::Map, &"enum")), |
1507 | | } |
1508 | 0 | } |
1509 | | } |
1510 | | |
1511 | | //////////////////////////////////////////////////////////////////////////////// |
1512 | | |
1513 | | /// A deserializer holding an `EnumAccess`. |
1514 | 0 | #[derive(Clone, Debug)] |
1515 | | pub struct EnumAccessDeserializer<A> { |
1516 | | access: A, |
1517 | | } |
1518 | | |
1519 | | impl<A> EnumAccessDeserializer<A> { |
1520 | | /// Construct a new `EnumAccessDeserializer<A>`. |
1521 | 0 | pub fn new(access: A) -> Self { |
1522 | 0 | EnumAccessDeserializer { access: access } |
1523 | 0 | } |
1524 | | } |
1525 | | |
1526 | | impl<'de, A> de::Deserializer<'de> for EnumAccessDeserializer<A> |
1527 | | where |
1528 | | A: de::EnumAccess<'de>, |
1529 | | { |
1530 | | type Error = A::Error; |
1531 | | |
1532 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1533 | 0 | where |
1534 | 0 | V: de::Visitor<'de>, |
1535 | 0 | { |
1536 | 0 | visitor.visit_enum(self.access) |
1537 | 0 | } |
1538 | | |
1539 | | forward_to_deserialize_any! { |
1540 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
1541 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
1542 | | tuple_struct map struct enum identifier ignored_any |
1543 | | } |
1544 | | } |
1545 | | |
1546 | | //////////////////////////////////////////////////////////////////////////////// |
1547 | | |
1548 | | mod private { |
1549 | | use lib::*; |
1550 | | |
1551 | | use de::{self, DeserializeSeed, Deserializer, MapAccess, Unexpected, VariantAccess, Visitor}; |
1552 | | |
1553 | | pub struct UnitOnly<E> { |
1554 | | marker: PhantomData<E>, |
1555 | | } |
1556 | | |
1557 | 0 | pub fn unit_only<T, E>(t: T) -> (T, UnitOnly<E>) { |
1558 | 0 | ( |
1559 | 0 | t, |
1560 | 0 | UnitOnly { |
1561 | 0 | marker: PhantomData, |
1562 | 0 | }, |
1563 | 0 | ) |
1564 | 0 | } |
1565 | | |
1566 | | impl<'de, E> de::VariantAccess<'de> for UnitOnly<E> |
1567 | | where |
1568 | | E: de::Error, |
1569 | | { |
1570 | | type Error = E; |
1571 | | |
1572 | 0 | fn unit_variant(self) -> Result<(), Self::Error> { |
1573 | 0 | Ok(()) |
1574 | 0 | } |
1575 | | |
1576 | 0 | fn newtype_variant_seed<T>(self, _seed: T) -> Result<T::Value, Self::Error> |
1577 | 0 | where |
1578 | 0 | T: de::DeserializeSeed<'de>, |
1579 | 0 | { |
1580 | 0 | Err(de::Error::invalid_type( |
1581 | 0 | Unexpected::UnitVariant, |
1582 | 0 | &"newtype variant", |
1583 | 0 | )) |
1584 | 0 | } |
1585 | | |
1586 | 0 | fn tuple_variant<V>(self, _len: usize, _visitor: V) -> Result<V::Value, Self::Error> |
1587 | 0 | where |
1588 | 0 | V: de::Visitor<'de>, |
1589 | 0 | { |
1590 | 0 | Err(de::Error::invalid_type( |
1591 | 0 | Unexpected::UnitVariant, |
1592 | 0 | &"tuple variant", |
1593 | 0 | )) |
1594 | 0 | } |
1595 | | |
1596 | 0 | fn struct_variant<V>( |
1597 | 0 | self, |
1598 | 0 | _fields: &'static [&'static str], |
1599 | 0 | _visitor: V, |
1600 | 0 | ) -> Result<V::Value, Self::Error> |
1601 | 0 | where |
1602 | 0 | V: de::Visitor<'de>, |
1603 | 0 | { |
1604 | 0 | Err(de::Error::invalid_type( |
1605 | 0 | Unexpected::UnitVariant, |
1606 | 0 | &"struct variant", |
1607 | 0 | )) |
1608 | 0 | } |
1609 | | } |
1610 | | |
1611 | | pub struct MapAsEnum<A> { |
1612 | | map: A, |
1613 | | } |
1614 | | |
1615 | 0 | pub fn map_as_enum<A>(map: A) -> MapAsEnum<A> { |
1616 | 0 | MapAsEnum { map: map } |
1617 | 0 | } |
1618 | | |
1619 | | impl<'de, A> VariantAccess<'de> for MapAsEnum<A> |
1620 | | where |
1621 | | A: MapAccess<'de>, |
1622 | | { |
1623 | | type Error = A::Error; |
1624 | | |
1625 | 0 | fn unit_variant(mut self) -> Result<(), Self::Error> { |
1626 | 0 | self.map.next_value() |
1627 | 0 | } |
1628 | | |
1629 | 0 | fn newtype_variant_seed<T>(mut self, seed: T) -> Result<T::Value, Self::Error> |
1630 | 0 | where |
1631 | 0 | T: DeserializeSeed<'de>, |
1632 | 0 | { |
1633 | 0 | self.map.next_value_seed(seed) |
1634 | 0 | } |
1635 | | |
1636 | 0 | fn tuple_variant<V>(mut self, len: usize, visitor: V) -> Result<V::Value, Self::Error> |
1637 | 0 | where |
1638 | 0 | V: Visitor<'de>, |
1639 | 0 | { |
1640 | 0 | self.map.next_value_seed(SeedTupleVariant { |
1641 | 0 | len: len, |
1642 | 0 | visitor: visitor, |
1643 | 0 | }) |
1644 | 0 | } |
1645 | | |
1646 | 0 | fn struct_variant<V>( |
1647 | 0 | mut self, |
1648 | 0 | _fields: &'static [&'static str], |
1649 | 0 | visitor: V, |
1650 | 0 | ) -> Result<V::Value, Self::Error> |
1651 | 0 | where |
1652 | 0 | V: Visitor<'de>, |
1653 | 0 | { |
1654 | 0 | self.map |
1655 | 0 | .next_value_seed(SeedStructVariant { visitor: visitor }) |
1656 | 0 | } |
1657 | | } |
1658 | | |
1659 | | struct SeedTupleVariant<V> { |
1660 | | len: usize, |
1661 | | visitor: V, |
1662 | | } |
1663 | | |
1664 | | impl<'de, V> DeserializeSeed<'de> for SeedTupleVariant<V> |
1665 | | where |
1666 | | V: Visitor<'de>, |
1667 | | { |
1668 | | type Value = V::Value; |
1669 | | |
1670 | 0 | fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
1671 | 0 | where |
1672 | 0 | D: Deserializer<'de>, |
1673 | 0 | { |
1674 | 0 | deserializer.deserialize_tuple(self.len, self.visitor) |
1675 | 0 | } |
1676 | | } |
1677 | | |
1678 | | struct SeedStructVariant<V> { |
1679 | | visitor: V, |
1680 | | } |
1681 | | |
1682 | | impl<'de, V> DeserializeSeed<'de> for SeedStructVariant<V> |
1683 | | where |
1684 | | V: Visitor<'de>, |
1685 | | { |
1686 | | type Value = V::Value; |
1687 | | |
1688 | 0 | fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
1689 | 0 | where |
1690 | 0 | D: Deserializer<'de>, |
1691 | 0 | { |
1692 | 0 | deserializer.deserialize_map(self.visitor) |
1693 | 0 | } |
1694 | | } |
1695 | | |
1696 | | /// Avoid having to restate the generic types on `MapDeserializer`. The |
1697 | | /// `Iterator::Item` contains enough information to figure out K and V. |
1698 | | pub trait Pair { |
1699 | | type First; |
1700 | | type Second; |
1701 | | fn split(self) -> (Self::First, Self::Second); |
1702 | | } |
1703 | | |
1704 | | impl<A, B> Pair for (A, B) { |
1705 | | type First = A; |
1706 | | type Second = B; |
1707 | 0 | fn split(self) -> (A, B) { |
1708 | 0 | self |
1709 | 0 | } |
1710 | | } |
1711 | | |
1712 | | pub type First<T> = <T as Pair>::First; |
1713 | | pub type Second<T> = <T as Pair>::Second; |
1714 | | } |