/rust/registry/src/index.crates.io-6f17d22bba15001f/serde-1.0.152/src/private/de.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use lib::*; |
2 | | |
3 | | use de::value::{BorrowedBytesDeserializer, BytesDeserializer}; |
4 | | use de::{Deserialize, Deserializer, Error, IntoDeserializer, Visitor}; |
5 | | |
6 | | #[cfg(any(feature = "std", feature = "alloc"))] |
7 | | use de::{DeserializeSeed, MapAccess, Unexpected}; |
8 | | |
9 | | #[cfg(any(feature = "std", feature = "alloc"))] |
10 | | pub use self::content::{ |
11 | | Content, ContentDeserializer, ContentRefDeserializer, EnumDeserializer, |
12 | | InternallyTaggedUnitVisitor, TagContentOtherField, TagContentOtherFieldVisitor, |
13 | | TagOrContentField, TagOrContentFieldVisitor, TaggedContentVisitor, UntaggedUnitVisitor, |
14 | | }; |
15 | | |
16 | | pub use seed::InPlaceSeed; |
17 | | |
18 | | /// If the missing field is of type `Option<T>` then treat is as `None`, |
19 | | /// otherwise it is an error. |
20 | 0 | pub fn missing_field<'de, V, E>(field: &'static str) -> Result<V, E> |
21 | 0 | where |
22 | 0 | V: Deserialize<'de>, |
23 | 0 | E: Error, |
24 | 0 | { |
25 | | struct MissingFieldDeserializer<E>(&'static str, PhantomData<E>); |
26 | | |
27 | | impl<'de, E> Deserializer<'de> for MissingFieldDeserializer<E> |
28 | | where |
29 | | E: Error, |
30 | | { |
31 | | type Error = E; |
32 | | |
33 | 0 | fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, E> |
34 | 0 | where |
35 | 0 | V: Visitor<'de>, |
36 | 0 | { |
37 | 0 | Err(Error::missing_field(self.0)) |
38 | 0 | } Unexecuted instantiation: <serde::__private::de::missing_field::MissingFieldDeserializer<serde_json::error::Error> as serde::de::Deserializer>::deserialize_any::<<audio_processor::shape::Format as serde::de::Deserialize>::deserialize::__Visitor> Unexecuted instantiation: <serde::__private::de::missing_field::MissingFieldDeserializer<serde_json::error::Error> as serde::de::Deserializer>::deserialize_any::<<usize as serde::de::Deserialize>::deserialize::PrimitiveVisitor> Unexecuted instantiation: <serde::__private::de::missing_field::MissingFieldDeserializer<serde_json::error::Error> as serde::de::Deserializer>::deserialize_any::<serde::de::impls::PathBufVisitor> Unexecuted instantiation: <serde::__private::de::missing_field::MissingFieldDeserializer<serde_json::error::Error> as serde::de::Deserializer>::deserialize_any::<serde::de::impls::StringVisitor> Unexecuted instantiation: <serde::__private::de::missing_field::MissingFieldDeserializer<serde_json::error::Error> as serde::de::Deserializer>::deserialize_any::<serde::de::impls::BoolVisitor> Unexecuted instantiation: <serde::__private::de::missing_field::MissingFieldDeserializer<serde_json::error::Error> as serde::de::Deserializer>::deserialize_any::<<alloc::vec::Vec<_> as serde::de::Deserialize>::deserialize::VecVisitor<audio_processor::config::Processor>> Unexecuted instantiation: <serde::__private::de::missing_field::MissingFieldDeserializer<serde_json::error::Error> as serde::de::Deserializer>::deserialize_any::<<alloc::vec::Vec<_> as serde::de::Deserialize>::deserialize::VecVisitor<usize>> Unexecuted instantiation: <serde::__private::de::missing_field::MissingFieldDeserializer<serde_json::error::Error> as serde::de::Deserializer>::deserialize_any::<<audio_processor::config::Processor as serde::de::Deserialize>::deserialize::__Visitor> Unexecuted instantiation: <serde::__private::de::missing_field::MissingFieldDeserializer<_> as serde::de::Deserializer>::deserialize_any::<_> |
39 | | |
40 | 0 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, E> |
41 | 0 | where |
42 | 0 | V: Visitor<'de>, |
43 | 0 | { |
44 | 0 | visitor.visit_none() |
45 | 0 | } Unexecuted instantiation: <serde::__private::de::missing_field::MissingFieldDeserializer<serde_json::error::Error> as serde::de::Deserializer>::deserialize_option::<serde::de::impls::OptionVisitor<usize>> Unexecuted instantiation: <serde::__private::de::missing_field::MissingFieldDeserializer<_> as serde::de::Deserializer>::deserialize_option::<_> |
46 | | |
47 | | forward_to_deserialize_any! { |
48 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
49 | | bytes byte_buf unit unit_struct newtype_struct seq tuple |
50 | | tuple_struct map struct enum identifier ignored_any |
51 | | } |
52 | | } |
53 | | |
54 | 0 | let deserializer = MissingFieldDeserializer(field, PhantomData); |
55 | 0 | Deserialize::deserialize(deserializer) |
56 | 0 | } Unexecuted instantiation: serde::__private::de::missing_field::<audio_processor::shape::Format, serde_json::error::Error> Unexecuted instantiation: serde::__private::de::missing_field::<audio_processor::config::Processor, serde_json::error::Error> Unexecuted instantiation: serde::__private::de::missing_field::<usize, serde_json::error::Error> Unexecuted instantiation: serde::__private::de::missing_field::<std::path::PathBuf, serde_json::error::Error> Unexecuted instantiation: serde::__private::de::missing_field::<alloc::string::String, serde_json::error::Error> Unexecuted instantiation: serde::__private::de::missing_field::<alloc::boxed::Box<audio_processor::config::Processor>, serde_json::error::Error> Unexecuted instantiation: serde::__private::de::missing_field::<bool, serde_json::error::Error> Unexecuted instantiation: serde::__private::de::missing_field::<alloc::vec::Vec<audio_processor::config::Processor>, serde_json::error::Error> Unexecuted instantiation: serde::__private::de::missing_field::<alloc::vec::Vec<usize>, serde_json::error::Error> Unexecuted instantiation: serde::__private::de::missing_field::<core::option::Option<usize>, serde_json::error::Error> Unexecuted instantiation: serde::__private::de::missing_field::<_, _> |
57 | | |
58 | | #[cfg(any(feature = "std", feature = "alloc"))] |
59 | 0 | pub fn borrow_cow_str<'de: 'a, 'a, D, R>(deserializer: D) -> Result<R, D::Error> |
60 | 0 | where |
61 | 0 | D: Deserializer<'de>, |
62 | 0 | R: From<Cow<'a, str>>, |
63 | 0 | { |
64 | | struct CowStrVisitor; |
65 | | |
66 | | impl<'a> Visitor<'a> for CowStrVisitor { |
67 | | type Value = Cow<'a, str>; |
68 | | |
69 | 0 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
70 | 0 | formatter.write_str("a string") |
71 | 0 | } |
72 | | |
73 | 0 | fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
74 | 0 | where |
75 | 0 | E: Error, |
76 | 0 | { |
77 | 0 | Ok(Cow::Owned(v.to_owned())) |
78 | 0 | } |
79 | | |
80 | 0 | fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E> |
81 | 0 | where |
82 | 0 | E: Error, |
83 | 0 | { |
84 | 0 | Ok(Cow::Borrowed(v)) |
85 | 0 | } |
86 | | |
87 | 0 | fn visit_string<E>(self, v: String) -> Result<Self::Value, E> |
88 | 0 | where |
89 | 0 | E: Error, |
90 | 0 | { |
91 | 0 | Ok(Cow::Owned(v)) |
92 | 0 | } |
93 | | |
94 | 0 | fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> |
95 | 0 | where |
96 | 0 | E: Error, |
97 | 0 | { |
98 | 0 | match str::from_utf8(v) { |
99 | 0 | Ok(s) => Ok(Cow::Owned(s.to_owned())), |
100 | 0 | Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), |
101 | | } |
102 | 0 | } |
103 | | |
104 | 0 | fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E> |
105 | 0 | where |
106 | 0 | E: Error, |
107 | 0 | { |
108 | 0 | match str::from_utf8(v) { |
109 | 0 | Ok(s) => Ok(Cow::Borrowed(s)), |
110 | 0 | Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)), |
111 | | } |
112 | 0 | } |
113 | | |
114 | 0 | fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> |
115 | 0 | where |
116 | 0 | E: Error, |
117 | 0 | { |
118 | 0 | match String::from_utf8(v) { |
119 | 0 | Ok(s) => Ok(Cow::Owned(s)), |
120 | 0 | Err(e) => Err(Error::invalid_value( |
121 | 0 | Unexpected::Bytes(&e.into_bytes()), |
122 | 0 | &self, |
123 | 0 | )), |
124 | | } |
125 | 0 | } |
126 | | } |
127 | | |
128 | 0 | deserializer.deserialize_str(CowStrVisitor).map(From::from) |
129 | 0 | } |
130 | | |
131 | | #[cfg(any(feature = "std", feature = "alloc"))] |
132 | 0 | pub fn borrow_cow_bytes<'de: 'a, 'a, D, R>(deserializer: D) -> Result<R, D::Error> |
133 | 0 | where |
134 | 0 | D: Deserializer<'de>, |
135 | 0 | R: From<Cow<'a, [u8]>>, |
136 | 0 | { |
137 | | struct CowBytesVisitor; |
138 | | |
139 | | impl<'a> Visitor<'a> for CowBytesVisitor { |
140 | | type Value = Cow<'a, [u8]>; |
141 | | |
142 | 0 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
143 | 0 | formatter.write_str("a byte array") |
144 | 0 | } |
145 | | |
146 | 0 | fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
147 | 0 | where |
148 | 0 | E: Error, |
149 | 0 | { |
150 | 0 | Ok(Cow::Owned(v.as_bytes().to_vec())) |
151 | 0 | } |
152 | | |
153 | 0 | fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E> |
154 | 0 | where |
155 | 0 | E: Error, |
156 | 0 | { |
157 | 0 | Ok(Cow::Borrowed(v.as_bytes())) |
158 | 0 | } |
159 | | |
160 | 0 | fn visit_string<E>(self, v: String) -> Result<Self::Value, E> |
161 | 0 | where |
162 | 0 | E: Error, |
163 | 0 | { |
164 | 0 | Ok(Cow::Owned(v.into_bytes())) |
165 | 0 | } |
166 | | |
167 | 0 | fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> |
168 | 0 | where |
169 | 0 | E: Error, |
170 | 0 | { |
171 | 0 | Ok(Cow::Owned(v.to_vec())) |
172 | 0 | } |
173 | | |
174 | 0 | fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E> |
175 | 0 | where |
176 | 0 | E: Error, |
177 | 0 | { |
178 | 0 | Ok(Cow::Borrowed(v)) |
179 | 0 | } |
180 | | |
181 | 0 | fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E> |
182 | 0 | where |
183 | 0 | E: Error, |
184 | 0 | { |
185 | 0 | Ok(Cow::Owned(v)) |
186 | 0 | } |
187 | | } |
188 | | |
189 | 0 | deserializer |
190 | 0 | .deserialize_bytes(CowBytesVisitor) |
191 | 0 | .map(From::from) |
192 | 0 | } |
193 | | |
194 | | #[cfg(any(feature = "std", feature = "alloc"))] |
195 | | mod content { |
196 | | // This module is private and nothing here should be used outside of |
197 | | // generated code. |
198 | | // |
199 | | // We will iterate on the implementation for a few releases and only have to |
200 | | // worry about backward compatibility for the `untagged` and `tag` attributes |
201 | | // rather than for this entire mechanism. |
202 | | // |
203 | | // This issue is tracking making some of this stuff public: |
204 | | // https://github.com/serde-rs/serde/issues/741 |
205 | | |
206 | | use lib::*; |
207 | | |
208 | | use __private::size_hint; |
209 | | use actually_private; |
210 | | use de::{ |
211 | | self, Deserialize, DeserializeSeed, Deserializer, EnumAccess, Expected, IgnoredAny, |
212 | | MapAccess, SeqAccess, Unexpected, Visitor, |
213 | | }; |
214 | | |
215 | | /// Used from generated code to buffer the contents of the Deserializer when |
216 | | /// deserializing untagged enums and internally tagged enums. |
217 | | /// |
218 | | /// Not public API. Use serde-value instead. |
219 | | #[derive(Debug, Clone)] |
220 | | pub enum Content<'de> { |
221 | | Bool(bool), |
222 | | |
223 | | U8(u8), |
224 | | U16(u16), |
225 | | U32(u32), |
226 | | U64(u64), |
227 | | |
228 | | I8(i8), |
229 | | I16(i16), |
230 | | I32(i32), |
231 | | I64(i64), |
232 | | |
233 | | F32(f32), |
234 | | F64(f64), |
235 | | |
236 | | Char(char), |
237 | | String(String), |
238 | | Str(&'de str), |
239 | | ByteBuf(Vec<u8>), |
240 | | Bytes(&'de [u8]), |
241 | | |
242 | | None, |
243 | | Some(Box<Content<'de>>), |
244 | | |
245 | | Unit, |
246 | | Newtype(Box<Content<'de>>), |
247 | | Seq(Vec<Content<'de>>), |
248 | | Map(Vec<(Content<'de>, Content<'de>)>), |
249 | | } |
250 | | |
251 | | impl<'de> Content<'de> { |
252 | 0 | pub fn as_str(&self) -> Option<&str> { |
253 | 0 | match *self { |
254 | 0 | Content::Str(x) => Some(x), |
255 | 0 | Content::String(ref x) => Some(x), |
256 | 0 | Content::Bytes(x) => str::from_utf8(x).ok(), |
257 | 0 | Content::ByteBuf(ref x) => str::from_utf8(x).ok(), |
258 | 0 | _ => None, |
259 | | } |
260 | 0 | } |
261 | | |
262 | | #[cold] |
263 | 0 | fn unexpected(&self) -> Unexpected { |
264 | 0 | match *self { |
265 | 0 | Content::Bool(b) => Unexpected::Bool(b), |
266 | 0 | Content::U8(n) => Unexpected::Unsigned(n as u64), |
267 | 0 | Content::U16(n) => Unexpected::Unsigned(n as u64), |
268 | 0 | Content::U32(n) => Unexpected::Unsigned(n as u64), |
269 | 0 | Content::U64(n) => Unexpected::Unsigned(n), |
270 | 0 | Content::I8(n) => Unexpected::Signed(n as i64), |
271 | 0 | Content::I16(n) => Unexpected::Signed(n as i64), |
272 | 0 | Content::I32(n) => Unexpected::Signed(n as i64), |
273 | 0 | Content::I64(n) => Unexpected::Signed(n), |
274 | 0 | Content::F32(f) => Unexpected::Float(f as f64), |
275 | 0 | Content::F64(f) => Unexpected::Float(f), |
276 | 0 | Content::Char(c) => Unexpected::Char(c), |
277 | 0 | Content::String(ref s) => Unexpected::Str(s), |
278 | 0 | Content::Str(s) => Unexpected::Str(s), |
279 | 0 | Content::ByteBuf(ref b) => Unexpected::Bytes(b), |
280 | 0 | Content::Bytes(b) => Unexpected::Bytes(b), |
281 | 0 | Content::None | Content::Some(_) => Unexpected::Option, |
282 | 0 | Content::Unit => Unexpected::Unit, |
283 | 0 | Content::Newtype(_) => Unexpected::NewtypeStruct, |
284 | 0 | Content::Seq(_) => Unexpected::Seq, |
285 | 0 | Content::Map(_) => Unexpected::Map, |
286 | | } |
287 | 0 | } |
288 | | } |
289 | | |
290 | | impl<'de> Deserialize<'de> for Content<'de> { |
291 | 0 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
292 | 0 | where |
293 | 0 | D: Deserializer<'de>, |
294 | 0 | { |
295 | 0 | // Untagged and internally tagged enums are only supported in |
296 | 0 | // self-describing formats. |
297 | 0 | let visitor = ContentVisitor { value: PhantomData }; |
298 | 0 | deserializer.__deserialize_content(actually_private::T, visitor) |
299 | 0 | } |
300 | | } |
301 | | |
302 | | struct ContentVisitor<'de> { |
303 | | value: PhantomData<Content<'de>>, |
304 | | } |
305 | | |
306 | | impl<'de> ContentVisitor<'de> { |
307 | 0 | fn new() -> Self { |
308 | 0 | ContentVisitor { value: PhantomData } |
309 | 0 | } |
310 | | } |
311 | | |
312 | | impl<'de> Visitor<'de> for ContentVisitor<'de> { |
313 | | type Value = Content<'de>; |
314 | | |
315 | 0 | fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
316 | 0 | fmt.write_str("any value") |
317 | 0 | } |
318 | | |
319 | 0 | fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F> |
320 | 0 | where |
321 | 0 | F: de::Error, |
322 | 0 | { |
323 | 0 | Ok(Content::Bool(value)) |
324 | 0 | } |
325 | | |
326 | 0 | fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F> |
327 | 0 | where |
328 | 0 | F: de::Error, |
329 | 0 | { |
330 | 0 | Ok(Content::I8(value)) |
331 | 0 | } |
332 | | |
333 | 0 | fn visit_i16<F>(self, value: i16) -> Result<Self::Value, F> |
334 | 0 | where |
335 | 0 | F: de::Error, |
336 | 0 | { |
337 | 0 | Ok(Content::I16(value)) |
338 | 0 | } |
339 | | |
340 | 0 | fn visit_i32<F>(self, value: i32) -> Result<Self::Value, F> |
341 | 0 | where |
342 | 0 | F: de::Error, |
343 | 0 | { |
344 | 0 | Ok(Content::I32(value)) |
345 | 0 | } |
346 | | |
347 | 0 | fn visit_i64<F>(self, value: i64) -> Result<Self::Value, F> |
348 | 0 | where |
349 | 0 | F: de::Error, |
350 | 0 | { |
351 | 0 | Ok(Content::I64(value)) |
352 | 0 | } |
353 | | |
354 | 0 | fn visit_u8<F>(self, value: u8) -> Result<Self::Value, F> |
355 | 0 | where |
356 | 0 | F: de::Error, |
357 | 0 | { |
358 | 0 | Ok(Content::U8(value)) |
359 | 0 | } |
360 | | |
361 | 0 | fn visit_u16<F>(self, value: u16) -> Result<Self::Value, F> |
362 | 0 | where |
363 | 0 | F: de::Error, |
364 | 0 | { |
365 | 0 | Ok(Content::U16(value)) |
366 | 0 | } |
367 | | |
368 | 0 | fn visit_u32<F>(self, value: u32) -> Result<Self::Value, F> |
369 | 0 | where |
370 | 0 | F: de::Error, |
371 | 0 | { |
372 | 0 | Ok(Content::U32(value)) |
373 | 0 | } |
374 | | |
375 | 0 | fn visit_u64<F>(self, value: u64) -> Result<Self::Value, F> |
376 | 0 | where |
377 | 0 | F: de::Error, |
378 | 0 | { |
379 | 0 | Ok(Content::U64(value)) |
380 | 0 | } |
381 | | |
382 | 0 | fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F> |
383 | 0 | where |
384 | 0 | F: de::Error, |
385 | 0 | { |
386 | 0 | Ok(Content::F32(value)) |
387 | 0 | } |
388 | | |
389 | 0 | fn visit_f64<F>(self, value: f64) -> Result<Self::Value, F> |
390 | 0 | where |
391 | 0 | F: de::Error, |
392 | 0 | { |
393 | 0 | Ok(Content::F64(value)) |
394 | 0 | } |
395 | | |
396 | 0 | fn visit_char<F>(self, value: char) -> Result<Self::Value, F> |
397 | 0 | where |
398 | 0 | F: de::Error, |
399 | 0 | { |
400 | 0 | Ok(Content::Char(value)) |
401 | 0 | } |
402 | | |
403 | 0 | fn visit_str<F>(self, value: &str) -> Result<Self::Value, F> |
404 | 0 | where |
405 | 0 | F: de::Error, |
406 | 0 | { |
407 | 0 | Ok(Content::String(value.into())) |
408 | 0 | } |
409 | | |
410 | 0 | fn visit_borrowed_str<F>(self, value: &'de str) -> Result<Self::Value, F> |
411 | 0 | where |
412 | 0 | F: de::Error, |
413 | 0 | { |
414 | 0 | Ok(Content::Str(value)) |
415 | 0 | } |
416 | | |
417 | 0 | fn visit_string<F>(self, value: String) -> Result<Self::Value, F> |
418 | 0 | where |
419 | 0 | F: de::Error, |
420 | 0 | { |
421 | 0 | Ok(Content::String(value)) |
422 | 0 | } |
423 | | |
424 | 0 | fn visit_bytes<F>(self, value: &[u8]) -> Result<Self::Value, F> |
425 | 0 | where |
426 | 0 | F: de::Error, |
427 | 0 | { |
428 | 0 | Ok(Content::ByteBuf(value.into())) |
429 | 0 | } |
430 | | |
431 | 0 | fn visit_borrowed_bytes<F>(self, value: &'de [u8]) -> Result<Self::Value, F> |
432 | 0 | where |
433 | 0 | F: de::Error, |
434 | 0 | { |
435 | 0 | Ok(Content::Bytes(value)) |
436 | 0 | } |
437 | | |
438 | 0 | fn visit_byte_buf<F>(self, value: Vec<u8>) -> Result<Self::Value, F> |
439 | 0 | where |
440 | 0 | F: de::Error, |
441 | 0 | { |
442 | 0 | Ok(Content::ByteBuf(value)) |
443 | 0 | } |
444 | | |
445 | 0 | fn visit_unit<F>(self) -> Result<Self::Value, F> |
446 | 0 | where |
447 | 0 | F: de::Error, |
448 | 0 | { |
449 | 0 | Ok(Content::Unit) |
450 | 0 | } |
451 | | |
452 | 0 | fn visit_none<F>(self) -> Result<Self::Value, F> |
453 | 0 | where |
454 | 0 | F: de::Error, |
455 | 0 | { |
456 | 0 | Ok(Content::None) |
457 | 0 | } |
458 | | |
459 | 0 | fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
460 | 0 | where |
461 | 0 | D: Deserializer<'de>, |
462 | 0 | { |
463 | 0 | Deserialize::deserialize(deserializer).map(|v| Content::Some(Box::new(v))) |
464 | 0 | } |
465 | | |
466 | 0 | fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
467 | 0 | where |
468 | 0 | D: Deserializer<'de>, |
469 | 0 | { |
470 | 0 | Deserialize::deserialize(deserializer).map(|v| Content::Newtype(Box::new(v))) |
471 | 0 | } |
472 | | |
473 | 0 | fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error> |
474 | 0 | where |
475 | 0 | V: SeqAccess<'de>, |
476 | 0 | { |
477 | 0 | let mut vec = Vec::with_capacity(size_hint::cautious(visitor.size_hint())); |
478 | 0 | while let Some(e) = try!(visitor.next_element()) { |
479 | 0 | vec.push(e); |
480 | 0 | } |
481 | 0 | Ok(Content::Seq(vec)) |
482 | 0 | } |
483 | | |
484 | 0 | fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error> |
485 | 0 | where |
486 | 0 | V: MapAccess<'de>, |
487 | 0 | { |
488 | 0 | let mut vec = Vec::with_capacity(size_hint::cautious(visitor.size_hint())); |
489 | 0 | while let Some(kv) = try!(visitor.next_entry()) { |
490 | 0 | vec.push(kv); |
491 | 0 | } |
492 | 0 | Ok(Content::Map(vec)) |
493 | 0 | } |
494 | | |
495 | 0 | fn visit_enum<V>(self, _visitor: V) -> Result<Self::Value, V::Error> |
496 | 0 | where |
497 | 0 | V: EnumAccess<'de>, |
498 | 0 | { |
499 | 0 | Err(de::Error::custom( |
500 | 0 | "untagged and internally tagged enums do not support enum input", |
501 | 0 | )) |
502 | 0 | } |
503 | | } |
504 | | |
505 | | /// This is the type of the map keys in an internally tagged enum. |
506 | | /// |
507 | | /// Not public API. |
508 | | pub enum TagOrContent<'de> { |
509 | | Tag, |
510 | | Content(Content<'de>), |
511 | | } |
512 | | |
513 | | struct TagOrContentVisitor<'de> { |
514 | | name: &'static str, |
515 | | value: PhantomData<TagOrContent<'de>>, |
516 | | } |
517 | | |
518 | | impl<'de> TagOrContentVisitor<'de> { |
519 | 0 | fn new(name: &'static str) -> Self { |
520 | 0 | TagOrContentVisitor { |
521 | 0 | name: name, |
522 | 0 | value: PhantomData, |
523 | 0 | } |
524 | 0 | } |
525 | | } |
526 | | |
527 | | impl<'de> DeserializeSeed<'de> for TagOrContentVisitor<'de> { |
528 | | type Value = TagOrContent<'de>; |
529 | | |
530 | 0 | fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
531 | 0 | where |
532 | 0 | D: Deserializer<'de>, |
533 | 0 | { |
534 | 0 | // Internally tagged enums are only supported in self-describing |
535 | 0 | // formats. |
536 | 0 | deserializer.deserialize_any(self) |
537 | 0 | } |
538 | | } |
539 | | |
540 | | impl<'de> Visitor<'de> for TagOrContentVisitor<'de> { |
541 | | type Value = TagOrContent<'de>; |
542 | | |
543 | 0 | fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
544 | 0 | write!(fmt, "a type tag `{}` or any other value", self.name) |
545 | 0 | } |
546 | | |
547 | 0 | fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F> |
548 | 0 | where |
549 | 0 | F: de::Error, |
550 | 0 | { |
551 | 0 | ContentVisitor::new() |
552 | 0 | .visit_bool(value) |
553 | 0 | .map(TagOrContent::Content) |
554 | 0 | } |
555 | | |
556 | 0 | fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F> |
557 | 0 | where |
558 | 0 | F: de::Error, |
559 | 0 | { |
560 | 0 | ContentVisitor::new() |
561 | 0 | .visit_i8(value) |
562 | 0 | .map(TagOrContent::Content) |
563 | 0 | } |
564 | | |
565 | 0 | fn visit_i16<F>(self, value: i16) -> Result<Self::Value, F> |
566 | 0 | where |
567 | 0 | F: de::Error, |
568 | 0 | { |
569 | 0 | ContentVisitor::new() |
570 | 0 | .visit_i16(value) |
571 | 0 | .map(TagOrContent::Content) |
572 | 0 | } |
573 | | |
574 | 0 | fn visit_i32<F>(self, value: i32) -> Result<Self::Value, F> |
575 | 0 | where |
576 | 0 | F: de::Error, |
577 | 0 | { |
578 | 0 | ContentVisitor::new() |
579 | 0 | .visit_i32(value) |
580 | 0 | .map(TagOrContent::Content) |
581 | 0 | } |
582 | | |
583 | 0 | fn visit_i64<F>(self, value: i64) -> Result<Self::Value, F> |
584 | 0 | where |
585 | 0 | F: de::Error, |
586 | 0 | { |
587 | 0 | ContentVisitor::new() |
588 | 0 | .visit_i64(value) |
589 | 0 | .map(TagOrContent::Content) |
590 | 0 | } |
591 | | |
592 | 0 | fn visit_u8<F>(self, value: u8) -> Result<Self::Value, F> |
593 | 0 | where |
594 | 0 | F: de::Error, |
595 | 0 | { |
596 | 0 | ContentVisitor::new() |
597 | 0 | .visit_u8(value) |
598 | 0 | .map(TagOrContent::Content) |
599 | 0 | } |
600 | | |
601 | 0 | fn visit_u16<F>(self, value: u16) -> Result<Self::Value, F> |
602 | 0 | where |
603 | 0 | F: de::Error, |
604 | 0 | { |
605 | 0 | ContentVisitor::new() |
606 | 0 | .visit_u16(value) |
607 | 0 | .map(TagOrContent::Content) |
608 | 0 | } |
609 | | |
610 | 0 | fn visit_u32<F>(self, value: u32) -> Result<Self::Value, F> |
611 | 0 | where |
612 | 0 | F: de::Error, |
613 | 0 | { |
614 | 0 | ContentVisitor::new() |
615 | 0 | .visit_u32(value) |
616 | 0 | .map(TagOrContent::Content) |
617 | 0 | } |
618 | | |
619 | 0 | fn visit_u64<F>(self, value: u64) -> Result<Self::Value, F> |
620 | 0 | where |
621 | 0 | F: de::Error, |
622 | 0 | { |
623 | 0 | ContentVisitor::new() |
624 | 0 | .visit_u64(value) |
625 | 0 | .map(TagOrContent::Content) |
626 | 0 | } |
627 | | |
628 | 0 | fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F> |
629 | 0 | where |
630 | 0 | F: de::Error, |
631 | 0 | { |
632 | 0 | ContentVisitor::new() |
633 | 0 | .visit_f32(value) |
634 | 0 | .map(TagOrContent::Content) |
635 | 0 | } |
636 | | |
637 | 0 | fn visit_f64<F>(self, value: f64) -> Result<Self::Value, F> |
638 | 0 | where |
639 | 0 | F: de::Error, |
640 | 0 | { |
641 | 0 | ContentVisitor::new() |
642 | 0 | .visit_f64(value) |
643 | 0 | .map(TagOrContent::Content) |
644 | 0 | } |
645 | | |
646 | 0 | fn visit_char<F>(self, value: char) -> Result<Self::Value, F> |
647 | 0 | where |
648 | 0 | F: de::Error, |
649 | 0 | { |
650 | 0 | ContentVisitor::new() |
651 | 0 | .visit_char(value) |
652 | 0 | .map(TagOrContent::Content) |
653 | 0 | } |
654 | | |
655 | 0 | fn visit_str<F>(self, value: &str) -> Result<Self::Value, F> |
656 | 0 | where |
657 | 0 | F: de::Error, |
658 | 0 | { |
659 | 0 | if value == self.name { |
660 | 0 | Ok(TagOrContent::Tag) |
661 | | } else { |
662 | 0 | ContentVisitor::new() |
663 | 0 | .visit_str(value) |
664 | 0 | .map(TagOrContent::Content) |
665 | | } |
666 | 0 | } |
667 | | |
668 | 0 | fn visit_borrowed_str<F>(self, value: &'de str) -> Result<Self::Value, F> |
669 | 0 | where |
670 | 0 | F: de::Error, |
671 | 0 | { |
672 | 0 | if value == self.name { |
673 | 0 | Ok(TagOrContent::Tag) |
674 | | } else { |
675 | 0 | ContentVisitor::new() |
676 | 0 | .visit_borrowed_str(value) |
677 | 0 | .map(TagOrContent::Content) |
678 | | } |
679 | 0 | } |
680 | | |
681 | 0 | fn visit_string<F>(self, value: String) -> Result<Self::Value, F> |
682 | 0 | where |
683 | 0 | F: de::Error, |
684 | 0 | { |
685 | 0 | if value == self.name { |
686 | 0 | Ok(TagOrContent::Tag) |
687 | | } else { |
688 | 0 | ContentVisitor::new() |
689 | 0 | .visit_string(value) |
690 | 0 | .map(TagOrContent::Content) |
691 | | } |
692 | 0 | } |
693 | | |
694 | 0 | fn visit_bytes<F>(self, value: &[u8]) -> Result<Self::Value, F> |
695 | 0 | where |
696 | 0 | F: de::Error, |
697 | 0 | { |
698 | 0 | if value == self.name.as_bytes() { |
699 | 0 | Ok(TagOrContent::Tag) |
700 | | } else { |
701 | 0 | ContentVisitor::new() |
702 | 0 | .visit_bytes(value) |
703 | 0 | .map(TagOrContent::Content) |
704 | | } |
705 | 0 | } |
706 | | |
707 | 0 | fn visit_borrowed_bytes<F>(self, value: &'de [u8]) -> Result<Self::Value, F> |
708 | 0 | where |
709 | 0 | F: de::Error, |
710 | 0 | { |
711 | 0 | if value == self.name.as_bytes() { |
712 | 0 | Ok(TagOrContent::Tag) |
713 | | } else { |
714 | 0 | ContentVisitor::new() |
715 | 0 | .visit_borrowed_bytes(value) |
716 | 0 | .map(TagOrContent::Content) |
717 | | } |
718 | 0 | } |
719 | | |
720 | 0 | fn visit_byte_buf<F>(self, value: Vec<u8>) -> Result<Self::Value, F> |
721 | 0 | where |
722 | 0 | F: de::Error, |
723 | 0 | { |
724 | 0 | if value == self.name.as_bytes() { |
725 | 0 | Ok(TagOrContent::Tag) |
726 | | } else { |
727 | 0 | ContentVisitor::new() |
728 | 0 | .visit_byte_buf(value) |
729 | 0 | .map(TagOrContent::Content) |
730 | | } |
731 | 0 | } |
732 | | |
733 | 0 | fn visit_unit<F>(self) -> Result<Self::Value, F> |
734 | 0 | where |
735 | 0 | F: de::Error, |
736 | 0 | { |
737 | 0 | ContentVisitor::new() |
738 | 0 | .visit_unit() |
739 | 0 | .map(TagOrContent::Content) |
740 | 0 | } |
741 | | |
742 | 0 | fn visit_none<F>(self) -> Result<Self::Value, F> |
743 | 0 | where |
744 | 0 | F: de::Error, |
745 | 0 | { |
746 | 0 | ContentVisitor::new() |
747 | 0 | .visit_none() |
748 | 0 | .map(TagOrContent::Content) |
749 | 0 | } |
750 | | |
751 | 0 | fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
752 | 0 | where |
753 | 0 | D: Deserializer<'de>, |
754 | 0 | { |
755 | 0 | ContentVisitor::new() |
756 | 0 | .visit_some(deserializer) |
757 | 0 | .map(TagOrContent::Content) |
758 | 0 | } |
759 | | |
760 | 0 | fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
761 | 0 | where |
762 | 0 | D: Deserializer<'de>, |
763 | 0 | { |
764 | 0 | ContentVisitor::new() |
765 | 0 | .visit_newtype_struct(deserializer) |
766 | 0 | .map(TagOrContent::Content) |
767 | 0 | } |
768 | | |
769 | 0 | fn visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error> |
770 | 0 | where |
771 | 0 | V: SeqAccess<'de>, |
772 | 0 | { |
773 | 0 | ContentVisitor::new() |
774 | 0 | .visit_seq(visitor) |
775 | 0 | .map(TagOrContent::Content) |
776 | 0 | } |
777 | | |
778 | 0 | fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error> |
779 | 0 | where |
780 | 0 | V: MapAccess<'de>, |
781 | 0 | { |
782 | 0 | ContentVisitor::new() |
783 | 0 | .visit_map(visitor) |
784 | 0 | .map(TagOrContent::Content) |
785 | 0 | } |
786 | | |
787 | 0 | fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error> |
788 | 0 | where |
789 | 0 | V: EnumAccess<'de>, |
790 | 0 | { |
791 | 0 | ContentVisitor::new() |
792 | 0 | .visit_enum(visitor) |
793 | 0 | .map(TagOrContent::Content) |
794 | 0 | } |
795 | | } |
796 | | |
797 | | /// Used by generated code to deserialize an internally tagged enum. |
798 | | /// |
799 | | /// Not public API. |
800 | | pub struct TaggedContent<'de, T> { |
801 | | pub tag: T, |
802 | | pub content: Content<'de>, |
803 | | } |
804 | | |
805 | | /// Not public API. |
806 | | pub struct TaggedContentVisitor<'de, T> { |
807 | | tag_name: &'static str, |
808 | | expecting: &'static str, |
809 | | value: PhantomData<TaggedContent<'de, T>>, |
810 | | } |
811 | | |
812 | | impl<'de, T> TaggedContentVisitor<'de, T> { |
813 | | /// Visitor for the content of an internally tagged enum with the given |
814 | | /// tag name. |
815 | 0 | pub fn new(name: &'static str, expecting: &'static str) -> Self { |
816 | 0 | TaggedContentVisitor { |
817 | 0 | tag_name: name, |
818 | 0 | expecting: expecting, |
819 | 0 | value: PhantomData, |
820 | 0 | } |
821 | 0 | } |
822 | | } |
823 | | |
824 | | impl<'de, T> DeserializeSeed<'de> for TaggedContentVisitor<'de, T> |
825 | | where |
826 | | T: Deserialize<'de>, |
827 | | { |
828 | | type Value = TaggedContent<'de, T>; |
829 | | |
830 | 0 | fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
831 | 0 | where |
832 | 0 | D: Deserializer<'de>, |
833 | 0 | { |
834 | 0 | // Internally tagged enums are only supported in self-describing |
835 | 0 | // formats. |
836 | 0 | deserializer.deserialize_any(self) |
837 | 0 | } |
838 | | } |
839 | | |
840 | | impl<'de, T> Visitor<'de> for TaggedContentVisitor<'de, T> |
841 | | where |
842 | | T: Deserialize<'de>, |
843 | | { |
844 | | type Value = TaggedContent<'de, T>; |
845 | | |
846 | 0 | fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
847 | 0 | fmt.write_str(self.expecting) |
848 | 0 | } |
849 | | |
850 | 0 | fn visit_seq<S>(self, mut seq: S) -> Result<Self::Value, S::Error> |
851 | 0 | where |
852 | 0 | S: SeqAccess<'de>, |
853 | 0 | { |
854 | 0 | let tag = match try!(seq.next_element()) { |
855 | 0 | Some(tag) => tag, |
856 | | None => { |
857 | 0 | return Err(de::Error::missing_field(self.tag_name)); |
858 | | } |
859 | | }; |
860 | 0 | let rest = de::value::SeqAccessDeserializer::new(seq); |
861 | 0 | Ok(TaggedContent { |
862 | 0 | tag: tag, |
863 | 0 | content: try!(Content::deserialize(rest)), |
864 | | }) |
865 | 0 | } |
866 | | |
867 | 0 | fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> |
868 | 0 | where |
869 | 0 | M: MapAccess<'de>, |
870 | 0 | { |
871 | 0 | let mut tag = None; |
872 | 0 | let mut vec = Vec::with_capacity(size_hint::cautious(map.size_hint())); |
873 | 0 | while let Some(k) = try!(map.next_key_seed(TagOrContentVisitor::new(self.tag_name))) { |
874 | 0 | match k { |
875 | | TagOrContent::Tag => { |
876 | 0 | if tag.is_some() { |
877 | 0 | return Err(de::Error::duplicate_field(self.tag_name)); |
878 | 0 | } |
879 | 0 | tag = Some(try!(map.next_value())); |
880 | | } |
881 | 0 | TagOrContent::Content(k) => { |
882 | 0 | let v = try!(map.next_value()); |
883 | 0 | vec.push((k, v)); |
884 | | } |
885 | | } |
886 | | } |
887 | 0 | match tag { |
888 | 0 | None => Err(de::Error::missing_field(self.tag_name)), |
889 | 0 | Some(tag) => Ok(TaggedContent { |
890 | 0 | tag: tag, |
891 | 0 | content: Content::Map(vec), |
892 | 0 | }), |
893 | | } |
894 | 0 | } |
895 | | } |
896 | | |
897 | | /// Used by generated code to deserialize an adjacently tagged enum. |
898 | | /// |
899 | | /// Not public API. |
900 | | pub enum TagOrContentField { |
901 | | Tag, |
902 | | Content, |
903 | | } |
904 | | |
905 | | /// Not public API. |
906 | | pub struct TagOrContentFieldVisitor { |
907 | | pub tag: &'static str, |
908 | | pub content: &'static str, |
909 | | } |
910 | | |
911 | | impl<'de> DeserializeSeed<'de> for TagOrContentFieldVisitor { |
912 | | type Value = TagOrContentField; |
913 | | |
914 | 0 | fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
915 | 0 | where |
916 | 0 | D: Deserializer<'de>, |
917 | 0 | { |
918 | 0 | deserializer.deserialize_str(self) |
919 | 0 | } |
920 | | } |
921 | | |
922 | | impl<'de> Visitor<'de> for TagOrContentFieldVisitor { |
923 | | type Value = TagOrContentField; |
924 | | |
925 | 0 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
926 | 0 | write!(formatter, "{:?} or {:?}", self.tag, self.content) |
927 | 0 | } |
928 | | |
929 | 0 | fn visit_str<E>(self, field: &str) -> Result<Self::Value, E> |
930 | 0 | where |
931 | 0 | E: de::Error, |
932 | 0 | { |
933 | 0 | if field == self.tag { |
934 | 0 | Ok(TagOrContentField::Tag) |
935 | 0 | } else if field == self.content { |
936 | 0 | Ok(TagOrContentField::Content) |
937 | | } else { |
938 | 0 | Err(de::Error::invalid_value(Unexpected::Str(field), &self)) |
939 | | } |
940 | 0 | } |
941 | | } |
942 | | |
943 | | /// Used by generated code to deserialize an adjacently tagged enum when |
944 | | /// ignoring unrelated fields is allowed. |
945 | | /// |
946 | | /// Not public API. |
947 | | pub enum TagContentOtherField { |
948 | | Tag, |
949 | | Content, |
950 | | Other, |
951 | | } |
952 | | |
953 | | /// Not public API. |
954 | | pub struct TagContentOtherFieldVisitor { |
955 | | pub tag: &'static str, |
956 | | pub content: &'static str, |
957 | | } |
958 | | |
959 | | impl<'de> DeserializeSeed<'de> for TagContentOtherFieldVisitor { |
960 | | type Value = TagContentOtherField; |
961 | | |
962 | 0 | fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error> |
963 | 0 | where |
964 | 0 | D: Deserializer<'de>, |
965 | 0 | { |
966 | 0 | deserializer.deserialize_str(self) |
967 | 0 | } |
968 | | } |
969 | | |
970 | | impl<'de> Visitor<'de> for TagContentOtherFieldVisitor { |
971 | | type Value = TagContentOtherField; |
972 | | |
973 | 0 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
974 | 0 | write!( |
975 | 0 | formatter, |
976 | 0 | "{:?}, {:?}, or other ignored fields", |
977 | 0 | self.tag, self.content |
978 | 0 | ) |
979 | 0 | } |
980 | | |
981 | 0 | fn visit_str<E>(self, field: &str) -> Result<Self::Value, E> |
982 | 0 | where |
983 | 0 | E: de::Error, |
984 | 0 | { |
985 | 0 | if field == self.tag { |
986 | 0 | Ok(TagContentOtherField::Tag) |
987 | 0 | } else if field == self.content { |
988 | 0 | Ok(TagContentOtherField::Content) |
989 | | } else { |
990 | 0 | Ok(TagContentOtherField::Other) |
991 | | } |
992 | 0 | } |
993 | | } |
994 | | |
995 | | /// Not public API |
996 | | pub struct ContentDeserializer<'de, E> { |
997 | | content: Content<'de>, |
998 | | err: PhantomData<E>, |
999 | | } |
1000 | | |
1001 | | impl<'de, E> ContentDeserializer<'de, E> |
1002 | | where |
1003 | | E: de::Error, |
1004 | | { |
1005 | | #[cold] |
1006 | 0 | fn invalid_type(self, exp: &Expected) -> E { |
1007 | 0 | de::Error::invalid_type(self.content.unexpected(), exp) |
1008 | 0 | } |
1009 | | |
1010 | 0 | fn deserialize_integer<V>(self, visitor: V) -> Result<V::Value, E> |
1011 | 0 | where |
1012 | 0 | V: Visitor<'de>, |
1013 | 0 | { |
1014 | 0 | match self.content { |
1015 | 0 | Content::U8(v) => visitor.visit_u8(v), |
1016 | 0 | Content::U16(v) => visitor.visit_u16(v), |
1017 | 0 | Content::U32(v) => visitor.visit_u32(v), |
1018 | 0 | Content::U64(v) => visitor.visit_u64(v), |
1019 | 0 | Content::I8(v) => visitor.visit_i8(v), |
1020 | 0 | Content::I16(v) => visitor.visit_i16(v), |
1021 | 0 | Content::I32(v) => visitor.visit_i32(v), |
1022 | 0 | Content::I64(v) => visitor.visit_i64(v), |
1023 | 0 | _ => Err(self.invalid_type(&visitor)), |
1024 | | } |
1025 | 0 | } |
1026 | | |
1027 | 0 | fn deserialize_float<V>(self, visitor: V) -> Result<V::Value, E> |
1028 | 0 | where |
1029 | 0 | V: Visitor<'de>, |
1030 | 0 | { |
1031 | 0 | match self.content { |
1032 | 0 | Content::F32(v) => visitor.visit_f32(v), |
1033 | 0 | Content::F64(v) => visitor.visit_f64(v), |
1034 | 0 | Content::U8(v) => visitor.visit_u8(v), |
1035 | 0 | Content::U16(v) => visitor.visit_u16(v), |
1036 | 0 | Content::U32(v) => visitor.visit_u32(v), |
1037 | 0 | Content::U64(v) => visitor.visit_u64(v), |
1038 | 0 | Content::I8(v) => visitor.visit_i8(v), |
1039 | 0 | Content::I16(v) => visitor.visit_i16(v), |
1040 | 0 | Content::I32(v) => visitor.visit_i32(v), |
1041 | 0 | Content::I64(v) => visitor.visit_i64(v), |
1042 | 0 | _ => Err(self.invalid_type(&visitor)), |
1043 | | } |
1044 | 0 | } |
1045 | | } |
1046 | | |
1047 | 0 | fn visit_content_seq<'de, V, E>(content: Vec<Content<'de>>, visitor: V) -> Result<V::Value, E> |
1048 | 0 | where |
1049 | 0 | V: Visitor<'de>, |
1050 | 0 | E: de::Error, |
1051 | 0 | { |
1052 | 0 | let seq = content.into_iter().map(ContentDeserializer::new); |
1053 | 0 | let mut seq_visitor = de::value::SeqDeserializer::new(seq); |
1054 | 0 | let value = try!(visitor.visit_seq(&mut seq_visitor)); |
1055 | 0 | try!(seq_visitor.end()); |
1056 | 0 | Ok(value) |
1057 | 0 | } |
1058 | | |
1059 | 0 | fn visit_content_map<'de, V, E>( |
1060 | 0 | content: Vec<(Content<'de>, Content<'de>)>, |
1061 | 0 | visitor: V, |
1062 | 0 | ) -> Result<V::Value, E> |
1063 | 0 | where |
1064 | 0 | V: Visitor<'de>, |
1065 | 0 | E: de::Error, |
1066 | 0 | { |
1067 | 0 | let map = content |
1068 | 0 | .into_iter() |
1069 | 0 | .map(|(k, v)| (ContentDeserializer::new(k), ContentDeserializer::new(v))); |
1070 | 0 | let mut map_visitor = de::value::MapDeserializer::new(map); |
1071 | 0 | let value = try!(visitor.visit_map(&mut map_visitor)); |
1072 | 0 | try!(map_visitor.end()); |
1073 | 0 | Ok(value) |
1074 | 0 | } |
1075 | | |
1076 | | /// Used when deserializing an internally tagged enum because the content |
1077 | | /// will be used exactly once. |
1078 | | impl<'de, E> Deserializer<'de> for ContentDeserializer<'de, E> |
1079 | | where |
1080 | | E: de::Error, |
1081 | | { |
1082 | | type Error = E; |
1083 | | |
1084 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1085 | 0 | where |
1086 | 0 | V: Visitor<'de>, |
1087 | 0 | { |
1088 | 0 | match self.content { |
1089 | 0 | Content::Bool(v) => visitor.visit_bool(v), |
1090 | 0 | Content::U8(v) => visitor.visit_u8(v), |
1091 | 0 | Content::U16(v) => visitor.visit_u16(v), |
1092 | 0 | Content::U32(v) => visitor.visit_u32(v), |
1093 | 0 | Content::U64(v) => visitor.visit_u64(v), |
1094 | 0 | Content::I8(v) => visitor.visit_i8(v), |
1095 | 0 | Content::I16(v) => visitor.visit_i16(v), |
1096 | 0 | Content::I32(v) => visitor.visit_i32(v), |
1097 | 0 | Content::I64(v) => visitor.visit_i64(v), |
1098 | 0 | Content::F32(v) => visitor.visit_f32(v), |
1099 | 0 | Content::F64(v) => visitor.visit_f64(v), |
1100 | 0 | Content::Char(v) => visitor.visit_char(v), |
1101 | 0 | Content::String(v) => visitor.visit_string(v), |
1102 | 0 | Content::Str(v) => visitor.visit_borrowed_str(v), |
1103 | 0 | Content::ByteBuf(v) => visitor.visit_byte_buf(v), |
1104 | 0 | Content::Bytes(v) => visitor.visit_borrowed_bytes(v), |
1105 | 0 | Content::Unit => visitor.visit_unit(), |
1106 | 0 | Content::None => visitor.visit_none(), |
1107 | 0 | Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)), |
1108 | 0 | Content::Newtype(v) => visitor.visit_newtype_struct(ContentDeserializer::new(*v)), |
1109 | 0 | Content::Seq(v) => visit_content_seq(v, visitor), |
1110 | 0 | Content::Map(v) => visit_content_map(v, visitor), |
1111 | | } |
1112 | 0 | } |
1113 | | |
1114 | 0 | fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1115 | 0 | where |
1116 | 0 | V: Visitor<'de>, |
1117 | 0 | { |
1118 | 0 | match self.content { |
1119 | 0 | Content::Bool(v) => visitor.visit_bool(v), |
1120 | 0 | _ => Err(self.invalid_type(&visitor)), |
1121 | | } |
1122 | 0 | } |
1123 | | |
1124 | 0 | fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1125 | 0 | where |
1126 | 0 | V: Visitor<'de>, |
1127 | 0 | { |
1128 | 0 | self.deserialize_integer(visitor) |
1129 | 0 | } |
1130 | | |
1131 | 0 | fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1132 | 0 | where |
1133 | 0 | V: Visitor<'de>, |
1134 | 0 | { |
1135 | 0 | self.deserialize_integer(visitor) |
1136 | 0 | } |
1137 | | |
1138 | 0 | fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1139 | 0 | where |
1140 | 0 | V: Visitor<'de>, |
1141 | 0 | { |
1142 | 0 | self.deserialize_integer(visitor) |
1143 | 0 | } |
1144 | | |
1145 | 0 | fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1146 | 0 | where |
1147 | 0 | V: Visitor<'de>, |
1148 | 0 | { |
1149 | 0 | self.deserialize_integer(visitor) |
1150 | 0 | } |
1151 | | |
1152 | 0 | fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1153 | 0 | where |
1154 | 0 | V: Visitor<'de>, |
1155 | 0 | { |
1156 | 0 | self.deserialize_integer(visitor) |
1157 | 0 | } |
1158 | | |
1159 | 0 | fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1160 | 0 | where |
1161 | 0 | V: Visitor<'de>, |
1162 | 0 | { |
1163 | 0 | self.deserialize_integer(visitor) |
1164 | 0 | } |
1165 | | |
1166 | 0 | fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1167 | 0 | where |
1168 | 0 | V: Visitor<'de>, |
1169 | 0 | { |
1170 | 0 | self.deserialize_integer(visitor) |
1171 | 0 | } |
1172 | | |
1173 | 0 | fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1174 | 0 | where |
1175 | 0 | V: Visitor<'de>, |
1176 | 0 | { |
1177 | 0 | self.deserialize_integer(visitor) |
1178 | 0 | } |
1179 | | |
1180 | 0 | fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1181 | 0 | where |
1182 | 0 | V: Visitor<'de>, |
1183 | 0 | { |
1184 | 0 | self.deserialize_float(visitor) |
1185 | 0 | } |
1186 | | |
1187 | 0 | fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1188 | 0 | where |
1189 | 0 | V: Visitor<'de>, |
1190 | 0 | { |
1191 | 0 | self.deserialize_float(visitor) |
1192 | 0 | } |
1193 | | |
1194 | 0 | fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1195 | 0 | where |
1196 | 0 | V: Visitor<'de>, |
1197 | 0 | { |
1198 | 0 | match self.content { |
1199 | 0 | Content::Char(v) => visitor.visit_char(v), |
1200 | 0 | Content::String(v) => visitor.visit_string(v), |
1201 | 0 | Content::Str(v) => visitor.visit_borrowed_str(v), |
1202 | 0 | _ => Err(self.invalid_type(&visitor)), |
1203 | | } |
1204 | 0 | } |
1205 | | |
1206 | 0 | fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1207 | 0 | where |
1208 | 0 | V: Visitor<'de>, |
1209 | 0 | { |
1210 | 0 | self.deserialize_string(visitor) |
1211 | 0 | } |
1212 | | |
1213 | 0 | fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1214 | 0 | where |
1215 | 0 | V: Visitor<'de>, |
1216 | 0 | { |
1217 | 0 | match self.content { |
1218 | 0 | Content::String(v) => visitor.visit_string(v), |
1219 | 0 | Content::Str(v) => visitor.visit_borrowed_str(v), |
1220 | 0 | Content::ByteBuf(v) => visitor.visit_byte_buf(v), |
1221 | 0 | Content::Bytes(v) => visitor.visit_borrowed_bytes(v), |
1222 | 0 | _ => Err(self.invalid_type(&visitor)), |
1223 | | } |
1224 | 0 | } |
1225 | | |
1226 | 0 | fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1227 | 0 | where |
1228 | 0 | V: Visitor<'de>, |
1229 | 0 | { |
1230 | 0 | self.deserialize_byte_buf(visitor) |
1231 | 0 | } |
1232 | | |
1233 | 0 | fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1234 | 0 | where |
1235 | 0 | V: Visitor<'de>, |
1236 | 0 | { |
1237 | 0 | match self.content { |
1238 | 0 | Content::String(v) => visitor.visit_string(v), |
1239 | 0 | Content::Str(v) => visitor.visit_borrowed_str(v), |
1240 | 0 | Content::ByteBuf(v) => visitor.visit_byte_buf(v), |
1241 | 0 | Content::Bytes(v) => visitor.visit_borrowed_bytes(v), |
1242 | 0 | Content::Seq(v) => visit_content_seq(v, visitor), |
1243 | 0 | _ => Err(self.invalid_type(&visitor)), |
1244 | | } |
1245 | 0 | } |
1246 | | |
1247 | 0 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1248 | 0 | where |
1249 | 0 | V: Visitor<'de>, |
1250 | 0 | { |
1251 | 0 | match self.content { |
1252 | 0 | Content::None => visitor.visit_none(), |
1253 | 0 | Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)), |
1254 | 0 | Content::Unit => visitor.visit_unit(), |
1255 | 0 | _ => visitor.visit_some(self), |
1256 | | } |
1257 | 0 | } |
1258 | | |
1259 | 0 | fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1260 | 0 | where |
1261 | 0 | V: Visitor<'de>, |
1262 | 0 | { |
1263 | 0 | match self.content { |
1264 | 0 | Content::Unit => visitor.visit_unit(), |
1265 | | |
1266 | | // Allow deserializing newtype variant containing unit. |
1267 | | // |
1268 | | // #[derive(Deserialize)] |
1269 | | // #[serde(tag = "result")] |
1270 | | // enum Response<T> { |
1271 | | // Success(T), |
1272 | | // } |
1273 | | // |
1274 | | // We want {"result":"Success"} to deserialize into Response<()>. |
1275 | 0 | Content::Map(ref v) if v.is_empty() => visitor.visit_unit(), |
1276 | 0 | _ => Err(self.invalid_type(&visitor)), |
1277 | | } |
1278 | 0 | } |
1279 | | |
1280 | 0 | fn deserialize_unit_struct<V>( |
1281 | 0 | self, |
1282 | 0 | _name: &'static str, |
1283 | 0 | visitor: V, |
1284 | 0 | ) -> Result<V::Value, Self::Error> |
1285 | 0 | where |
1286 | 0 | V: Visitor<'de>, |
1287 | 0 | { |
1288 | 0 | match self.content { |
1289 | | // As a special case, allow deserializing untagged newtype |
1290 | | // variant containing unit struct. |
1291 | | // |
1292 | | // #[derive(Deserialize)] |
1293 | | // struct Info; |
1294 | | // |
1295 | | // #[derive(Deserialize)] |
1296 | | // #[serde(tag = "topic")] |
1297 | | // enum Message { |
1298 | | // Info(Info), |
1299 | | // } |
1300 | | // |
1301 | | // We want {"topic":"Info"} to deserialize even though |
1302 | | // ordinarily unit structs do not deserialize from empty map/seq. |
1303 | 0 | Content::Map(ref v) if v.is_empty() => visitor.visit_unit(), |
1304 | 0 | Content::Seq(ref v) if v.is_empty() => visitor.visit_unit(), |
1305 | 0 | _ => self.deserialize_any(visitor), |
1306 | | } |
1307 | 0 | } |
1308 | | |
1309 | 0 | fn deserialize_newtype_struct<V>( |
1310 | 0 | self, |
1311 | 0 | _name: &str, |
1312 | 0 | visitor: V, |
1313 | 0 | ) -> Result<V::Value, Self::Error> |
1314 | 0 | where |
1315 | 0 | V: Visitor<'de>, |
1316 | 0 | { |
1317 | 0 | match self.content { |
1318 | 0 | Content::Newtype(v) => visitor.visit_newtype_struct(ContentDeserializer::new(*v)), |
1319 | 0 | _ => visitor.visit_newtype_struct(self), |
1320 | | } |
1321 | 0 | } |
1322 | | |
1323 | 0 | fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1324 | 0 | where |
1325 | 0 | V: Visitor<'de>, |
1326 | 0 | { |
1327 | 0 | match self.content { |
1328 | 0 | Content::Seq(v) => visit_content_seq(v, visitor), |
1329 | 0 | _ => Err(self.invalid_type(&visitor)), |
1330 | | } |
1331 | 0 | } |
1332 | | |
1333 | 0 | fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error> |
1334 | 0 | where |
1335 | 0 | V: Visitor<'de>, |
1336 | 0 | { |
1337 | 0 | self.deserialize_seq(visitor) |
1338 | 0 | } |
1339 | | |
1340 | 0 | fn deserialize_tuple_struct<V>( |
1341 | 0 | self, |
1342 | 0 | _name: &'static str, |
1343 | 0 | _len: usize, |
1344 | 0 | visitor: V, |
1345 | 0 | ) -> Result<V::Value, Self::Error> |
1346 | 0 | where |
1347 | 0 | V: Visitor<'de>, |
1348 | 0 | { |
1349 | 0 | self.deserialize_seq(visitor) |
1350 | 0 | } |
1351 | | |
1352 | 0 | fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1353 | 0 | where |
1354 | 0 | V: Visitor<'de>, |
1355 | 0 | { |
1356 | 0 | match self.content { |
1357 | 0 | Content::Map(v) => visit_content_map(v, visitor), |
1358 | 0 | _ => Err(self.invalid_type(&visitor)), |
1359 | | } |
1360 | 0 | } |
1361 | | |
1362 | 0 | fn deserialize_struct<V>( |
1363 | 0 | self, |
1364 | 0 | _name: &'static str, |
1365 | 0 | _fields: &'static [&'static str], |
1366 | 0 | visitor: V, |
1367 | 0 | ) -> Result<V::Value, Self::Error> |
1368 | 0 | where |
1369 | 0 | V: Visitor<'de>, |
1370 | 0 | { |
1371 | 0 | match self.content { |
1372 | 0 | Content::Seq(v) => visit_content_seq(v, visitor), |
1373 | 0 | Content::Map(v) => visit_content_map(v, visitor), |
1374 | 0 | _ => Err(self.invalid_type(&visitor)), |
1375 | | } |
1376 | 0 | } |
1377 | | |
1378 | 0 | fn deserialize_enum<V>( |
1379 | 0 | self, |
1380 | 0 | _name: &str, |
1381 | 0 | _variants: &'static [&'static str], |
1382 | 0 | visitor: V, |
1383 | 0 | ) -> Result<V::Value, Self::Error> |
1384 | 0 | where |
1385 | 0 | V: Visitor<'de>, |
1386 | 0 | { |
1387 | 0 | let (variant, value) = match self.content { |
1388 | 0 | Content::Map(value) => { |
1389 | 0 | let mut iter = value.into_iter(); |
1390 | 0 | let (variant, value) = match iter.next() { |
1391 | 0 | Some(v) => v, |
1392 | | None => { |
1393 | 0 | return Err(de::Error::invalid_value( |
1394 | 0 | de::Unexpected::Map, |
1395 | 0 | &"map with a single key", |
1396 | 0 | )); |
1397 | | } |
1398 | | }; |
1399 | | // enums are encoded in json as maps with a single key:value pair |
1400 | 0 | if iter.next().is_some() { |
1401 | 0 | return Err(de::Error::invalid_value( |
1402 | 0 | de::Unexpected::Map, |
1403 | 0 | &"map with a single key", |
1404 | 0 | )); |
1405 | 0 | } |
1406 | 0 | (variant, Some(value)) |
1407 | | } |
1408 | 0 | s @ Content::String(_) | s @ Content::Str(_) => (s, None), |
1409 | 0 | other => { |
1410 | 0 | return Err(de::Error::invalid_type( |
1411 | 0 | other.unexpected(), |
1412 | 0 | &"string or map", |
1413 | 0 | )); |
1414 | | } |
1415 | | }; |
1416 | | |
1417 | 0 | visitor.visit_enum(EnumDeserializer::new(variant, value)) |
1418 | 0 | } |
1419 | | |
1420 | 0 | fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1421 | 0 | where |
1422 | 0 | V: Visitor<'de>, |
1423 | 0 | { |
1424 | 0 | match self.content { |
1425 | 0 | Content::String(v) => visitor.visit_string(v), |
1426 | 0 | Content::Str(v) => visitor.visit_borrowed_str(v), |
1427 | 0 | Content::ByteBuf(v) => visitor.visit_byte_buf(v), |
1428 | 0 | Content::Bytes(v) => visitor.visit_borrowed_bytes(v), |
1429 | 0 | Content::U8(v) => visitor.visit_u8(v), |
1430 | 0 | Content::U64(v) => visitor.visit_u64(v), |
1431 | 0 | _ => Err(self.invalid_type(&visitor)), |
1432 | | } |
1433 | 0 | } |
1434 | | |
1435 | 0 | fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1436 | 0 | where |
1437 | 0 | V: Visitor<'de>, |
1438 | 0 | { |
1439 | 0 | drop(self); |
1440 | 0 | visitor.visit_unit() |
1441 | 0 | } |
1442 | | |
1443 | 0 | fn __deserialize_content<V>( |
1444 | 0 | self, |
1445 | 0 | _: actually_private::T, |
1446 | 0 | visitor: V, |
1447 | 0 | ) -> Result<Content<'de>, Self::Error> |
1448 | 0 | where |
1449 | 0 | V: Visitor<'de, Value = Content<'de>>, |
1450 | 0 | { |
1451 | 0 | let _ = visitor; |
1452 | 0 | Ok(self.content) |
1453 | 0 | } |
1454 | | } |
1455 | | |
1456 | | impl<'de, E> ContentDeserializer<'de, E> { |
1457 | | /// private API, don't use |
1458 | 0 | pub fn new(content: Content<'de>) -> Self { |
1459 | 0 | ContentDeserializer { |
1460 | 0 | content: content, |
1461 | 0 | err: PhantomData, |
1462 | 0 | } |
1463 | 0 | } |
1464 | | } |
1465 | | |
1466 | | pub struct EnumDeserializer<'de, E> |
1467 | | where |
1468 | | E: de::Error, |
1469 | | { |
1470 | | variant: Content<'de>, |
1471 | | value: Option<Content<'de>>, |
1472 | | err: PhantomData<E>, |
1473 | | } |
1474 | | |
1475 | | impl<'de, E> EnumDeserializer<'de, E> |
1476 | | where |
1477 | | E: de::Error, |
1478 | | { |
1479 | 0 | pub fn new(variant: Content<'de>, value: Option<Content<'de>>) -> EnumDeserializer<'de, E> { |
1480 | 0 | EnumDeserializer { |
1481 | 0 | variant: variant, |
1482 | 0 | value: value, |
1483 | 0 | err: PhantomData, |
1484 | 0 | } |
1485 | 0 | } |
1486 | | } |
1487 | | |
1488 | | impl<'de, E> de::EnumAccess<'de> for EnumDeserializer<'de, E> |
1489 | | where |
1490 | | E: de::Error, |
1491 | | { |
1492 | | type Error = E; |
1493 | | type Variant = VariantDeserializer<'de, Self::Error>; |
1494 | | |
1495 | 0 | fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), E> |
1496 | 0 | where |
1497 | 0 | V: de::DeserializeSeed<'de>, |
1498 | 0 | { |
1499 | 0 | let visitor = VariantDeserializer { |
1500 | 0 | value: self.value, |
1501 | 0 | err: PhantomData, |
1502 | 0 | }; |
1503 | 0 | seed.deserialize(ContentDeserializer::new(self.variant)) |
1504 | 0 | .map(|v| (v, visitor)) |
1505 | 0 | } |
1506 | | } |
1507 | | |
1508 | | pub struct VariantDeserializer<'de, E> |
1509 | | where |
1510 | | E: de::Error, |
1511 | | { |
1512 | | value: Option<Content<'de>>, |
1513 | | err: PhantomData<E>, |
1514 | | } |
1515 | | |
1516 | | impl<'de, E> de::VariantAccess<'de> for VariantDeserializer<'de, E> |
1517 | | where |
1518 | | E: de::Error, |
1519 | | { |
1520 | | type Error = E; |
1521 | | |
1522 | 0 | fn unit_variant(self) -> Result<(), E> { |
1523 | 0 | match self.value { |
1524 | 0 | Some(value) => de::Deserialize::deserialize(ContentDeserializer::new(value)), |
1525 | 0 | None => Ok(()), |
1526 | | } |
1527 | 0 | } |
1528 | | |
1529 | 0 | fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, E> |
1530 | 0 | where |
1531 | 0 | T: de::DeserializeSeed<'de>, |
1532 | 0 | { |
1533 | 0 | match self.value { |
1534 | 0 | Some(value) => seed.deserialize(ContentDeserializer::new(value)), |
1535 | 0 | None => Err(de::Error::invalid_type( |
1536 | 0 | de::Unexpected::UnitVariant, |
1537 | 0 | &"newtype variant", |
1538 | 0 | )), |
1539 | | } |
1540 | 0 | } |
1541 | | |
1542 | 0 | fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error> |
1543 | 0 | where |
1544 | 0 | V: de::Visitor<'de>, |
1545 | 0 | { |
1546 | 0 | match self.value { |
1547 | 0 | Some(Content::Seq(v)) => { |
1548 | 0 | de::Deserializer::deserialize_any(SeqDeserializer::new(v), visitor) |
1549 | | } |
1550 | 0 | Some(other) => Err(de::Error::invalid_type( |
1551 | 0 | other.unexpected(), |
1552 | 0 | &"tuple variant", |
1553 | 0 | )), |
1554 | 0 | None => Err(de::Error::invalid_type( |
1555 | 0 | de::Unexpected::UnitVariant, |
1556 | 0 | &"tuple variant", |
1557 | 0 | )), |
1558 | | } |
1559 | 0 | } |
1560 | | |
1561 | 0 | fn struct_variant<V>( |
1562 | 0 | self, |
1563 | 0 | _fields: &'static [&'static str], |
1564 | 0 | visitor: V, |
1565 | 0 | ) -> Result<V::Value, Self::Error> |
1566 | 0 | where |
1567 | 0 | V: de::Visitor<'de>, |
1568 | 0 | { |
1569 | 0 | match self.value { |
1570 | 0 | Some(Content::Map(v)) => { |
1571 | 0 | de::Deserializer::deserialize_any(MapDeserializer::new(v), visitor) |
1572 | | } |
1573 | 0 | Some(Content::Seq(v)) => { |
1574 | 0 | de::Deserializer::deserialize_any(SeqDeserializer::new(v), visitor) |
1575 | | } |
1576 | 0 | Some(other) => Err(de::Error::invalid_type( |
1577 | 0 | other.unexpected(), |
1578 | 0 | &"struct variant", |
1579 | 0 | )), |
1580 | 0 | None => Err(de::Error::invalid_type( |
1581 | 0 | de::Unexpected::UnitVariant, |
1582 | 0 | &"struct variant", |
1583 | 0 | )), |
1584 | | } |
1585 | 0 | } |
1586 | | } |
1587 | | |
1588 | | struct SeqDeserializer<'de, E> |
1589 | | where |
1590 | | E: de::Error, |
1591 | | { |
1592 | | iter: <Vec<Content<'de>> as IntoIterator>::IntoIter, |
1593 | | err: PhantomData<E>, |
1594 | | } |
1595 | | |
1596 | | impl<'de, E> SeqDeserializer<'de, E> |
1597 | | where |
1598 | | E: de::Error, |
1599 | | { |
1600 | 0 | fn new(vec: Vec<Content<'de>>) -> Self { |
1601 | 0 | SeqDeserializer { |
1602 | 0 | iter: vec.into_iter(), |
1603 | 0 | err: PhantomData, |
1604 | 0 | } |
1605 | 0 | } |
1606 | | } |
1607 | | |
1608 | | impl<'de, E> de::Deserializer<'de> for SeqDeserializer<'de, E> |
1609 | | where |
1610 | | E: de::Error, |
1611 | | { |
1612 | | type Error = E; |
1613 | | |
1614 | | #[inline] |
1615 | 0 | fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error> |
1616 | 0 | where |
1617 | 0 | V: de::Visitor<'de>, |
1618 | 0 | { |
1619 | 0 | let len = self.iter.len(); |
1620 | 0 | if len == 0 { |
1621 | 0 | visitor.visit_unit() |
1622 | | } else { |
1623 | 0 | let ret = try!(visitor.visit_seq(&mut self)); |
1624 | 0 | let remaining = self.iter.len(); |
1625 | 0 | if remaining == 0 { |
1626 | 0 | Ok(ret) |
1627 | | } else { |
1628 | 0 | Err(de::Error::invalid_length(len, &"fewer elements in array")) |
1629 | | } |
1630 | | } |
1631 | 0 | } |
1632 | | |
1633 | | forward_to_deserialize_any! { |
1634 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
1635 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
1636 | | tuple_struct map struct enum identifier ignored_any |
1637 | | } |
1638 | | } |
1639 | | |
1640 | | impl<'de, E> de::SeqAccess<'de> for SeqDeserializer<'de, E> |
1641 | | where |
1642 | | E: de::Error, |
1643 | | { |
1644 | | type Error = E; |
1645 | | |
1646 | 0 | fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> |
1647 | 0 | where |
1648 | 0 | T: de::DeserializeSeed<'de>, |
1649 | 0 | { |
1650 | 0 | match self.iter.next() { |
1651 | 0 | Some(value) => seed.deserialize(ContentDeserializer::new(value)).map(Some), |
1652 | 0 | None => Ok(None), |
1653 | | } |
1654 | 0 | } |
1655 | | |
1656 | 0 | fn size_hint(&self) -> Option<usize> { |
1657 | 0 | size_hint::from_bounds(&self.iter) |
1658 | 0 | } |
1659 | | } |
1660 | | |
1661 | | struct MapDeserializer<'de, E> |
1662 | | where |
1663 | | E: de::Error, |
1664 | | { |
1665 | | iter: <Vec<(Content<'de>, Content<'de>)> as IntoIterator>::IntoIter, |
1666 | | value: Option<Content<'de>>, |
1667 | | err: PhantomData<E>, |
1668 | | } |
1669 | | |
1670 | | impl<'de, E> MapDeserializer<'de, E> |
1671 | | where |
1672 | | E: de::Error, |
1673 | | { |
1674 | 0 | fn new(map: Vec<(Content<'de>, Content<'de>)>) -> Self { |
1675 | 0 | MapDeserializer { |
1676 | 0 | iter: map.into_iter(), |
1677 | 0 | value: None, |
1678 | 0 | err: PhantomData, |
1679 | 0 | } |
1680 | 0 | } |
1681 | | } |
1682 | | |
1683 | | impl<'de, E> de::MapAccess<'de> for MapDeserializer<'de, E> |
1684 | | where |
1685 | | E: de::Error, |
1686 | | { |
1687 | | type Error = E; |
1688 | | |
1689 | 0 | fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> |
1690 | 0 | where |
1691 | 0 | T: de::DeserializeSeed<'de>, |
1692 | 0 | { |
1693 | 0 | match self.iter.next() { |
1694 | 0 | Some((key, value)) => { |
1695 | 0 | self.value = Some(value); |
1696 | 0 | seed.deserialize(ContentDeserializer::new(key)).map(Some) |
1697 | | } |
1698 | 0 | None => Ok(None), |
1699 | | } |
1700 | 0 | } |
1701 | | |
1702 | 0 | fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error> |
1703 | 0 | where |
1704 | 0 | T: de::DeserializeSeed<'de>, |
1705 | 0 | { |
1706 | 0 | match self.value.take() { |
1707 | 0 | Some(value) => seed.deserialize(ContentDeserializer::new(value)), |
1708 | 0 | None => Err(de::Error::custom("value is missing")), |
1709 | | } |
1710 | 0 | } |
1711 | | |
1712 | 0 | fn size_hint(&self) -> Option<usize> { |
1713 | 0 | size_hint::from_bounds(&self.iter) |
1714 | 0 | } |
1715 | | } |
1716 | | |
1717 | | impl<'de, E> de::Deserializer<'de> for MapDeserializer<'de, E> |
1718 | | where |
1719 | | E: de::Error, |
1720 | | { |
1721 | | type Error = E; |
1722 | | |
1723 | | #[inline] |
1724 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1725 | 0 | where |
1726 | 0 | V: de::Visitor<'de>, |
1727 | 0 | { |
1728 | 0 | visitor.visit_map(self) |
1729 | 0 | } |
1730 | | |
1731 | | forward_to_deserialize_any! { |
1732 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
1733 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
1734 | | tuple_struct map struct enum identifier ignored_any |
1735 | | } |
1736 | | } |
1737 | | |
1738 | | /// Not public API. |
1739 | | pub struct ContentRefDeserializer<'a, 'de: 'a, E> { |
1740 | | content: &'a Content<'de>, |
1741 | | err: PhantomData<E>, |
1742 | | } |
1743 | | |
1744 | | impl<'a, 'de, E> ContentRefDeserializer<'a, 'de, E> |
1745 | | where |
1746 | | E: de::Error, |
1747 | | { |
1748 | | #[cold] |
1749 | 0 | fn invalid_type(self, exp: &Expected) -> E { |
1750 | 0 | de::Error::invalid_type(self.content.unexpected(), exp) |
1751 | 0 | } |
1752 | | |
1753 | 0 | fn deserialize_integer<V>(self, visitor: V) -> Result<V::Value, E> |
1754 | 0 | where |
1755 | 0 | V: Visitor<'de>, |
1756 | 0 | { |
1757 | 0 | match *self.content { |
1758 | 0 | Content::U8(v) => visitor.visit_u8(v), |
1759 | 0 | Content::U16(v) => visitor.visit_u16(v), |
1760 | 0 | Content::U32(v) => visitor.visit_u32(v), |
1761 | 0 | Content::U64(v) => visitor.visit_u64(v), |
1762 | 0 | Content::I8(v) => visitor.visit_i8(v), |
1763 | 0 | Content::I16(v) => visitor.visit_i16(v), |
1764 | 0 | Content::I32(v) => visitor.visit_i32(v), |
1765 | 0 | Content::I64(v) => visitor.visit_i64(v), |
1766 | 0 | _ => Err(self.invalid_type(&visitor)), |
1767 | | } |
1768 | 0 | } |
1769 | | |
1770 | 0 | fn deserialize_float<V>(self, visitor: V) -> Result<V::Value, E> |
1771 | 0 | where |
1772 | 0 | V: Visitor<'de>, |
1773 | 0 | { |
1774 | 0 | match *self.content { |
1775 | 0 | Content::F32(v) => visitor.visit_f32(v), |
1776 | 0 | Content::F64(v) => visitor.visit_f64(v), |
1777 | 0 | Content::U8(v) => visitor.visit_u8(v), |
1778 | 0 | Content::U16(v) => visitor.visit_u16(v), |
1779 | 0 | Content::U32(v) => visitor.visit_u32(v), |
1780 | 0 | Content::U64(v) => visitor.visit_u64(v), |
1781 | 0 | Content::I8(v) => visitor.visit_i8(v), |
1782 | 0 | Content::I16(v) => visitor.visit_i16(v), |
1783 | 0 | Content::I32(v) => visitor.visit_i32(v), |
1784 | 0 | Content::I64(v) => visitor.visit_i64(v), |
1785 | 0 | _ => Err(self.invalid_type(&visitor)), |
1786 | | } |
1787 | 0 | } |
1788 | | } |
1789 | | |
1790 | 0 | fn visit_content_seq_ref<'a, 'de, V, E>( |
1791 | 0 | content: &'a [Content<'de>], |
1792 | 0 | visitor: V, |
1793 | 0 | ) -> Result<V::Value, E> |
1794 | 0 | where |
1795 | 0 | V: Visitor<'de>, |
1796 | 0 | E: de::Error, |
1797 | 0 | { |
1798 | 0 | let seq = content.iter().map(ContentRefDeserializer::new); |
1799 | 0 | let mut seq_visitor = de::value::SeqDeserializer::new(seq); |
1800 | 0 | let value = try!(visitor.visit_seq(&mut seq_visitor)); |
1801 | 0 | try!(seq_visitor.end()); |
1802 | 0 | Ok(value) |
1803 | 0 | } |
1804 | | |
1805 | 0 | fn visit_content_map_ref<'a, 'de, V, E>( |
1806 | 0 | content: &'a [(Content<'de>, Content<'de>)], |
1807 | 0 | visitor: V, |
1808 | 0 | ) -> Result<V::Value, E> |
1809 | 0 | where |
1810 | 0 | V: Visitor<'de>, |
1811 | 0 | E: de::Error, |
1812 | 0 | { |
1813 | 0 | let map = content.iter().map(|(k, v)| { |
1814 | 0 | ( |
1815 | 0 | ContentRefDeserializer::new(k), |
1816 | 0 | ContentRefDeserializer::new(v), |
1817 | 0 | ) |
1818 | 0 | }); |
1819 | 0 | let mut map_visitor = de::value::MapDeserializer::new(map); |
1820 | 0 | let value = try!(visitor.visit_map(&mut map_visitor)); |
1821 | 0 | try!(map_visitor.end()); |
1822 | 0 | Ok(value) |
1823 | 0 | } |
1824 | | |
1825 | | /// Used when deserializing an untagged enum because the content may need |
1826 | | /// to be used more than once. |
1827 | | impl<'de, 'a, E> Deserializer<'de> for ContentRefDeserializer<'a, 'de, E> |
1828 | | where |
1829 | | E: de::Error, |
1830 | | { |
1831 | | type Error = E; |
1832 | | |
1833 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, E> |
1834 | 0 | where |
1835 | 0 | V: Visitor<'de>, |
1836 | 0 | { |
1837 | 0 | match *self.content { |
1838 | 0 | Content::Bool(v) => visitor.visit_bool(v), |
1839 | 0 | Content::U8(v) => visitor.visit_u8(v), |
1840 | 0 | Content::U16(v) => visitor.visit_u16(v), |
1841 | 0 | Content::U32(v) => visitor.visit_u32(v), |
1842 | 0 | Content::U64(v) => visitor.visit_u64(v), |
1843 | 0 | Content::I8(v) => visitor.visit_i8(v), |
1844 | 0 | Content::I16(v) => visitor.visit_i16(v), |
1845 | 0 | Content::I32(v) => visitor.visit_i32(v), |
1846 | 0 | Content::I64(v) => visitor.visit_i64(v), |
1847 | 0 | Content::F32(v) => visitor.visit_f32(v), |
1848 | 0 | Content::F64(v) => visitor.visit_f64(v), |
1849 | 0 | Content::Char(v) => visitor.visit_char(v), |
1850 | 0 | Content::String(ref v) => visitor.visit_str(v), |
1851 | 0 | Content::Str(v) => visitor.visit_borrowed_str(v), |
1852 | 0 | Content::ByteBuf(ref v) => visitor.visit_bytes(v), |
1853 | 0 | Content::Bytes(v) => visitor.visit_borrowed_bytes(v), |
1854 | 0 | Content::Unit => visitor.visit_unit(), |
1855 | 0 | Content::None => visitor.visit_none(), |
1856 | 0 | Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)), |
1857 | 0 | Content::Newtype(ref v) => { |
1858 | 0 | visitor.visit_newtype_struct(ContentRefDeserializer::new(v)) |
1859 | | } |
1860 | 0 | Content::Seq(ref v) => visit_content_seq_ref(v, visitor), |
1861 | 0 | Content::Map(ref v) => visit_content_map_ref(v, visitor), |
1862 | | } |
1863 | 0 | } |
1864 | | |
1865 | 0 | fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1866 | 0 | where |
1867 | 0 | V: Visitor<'de>, |
1868 | 0 | { |
1869 | 0 | match *self.content { |
1870 | 0 | Content::Bool(v) => visitor.visit_bool(v), |
1871 | 0 | _ => Err(self.invalid_type(&visitor)), |
1872 | | } |
1873 | 0 | } |
1874 | | |
1875 | 0 | fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1876 | 0 | where |
1877 | 0 | V: Visitor<'de>, |
1878 | 0 | { |
1879 | 0 | self.deserialize_integer(visitor) |
1880 | 0 | } |
1881 | | |
1882 | 0 | fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1883 | 0 | where |
1884 | 0 | V: Visitor<'de>, |
1885 | 0 | { |
1886 | 0 | self.deserialize_integer(visitor) |
1887 | 0 | } |
1888 | | |
1889 | 0 | fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1890 | 0 | where |
1891 | 0 | V: Visitor<'de>, |
1892 | 0 | { |
1893 | 0 | self.deserialize_integer(visitor) |
1894 | 0 | } |
1895 | | |
1896 | 0 | fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1897 | 0 | where |
1898 | 0 | V: Visitor<'de>, |
1899 | 0 | { |
1900 | 0 | self.deserialize_integer(visitor) |
1901 | 0 | } |
1902 | | |
1903 | 0 | fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1904 | 0 | where |
1905 | 0 | V: Visitor<'de>, |
1906 | 0 | { |
1907 | 0 | self.deserialize_integer(visitor) |
1908 | 0 | } |
1909 | | |
1910 | 0 | fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1911 | 0 | where |
1912 | 0 | V: Visitor<'de>, |
1913 | 0 | { |
1914 | 0 | self.deserialize_integer(visitor) |
1915 | 0 | } |
1916 | | |
1917 | 0 | fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1918 | 0 | where |
1919 | 0 | V: Visitor<'de>, |
1920 | 0 | { |
1921 | 0 | self.deserialize_integer(visitor) |
1922 | 0 | } |
1923 | | |
1924 | 0 | fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1925 | 0 | where |
1926 | 0 | V: Visitor<'de>, |
1927 | 0 | { |
1928 | 0 | self.deserialize_integer(visitor) |
1929 | 0 | } |
1930 | | |
1931 | 0 | fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1932 | 0 | where |
1933 | 0 | V: Visitor<'de>, |
1934 | 0 | { |
1935 | 0 | self.deserialize_float(visitor) |
1936 | 0 | } |
1937 | | |
1938 | 0 | fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1939 | 0 | where |
1940 | 0 | V: Visitor<'de>, |
1941 | 0 | { |
1942 | 0 | self.deserialize_float(visitor) |
1943 | 0 | } |
1944 | | |
1945 | 0 | fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1946 | 0 | where |
1947 | 0 | V: Visitor<'de>, |
1948 | 0 | { |
1949 | 0 | match *self.content { |
1950 | 0 | Content::Char(v) => visitor.visit_char(v), |
1951 | 0 | Content::String(ref v) => visitor.visit_str(v), |
1952 | 0 | Content::Str(v) => visitor.visit_borrowed_str(v), |
1953 | 0 | _ => Err(self.invalid_type(&visitor)), |
1954 | | } |
1955 | 0 | } |
1956 | | |
1957 | 0 | fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1958 | 0 | where |
1959 | 0 | V: Visitor<'de>, |
1960 | 0 | { |
1961 | 0 | match *self.content { |
1962 | 0 | Content::String(ref v) => visitor.visit_str(v), |
1963 | 0 | Content::Str(v) => visitor.visit_borrowed_str(v), |
1964 | 0 | Content::ByteBuf(ref v) => visitor.visit_bytes(v), |
1965 | 0 | Content::Bytes(v) => visitor.visit_borrowed_bytes(v), |
1966 | 0 | _ => Err(self.invalid_type(&visitor)), |
1967 | | } |
1968 | 0 | } |
1969 | | |
1970 | 0 | fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1971 | 0 | where |
1972 | 0 | V: Visitor<'de>, |
1973 | 0 | { |
1974 | 0 | self.deserialize_str(visitor) |
1975 | 0 | } |
1976 | | |
1977 | 0 | fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1978 | 0 | where |
1979 | 0 | V: Visitor<'de>, |
1980 | 0 | { |
1981 | 0 | match *self.content { |
1982 | 0 | Content::String(ref v) => visitor.visit_str(v), |
1983 | 0 | Content::Str(v) => visitor.visit_borrowed_str(v), |
1984 | 0 | Content::ByteBuf(ref v) => visitor.visit_bytes(v), |
1985 | 0 | Content::Bytes(v) => visitor.visit_borrowed_bytes(v), |
1986 | 0 | Content::Seq(ref v) => visit_content_seq_ref(v, visitor), |
1987 | 0 | _ => Err(self.invalid_type(&visitor)), |
1988 | | } |
1989 | 0 | } |
1990 | | |
1991 | 0 | fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
1992 | 0 | where |
1993 | 0 | V: Visitor<'de>, |
1994 | 0 | { |
1995 | 0 | self.deserialize_bytes(visitor) |
1996 | 0 | } |
1997 | | |
1998 | 0 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, E> |
1999 | 0 | where |
2000 | 0 | V: Visitor<'de>, |
2001 | 0 | { |
2002 | 0 | match *self.content { |
2003 | 0 | Content::None => visitor.visit_none(), |
2004 | 0 | Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)), |
2005 | 0 | Content::Unit => visitor.visit_unit(), |
2006 | 0 | _ => visitor.visit_some(self), |
2007 | | } |
2008 | 0 | } |
2009 | | |
2010 | 0 | fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
2011 | 0 | where |
2012 | 0 | V: Visitor<'de>, |
2013 | 0 | { |
2014 | 0 | match *self.content { |
2015 | 0 | Content::Unit => visitor.visit_unit(), |
2016 | 0 | _ => Err(self.invalid_type(&visitor)), |
2017 | | } |
2018 | 0 | } |
2019 | | |
2020 | 0 | fn deserialize_unit_struct<V>( |
2021 | 0 | self, |
2022 | 0 | _name: &'static str, |
2023 | 0 | visitor: V, |
2024 | 0 | ) -> Result<V::Value, Self::Error> |
2025 | 0 | where |
2026 | 0 | V: Visitor<'de>, |
2027 | 0 | { |
2028 | 0 | self.deserialize_unit(visitor) |
2029 | 0 | } |
2030 | | |
2031 | 0 | fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, E> |
2032 | 0 | where |
2033 | 0 | V: Visitor<'de>, |
2034 | 0 | { |
2035 | 0 | match *self.content { |
2036 | 0 | Content::Newtype(ref v) => { |
2037 | 0 | visitor.visit_newtype_struct(ContentRefDeserializer::new(v)) |
2038 | | } |
2039 | 0 | _ => visitor.visit_newtype_struct(self), |
2040 | | } |
2041 | 0 | } |
2042 | | |
2043 | 0 | fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
2044 | 0 | where |
2045 | 0 | V: Visitor<'de>, |
2046 | 0 | { |
2047 | 0 | match *self.content { |
2048 | 0 | Content::Seq(ref v) => visit_content_seq_ref(v, visitor), |
2049 | 0 | _ => Err(self.invalid_type(&visitor)), |
2050 | | } |
2051 | 0 | } |
2052 | | |
2053 | 0 | fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error> |
2054 | 0 | where |
2055 | 0 | V: Visitor<'de>, |
2056 | 0 | { |
2057 | 0 | self.deserialize_seq(visitor) |
2058 | 0 | } |
2059 | | |
2060 | 0 | fn deserialize_tuple_struct<V>( |
2061 | 0 | self, |
2062 | 0 | _name: &'static str, |
2063 | 0 | _len: usize, |
2064 | 0 | visitor: V, |
2065 | 0 | ) -> Result<V::Value, Self::Error> |
2066 | 0 | where |
2067 | 0 | V: Visitor<'de>, |
2068 | 0 | { |
2069 | 0 | self.deserialize_seq(visitor) |
2070 | 0 | } |
2071 | | |
2072 | 0 | fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
2073 | 0 | where |
2074 | 0 | V: Visitor<'de>, |
2075 | 0 | { |
2076 | 0 | match *self.content { |
2077 | 0 | Content::Map(ref v) => visit_content_map_ref(v, visitor), |
2078 | 0 | _ => Err(self.invalid_type(&visitor)), |
2079 | | } |
2080 | 0 | } |
2081 | | |
2082 | 0 | fn deserialize_struct<V>( |
2083 | 0 | self, |
2084 | 0 | _name: &'static str, |
2085 | 0 | _fields: &'static [&'static str], |
2086 | 0 | visitor: V, |
2087 | 0 | ) -> Result<V::Value, Self::Error> |
2088 | 0 | where |
2089 | 0 | V: Visitor<'de>, |
2090 | 0 | { |
2091 | 0 | match *self.content { |
2092 | 0 | Content::Seq(ref v) => visit_content_seq_ref(v, visitor), |
2093 | 0 | Content::Map(ref v) => visit_content_map_ref(v, visitor), |
2094 | 0 | _ => Err(self.invalid_type(&visitor)), |
2095 | | } |
2096 | 0 | } |
2097 | | |
2098 | 0 | fn deserialize_enum<V>( |
2099 | 0 | self, |
2100 | 0 | _name: &str, |
2101 | 0 | _variants: &'static [&'static str], |
2102 | 0 | visitor: V, |
2103 | 0 | ) -> Result<V::Value, Self::Error> |
2104 | 0 | where |
2105 | 0 | V: Visitor<'de>, |
2106 | 0 | { |
2107 | 0 | let (variant, value) = match *self.content { |
2108 | 0 | Content::Map(ref value) => { |
2109 | 0 | let mut iter = value.iter(); |
2110 | 0 | let (variant, value) = match iter.next() { |
2111 | 0 | Some(v) => v, |
2112 | | None => { |
2113 | 0 | return Err(de::Error::invalid_value( |
2114 | 0 | de::Unexpected::Map, |
2115 | 0 | &"map with a single key", |
2116 | 0 | )); |
2117 | | } |
2118 | | }; |
2119 | | // enums are encoded in json as maps with a single key:value pair |
2120 | 0 | if iter.next().is_some() { |
2121 | 0 | return Err(de::Error::invalid_value( |
2122 | 0 | de::Unexpected::Map, |
2123 | 0 | &"map with a single key", |
2124 | 0 | )); |
2125 | 0 | } |
2126 | 0 | (variant, Some(value)) |
2127 | | } |
2128 | 0 | ref s @ Content::String(_) | ref s @ Content::Str(_) => (s, None), |
2129 | 0 | ref other => { |
2130 | 0 | return Err(de::Error::invalid_type( |
2131 | 0 | other.unexpected(), |
2132 | 0 | &"string or map", |
2133 | 0 | )); |
2134 | | } |
2135 | | }; |
2136 | | |
2137 | 0 | visitor.visit_enum(EnumRefDeserializer { |
2138 | 0 | variant: variant, |
2139 | 0 | value: value, |
2140 | 0 | err: PhantomData, |
2141 | 0 | }) |
2142 | 0 | } |
2143 | | |
2144 | 0 | fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
2145 | 0 | where |
2146 | 0 | V: Visitor<'de>, |
2147 | 0 | { |
2148 | 0 | match *self.content { |
2149 | 0 | Content::String(ref v) => visitor.visit_str(v), |
2150 | 0 | Content::Str(v) => visitor.visit_borrowed_str(v), |
2151 | 0 | Content::ByteBuf(ref v) => visitor.visit_bytes(v), |
2152 | 0 | Content::Bytes(v) => visitor.visit_borrowed_bytes(v), |
2153 | 0 | Content::U8(v) => visitor.visit_u8(v), |
2154 | 0 | Content::U64(v) => visitor.visit_u64(v), |
2155 | 0 | _ => Err(self.invalid_type(&visitor)), |
2156 | | } |
2157 | 0 | } |
2158 | | |
2159 | 0 | fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
2160 | 0 | where |
2161 | 0 | V: Visitor<'de>, |
2162 | 0 | { |
2163 | 0 | visitor.visit_unit() |
2164 | 0 | } |
2165 | | |
2166 | 0 | fn __deserialize_content<V>( |
2167 | 0 | self, |
2168 | 0 | _: actually_private::T, |
2169 | 0 | visitor: V, |
2170 | 0 | ) -> Result<Content<'de>, Self::Error> |
2171 | 0 | where |
2172 | 0 | V: Visitor<'de, Value = Content<'de>>, |
2173 | 0 | { |
2174 | 0 | let _ = visitor; |
2175 | 0 | Ok(self.content.clone()) |
2176 | 0 | } |
2177 | | } |
2178 | | |
2179 | | impl<'a, 'de, E> ContentRefDeserializer<'a, 'de, E> { |
2180 | | /// private API, don't use |
2181 | 0 | pub fn new(content: &'a Content<'de>) -> Self { |
2182 | 0 | ContentRefDeserializer { |
2183 | 0 | content: content, |
2184 | 0 | err: PhantomData, |
2185 | 0 | } |
2186 | 0 | } |
2187 | | } |
2188 | | |
2189 | | struct EnumRefDeserializer<'a, 'de: 'a, E> |
2190 | | where |
2191 | | E: de::Error, |
2192 | | { |
2193 | | variant: &'a Content<'de>, |
2194 | | value: Option<&'a Content<'de>>, |
2195 | | err: PhantomData<E>, |
2196 | | } |
2197 | | |
2198 | | impl<'de, 'a, E> de::EnumAccess<'de> for EnumRefDeserializer<'a, 'de, E> |
2199 | | where |
2200 | | E: de::Error, |
2201 | | { |
2202 | | type Error = E; |
2203 | | type Variant = VariantRefDeserializer<'a, 'de, Self::Error>; |
2204 | | |
2205 | 0 | fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error> |
2206 | 0 | where |
2207 | 0 | V: de::DeserializeSeed<'de>, |
2208 | 0 | { |
2209 | 0 | let visitor = VariantRefDeserializer { |
2210 | 0 | value: self.value, |
2211 | 0 | err: PhantomData, |
2212 | 0 | }; |
2213 | 0 | seed.deserialize(ContentRefDeserializer::new(self.variant)) |
2214 | 0 | .map(|v| (v, visitor)) |
2215 | 0 | } |
2216 | | } |
2217 | | |
2218 | | struct VariantRefDeserializer<'a, 'de: 'a, E> |
2219 | | where |
2220 | | E: de::Error, |
2221 | | { |
2222 | | value: Option<&'a Content<'de>>, |
2223 | | err: PhantomData<E>, |
2224 | | } |
2225 | | |
2226 | | impl<'de, 'a, E> de::VariantAccess<'de> for VariantRefDeserializer<'a, 'de, E> |
2227 | | where |
2228 | | E: de::Error, |
2229 | | { |
2230 | | type Error = E; |
2231 | | |
2232 | 0 | fn unit_variant(self) -> Result<(), E> { |
2233 | 0 | match self.value { |
2234 | 0 | Some(value) => de::Deserialize::deserialize(ContentRefDeserializer::new(value)), |
2235 | 0 | None => Ok(()), |
2236 | | } |
2237 | 0 | } |
2238 | | |
2239 | 0 | fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, E> |
2240 | 0 | where |
2241 | 0 | T: de::DeserializeSeed<'de>, |
2242 | 0 | { |
2243 | 0 | match self.value { |
2244 | 0 | Some(value) => seed.deserialize(ContentRefDeserializer::new(value)), |
2245 | 0 | None => Err(de::Error::invalid_type( |
2246 | 0 | de::Unexpected::UnitVariant, |
2247 | 0 | &"newtype variant", |
2248 | 0 | )), |
2249 | | } |
2250 | 0 | } |
2251 | | |
2252 | 0 | fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error> |
2253 | 0 | where |
2254 | 0 | V: de::Visitor<'de>, |
2255 | 0 | { |
2256 | 0 | match self.value { |
2257 | 0 | Some(Content::Seq(v)) => { |
2258 | 0 | de::Deserializer::deserialize_any(SeqRefDeserializer::new(v), visitor) |
2259 | | } |
2260 | 0 | Some(other) => Err(de::Error::invalid_type( |
2261 | 0 | other.unexpected(), |
2262 | 0 | &"tuple variant", |
2263 | 0 | )), |
2264 | 0 | None => Err(de::Error::invalid_type( |
2265 | 0 | de::Unexpected::UnitVariant, |
2266 | 0 | &"tuple variant", |
2267 | 0 | )), |
2268 | | } |
2269 | 0 | } |
2270 | | |
2271 | 0 | fn struct_variant<V>( |
2272 | 0 | self, |
2273 | 0 | _fields: &'static [&'static str], |
2274 | 0 | visitor: V, |
2275 | 0 | ) -> Result<V::Value, Self::Error> |
2276 | 0 | where |
2277 | 0 | V: de::Visitor<'de>, |
2278 | 0 | { |
2279 | 0 | match self.value { |
2280 | 0 | Some(Content::Map(v)) => { |
2281 | 0 | de::Deserializer::deserialize_any(MapRefDeserializer::new(v), visitor) |
2282 | | } |
2283 | 0 | Some(Content::Seq(v)) => { |
2284 | 0 | de::Deserializer::deserialize_any(SeqRefDeserializer::new(v), visitor) |
2285 | | } |
2286 | 0 | Some(other) => Err(de::Error::invalid_type( |
2287 | 0 | other.unexpected(), |
2288 | 0 | &"struct variant", |
2289 | 0 | )), |
2290 | 0 | None => Err(de::Error::invalid_type( |
2291 | 0 | de::Unexpected::UnitVariant, |
2292 | 0 | &"struct variant", |
2293 | 0 | )), |
2294 | | } |
2295 | 0 | } |
2296 | | } |
2297 | | |
2298 | | struct SeqRefDeserializer<'a, 'de: 'a, E> |
2299 | | where |
2300 | | E: de::Error, |
2301 | | { |
2302 | | iter: <&'a [Content<'de>] as IntoIterator>::IntoIter, |
2303 | | err: PhantomData<E>, |
2304 | | } |
2305 | | |
2306 | | impl<'a, 'de, E> SeqRefDeserializer<'a, 'de, E> |
2307 | | where |
2308 | | E: de::Error, |
2309 | | { |
2310 | 0 | fn new(slice: &'a [Content<'de>]) -> Self { |
2311 | 0 | SeqRefDeserializer { |
2312 | 0 | iter: slice.iter(), |
2313 | 0 | err: PhantomData, |
2314 | 0 | } |
2315 | 0 | } |
2316 | | } |
2317 | | |
2318 | | impl<'de, 'a, E> de::Deserializer<'de> for SeqRefDeserializer<'a, 'de, E> |
2319 | | where |
2320 | | E: de::Error, |
2321 | | { |
2322 | | type Error = E; |
2323 | | |
2324 | | #[inline] |
2325 | 0 | fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error> |
2326 | 0 | where |
2327 | 0 | V: de::Visitor<'de>, |
2328 | 0 | { |
2329 | 0 | let len = self.iter.len(); |
2330 | 0 | if len == 0 { |
2331 | 0 | visitor.visit_unit() |
2332 | | } else { |
2333 | 0 | let ret = try!(visitor.visit_seq(&mut self)); |
2334 | 0 | let remaining = self.iter.len(); |
2335 | 0 | if remaining == 0 { |
2336 | 0 | Ok(ret) |
2337 | | } else { |
2338 | 0 | Err(de::Error::invalid_length(len, &"fewer elements in array")) |
2339 | | } |
2340 | | } |
2341 | 0 | } |
2342 | | |
2343 | | forward_to_deserialize_any! { |
2344 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
2345 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
2346 | | tuple_struct map struct enum identifier ignored_any |
2347 | | } |
2348 | | } |
2349 | | |
2350 | | impl<'de, 'a, E> de::SeqAccess<'de> for SeqRefDeserializer<'a, 'de, E> |
2351 | | where |
2352 | | E: de::Error, |
2353 | | { |
2354 | | type Error = E; |
2355 | | |
2356 | 0 | fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> |
2357 | 0 | where |
2358 | 0 | T: de::DeserializeSeed<'de>, |
2359 | 0 | { |
2360 | 0 | match self.iter.next() { |
2361 | 0 | Some(value) => seed |
2362 | 0 | .deserialize(ContentRefDeserializer::new(value)) |
2363 | 0 | .map(Some), |
2364 | 0 | None => Ok(None), |
2365 | | } |
2366 | 0 | } |
2367 | | |
2368 | 0 | fn size_hint(&self) -> Option<usize> { |
2369 | 0 | size_hint::from_bounds(&self.iter) |
2370 | 0 | } |
2371 | | } |
2372 | | |
2373 | | struct MapRefDeserializer<'a, 'de: 'a, E> |
2374 | | where |
2375 | | E: de::Error, |
2376 | | { |
2377 | | iter: <&'a [(Content<'de>, Content<'de>)] as IntoIterator>::IntoIter, |
2378 | | value: Option<&'a Content<'de>>, |
2379 | | err: PhantomData<E>, |
2380 | | } |
2381 | | |
2382 | | impl<'a, 'de, E> MapRefDeserializer<'a, 'de, E> |
2383 | | where |
2384 | | E: de::Error, |
2385 | | { |
2386 | 0 | fn new(map: &'a [(Content<'de>, Content<'de>)]) -> Self { |
2387 | 0 | MapRefDeserializer { |
2388 | 0 | iter: map.iter(), |
2389 | 0 | value: None, |
2390 | 0 | err: PhantomData, |
2391 | 0 | } |
2392 | 0 | } |
2393 | | } |
2394 | | |
2395 | | impl<'de, 'a, E> de::MapAccess<'de> for MapRefDeserializer<'a, 'de, E> |
2396 | | where |
2397 | | E: de::Error, |
2398 | | { |
2399 | | type Error = E; |
2400 | | |
2401 | 0 | fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> |
2402 | 0 | where |
2403 | 0 | T: de::DeserializeSeed<'de>, |
2404 | 0 | { |
2405 | 0 | match self.iter.next() { |
2406 | 0 | Some((key, value)) => { |
2407 | 0 | self.value = Some(value); |
2408 | 0 | seed.deserialize(ContentRefDeserializer::new(key)).map(Some) |
2409 | | } |
2410 | 0 | None => Ok(None), |
2411 | | } |
2412 | 0 | } |
2413 | | |
2414 | 0 | fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error> |
2415 | 0 | where |
2416 | 0 | T: de::DeserializeSeed<'de>, |
2417 | 0 | { |
2418 | 0 | match self.value.take() { |
2419 | 0 | Some(value) => seed.deserialize(ContentRefDeserializer::new(value)), |
2420 | 0 | None => Err(de::Error::custom("value is missing")), |
2421 | | } |
2422 | 0 | } |
2423 | | |
2424 | 0 | fn size_hint(&self) -> Option<usize> { |
2425 | 0 | size_hint::from_bounds(&self.iter) |
2426 | 0 | } |
2427 | | } |
2428 | | |
2429 | | impl<'de, 'a, E> de::Deserializer<'de> for MapRefDeserializer<'a, 'de, E> |
2430 | | where |
2431 | | E: de::Error, |
2432 | | { |
2433 | | type Error = E; |
2434 | | |
2435 | | #[inline] |
2436 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
2437 | 0 | where |
2438 | 0 | V: de::Visitor<'de>, |
2439 | 0 | { |
2440 | 0 | visitor.visit_map(self) |
2441 | 0 | } |
2442 | | |
2443 | | forward_to_deserialize_any! { |
2444 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
2445 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
2446 | | tuple_struct map struct enum identifier ignored_any |
2447 | | } |
2448 | | } |
2449 | | |
2450 | | impl<'de, E> de::IntoDeserializer<'de, E> for ContentDeserializer<'de, E> |
2451 | | where |
2452 | | E: de::Error, |
2453 | | { |
2454 | | type Deserializer = Self; |
2455 | | |
2456 | 0 | fn into_deserializer(self) -> Self { |
2457 | 0 | self |
2458 | 0 | } |
2459 | | } |
2460 | | |
2461 | | impl<'de, 'a, E> de::IntoDeserializer<'de, E> for ContentRefDeserializer<'a, 'de, E> |
2462 | | where |
2463 | | E: de::Error, |
2464 | | { |
2465 | | type Deserializer = Self; |
2466 | | |
2467 | 0 | fn into_deserializer(self) -> Self { |
2468 | 0 | self |
2469 | 0 | } |
2470 | | } |
2471 | | |
2472 | | /// Visitor for deserializing an internally tagged unit variant. |
2473 | | /// |
2474 | | /// Not public API. |
2475 | | pub struct InternallyTaggedUnitVisitor<'a> { |
2476 | | type_name: &'a str, |
2477 | | variant_name: &'a str, |
2478 | | } |
2479 | | |
2480 | | impl<'a> InternallyTaggedUnitVisitor<'a> { |
2481 | | /// Not public API. |
2482 | 0 | pub fn new(type_name: &'a str, variant_name: &'a str) -> Self { |
2483 | 0 | InternallyTaggedUnitVisitor { |
2484 | 0 | type_name: type_name, |
2485 | 0 | variant_name: variant_name, |
2486 | 0 | } |
2487 | 0 | } |
2488 | | } |
2489 | | |
2490 | | impl<'de, 'a> Visitor<'de> for InternallyTaggedUnitVisitor<'a> { |
2491 | | type Value = (); |
2492 | | |
2493 | 0 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
2494 | 0 | write!( |
2495 | 0 | formatter, |
2496 | 0 | "unit variant {}::{}", |
2497 | 0 | self.type_name, self.variant_name |
2498 | 0 | ) |
2499 | 0 | } |
2500 | | |
2501 | 0 | fn visit_seq<S>(self, _: S) -> Result<(), S::Error> |
2502 | 0 | where |
2503 | 0 | S: SeqAccess<'de>, |
2504 | 0 | { |
2505 | 0 | Ok(()) |
2506 | 0 | } |
2507 | | |
2508 | 0 | fn visit_map<M>(self, mut access: M) -> Result<(), M::Error> |
2509 | 0 | where |
2510 | 0 | M: MapAccess<'de>, |
2511 | 0 | { |
2512 | 0 | while try!(access.next_entry::<IgnoredAny, IgnoredAny>()).is_some() {} |
2513 | 0 | Ok(()) |
2514 | 0 | } |
2515 | | } |
2516 | | |
2517 | | /// Visitor for deserializing an untagged unit variant. |
2518 | | /// |
2519 | | /// Not public API. |
2520 | | pub struct UntaggedUnitVisitor<'a> { |
2521 | | type_name: &'a str, |
2522 | | variant_name: &'a str, |
2523 | | } |
2524 | | |
2525 | | impl<'a> UntaggedUnitVisitor<'a> { |
2526 | | /// Not public API. |
2527 | 0 | pub fn new(type_name: &'a str, variant_name: &'a str) -> Self { |
2528 | 0 | UntaggedUnitVisitor { |
2529 | 0 | type_name: type_name, |
2530 | 0 | variant_name: variant_name, |
2531 | 0 | } |
2532 | 0 | } |
2533 | | } |
2534 | | |
2535 | | impl<'de, 'a> Visitor<'de> for UntaggedUnitVisitor<'a> { |
2536 | | type Value = (); |
2537 | | |
2538 | 0 | fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
2539 | 0 | write!( |
2540 | 0 | formatter, |
2541 | 0 | "unit variant {}::{}", |
2542 | 0 | self.type_name, self.variant_name |
2543 | 0 | ) |
2544 | 0 | } |
2545 | | |
2546 | 0 | fn visit_unit<E>(self) -> Result<(), E> |
2547 | 0 | where |
2548 | 0 | E: de::Error, |
2549 | 0 | { |
2550 | 0 | Ok(()) |
2551 | 0 | } |
2552 | | |
2553 | 0 | fn visit_none<E>(self) -> Result<(), E> |
2554 | 0 | where |
2555 | 0 | E: de::Error, |
2556 | 0 | { |
2557 | 0 | Ok(()) |
2558 | 0 | } |
2559 | | } |
2560 | | } |
2561 | | |
2562 | | //////////////////////////////////////////////////////////////////////////////// |
2563 | | |
2564 | | // Like `IntoDeserializer` but also implemented for `&[u8]`. This is used for |
2565 | | // the newtype fallthrough case of `field_identifier`. |
2566 | | // |
2567 | | // #[derive(Deserialize)] |
2568 | | // #[serde(field_identifier)] |
2569 | | // enum F { |
2570 | | // A, |
2571 | | // B, |
2572 | | // Other(String), // deserialized using IdentifierDeserializer |
2573 | | // } |
2574 | | pub trait IdentifierDeserializer<'de, E: Error> { |
2575 | | type Deserializer: Deserializer<'de, Error = E>; |
2576 | | |
2577 | | fn from(self) -> Self::Deserializer; |
2578 | | } |
2579 | | |
2580 | | pub struct Borrowed<'de, T: 'de + ?Sized>(pub &'de T); |
2581 | | |
2582 | | impl<'de, E> IdentifierDeserializer<'de, E> for u64 |
2583 | | where |
2584 | | E: Error, |
2585 | | { |
2586 | | type Deserializer = <u64 as IntoDeserializer<'de, E>>::Deserializer; |
2587 | | |
2588 | 0 | fn from(self) -> Self::Deserializer { |
2589 | 0 | self.into_deserializer() |
2590 | 0 | } |
2591 | | } |
2592 | | |
2593 | | pub struct StrDeserializer<'a, E> { |
2594 | | value: &'a str, |
2595 | | marker: PhantomData<E>, |
2596 | | } |
2597 | | |
2598 | | impl<'de, 'a, E> Deserializer<'de> for StrDeserializer<'a, E> |
2599 | | where |
2600 | | E: Error, |
2601 | | { |
2602 | | type Error = E; |
2603 | | |
2604 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
2605 | 0 | where |
2606 | 0 | V: Visitor<'de>, |
2607 | 0 | { |
2608 | 0 | visitor.visit_str(self.value) |
2609 | 0 | } |
2610 | | |
2611 | | forward_to_deserialize_any! { |
2612 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
2613 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
2614 | | tuple_struct map struct enum identifier ignored_any |
2615 | | } |
2616 | | } |
2617 | | |
2618 | | pub struct BorrowedStrDeserializer<'de, E> { |
2619 | | value: &'de str, |
2620 | | marker: PhantomData<E>, |
2621 | | } |
2622 | | |
2623 | | impl<'de, E> Deserializer<'de> for BorrowedStrDeserializer<'de, E> |
2624 | | where |
2625 | | E: Error, |
2626 | | { |
2627 | | type Error = E; |
2628 | | |
2629 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
2630 | 0 | where |
2631 | 0 | V: Visitor<'de>, |
2632 | 0 | { |
2633 | 0 | visitor.visit_borrowed_str(self.value) |
2634 | 0 | } |
2635 | | |
2636 | | forward_to_deserialize_any! { |
2637 | | bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string |
2638 | | bytes byte_buf option unit unit_struct newtype_struct seq tuple |
2639 | | tuple_struct map struct enum identifier ignored_any |
2640 | | } |
2641 | | } |
2642 | | |
2643 | | impl<'a, E> IdentifierDeserializer<'a, E> for &'a str |
2644 | | where |
2645 | | E: Error, |
2646 | | { |
2647 | | type Deserializer = StrDeserializer<'a, E>; |
2648 | | |
2649 | 0 | fn from(self) -> Self::Deserializer { |
2650 | 0 | StrDeserializer { |
2651 | 0 | value: self, |
2652 | 0 | marker: PhantomData, |
2653 | 0 | } |
2654 | 0 | } |
2655 | | } |
2656 | | |
2657 | | impl<'de, E> IdentifierDeserializer<'de, E> for Borrowed<'de, str> |
2658 | | where |
2659 | | E: Error, |
2660 | | { |
2661 | | type Deserializer = BorrowedStrDeserializer<'de, E>; |
2662 | | |
2663 | 0 | fn from(self) -> Self::Deserializer { |
2664 | 0 | BorrowedStrDeserializer { |
2665 | 0 | value: self.0, |
2666 | 0 | marker: PhantomData, |
2667 | 0 | } |
2668 | 0 | } |
2669 | | } |
2670 | | |
2671 | | impl<'a, E> IdentifierDeserializer<'a, E> for &'a [u8] |
2672 | | where |
2673 | | E: Error, |
2674 | | { |
2675 | | type Deserializer = BytesDeserializer<'a, E>; |
2676 | | |
2677 | 0 | fn from(self) -> Self::Deserializer { |
2678 | 0 | BytesDeserializer::new(self) |
2679 | 0 | } |
2680 | | } |
2681 | | |
2682 | | impl<'de, E> IdentifierDeserializer<'de, E> for Borrowed<'de, [u8]> |
2683 | | where |
2684 | | E: Error, |
2685 | | { |
2686 | | type Deserializer = BorrowedBytesDeserializer<'de, E>; |
2687 | | |
2688 | 0 | fn from(self) -> Self::Deserializer { |
2689 | 0 | BorrowedBytesDeserializer::new(self.0) |
2690 | 0 | } |
2691 | | } |
2692 | | |
2693 | | #[cfg(any(feature = "std", feature = "alloc"))] |
2694 | | pub struct FlatMapDeserializer<'a, 'de: 'a, E>( |
2695 | | pub &'a mut Vec<Option<(Content<'de>, Content<'de>)>>, |
2696 | | pub PhantomData<E>, |
2697 | | ); |
2698 | | |
2699 | | #[cfg(any(feature = "std", feature = "alloc"))] |
2700 | | impl<'a, 'de, E> FlatMapDeserializer<'a, 'de, E> |
2701 | | where |
2702 | | E: Error, |
2703 | | { |
2704 | 0 | fn deserialize_other<V>() -> Result<V, E> { |
2705 | 0 | Err(Error::custom("can only flatten structs and maps")) |
2706 | 0 | } |
2707 | | } |
2708 | | |
2709 | | #[cfg(any(feature = "std", feature = "alloc"))] |
2710 | | macro_rules! forward_to_deserialize_other { |
2711 | | ($($func:ident ($($arg:ty),*))*) => { |
2712 | | $( |
2713 | 0 | fn $func<V>(self, $(_: $arg,)* _visitor: V) -> Result<V::Value, Self::Error> |
2714 | 0 | where |
2715 | 0 | V: Visitor<'de>, |
2716 | 0 | { |
2717 | 0 | Self::deserialize_other() |
2718 | 0 | } Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_bool::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_i8::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_i16::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_i32::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_i64::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_u8::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_u16::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_u32::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_u64::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_f32::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_f64::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_char::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_str::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_string::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_bytes::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_byte_buf::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_unit_struct::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_seq::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_tuple::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_tuple_struct::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_identifier::<_> Unexecuted instantiation: <serde::__private::de::FlatMapDeserializer<_> as serde::de::Deserializer>::deserialize_ignored_any::<_> |
2719 | | )* |
2720 | | } |
2721 | | } |
2722 | | |
2723 | | #[cfg(any(feature = "std", feature = "alloc"))] |
2724 | | impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E> |
2725 | | where |
2726 | | E: Error, |
2727 | | { |
2728 | | type Error = E; |
2729 | | |
2730 | 0 | fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
2731 | 0 | where |
2732 | 0 | V: Visitor<'de>, |
2733 | 0 | { |
2734 | 0 | visitor.visit_map(FlatInternallyTaggedAccess { |
2735 | 0 | iter: self.0.iter_mut(), |
2736 | 0 | pending: None, |
2737 | 0 | _marker: PhantomData, |
2738 | 0 | }) |
2739 | 0 | } |
2740 | | |
2741 | 0 | fn deserialize_enum<V>( |
2742 | 0 | self, |
2743 | 0 | name: &'static str, |
2744 | 0 | variants: &'static [&'static str], |
2745 | 0 | visitor: V, |
2746 | 0 | ) -> Result<V::Value, Self::Error> |
2747 | 0 | where |
2748 | 0 | V: Visitor<'de>, |
2749 | 0 | { |
2750 | 0 | for item in self.0.iter_mut() { |
2751 | | // items in the vector are nulled out when used. So we can only use |
2752 | | // an item if it's still filled in and if the field is one we care |
2753 | | // about. |
2754 | 0 | let use_item = match *item { |
2755 | 0 | None => false, |
2756 | 0 | Some((ref c, _)) => c.as_str().map_or(false, |x| variants.contains(&x)), |
2757 | | }; |
2758 | | |
2759 | 0 | if use_item { |
2760 | 0 | let (key, value) = item.take().unwrap(); |
2761 | 0 | return visitor.visit_enum(EnumDeserializer::new(key, Some(value))); |
2762 | 0 | } |
2763 | | } |
2764 | | |
2765 | 0 | Err(Error::custom(format_args!( |
2766 | 0 | "no variant of enum {} found in flattened data", |
2767 | 0 | name |
2768 | 0 | ))) |
2769 | 0 | } |
2770 | | |
2771 | 0 | fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
2772 | 0 | where |
2773 | 0 | V: Visitor<'de>, |
2774 | 0 | { |
2775 | 0 | visitor.visit_map(FlatMapAccess::new(self.0.iter())) |
2776 | 0 | } |
2777 | | |
2778 | 0 | fn deserialize_struct<V>( |
2779 | 0 | self, |
2780 | 0 | _: &'static str, |
2781 | 0 | fields: &'static [&'static str], |
2782 | 0 | visitor: V, |
2783 | 0 | ) -> Result<V::Value, Self::Error> |
2784 | 0 | where |
2785 | 0 | V: Visitor<'de>, |
2786 | 0 | { |
2787 | 0 | visitor.visit_map(FlatStructAccess::new(self.0.iter_mut(), fields)) |
2788 | 0 | } |
2789 | | |
2790 | 0 | fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, Self::Error> |
2791 | 0 | where |
2792 | 0 | V: Visitor<'de>, |
2793 | 0 | { |
2794 | 0 | visitor.visit_newtype_struct(self) |
2795 | 0 | } |
2796 | | |
2797 | 0 | fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
2798 | 0 | where |
2799 | 0 | V: Visitor<'de>, |
2800 | 0 | { |
2801 | 0 | match visitor.__private_visit_untagged_option(self) { |
2802 | 0 | Ok(value) => Ok(value), |
2803 | 0 | Err(()) => Self::deserialize_other(), |
2804 | | } |
2805 | 0 | } |
2806 | | |
2807 | 0 | fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error> |
2808 | 0 | where |
2809 | 0 | V: Visitor<'de>, |
2810 | 0 | { |
2811 | 0 | visitor.visit_unit() |
2812 | 0 | } |
2813 | | |
2814 | | forward_to_deserialize_other! { |
2815 | | deserialize_bool() |
2816 | | deserialize_i8() |
2817 | | deserialize_i16() |
2818 | | deserialize_i32() |
2819 | | deserialize_i64() |
2820 | | deserialize_u8() |
2821 | | deserialize_u16() |
2822 | | deserialize_u32() |
2823 | | deserialize_u64() |
2824 | | deserialize_f32() |
2825 | | deserialize_f64() |
2826 | | deserialize_char() |
2827 | | deserialize_str() |
2828 | | deserialize_string() |
2829 | | deserialize_bytes() |
2830 | | deserialize_byte_buf() |
2831 | | deserialize_unit_struct(&'static str) |
2832 | | deserialize_seq() |
2833 | | deserialize_tuple(usize) |
2834 | | deserialize_tuple_struct(&'static str, usize) |
2835 | | deserialize_identifier() |
2836 | | deserialize_ignored_any() |
2837 | | } |
2838 | | } |
2839 | | |
2840 | | #[cfg(any(feature = "std", feature = "alloc"))] |
2841 | | pub struct FlatMapAccess<'a, 'de: 'a, E> { |
2842 | | iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>, |
2843 | | pending_content: Option<&'a Content<'de>>, |
2844 | | _marker: PhantomData<E>, |
2845 | | } |
2846 | | |
2847 | | #[cfg(any(feature = "std", feature = "alloc"))] |
2848 | | impl<'a, 'de, E> FlatMapAccess<'a, 'de, E> { |
2849 | 0 | fn new( |
2850 | 0 | iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>, |
2851 | 0 | ) -> FlatMapAccess<'a, 'de, E> { |
2852 | 0 | FlatMapAccess { |
2853 | 0 | iter: iter, |
2854 | 0 | pending_content: None, |
2855 | 0 | _marker: PhantomData, |
2856 | 0 | } |
2857 | 0 | } |
2858 | | } |
2859 | | |
2860 | | #[cfg(any(feature = "std", feature = "alloc"))] |
2861 | | impl<'a, 'de, E> MapAccess<'de> for FlatMapAccess<'a, 'de, E> |
2862 | | where |
2863 | | E: Error, |
2864 | | { |
2865 | | type Error = E; |
2866 | | |
2867 | 0 | fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> |
2868 | 0 | where |
2869 | 0 | T: DeserializeSeed<'de>, |
2870 | 0 | { |
2871 | 0 | for item in &mut self.iter { |
2872 | | // Items in the vector are nulled out when used by a struct. |
2873 | 0 | if let Some((ref key, ref content)) = *item { |
2874 | 0 | self.pending_content = Some(content); |
2875 | 0 | return seed.deserialize(ContentRefDeserializer::new(key)).map(Some); |
2876 | 0 | } |
2877 | | } |
2878 | 0 | Ok(None) |
2879 | 0 | } |
2880 | | |
2881 | 0 | fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error> |
2882 | 0 | where |
2883 | 0 | T: DeserializeSeed<'de>, |
2884 | 0 | { |
2885 | 0 | match self.pending_content.take() { |
2886 | 0 | Some(value) => seed.deserialize(ContentRefDeserializer::new(value)), |
2887 | 0 | None => Err(Error::custom("value is missing")), |
2888 | | } |
2889 | 0 | } |
2890 | | } |
2891 | | |
2892 | | #[cfg(any(feature = "std", feature = "alloc"))] |
2893 | | pub struct FlatStructAccess<'a, 'de: 'a, E> { |
2894 | | iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>, |
2895 | | pending_content: Option<Content<'de>>, |
2896 | | fields: &'static [&'static str], |
2897 | | _marker: PhantomData<E>, |
2898 | | } |
2899 | | |
2900 | | #[cfg(any(feature = "std", feature = "alloc"))] |
2901 | | impl<'a, 'de, E> FlatStructAccess<'a, 'de, E> { |
2902 | 0 | fn new( |
2903 | 0 | iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>, |
2904 | 0 | fields: &'static [&'static str], |
2905 | 0 | ) -> FlatStructAccess<'a, 'de, E> { |
2906 | 0 | FlatStructAccess { |
2907 | 0 | iter: iter, |
2908 | 0 | pending_content: None, |
2909 | 0 | fields: fields, |
2910 | 0 | _marker: PhantomData, |
2911 | 0 | } |
2912 | 0 | } |
2913 | | } |
2914 | | |
2915 | | #[cfg(any(feature = "std", feature = "alloc"))] |
2916 | | impl<'a, 'de, E> MapAccess<'de> for FlatStructAccess<'a, 'de, E> |
2917 | | where |
2918 | | E: Error, |
2919 | | { |
2920 | | type Error = E; |
2921 | | |
2922 | 0 | fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> |
2923 | 0 | where |
2924 | 0 | T: DeserializeSeed<'de>, |
2925 | 0 | { |
2926 | 0 | while let Some(item) = self.iter.next() { |
2927 | | // items in the vector are nulled out when used. So we can only use |
2928 | | // an item if it's still filled in and if the field is one we care |
2929 | | // about. In case we do not know which fields we want, we take them all. |
2930 | 0 | let use_item = match *item { |
2931 | 0 | None => false, |
2932 | 0 | Some((ref c, _)) => c.as_str().map_or(false, |key| self.fields.contains(&key)), |
2933 | | }; |
2934 | | |
2935 | 0 | if use_item { |
2936 | 0 | let (key, content) = item.take().unwrap(); |
2937 | 0 | self.pending_content = Some(content); |
2938 | 0 | return seed.deserialize(ContentDeserializer::new(key)).map(Some); |
2939 | 0 | } |
2940 | | } |
2941 | 0 | Ok(None) |
2942 | 0 | } |
2943 | | |
2944 | 0 | fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error> |
2945 | 0 | where |
2946 | 0 | T: DeserializeSeed<'de>, |
2947 | 0 | { |
2948 | 0 | match self.pending_content.take() { |
2949 | 0 | Some(value) => seed.deserialize(ContentDeserializer::new(value)), |
2950 | 0 | None => Err(Error::custom("value is missing")), |
2951 | | } |
2952 | 0 | } |
2953 | | } |
2954 | | |
2955 | | #[cfg(any(feature = "std", feature = "alloc"))] |
2956 | | pub struct FlatInternallyTaggedAccess<'a, 'de: 'a, E> { |
2957 | | iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>, |
2958 | | pending: Option<&'a Content<'de>>, |
2959 | | _marker: PhantomData<E>, |
2960 | | } |
2961 | | |
2962 | | #[cfg(any(feature = "std", feature = "alloc"))] |
2963 | | impl<'a, 'de, E> MapAccess<'de> for FlatInternallyTaggedAccess<'a, 'de, E> |
2964 | | where |
2965 | | E: Error, |
2966 | | { |
2967 | | type Error = E; |
2968 | | |
2969 | 0 | fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> |
2970 | 0 | where |
2971 | 0 | T: DeserializeSeed<'de>, |
2972 | 0 | { |
2973 | 0 | for item in &mut self.iter { |
2974 | 0 | if let Some((ref key, ref content)) = *item { |
2975 | | // Do not take(), instead borrow this entry. The internally tagged |
2976 | | // enum does its own buffering so we can't tell whether this entry |
2977 | | // is going to be consumed. Borrowing here leaves the entry |
2978 | | // available for later flattened fields. |
2979 | 0 | self.pending = Some(content); |
2980 | 0 | return seed.deserialize(ContentRefDeserializer::new(key)).map(Some); |
2981 | 0 | } |
2982 | | } |
2983 | 0 | Ok(None) |
2984 | 0 | } |
2985 | | |
2986 | 0 | fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error> |
2987 | 0 | where |
2988 | 0 | T: DeserializeSeed<'de>, |
2989 | 0 | { |
2990 | 0 | match self.pending.take() { |
2991 | 0 | Some(value) => seed.deserialize(ContentRefDeserializer::new(value)), |
2992 | 0 | None => panic!("value is missing"), |
2993 | | } |
2994 | 0 | } |
2995 | | } |