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