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