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