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