mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-28 05:47:56 +00:00
Compare commits
44 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a866b336f1 | |||
| 5adc9e816c | |||
| ab581789f4 | |||
| 415d9fc560 | |||
| 7c58427e12 | |||
| 9d3410e3f4 | |||
| 2fb6748bf1 | |||
| f8137c79a2 | |||
| b7dbf7e3cb | |||
| 7c836915fc | |||
| aa2d9b208a | |||
| 3a7c11c82c | |||
| 11ce4402bd | |||
| 8047570160 | |||
| b395215875 | |||
| a437ff6bf7 | |||
| 20fc31c78a | |||
| ad2b064b0a | |||
| dec153b8fa | |||
| e219a0d4b7 | |||
| c78f7d2e30 | |||
| 502a065a4c | |||
| 027112eccb | |||
| 8c3445efe4 | |||
| 88b6a9abf4 | |||
| fa298c9df4 | |||
| c4a07ed4e8 | |||
| c4986f10b9 | |||
| 2d607de146 | |||
| 19956e6b8e | |||
| ec13eb4ed6 | |||
| ae00c4acb7 | |||
| 85d0b9ccd8 | |||
| e60f62a289 | |||
| e04f0ba2de | |||
| c8184be238 | |||
| 896d91f0bb | |||
| 915686d0ec | |||
| 3a682c951b | |||
| ca79f61d0f | |||
| ddb7c4b30f | |||
| 4855fb467f | |||
| 6dffc0b1c8 | |||
| 15be2a600a |
@@ -133,6 +133,7 @@ jobs:
|
|||||||
- uses: dtolnay/rust-toolchain@nightly
|
- uses: dtolnay/rust-toolchain@nightly
|
||||||
- uses: dtolnay/install@cargo-docs-rs
|
- uses: dtolnay/install@cargo-docs-rs
|
||||||
- run: cargo docs-rs -p serde
|
- run: cargo docs-rs -p serde
|
||||||
|
- run: cargo docs-rs -p serde_core
|
||||||
- run: cargo docs-rs -p serde_derive
|
- run: cargo docs-rs -p serde_derive
|
||||||
- run: cargo docs-rs -p serde_derive_internals
|
- run: cargo docs-rs -p serde_derive_internals
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.226"
|
version = "1.0.228"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||||
build = "build.rs"
|
build = "build.rs"
|
||||||
categories = ["encoding", "no-std", "no-std::no-alloc"]
|
categories = ["encoding", "no-std", "no-std::no-alloc"]
|
||||||
@@ -15,7 +15,7 @@ repository = "https://github.com/serde-rs/serde"
|
|||||||
rust-version = "1.56"
|
rust-version = "1.56"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serde_core = { version = "=1.0.226", path = "../serde_core", default-features = false, features = ["result"] }
|
serde_core = { version = "=1.0.228", path = "../serde_core", default-features = false, features = ["result"] }
|
||||||
serde_derive = { version = "1", optional = true, path = "../serde_derive" }
|
serde_derive = { version = "1", optional = true, path = "../serde_derive" }
|
||||||
|
|
||||||
[package.metadata.playground]
|
[package.metadata.playground]
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ use serde_core::__private$$ as serde_core_private;
|
|||||||
fn main() {
|
fn main() {
|
||||||
println!("cargo:rerun-if-changed=build.rs");
|
println!("cargo:rerun-if-changed=build.rs");
|
||||||
|
|
||||||
|
println!("cargo:rustc-cfg=if_docsrs_then_no_serde_core");
|
||||||
|
|
||||||
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
||||||
let patch_version = env::var("CARGO_PKG_VERSION_PATCH").unwrap();
|
let patch_version = env::var("CARGO_PKG_VERSION_PATCH").unwrap();
|
||||||
let module = PRIVATE.replace("$$", &patch_version);
|
let module = PRIVATE.replace("$$", &patch_version);
|
||||||
@@ -30,8 +32,17 @@ fn main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if minor >= 77 {
|
if minor >= 77 {
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(feature, values(\"result\"))");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(if_docsrs_then_no_serde_core)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(no_core_cstr)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(no_core_error)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(no_core_net)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(no_core_num_saturating)");
|
||||||
println!("cargo:rustc-check-cfg=cfg(no_diagnostic_namespace)");
|
println!("cargo:rustc-check-cfg=cfg(no_diagnostic_namespace)");
|
||||||
println!("cargo:rustc-check-cfg=cfg(no_serde_derive)");
|
println!("cargo:rustc-check-cfg=cfg(no_serde_derive)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(no_std_atomic)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(no_std_atomic64)");
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(no_target_has_atomic)");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Current minimum supported version of serde_derive crate is Rust 1.61.
|
// Current minimum supported version of serde_derive crate is Rust 1.61.
|
||||||
|
|||||||
Symlink
+1
@@ -0,0 +1 @@
|
|||||||
|
../../serde_core/src
|
||||||
+83
-61
@@ -95,7 +95,7 @@
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// Serde types in rustdoc of other crates get linked to here.
|
// Serde types in rustdoc of other crates get linked to here.
|
||||||
#![doc(html_root_url = "https://docs.rs/serde/1.0.226")]
|
#![doc(html_root_url = "https://docs.rs/serde/1.0.228")]
|
||||||
// Support using Serde without the standard library!
|
// Support using Serde without the standard library!
|
||||||
#![cfg_attr(not(feature = "std"), no_std)]
|
#![cfg_attr(not(feature = "std"), no_std)]
|
||||||
// Show which crate feature enables conditionally compiled APIs in documentation.
|
// Show which crate feature enables conditionally compiled APIs in documentation.
|
||||||
@@ -168,81 +168,103 @@
|
|||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
/// A facade around all the types we need from the `std`, `core`, and `alloc`
|
// Rustdoc has a lot of shortcomings related to cross-crate re-exports that make
|
||||||
/// crates. This avoids elaborate import wrangling having to happen in every
|
// the rendered documentation of serde_core traits in serde more challenging to
|
||||||
/// module.
|
// understand than the equivalent documentation of the same items in serde_core.
|
||||||
mod lib {
|
// https://github.com/rust-lang/rust/labels/A-cross-crate-reexports
|
||||||
mod core {
|
// So, just for the purpose of docs.rs documentation, we inline the contents of
|
||||||
#[cfg(not(feature = "std"))]
|
// serde_core into serde. This sidesteps all the cross-crate rustdoc bugs.
|
||||||
pub use core::*;
|
#[cfg(docsrs)]
|
||||||
#[cfg(feature = "std")]
|
#[macro_use]
|
||||||
pub use std::*;
|
#[path = "core/crate_root.rs"]
|
||||||
}
|
mod crate_root;
|
||||||
|
|
||||||
pub use self::core::{f32, f64};
|
#[cfg(docsrs)]
|
||||||
pub use self::core::{ptr, str};
|
#[macro_use]
|
||||||
|
#[path = "core/macros.rs"]
|
||||||
|
mod macros;
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
#[cfg(not(docsrs))]
|
||||||
pub use self::core::slice;
|
macro_rules! crate_root {
|
||||||
|
() => {
|
||||||
|
/// A facade around all the types we need from the `std`, `core`, and `alloc`
|
||||||
|
/// crates. This avoids elaborate import wrangling having to happen in every
|
||||||
|
/// module.
|
||||||
|
mod lib {
|
||||||
|
mod core {
|
||||||
|
#[cfg(not(feature = "std"))]
|
||||||
|
pub use core::*;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::*;
|
||||||
|
}
|
||||||
|
|
||||||
pub use self::core::clone;
|
pub use self::core::{f32, f64};
|
||||||
pub use self::core::convert;
|
pub use self::core::{ptr, str};
|
||||||
pub use self::core::default;
|
|
||||||
pub use self::core::fmt::{self, Debug, Display, Write as FmtWrite};
|
|
||||||
pub use self::core::marker::{self, PhantomData};
|
|
||||||
pub use self::core::option;
|
|
||||||
pub use self::core::result;
|
|
||||||
|
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
pub use alloc::borrow::{Cow, ToOwned};
|
pub use self::core::slice;
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::borrow::{Cow, ToOwned};
|
|
||||||
|
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
pub use self::core::clone;
|
||||||
pub use alloc::string::{String, ToString};
|
pub use self::core::convert;
|
||||||
#[cfg(feature = "std")]
|
pub use self::core::default;
|
||||||
pub use std::string::{String, ToString};
|
pub use self::core::fmt::{self, Debug, Display, Write as FmtWrite};
|
||||||
|
pub use self::core::marker::{self, PhantomData};
|
||||||
|
pub use self::core::option;
|
||||||
|
pub use self::core::result;
|
||||||
|
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
pub use alloc::vec::Vec;
|
pub use alloc::borrow::{Cow, ToOwned};
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
pub use std::vec::Vec;
|
pub use std::borrow::{Cow, ToOwned};
|
||||||
|
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
pub use alloc::boxed::Box;
|
pub use alloc::string::{String, ToString};
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
pub use std::boxed::Box;
|
pub use std::string::{String, ToString};
|
||||||
}
|
|
||||||
|
|
||||||
// None of this crate's error handling needs the `From::from` error conversion
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
// performed implicitly by the `?` operator or the standard library's `try!`
|
pub use alloc::vec::Vec;
|
||||||
// macro. This simplified macro gives a 5.5% improvement in compile time
|
#[cfg(feature = "std")]
|
||||||
// compared to standard `try!`, and 9% improvement compared to `?`.
|
pub use std::vec::Vec;
|
||||||
#[cfg(not(no_serde_derive))]
|
|
||||||
macro_rules! tri {
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
($expr:expr) => {
|
pub use alloc::boxed::Box;
|
||||||
match $expr {
|
#[cfg(feature = "std")]
|
||||||
Ok(val) => val,
|
pub use std::boxed::Box;
|
||||||
Err(err) => return Err(err),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// None of this crate's error handling needs the `From::from` error conversion
|
||||||
|
// performed implicitly by the `?` operator or the standard library's `try!`
|
||||||
|
// macro. This simplified macro gives a 5.5% improvement in compile time
|
||||||
|
// compared to standard `try!`, and 9% improvement compared to `?`.
|
||||||
|
#[cfg(not(no_serde_derive))]
|
||||||
|
macro_rules! tri {
|
||||||
|
($expr:expr) => {
|
||||||
|
match $expr {
|
||||||
|
Ok(val) => val,
|
||||||
|
Err(err) => return Err(err),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
pub use serde_core::{
|
||||||
|
de, forward_to_deserialize_any, ser, Deserialize, Deserializer, Serialize, Serializer,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Used by generated code and doc tests. Not public API.
|
||||||
|
#[doc(hidden)]
|
||||||
|
mod private;
|
||||||
|
|
||||||
|
include!(concat!(env!("OUT_DIR"), "/private.rs"));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
crate_root!();
|
||||||
|
|
||||||
pub use serde_core::{
|
|
||||||
de, forward_to_deserialize_any, ser, Deserialize, Deserializer, Serialize, Serializer,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
mod integer128;
|
mod integer128;
|
||||||
|
|
||||||
// Used by generated code and doc tests. Not public API.
|
|
||||||
#[doc(hidden)]
|
|
||||||
mod private;
|
|
||||||
|
|
||||||
include!(concat!(env!("OUT_DIR"), "/private.rs"));
|
|
||||||
|
|
||||||
// Re-export #[derive(Serialize, Deserialize)].
|
// Re-export #[derive(Serialize, Deserialize)].
|
||||||
//
|
//
|
||||||
// The reason re-exporting is not enabled by default is that disabling it would
|
// The reason re-exporting is not enabled by default is that disabling it would
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_core"
|
name = "serde_core"
|
||||||
version = "1.0.226"
|
version = "1.0.228"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||||
build = "build.rs"
|
build = "build.rs"
|
||||||
categories = ["encoding", "no-std", "no-std::no-alloc"]
|
categories = ["encoding", "no-std", "no-std::no-alloc"]
|
||||||
@@ -37,7 +37,7 @@ rustdoc-args = [
|
|||||||
# is compatible with exactly one serde release because the generated code
|
# is compatible with exactly one serde release because the generated code
|
||||||
# involves nonpublic APIs which are not bound by semver.
|
# involves nonpublic APIs which are not bound by semver.
|
||||||
[target.'cfg(any())'.dependencies]
|
[target.'cfg(any())'.dependencies]
|
||||||
serde_derive = { version = "=1.0.226", path = "../serde_derive" }
|
serde_derive = { version = "=1.0.228", path = "../serde_derive" }
|
||||||
|
|
||||||
|
|
||||||
### FEATURES #################################################################
|
### FEATURES #################################################################
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ fn main() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if minor >= 77 {
|
if minor >= 77 {
|
||||||
|
println!("cargo:rustc-check-cfg=cfg(if_docsrs_then_no_serde_core)");
|
||||||
println!("cargo:rustc-check-cfg=cfg(no_core_cstr)");
|
println!("cargo:rustc-check-cfg=cfg(no_core_cstr)");
|
||||||
println!("cargo:rustc-check-cfg=cfg(no_core_error)");
|
println!("cargo:rustc-check-cfg=cfg(no_core_error)");
|
||||||
println!("cargo:rustc-check-cfg=cfg(no_core_net)");
|
println!("cargo:rustc-check-cfg=cfg(no_core_net)");
|
||||||
|
|||||||
@@ -0,0 +1,171 @@
|
|||||||
|
macro_rules! crate_root {
|
||||||
|
() => {
|
||||||
|
/// A facade around all the types we need from the `std`, `core`, and `alloc`
|
||||||
|
/// crates. This avoids elaborate import wrangling having to happen in every
|
||||||
|
/// module.
|
||||||
|
mod lib {
|
||||||
|
mod core {
|
||||||
|
#[cfg(not(feature = "std"))]
|
||||||
|
pub use core::*;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::*;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub use self::core::{f32, f64};
|
||||||
|
pub use self::core::{iter, num, str};
|
||||||
|
|
||||||
|
#[cfg(any(feature = "std", feature = "alloc"))]
|
||||||
|
pub use self::core::{cmp, mem};
|
||||||
|
|
||||||
|
pub use self::core::cell::{Cell, RefCell};
|
||||||
|
pub use self::core::cmp::Reverse;
|
||||||
|
pub use self::core::fmt::{self, Debug, Display, Write as FmtWrite};
|
||||||
|
pub use self::core::marker::PhantomData;
|
||||||
|
pub use self::core::num::Wrapping;
|
||||||
|
pub use self::core::ops::{Bound, Range, RangeFrom, RangeInclusive, RangeTo};
|
||||||
|
pub use self::core::result;
|
||||||
|
pub use self::core::time::Duration;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
|
pub use alloc::borrow::{Cow, ToOwned};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::borrow::{Cow, ToOwned};
|
||||||
|
|
||||||
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
|
pub use alloc::string::{String, ToString};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::string::{String, ToString};
|
||||||
|
|
||||||
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
|
pub use alloc::vec::Vec;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::vec::Vec;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
|
pub use alloc::boxed::Box;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::boxed::Box;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))]
|
||||||
|
pub use alloc::rc::{Rc, Weak as RcWeak};
|
||||||
|
#[cfg(all(feature = "rc", feature = "std"))]
|
||||||
|
pub use std::rc::{Rc, Weak as RcWeak};
|
||||||
|
|
||||||
|
#[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))]
|
||||||
|
pub use alloc::sync::{Arc, Weak as ArcWeak};
|
||||||
|
#[cfg(all(feature = "rc", feature = "std"))]
|
||||||
|
pub use std::sync::{Arc, Weak as ArcWeak};
|
||||||
|
|
||||||
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
|
pub use alloc::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
|
||||||
|
|
||||||
|
#[cfg(all(not(no_core_cstr), not(feature = "std")))]
|
||||||
|
pub use self::core::ffi::CStr;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::ffi::CStr;
|
||||||
|
|
||||||
|
#[cfg(all(not(no_core_cstr), feature = "alloc", not(feature = "std")))]
|
||||||
|
pub use alloc::ffi::CString;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::ffi::CString;
|
||||||
|
|
||||||
|
#[cfg(all(not(no_core_net), not(feature = "std")))]
|
||||||
|
pub use self::core::net;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::net;
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::error;
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::collections::{HashMap, HashSet};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::ffi::{OsStr, OsString};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::hash::{BuildHasher, Hash};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::io::Write;
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::path::{Path, PathBuf};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::sync::{Mutex, RwLock};
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
|
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))]
|
||||||
|
pub use std::sync::atomic::{
|
||||||
|
AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicIsize, AtomicU16, AtomicU32,
|
||||||
|
AtomicU8, AtomicUsize, Ordering,
|
||||||
|
};
|
||||||
|
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic64)))]
|
||||||
|
pub use std::sync::atomic::{AtomicI64, AtomicU64};
|
||||||
|
|
||||||
|
#[cfg(all(feature = "std", not(no_target_has_atomic)))]
|
||||||
|
pub use std::sync::atomic::Ordering;
|
||||||
|
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "8"))]
|
||||||
|
pub use std::sync::atomic::{AtomicBool, AtomicI8, AtomicU8};
|
||||||
|
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "16"))]
|
||||||
|
pub use std::sync::atomic::{AtomicI16, AtomicU16};
|
||||||
|
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "32"))]
|
||||||
|
pub use std::sync::atomic::{AtomicI32, AtomicU32};
|
||||||
|
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "64"))]
|
||||||
|
pub use std::sync::atomic::{AtomicI64, AtomicU64};
|
||||||
|
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "ptr"))]
|
||||||
|
pub use std::sync::atomic::{AtomicIsize, AtomicUsize};
|
||||||
|
|
||||||
|
#[cfg(not(no_core_num_saturating))]
|
||||||
|
pub use self::core::num::Saturating;
|
||||||
|
}
|
||||||
|
|
||||||
|
// None of this crate's error handling needs the `From::from` error conversion
|
||||||
|
// performed implicitly by the `?` operator or the standard library's `try!`
|
||||||
|
// macro. This simplified macro gives a 5.5% improvement in compile time
|
||||||
|
// compared to standard `try!`, and 9% improvement compared to `?`.
|
||||||
|
macro_rules! tri {
|
||||||
|
($expr:expr) => {
|
||||||
|
match $expr {
|
||||||
|
Ok(val) => val,
|
||||||
|
Err(err) => return Err(err),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg_attr(all(docsrs, if_docsrs_then_no_serde_core), path = "core/de/mod.rs")]
|
||||||
|
pub mod de;
|
||||||
|
#[cfg_attr(all(docsrs, if_docsrs_then_no_serde_core), path = "core/ser/mod.rs")]
|
||||||
|
pub mod ser;
|
||||||
|
|
||||||
|
#[cfg_attr(all(docsrs, if_docsrs_then_no_serde_core), path = "core/format.rs")]
|
||||||
|
mod format;
|
||||||
|
|
||||||
|
#[doc(inline)]
|
||||||
|
pub use crate::de::{Deserialize, Deserializer};
|
||||||
|
#[doc(inline)]
|
||||||
|
pub use crate::ser::{Serialize, Serializer};
|
||||||
|
|
||||||
|
// Used by generated code. Not public API.
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[cfg_attr(
|
||||||
|
all(docsrs, if_docsrs_then_no_serde_core),
|
||||||
|
path = "core/private/mod.rs"
|
||||||
|
)]
|
||||||
|
mod private;
|
||||||
|
|
||||||
|
// Used by declarative macro generated code. Not public API.
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub mod __private {
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub use crate::private::doc;
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub use core::result::Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
include!(concat!(env!("OUT_DIR"), "/private.rs"));
|
||||||
|
|
||||||
|
#[cfg(all(not(feature = "std"), no_core_error))]
|
||||||
|
#[cfg_attr(all(docsrs, if_docsrs_then_no_serde_core), path = "core/std_error.rs")]
|
||||||
|
mod std_error;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -122,7 +122,6 @@ mod ignored_any;
|
|||||||
mod impls;
|
mod impls;
|
||||||
|
|
||||||
pub use self::ignored_any::IgnoredAny;
|
pub use self::ignored_any::IgnoredAny;
|
||||||
pub use crate::private::InPlaceSeed;
|
|
||||||
#[cfg(all(not(feature = "std"), no_core_error))]
|
#[cfg(all(not(feature = "std"), no_core_error))]
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use crate::std_error::Error as StdError;
|
pub use crate::std_error::Error as StdError;
|
||||||
|
|||||||
+4
-162
@@ -35,7 +35,7 @@
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// Serde types in rustdoc of other crates get linked to here.
|
// Serde types in rustdoc of other crates get linked to here.
|
||||||
#![doc(html_root_url = "https://docs.rs/serde_core/1.0.226")]
|
#![doc(html_root_url = "https://docs.rs/serde_core/1.0.228")]
|
||||||
// Support using Serde without the standard library!
|
// Support using Serde without the standard library!
|
||||||
#![cfg_attr(not(feature = "std"), no_std)]
|
#![cfg_attr(not(feature = "std"), no_std)]
|
||||||
// Show which crate feature enables conditionally compiled APIs in documentation.
|
// Show which crate feature enables conditionally compiled APIs in documentation.
|
||||||
@@ -103,170 +103,12 @@
|
|||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
/// A facade around all the types we need from the `std`, `core`, and `alloc`
|
#[macro_use]
|
||||||
/// crates. This avoids elaborate import wrangling having to happen in every
|
mod crate_root;
|
||||||
/// module.
|
|
||||||
mod lib {
|
|
||||||
mod core {
|
|
||||||
#[cfg(not(feature = "std"))]
|
|
||||||
pub use core::*;
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::*;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub use self::core::{f32, f64};
|
|
||||||
pub use self::core::{iter, num, str};
|
|
||||||
|
|
||||||
#[cfg(any(feature = "std", feature = "alloc"))]
|
|
||||||
pub use self::core::{cmp, mem};
|
|
||||||
|
|
||||||
pub use self::core::cell::{Cell, RefCell};
|
|
||||||
pub use self::core::cmp::Reverse;
|
|
||||||
pub use self::core::fmt::{self, Debug, Display, Write as FmtWrite};
|
|
||||||
pub use self::core::marker::PhantomData;
|
|
||||||
pub use self::core::num::Wrapping;
|
|
||||||
pub use self::core::ops::{Bound, Range, RangeFrom, RangeInclusive, RangeTo};
|
|
||||||
pub use self::core::result;
|
|
||||||
pub use self::core::time::Duration;
|
|
||||||
|
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
|
||||||
pub use alloc::borrow::{Cow, ToOwned};
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::borrow::{Cow, ToOwned};
|
|
||||||
|
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
|
||||||
pub use alloc::string::{String, ToString};
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::string::{String, ToString};
|
|
||||||
|
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
|
||||||
pub use alloc::vec::Vec;
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::vec::Vec;
|
|
||||||
|
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
|
||||||
pub use alloc::boxed::Box;
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::boxed::Box;
|
|
||||||
|
|
||||||
#[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))]
|
|
||||||
pub use alloc::rc::{Rc, Weak as RcWeak};
|
|
||||||
#[cfg(all(feature = "rc", feature = "std"))]
|
|
||||||
pub use std::rc::{Rc, Weak as RcWeak};
|
|
||||||
|
|
||||||
#[cfg(all(feature = "rc", feature = "alloc", not(feature = "std")))]
|
|
||||||
pub use alloc::sync::{Arc, Weak as ArcWeak};
|
|
||||||
#[cfg(all(feature = "rc", feature = "std"))]
|
|
||||||
pub use std::sync::{Arc, Weak as ArcWeak};
|
|
||||||
|
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
|
||||||
pub use alloc::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::collections::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, VecDeque};
|
|
||||||
|
|
||||||
#[cfg(all(not(no_core_cstr), not(feature = "std")))]
|
|
||||||
pub use self::core::ffi::CStr;
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::ffi::CStr;
|
|
||||||
|
|
||||||
#[cfg(all(not(no_core_cstr), feature = "alloc", not(feature = "std")))]
|
|
||||||
pub use alloc::ffi::CString;
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::ffi::CString;
|
|
||||||
|
|
||||||
#[cfg(all(not(no_core_net), not(feature = "std")))]
|
|
||||||
pub use self::core::net;
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::net;
|
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::error;
|
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::collections::{HashMap, HashSet};
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::ffi::{OsStr, OsString};
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::hash::{BuildHasher, Hash};
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::io::Write;
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::path::{Path, PathBuf};
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::sync::{Mutex, RwLock};
|
|
||||||
#[cfg(feature = "std")]
|
|
||||||
pub use std::time::{SystemTime, UNIX_EPOCH};
|
|
||||||
|
|
||||||
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic)))]
|
|
||||||
pub use std::sync::atomic::{
|
|
||||||
AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicIsize, AtomicU16, AtomicU32, AtomicU8,
|
|
||||||
AtomicUsize, Ordering,
|
|
||||||
};
|
|
||||||
#[cfg(all(feature = "std", no_target_has_atomic, not(no_std_atomic64)))]
|
|
||||||
pub use std::sync::atomic::{AtomicI64, AtomicU64};
|
|
||||||
|
|
||||||
#[cfg(all(feature = "std", not(no_target_has_atomic)))]
|
|
||||||
pub use std::sync::atomic::Ordering;
|
|
||||||
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "8"))]
|
|
||||||
pub use std::sync::atomic::{AtomicBool, AtomicI8, AtomicU8};
|
|
||||||
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "16"))]
|
|
||||||
pub use std::sync::atomic::{AtomicI16, AtomicU16};
|
|
||||||
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "32"))]
|
|
||||||
pub use std::sync::atomic::{AtomicI32, AtomicU32};
|
|
||||||
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "64"))]
|
|
||||||
pub use std::sync::atomic::{AtomicI64, AtomicU64};
|
|
||||||
#[cfg(all(feature = "std", not(no_target_has_atomic), target_has_atomic = "ptr"))]
|
|
||||||
pub use std::sync::atomic::{AtomicIsize, AtomicUsize};
|
|
||||||
|
|
||||||
#[cfg(not(no_core_num_saturating))]
|
|
||||||
pub use self::core::num::Saturating;
|
|
||||||
}
|
|
||||||
|
|
||||||
// None of this crate's error handling needs the `From::from` error conversion
|
|
||||||
// performed implicitly by the `?` operator or the standard library's `try!`
|
|
||||||
// macro. This simplified macro gives a 5.5% improvement in compile time
|
|
||||||
// compared to standard `try!`, and 9% improvement compared to `?`.
|
|
||||||
macro_rules! tri {
|
|
||||||
($expr:expr) => {
|
|
||||||
match $expr {
|
|
||||||
Ok(val) => val,
|
|
||||||
Err(err) => return Err(err),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod macros;
|
mod macros;
|
||||||
|
|
||||||
pub mod de;
|
crate_root!();
|
||||||
pub mod ser;
|
|
||||||
|
|
||||||
mod format;
|
|
||||||
|
|
||||||
#[doc(inline)]
|
|
||||||
pub use crate::de::{Deserialize, Deserializer};
|
|
||||||
#[doc(inline)]
|
|
||||||
pub use crate::ser::{Serialize, Serializer};
|
|
||||||
|
|
||||||
// Used by generated code. Not public API.
|
|
||||||
#[doc(hidden)]
|
|
||||||
mod private;
|
|
||||||
|
|
||||||
// Used by declarative macro generated code. Not public API.
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub mod __private {
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub use crate::private::doc;
|
|
||||||
#[doc(hidden)]
|
|
||||||
pub use core::result::Result;
|
|
||||||
}
|
|
||||||
|
|
||||||
include!(concat!(env!("OUT_DIR"), "/private.rs"));
|
|
||||||
|
|
||||||
#[cfg(all(not(feature = "std"), no_core_error))]
|
|
||||||
mod std_error;
|
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "1.0.226"
|
version = "1.0.228"
|
||||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>", "David Tolnay <dtolnay@gmail.com>"]
|
||||||
categories = ["no-std", "no-std::no-alloc"]
|
categories = ["no-std", "no-std::no-alloc"]
|
||||||
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
description = "Macros 1.1 implementation of #[derive(Serialize, Deserialize)]"
|
||||||
|
|||||||
+44
-2323
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,96 @@
|
|||||||
|
use crate::de::enum_adjacently;
|
||||||
|
use crate::de::enum_externally;
|
||||||
|
use crate::de::enum_internally;
|
||||||
|
use crate::de::enum_untagged;
|
||||||
|
use crate::de::identifier;
|
||||||
|
use crate::de::{field_i, FieldWithAliases, Parameters};
|
||||||
|
use crate::fragment::{Expr, Fragment, Stmts};
|
||||||
|
use crate::internals::ast::Variant;
|
||||||
|
use crate::internals::attr;
|
||||||
|
use crate::private;
|
||||||
|
use proc_macro2::TokenStream;
|
||||||
|
use quote::quote;
|
||||||
|
|
||||||
|
/// Generates `Deserialize::deserialize` body for an `enum Enum {...}`
|
||||||
|
pub(super) fn deserialize(
|
||||||
|
params: &Parameters,
|
||||||
|
variants: &[Variant],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
) -> Fragment {
|
||||||
|
// The variants have already been checked (in ast.rs) that all untagged variants appear at the end
|
||||||
|
match variants.iter().position(|var| var.attrs.untagged()) {
|
||||||
|
Some(variant_idx) => {
|
||||||
|
let (tagged, untagged) = variants.split_at(variant_idx);
|
||||||
|
let tagged_frag = Expr(deserialize_homogeneous_enum(params, tagged, cattrs));
|
||||||
|
// Ignore any error associated with non-untagged deserialization so that we
|
||||||
|
// can fall through to the untagged variants. This may be infallible so we
|
||||||
|
// need to provide the error type.
|
||||||
|
let first_attempt = quote! {
|
||||||
|
if let _serde::#private::Result::<_, __D::Error>::Ok(__ok) = (|| #tagged_frag)() {
|
||||||
|
return _serde::#private::Ok(__ok);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
enum_untagged::deserialize(params, untagged, cattrs, Some(first_attempt))
|
||||||
|
}
|
||||||
|
None => deserialize_homogeneous_enum(params, variants, cattrs),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_homogeneous_enum(
|
||||||
|
params: &Parameters,
|
||||||
|
variants: &[Variant],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
) -> Fragment {
|
||||||
|
match cattrs.tag() {
|
||||||
|
attr::TagType::External => enum_externally::deserialize(params, variants, cattrs),
|
||||||
|
attr::TagType::Internal { tag } => {
|
||||||
|
enum_internally::deserialize(params, variants, cattrs, tag)
|
||||||
|
}
|
||||||
|
attr::TagType::Adjacent { tag, content } => {
|
||||||
|
enum_adjacently::deserialize(params, variants, cattrs, tag, content)
|
||||||
|
}
|
||||||
|
attr::TagType::None => enum_untagged::deserialize(params, variants, cattrs, None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn prepare_enum_variant_enum(variants: &[Variant]) -> (TokenStream, Stmts) {
|
||||||
|
let deserialized_variants = variants
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter(|&(_i, variant)| !variant.attrs.skip_deserializing());
|
||||||
|
|
||||||
|
let fallthrough = deserialized_variants
|
||||||
|
.clone()
|
||||||
|
.find(|(_i, variant)| variant.attrs.other())
|
||||||
|
.map(|(i, _variant)| {
|
||||||
|
let ignore_variant = field_i(i);
|
||||||
|
quote!(_serde::#private::Ok(__Field::#ignore_variant))
|
||||||
|
});
|
||||||
|
|
||||||
|
let variants_stmt = {
|
||||||
|
let variant_names = deserialized_variants
|
||||||
|
.clone()
|
||||||
|
.flat_map(|(_i, variant)| variant.attrs.aliases());
|
||||||
|
quote! {
|
||||||
|
#[doc(hidden)]
|
||||||
|
const VARIANTS: &'static [&'static str] = &[ #(#variant_names),* ];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let deserialized_variants: Vec<_> = deserialized_variants
|
||||||
|
.map(|(i, variant)| FieldWithAliases {
|
||||||
|
ident: field_i(i),
|
||||||
|
aliases: variant.attrs.aliases(),
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let variant_visitor = Stmts(identifier::deserialize_generated(
|
||||||
|
&deserialized_variants,
|
||||||
|
false, // variant identifiers do not depend on the presence of flatten fields
|
||||||
|
true,
|
||||||
|
None,
|
||||||
|
fallthrough,
|
||||||
|
));
|
||||||
|
|
||||||
|
(variants_stmt, variant_visitor)
|
||||||
|
}
|
||||||
@@ -0,0 +1,323 @@
|
|||||||
|
//! Deserialization for adjacently tagged enums:
|
||||||
|
//!
|
||||||
|
//! ```ignore
|
||||||
|
//! #[serde(tag = "...", content = "...")]
|
||||||
|
//! enum Enum {}
|
||||||
|
//! ```
|
||||||
|
|
||||||
|
use crate::de::enum_;
|
||||||
|
use crate::de::enum_untagged;
|
||||||
|
use crate::de::{field_i, Parameters};
|
||||||
|
use crate::fragment::{Fragment, Match};
|
||||||
|
use crate::internals::ast::{Style, Variant};
|
||||||
|
use crate::internals::attr;
|
||||||
|
use crate::private;
|
||||||
|
use quote::{quote, quote_spanned};
|
||||||
|
use syn::spanned::Spanned;
|
||||||
|
|
||||||
|
/// Generates `Deserialize::deserialize` body for an `enum Enum {...}` with `#[serde(tag, content)]` attributes
|
||||||
|
pub(super) fn deserialize(
|
||||||
|
params: &Parameters,
|
||||||
|
variants: &[Variant],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
tag: &str,
|
||||||
|
content: &str,
|
||||||
|
) -> Fragment {
|
||||||
|
let this_type = ¶ms.this_type;
|
||||||
|
let this_value = ¶ms.this_value;
|
||||||
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||||
|
params.generics_with_de_lifetime();
|
||||||
|
let delife = params.borrowed.de_lifetime();
|
||||||
|
|
||||||
|
let (variants_stmt, variant_visitor) = enum_::prepare_enum_variant_enum(variants);
|
||||||
|
|
||||||
|
let variant_arms: &Vec<_> = &variants
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
||||||
|
.map(|(i, variant)| {
|
||||||
|
let variant_index = field_i(i);
|
||||||
|
|
||||||
|
let block = Match(enum_untagged::deserialize_variant(params, variant, cattrs));
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
__Field::#variant_index => #block
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let rust_name = params.type_name();
|
||||||
|
let expecting = format!("adjacently tagged enum {}", rust_name);
|
||||||
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
||||||
|
let type_name = cattrs.name().deserialize_name();
|
||||||
|
let deny_unknown_fields = cattrs.deny_unknown_fields();
|
||||||
|
|
||||||
|
// If unknown fields are allowed, we pick the visitor that can step over
|
||||||
|
// those. Otherwise we pick the visitor that fails on unknown keys.
|
||||||
|
let field_visitor_ty = if deny_unknown_fields {
|
||||||
|
quote! { _serde::#private::de::TagOrContentFieldVisitor }
|
||||||
|
} else {
|
||||||
|
quote! { _serde::#private::de::TagContentOtherFieldVisitor }
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut missing_content = quote! {
|
||||||
|
_serde::#private::Err(<__A::Error as _serde::de::Error>::missing_field(#content))
|
||||||
|
};
|
||||||
|
let mut missing_content_fallthrough = quote!();
|
||||||
|
let missing_content_arms = variants
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
||||||
|
.filter_map(|(i, variant)| {
|
||||||
|
let variant_index = field_i(i);
|
||||||
|
let variant_ident = &variant.ident;
|
||||||
|
|
||||||
|
let arm = match variant.style {
|
||||||
|
Style::Unit => quote! {
|
||||||
|
_serde::#private::Ok(#this_value::#variant_ident)
|
||||||
|
},
|
||||||
|
Style::Newtype if variant.attrs.deserialize_with().is_none() => {
|
||||||
|
let span = variant.original.span();
|
||||||
|
let func = quote_spanned!(span=> _serde::#private::de::missing_field);
|
||||||
|
quote! {
|
||||||
|
#func(#content).map(#this_value::#variant_ident)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
missing_content_fallthrough = quote!(_ => #missing_content);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Some(quote! {
|
||||||
|
__Field::#variant_index => #arm,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
if !missing_content_arms.is_empty() {
|
||||||
|
missing_content = quote! {
|
||||||
|
match __field {
|
||||||
|
#(#missing_content_arms)*
|
||||||
|
#missing_content_fallthrough
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Advance the map by one key, returning early in case of error.
|
||||||
|
let next_key = quote! {
|
||||||
|
_serde::de::MapAccess::next_key_seed(&mut __map, #field_visitor_ty {
|
||||||
|
tag: #tag,
|
||||||
|
content: #content,
|
||||||
|
})?
|
||||||
|
};
|
||||||
|
|
||||||
|
let variant_from_map = quote! {
|
||||||
|
_serde::de::MapAccess::next_value_seed(&mut __map, _serde::#private::de::AdjacentlyTaggedEnumVariantSeed::<__Field> {
|
||||||
|
enum_name: #rust_name,
|
||||||
|
variants: VARIANTS,
|
||||||
|
fields_enum: _serde::#private::PhantomData
|
||||||
|
})?
|
||||||
|
};
|
||||||
|
|
||||||
|
// When allowing unknown fields, we want to transparently step through keys
|
||||||
|
// we don't care about until we find `tag`, `content`, or run out of keys.
|
||||||
|
let next_relevant_key = if deny_unknown_fields {
|
||||||
|
next_key
|
||||||
|
} else {
|
||||||
|
quote!({
|
||||||
|
let mut __rk : _serde::#private::Option<_serde::#private::de::TagOrContentField> = _serde::#private::None;
|
||||||
|
while let _serde::#private::Some(__k) = #next_key {
|
||||||
|
match __k {
|
||||||
|
_serde::#private::de::TagContentOtherField::Other => {
|
||||||
|
let _ = _serde::de::MapAccess::next_value::<_serde::de::IgnoredAny>(&mut __map)?;
|
||||||
|
continue;
|
||||||
|
},
|
||||||
|
_serde::#private::de::TagContentOtherField::Tag => {
|
||||||
|
__rk = _serde::#private::Some(_serde::#private::de::TagOrContentField::Tag);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_serde::#private::de::TagContentOtherField::Content => {
|
||||||
|
__rk = _serde::#private::Some(_serde::#private::de::TagOrContentField::Content);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
__rk
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
// Step through remaining keys, looking for duplicates of previously-seen
|
||||||
|
// keys. When unknown fields are denied, any key that isn't a duplicate will
|
||||||
|
// at this point immediately produce an error.
|
||||||
|
let visit_remaining_keys = quote! {
|
||||||
|
match #next_relevant_key {
|
||||||
|
_serde::#private::Some(_serde::#private::de::TagOrContentField::Tag) => {
|
||||||
|
_serde::#private::Err(<__A::Error as _serde::de::Error>::duplicate_field(#tag))
|
||||||
|
}
|
||||||
|
_serde::#private::Some(_serde::#private::de::TagOrContentField::Content) => {
|
||||||
|
_serde::#private::Err(<__A::Error as _serde::de::Error>::duplicate_field(#content))
|
||||||
|
}
|
||||||
|
_serde::#private::None => _serde::#private::Ok(__ret),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let finish_content_then_tag = if variant_arms.is_empty() {
|
||||||
|
quote! {
|
||||||
|
match #variant_from_map {}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
quote! {
|
||||||
|
let __seed = __Seed {
|
||||||
|
variant: #variant_from_map,
|
||||||
|
marker: _serde::#private::PhantomData,
|
||||||
|
lifetime: _serde::#private::PhantomData,
|
||||||
|
};
|
||||||
|
let __deserializer = _serde::#private::de::ContentDeserializer::<__A::Error>::new(__content);
|
||||||
|
let __ret = _serde::de::DeserializeSeed::deserialize(__seed, __deserializer)?;
|
||||||
|
// Visit remaining keys, looking for duplicates.
|
||||||
|
#visit_remaining_keys
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
quote_block! {
|
||||||
|
#variant_visitor
|
||||||
|
|
||||||
|
#variants_stmt
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
struct __Seed #de_impl_generics #where_clause {
|
||||||
|
variant: __Field,
|
||||||
|
marker: _serde::#private::PhantomData<#this_type #ty_generics>,
|
||||||
|
lifetime: _serde::#private::PhantomData<&#delife ()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[automatically_derived]
|
||||||
|
impl #de_impl_generics _serde::de::DeserializeSeed<#delife> for __Seed #de_ty_generics #where_clause {
|
||||||
|
type Value = #this_type #ty_generics;
|
||||||
|
|
||||||
|
fn deserialize<__D>(self, __deserializer: __D) -> _serde::#private::Result<Self::Value, __D::Error>
|
||||||
|
where
|
||||||
|
__D: _serde::Deserializer<#delife>,
|
||||||
|
{
|
||||||
|
match self.variant {
|
||||||
|
#(#variant_arms)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
struct __Visitor #de_impl_generics #where_clause {
|
||||||
|
marker: _serde::#private::PhantomData<#this_type #ty_generics>,
|
||||||
|
lifetime: _serde::#private::PhantomData<&#delife ()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[automatically_derived]
|
||||||
|
impl #de_impl_generics _serde::de::Visitor<#delife> for __Visitor #de_ty_generics #where_clause {
|
||||||
|
type Value = #this_type #ty_generics;
|
||||||
|
|
||||||
|
fn expecting(&self, __formatter: &mut _serde::#private::Formatter) -> _serde::#private::fmt::Result {
|
||||||
|
_serde::#private::Formatter::write_str(__formatter, #expecting)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_map<__A>(self, mut __map: __A) -> _serde::#private::Result<Self::Value, __A::Error>
|
||||||
|
where
|
||||||
|
__A: _serde::de::MapAccess<#delife>,
|
||||||
|
{
|
||||||
|
// Visit the first relevant key.
|
||||||
|
match #next_relevant_key {
|
||||||
|
// First key is the tag.
|
||||||
|
_serde::#private::Some(_serde::#private::de::TagOrContentField::Tag) => {
|
||||||
|
// Parse the tag.
|
||||||
|
let __field = #variant_from_map;
|
||||||
|
// Visit the second key.
|
||||||
|
match #next_relevant_key {
|
||||||
|
// Second key is a duplicate of the tag.
|
||||||
|
_serde::#private::Some(_serde::#private::de::TagOrContentField::Tag) => {
|
||||||
|
_serde::#private::Err(<__A::Error as _serde::de::Error>::duplicate_field(#tag))
|
||||||
|
}
|
||||||
|
// Second key is the content.
|
||||||
|
_serde::#private::Some(_serde::#private::de::TagOrContentField::Content) => {
|
||||||
|
let __ret = _serde::de::MapAccess::next_value_seed(&mut __map,
|
||||||
|
__Seed {
|
||||||
|
variant: __field,
|
||||||
|
marker: _serde::#private::PhantomData,
|
||||||
|
lifetime: _serde::#private::PhantomData,
|
||||||
|
})?;
|
||||||
|
// Visit remaining keys, looking for duplicates.
|
||||||
|
#visit_remaining_keys
|
||||||
|
}
|
||||||
|
// There is no second key; might be okay if the we have a unit variant.
|
||||||
|
_serde::#private::None => #missing_content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// First key is the content.
|
||||||
|
_serde::#private::Some(_serde::#private::de::TagOrContentField::Content) => {
|
||||||
|
// Buffer up the content.
|
||||||
|
let __content = _serde::de::MapAccess::next_value_seed(&mut __map, _serde::#private::de::ContentVisitor::new())?;
|
||||||
|
// Visit the second key.
|
||||||
|
match #next_relevant_key {
|
||||||
|
// Second key is the tag.
|
||||||
|
_serde::#private::Some(_serde::#private::de::TagOrContentField::Tag) => {
|
||||||
|
#finish_content_then_tag
|
||||||
|
}
|
||||||
|
// Second key is a duplicate of the content.
|
||||||
|
_serde::#private::Some(_serde::#private::de::TagOrContentField::Content) => {
|
||||||
|
_serde::#private::Err(<__A::Error as _serde::de::Error>::duplicate_field(#content))
|
||||||
|
}
|
||||||
|
// There is no second key.
|
||||||
|
_serde::#private::None => {
|
||||||
|
_serde::#private::Err(<__A::Error as _serde::de::Error>::missing_field(#tag))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// There is no first key.
|
||||||
|
_serde::#private::None => {
|
||||||
|
_serde::#private::Err(<__A::Error as _serde::de::Error>::missing_field(#tag))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_seq<__A>(self, mut __seq: __A) -> _serde::#private::Result<Self::Value, __A::Error>
|
||||||
|
where
|
||||||
|
__A: _serde::de::SeqAccess<#delife>,
|
||||||
|
{
|
||||||
|
// Visit the first element - the tag.
|
||||||
|
match _serde::de::SeqAccess::next_element(&mut __seq)? {
|
||||||
|
_serde::#private::Some(__variant) => {
|
||||||
|
// Visit the second element - the content.
|
||||||
|
match _serde::de::SeqAccess::next_element_seed(
|
||||||
|
&mut __seq,
|
||||||
|
__Seed {
|
||||||
|
variant: __variant,
|
||||||
|
marker: _serde::#private::PhantomData,
|
||||||
|
lifetime: _serde::#private::PhantomData,
|
||||||
|
},
|
||||||
|
)? {
|
||||||
|
_serde::#private::Some(__ret) => _serde::#private::Ok(__ret),
|
||||||
|
// There is no second element.
|
||||||
|
_serde::#private::None => {
|
||||||
|
_serde::#private::Err(_serde::de::Error::invalid_length(1, &self))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// There is no first element.
|
||||||
|
_serde::#private::None => {
|
||||||
|
_serde::#private::Err(_serde::de::Error::invalid_length(0, &self))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
const FIELDS: &'static [&'static str] = &[#tag, #content];
|
||||||
|
_serde::Deserializer::deserialize_struct(
|
||||||
|
__deserializer,
|
||||||
|
#type_name,
|
||||||
|
FIELDS,
|
||||||
|
__Visitor {
|
||||||
|
marker: _serde::#private::PhantomData::<#this_type #ty_generics>,
|
||||||
|
lifetime: _serde::#private::PhantomData,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,212 @@
|
|||||||
|
//! Deserialization for externally tagged enums:
|
||||||
|
//!
|
||||||
|
//! ```ignore
|
||||||
|
//! enum Enum {}
|
||||||
|
//! ```
|
||||||
|
|
||||||
|
use crate::de::enum_;
|
||||||
|
use crate::de::struct_;
|
||||||
|
use crate::de::tuple;
|
||||||
|
use crate::de::{
|
||||||
|
expr_is_missing, field_i, unwrap_to_variant_closure, wrap_deserialize_field_with,
|
||||||
|
wrap_deserialize_with, Parameters, StructForm, TupleForm,
|
||||||
|
};
|
||||||
|
use crate::fragment::{Expr, Fragment, Match};
|
||||||
|
use crate::internals::ast::{Field, Style, Variant};
|
||||||
|
use crate::internals::attr;
|
||||||
|
use crate::private;
|
||||||
|
use proc_macro2::TokenStream;
|
||||||
|
use quote::{quote, quote_spanned};
|
||||||
|
use syn::spanned::Spanned;
|
||||||
|
|
||||||
|
/// Generates `Deserialize::deserialize` body for an `enum Enum {...}` without additional attributes
|
||||||
|
pub(super) fn deserialize(
|
||||||
|
params: &Parameters,
|
||||||
|
variants: &[Variant],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
) -> Fragment {
|
||||||
|
let this_type = ¶ms.this_type;
|
||||||
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||||
|
params.generics_with_de_lifetime();
|
||||||
|
let delife = params.borrowed.de_lifetime();
|
||||||
|
|
||||||
|
let type_name = cattrs.name().deserialize_name();
|
||||||
|
let expecting = format!("enum {}", params.type_name());
|
||||||
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
||||||
|
|
||||||
|
let (variants_stmt, variant_visitor) = enum_::prepare_enum_variant_enum(variants);
|
||||||
|
|
||||||
|
// Match arms to extract a variant from a string
|
||||||
|
let variant_arms = variants
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
||||||
|
.map(|(i, variant)| {
|
||||||
|
let variant_name = field_i(i);
|
||||||
|
|
||||||
|
let block = Match(deserialize_externally_tagged_variant(
|
||||||
|
params, variant, cattrs,
|
||||||
|
));
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
(__Field::#variant_name, __variant) => #block
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let all_skipped = variants
|
||||||
|
.iter()
|
||||||
|
.all(|variant| variant.attrs.skip_deserializing());
|
||||||
|
let match_variant = if all_skipped {
|
||||||
|
// This is an empty enum like `enum Impossible {}` or an enum in which
|
||||||
|
// all variants have `#[serde(skip_deserializing)]`.
|
||||||
|
quote! {
|
||||||
|
// FIXME: Once feature(exhaustive_patterns) is stable:
|
||||||
|
// let _serde::#private::Err(__err) = _serde::de::EnumAccess::variant::<__Field>(__data);
|
||||||
|
// _serde::#private::Err(__err)
|
||||||
|
_serde::#private::Result::map(
|
||||||
|
_serde::de::EnumAccess::variant::<__Field>(__data),
|
||||||
|
|(__impossible, _)| match __impossible {})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
quote! {
|
||||||
|
match _serde::de::EnumAccess::variant(__data)? {
|
||||||
|
#(#variant_arms)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
quote_block! {
|
||||||
|
#variant_visitor
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
struct __Visitor #de_impl_generics #where_clause {
|
||||||
|
marker: _serde::#private::PhantomData<#this_type #ty_generics>,
|
||||||
|
lifetime: _serde::#private::PhantomData<&#delife ()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[automatically_derived]
|
||||||
|
impl #de_impl_generics _serde::de::Visitor<#delife> for __Visitor #de_ty_generics #where_clause {
|
||||||
|
type Value = #this_type #ty_generics;
|
||||||
|
|
||||||
|
fn expecting(&self, __formatter: &mut _serde::#private::Formatter) -> _serde::#private::fmt::Result {
|
||||||
|
_serde::#private::Formatter::write_str(__formatter, #expecting)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_enum<__A>(self, __data: __A) -> _serde::#private::Result<Self::Value, __A::Error>
|
||||||
|
where
|
||||||
|
__A: _serde::de::EnumAccess<#delife>,
|
||||||
|
{
|
||||||
|
#match_variant
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#variants_stmt
|
||||||
|
|
||||||
|
_serde::Deserializer::deserialize_enum(
|
||||||
|
__deserializer,
|
||||||
|
#type_name,
|
||||||
|
VARIANTS,
|
||||||
|
__Visitor {
|
||||||
|
marker: _serde::#private::PhantomData::<#this_type #ty_generics>,
|
||||||
|
lifetime: _serde::#private::PhantomData,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_externally_tagged_variant(
|
||||||
|
params: &Parameters,
|
||||||
|
variant: &Variant,
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
) -> Fragment {
|
||||||
|
if let Some(path) = variant.attrs.deserialize_with() {
|
||||||
|
let (wrapper, wrapper_ty, unwrap_fn) = wrap_deserialize_variant_with(params, variant, path);
|
||||||
|
return quote_block! {
|
||||||
|
#wrapper
|
||||||
|
_serde::#private::Result::map(
|
||||||
|
_serde::de::VariantAccess::newtype_variant::<#wrapper_ty>(__variant), #unwrap_fn)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let variant_ident = &variant.ident;
|
||||||
|
|
||||||
|
match variant.style {
|
||||||
|
Style::Unit => {
|
||||||
|
let this_value = ¶ms.this_value;
|
||||||
|
quote_block! {
|
||||||
|
_serde::de::VariantAccess::unit_variant(__variant)?;
|
||||||
|
_serde::#private::Ok(#this_value::#variant_ident)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Style::Newtype => deserialize_externally_tagged_newtype_variant(
|
||||||
|
variant_ident,
|
||||||
|
params,
|
||||||
|
&variant.fields[0],
|
||||||
|
cattrs,
|
||||||
|
),
|
||||||
|
Style::Tuple => tuple::deserialize(
|
||||||
|
params,
|
||||||
|
&variant.fields,
|
||||||
|
cattrs,
|
||||||
|
TupleForm::ExternallyTagged(variant_ident),
|
||||||
|
),
|
||||||
|
Style::Struct => struct_::deserialize(
|
||||||
|
params,
|
||||||
|
&variant.fields,
|
||||||
|
cattrs,
|
||||||
|
StructForm::ExternallyTagged(variant_ident),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wrap_deserialize_variant_with(
|
||||||
|
params: &Parameters,
|
||||||
|
variant: &Variant,
|
||||||
|
deserialize_with: &syn::ExprPath,
|
||||||
|
) -> (TokenStream, TokenStream, TokenStream) {
|
||||||
|
let field_tys = variant.fields.iter().map(|field| field.ty);
|
||||||
|
let (wrapper, wrapper_ty) =
|
||||||
|
wrap_deserialize_with(params, "e!((#(#field_tys),*)), deserialize_with);
|
||||||
|
|
||||||
|
let unwrap_fn = unwrap_to_variant_closure(params, variant, true);
|
||||||
|
|
||||||
|
(wrapper, wrapper_ty, unwrap_fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_externally_tagged_newtype_variant(
|
||||||
|
variant_ident: &syn::Ident,
|
||||||
|
params: &Parameters,
|
||||||
|
field: &Field,
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
) -> Fragment {
|
||||||
|
let this_value = ¶ms.this_value;
|
||||||
|
|
||||||
|
if field.attrs.skip_deserializing() {
|
||||||
|
let default = Expr(expr_is_missing(field, cattrs));
|
||||||
|
return quote_block! {
|
||||||
|
_serde::de::VariantAccess::unit_variant(__variant)?;
|
||||||
|
_serde::#private::Ok(#this_value::#variant_ident(#default))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
match field.attrs.deserialize_with() {
|
||||||
|
None => {
|
||||||
|
let field_ty = field.ty;
|
||||||
|
let span = field.original.span();
|
||||||
|
let func =
|
||||||
|
quote_spanned!(span=> _serde::de::VariantAccess::newtype_variant::<#field_ty>);
|
||||||
|
quote_expr! {
|
||||||
|
_serde::#private::Result::map(#func(__variant), #this_value::#variant_ident)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(path) => {
|
||||||
|
let (wrapper, wrapper_ty) = wrap_deserialize_field_with(params, field.ty, path);
|
||||||
|
quote_block! {
|
||||||
|
#wrapper
|
||||||
|
_serde::#private::Result::map(
|
||||||
|
_serde::de::VariantAccess::newtype_variant::<#wrapper_ty>(__variant),
|
||||||
|
|__wrapper| #this_value::#variant_ident(__wrapper.value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
//! Deserialization for internally tagged enums:
|
||||||
|
//!
|
||||||
|
//! ```ignore
|
||||||
|
//! #[serde(tag = "...")]
|
||||||
|
//! enum Enum {}
|
||||||
|
//! ```
|
||||||
|
|
||||||
|
use crate::de::enum_;
|
||||||
|
use crate::de::enum_untagged;
|
||||||
|
use crate::de::struct_;
|
||||||
|
use crate::de::{
|
||||||
|
effective_style, expr_is_missing, field_i, unwrap_to_variant_closure, Parameters, StructForm,
|
||||||
|
};
|
||||||
|
use crate::fragment::{Expr, Fragment, Match};
|
||||||
|
use crate::internals::ast::{Style, Variant};
|
||||||
|
use crate::internals::attr;
|
||||||
|
use crate::private;
|
||||||
|
use quote::quote;
|
||||||
|
|
||||||
|
/// Generates `Deserialize::deserialize` body for an `enum Enum {...}` with `#[serde(tag)]` attribute
|
||||||
|
pub(super) fn deserialize(
|
||||||
|
params: &Parameters,
|
||||||
|
variants: &[Variant],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
tag: &str,
|
||||||
|
) -> Fragment {
|
||||||
|
let (variants_stmt, variant_visitor) = enum_::prepare_enum_variant_enum(variants);
|
||||||
|
|
||||||
|
// Match arms to extract a variant from a string
|
||||||
|
let variant_arms = variants
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
||||||
|
.map(|(i, variant)| {
|
||||||
|
let variant_name = field_i(i);
|
||||||
|
|
||||||
|
let block = Match(deserialize_internally_tagged_variant(
|
||||||
|
params, variant, cattrs,
|
||||||
|
));
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
__Field::#variant_name => #block
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let expecting = format!("internally tagged enum {}", params.type_name());
|
||||||
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
||||||
|
|
||||||
|
quote_block! {
|
||||||
|
#variant_visitor
|
||||||
|
|
||||||
|
#variants_stmt
|
||||||
|
|
||||||
|
let (__tag, __content) = _serde::Deserializer::deserialize_any(
|
||||||
|
__deserializer,
|
||||||
|
_serde::#private::de::TaggedContentVisitor::<__Field>::new(#tag, #expecting))?;
|
||||||
|
let __deserializer = _serde::#private::de::ContentDeserializer::<__D::Error>::new(__content);
|
||||||
|
|
||||||
|
match __tag {
|
||||||
|
#(#variant_arms)*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generates significant part of the visit_seq and visit_map bodies of visitors
|
||||||
|
// for the variants of internally tagged enum.
|
||||||
|
fn deserialize_internally_tagged_variant(
|
||||||
|
params: &Parameters,
|
||||||
|
variant: &Variant,
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
) -> Fragment {
|
||||||
|
if let Some(path) = variant.attrs.deserialize_with() {
|
||||||
|
let unwrap_fn = unwrap_to_variant_closure(params, variant, false);
|
||||||
|
return quote_block! {
|
||||||
|
_serde::#private::Result::map(#path(__deserializer), #unwrap_fn)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let variant_ident = &variant.ident;
|
||||||
|
|
||||||
|
match effective_style(variant) {
|
||||||
|
Style::Unit => {
|
||||||
|
let this_value = ¶ms.this_value;
|
||||||
|
let type_name = params.type_name();
|
||||||
|
let variant_name = variant.ident.to_string();
|
||||||
|
let default = variant.fields.first().map(|field| {
|
||||||
|
let default = Expr(expr_is_missing(field, cattrs));
|
||||||
|
quote!((#default))
|
||||||
|
});
|
||||||
|
quote_block! {
|
||||||
|
_serde::Deserializer::deserialize_any(__deserializer, _serde::#private::de::InternallyTaggedUnitVisitor::new(#type_name, #variant_name))?;
|
||||||
|
_serde::#private::Ok(#this_value::#variant_ident #default)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Style::Newtype => {
|
||||||
|
enum_untagged::deserialize_newtype_variant(variant_ident, params, &variant.fields[0])
|
||||||
|
}
|
||||||
|
Style::Struct => struct_::deserialize(
|
||||||
|
params,
|
||||||
|
&variant.fields,
|
||||||
|
cattrs,
|
||||||
|
StructForm::InternallyTagged(variant_ident),
|
||||||
|
),
|
||||||
|
Style::Tuple => unreachable!("checked in serde_derive_internals"),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
//! Deserialization for untagged enums:
|
||||||
|
//!
|
||||||
|
//! ```ignore
|
||||||
|
//! #[serde(untagged)]
|
||||||
|
//! enum Enum {}
|
||||||
|
//! ```
|
||||||
|
|
||||||
|
use crate::de::struct_;
|
||||||
|
use crate::de::tuple;
|
||||||
|
use crate::de::{
|
||||||
|
effective_style, expr_is_missing, unwrap_to_variant_closure, Parameters, StructForm, TupleForm,
|
||||||
|
};
|
||||||
|
use crate::fragment::{Expr, Fragment};
|
||||||
|
use crate::internals::ast::{Field, Style, Variant};
|
||||||
|
use crate::internals::attr;
|
||||||
|
use crate::private;
|
||||||
|
use proc_macro2::TokenStream;
|
||||||
|
use quote::{quote, quote_spanned};
|
||||||
|
use syn::spanned::Spanned;
|
||||||
|
|
||||||
|
/// Generates `Deserialize::deserialize` body for an `enum Enum {...}` with `#[serde(untagged)]` attribute
|
||||||
|
pub(super) fn deserialize(
|
||||||
|
params: &Parameters,
|
||||||
|
variants: &[Variant],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
first_attempt: Option<TokenStream>,
|
||||||
|
) -> Fragment {
|
||||||
|
let attempts = variants
|
||||||
|
.iter()
|
||||||
|
.filter(|variant| !variant.attrs.skip_deserializing())
|
||||||
|
.map(|variant| Expr(deserialize_variant(params, variant, cattrs)));
|
||||||
|
// TODO this message could be better by saving the errors from the failed
|
||||||
|
// attempts. The heuristic used by TOML was to count the number of fields
|
||||||
|
// processed before an error, and use the error that happened after the
|
||||||
|
// largest number of fields. I'm not sure I like that. Maybe it would be
|
||||||
|
// better to save all the errors and combine them into one message that
|
||||||
|
// explains why none of the variants matched.
|
||||||
|
let fallthrough_msg = format!(
|
||||||
|
"data did not match any variant of untagged enum {}",
|
||||||
|
params.type_name()
|
||||||
|
);
|
||||||
|
let fallthrough_msg = cattrs.expecting().unwrap_or(&fallthrough_msg);
|
||||||
|
|
||||||
|
let private2 = private;
|
||||||
|
quote_block! {
|
||||||
|
let __content = _serde::de::DeserializeSeed::deserialize(_serde::#private::de::ContentVisitor::new(), __deserializer)?;
|
||||||
|
let __deserializer = _serde::#private::de::ContentRefDeserializer::<__D::Error>::new(&__content);
|
||||||
|
|
||||||
|
#first_attempt
|
||||||
|
|
||||||
|
#(
|
||||||
|
if let _serde::#private2::Ok(__ok) = #attempts {
|
||||||
|
return _serde::#private2::Ok(__ok);
|
||||||
|
}
|
||||||
|
)*
|
||||||
|
|
||||||
|
_serde::#private::Err(_serde::de::Error::custom(#fallthrough_msg))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Also used by adjacently tagged enums
|
||||||
|
pub(super) fn deserialize_variant(
|
||||||
|
params: &Parameters,
|
||||||
|
variant: &Variant,
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
) -> Fragment {
|
||||||
|
if let Some(path) = variant.attrs.deserialize_with() {
|
||||||
|
let unwrap_fn = unwrap_to_variant_closure(params, variant, false);
|
||||||
|
return quote_block! {
|
||||||
|
_serde::#private::Result::map(#path(__deserializer), #unwrap_fn)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let variant_ident = &variant.ident;
|
||||||
|
|
||||||
|
match effective_style(variant) {
|
||||||
|
Style::Unit => {
|
||||||
|
let this_value = ¶ms.this_value;
|
||||||
|
let type_name = params.type_name();
|
||||||
|
let variant_name = variant.ident.to_string();
|
||||||
|
let default = variant.fields.first().map(|field| {
|
||||||
|
let default = Expr(expr_is_missing(field, cattrs));
|
||||||
|
quote!((#default))
|
||||||
|
});
|
||||||
|
quote_expr! {
|
||||||
|
match _serde::Deserializer::deserialize_any(
|
||||||
|
__deserializer,
|
||||||
|
_serde::#private::de::UntaggedUnitVisitor::new(#type_name, #variant_name)
|
||||||
|
) {
|
||||||
|
_serde::#private::Ok(()) => _serde::#private::Ok(#this_value::#variant_ident #default),
|
||||||
|
_serde::#private::Err(__err) => _serde::#private::Err(__err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Style::Newtype => deserialize_newtype_variant(variant_ident, params, &variant.fields[0]),
|
||||||
|
Style::Tuple => tuple::deserialize(
|
||||||
|
params,
|
||||||
|
&variant.fields,
|
||||||
|
cattrs,
|
||||||
|
TupleForm::Untagged(variant_ident),
|
||||||
|
),
|
||||||
|
Style::Struct => struct_::deserialize(
|
||||||
|
params,
|
||||||
|
&variant.fields,
|
||||||
|
cattrs,
|
||||||
|
StructForm::Untagged(variant_ident),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Also used by internally tagged enums
|
||||||
|
// Implicitly (via `generate_variant`) used by adjacently tagged enums
|
||||||
|
pub(super) fn deserialize_newtype_variant(
|
||||||
|
variant_ident: &syn::Ident,
|
||||||
|
params: &Parameters,
|
||||||
|
field: &Field,
|
||||||
|
) -> Fragment {
|
||||||
|
let this_value = ¶ms.this_value;
|
||||||
|
let field_ty = field.ty;
|
||||||
|
match field.attrs.deserialize_with() {
|
||||||
|
None => {
|
||||||
|
let span = field.original.span();
|
||||||
|
let func = quote_spanned!(span=> <#field_ty as _serde::Deserialize>::deserialize);
|
||||||
|
quote_expr! {
|
||||||
|
_serde::#private::Result::map(#func(__deserializer), #this_value::#variant_ident)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(path) => {
|
||||||
|
quote_block! {
|
||||||
|
let __value: _serde::#private::Result<#field_ty, _> = #path(__deserializer);
|
||||||
|
_serde::#private::Result::map(__value, #this_value::#variant_ident)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,477 @@
|
|||||||
|
//! Deserialization of struct field identifiers and enum variant identifiers by
|
||||||
|
//! way of a Rust enum.
|
||||||
|
|
||||||
|
use crate::de::{FieldWithAliases, Parameters};
|
||||||
|
use crate::fragment::{Fragment, Stmts};
|
||||||
|
use crate::internals::ast::{Style, Variant};
|
||||||
|
use crate::internals::attr;
|
||||||
|
use crate::private;
|
||||||
|
use proc_macro2::{Literal, TokenStream};
|
||||||
|
use quote::{quote, ToTokens};
|
||||||
|
|
||||||
|
// Generates `Deserialize::deserialize` body for an enum with
|
||||||
|
// `serde(field_identifier)` or `serde(variant_identifier)` attribute.
|
||||||
|
pub(super) fn deserialize_custom(
|
||||||
|
params: &Parameters,
|
||||||
|
variants: &[Variant],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
) -> Fragment {
|
||||||
|
let is_variant = match cattrs.identifier() {
|
||||||
|
attr::Identifier::Variant => true,
|
||||||
|
attr::Identifier::Field => false,
|
||||||
|
attr::Identifier::No => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let this_type = params.this_type.to_token_stream();
|
||||||
|
let this_value = params.this_value.to_token_stream();
|
||||||
|
|
||||||
|
let (ordinary, fallthrough, fallthrough_borrowed) = if let Some(last) = variants.last() {
|
||||||
|
let last_ident = &last.ident;
|
||||||
|
if last.attrs.other() {
|
||||||
|
// Process `serde(other)` attribute. It would always be found on the
|
||||||
|
// last variant (checked in `check_identifier`), so all preceding
|
||||||
|
// are ordinary variants.
|
||||||
|
let ordinary = &variants[..variants.len() - 1];
|
||||||
|
let fallthrough = quote!(_serde::#private::Ok(#this_value::#last_ident));
|
||||||
|
(ordinary, Some(fallthrough), None)
|
||||||
|
} else if let Style::Newtype = last.style {
|
||||||
|
let ordinary = &variants[..variants.len() - 1];
|
||||||
|
let fallthrough = |value| {
|
||||||
|
quote! {
|
||||||
|
_serde::#private::Result::map(
|
||||||
|
_serde::Deserialize::deserialize(
|
||||||
|
_serde::#private::de::IdentifierDeserializer::from(#value)
|
||||||
|
),
|
||||||
|
#this_value::#last_ident)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
(
|
||||||
|
ordinary,
|
||||||
|
Some(fallthrough(quote!(__value))),
|
||||||
|
Some(fallthrough(quote!(_serde::#private::de::Borrowed(
|
||||||
|
__value
|
||||||
|
)))),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(variants, None, None)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
(variants, None, None)
|
||||||
|
};
|
||||||
|
|
||||||
|
let idents_aliases: Vec<_> = ordinary
|
||||||
|
.iter()
|
||||||
|
.map(|variant| FieldWithAliases {
|
||||||
|
ident: variant.ident.clone(),
|
||||||
|
aliases: variant.attrs.aliases(),
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let names = idents_aliases.iter().flat_map(|variant| variant.aliases);
|
||||||
|
|
||||||
|
let names_const = if fallthrough.is_some() {
|
||||||
|
None
|
||||||
|
} else if is_variant {
|
||||||
|
let variants = quote! {
|
||||||
|
#[doc(hidden)]
|
||||||
|
const VARIANTS: &'static [&'static str] = &[ #(#names),* ];
|
||||||
|
};
|
||||||
|
Some(variants)
|
||||||
|
} else {
|
||||||
|
let fields = quote! {
|
||||||
|
#[doc(hidden)]
|
||||||
|
const FIELDS: &'static [&'static str] = &[ #(#names),* ];
|
||||||
|
};
|
||||||
|
Some(fields)
|
||||||
|
};
|
||||||
|
|
||||||
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||||
|
params.generics_with_de_lifetime();
|
||||||
|
let delife = params.borrowed.de_lifetime();
|
||||||
|
let visitor_impl = Stmts(deserialize_identifier(
|
||||||
|
&this_value,
|
||||||
|
&idents_aliases,
|
||||||
|
is_variant,
|
||||||
|
fallthrough,
|
||||||
|
fallthrough_borrowed,
|
||||||
|
false,
|
||||||
|
cattrs.expecting(),
|
||||||
|
));
|
||||||
|
|
||||||
|
quote_block! {
|
||||||
|
#names_const
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
struct __FieldVisitor #de_impl_generics #where_clause {
|
||||||
|
marker: _serde::#private::PhantomData<#this_type #ty_generics>,
|
||||||
|
lifetime: _serde::#private::PhantomData<&#delife ()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[automatically_derived]
|
||||||
|
impl #de_impl_generics _serde::de::Visitor<#delife> for __FieldVisitor #de_ty_generics #where_clause {
|
||||||
|
type Value = #this_type #ty_generics;
|
||||||
|
|
||||||
|
#visitor_impl
|
||||||
|
}
|
||||||
|
|
||||||
|
let __visitor = __FieldVisitor {
|
||||||
|
marker: _serde::#private::PhantomData::<#this_type #ty_generics>,
|
||||||
|
lifetime: _serde::#private::PhantomData,
|
||||||
|
};
|
||||||
|
_serde::Deserializer::deserialize_identifier(__deserializer, __visitor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn deserialize_generated(
|
||||||
|
deserialized_fields: &[FieldWithAliases],
|
||||||
|
has_flatten: bool,
|
||||||
|
is_variant: bool,
|
||||||
|
ignore_variant: Option<TokenStream>,
|
||||||
|
fallthrough: Option<TokenStream>,
|
||||||
|
) -> Fragment {
|
||||||
|
let this_value = quote!(__Field);
|
||||||
|
let field_idents: &Vec<_> = &deserialized_fields
|
||||||
|
.iter()
|
||||||
|
.map(|field| &field.ident)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let visitor_impl = Stmts(deserialize_identifier(
|
||||||
|
&this_value,
|
||||||
|
deserialized_fields,
|
||||||
|
is_variant,
|
||||||
|
fallthrough,
|
||||||
|
None,
|
||||||
|
!is_variant && has_flatten,
|
||||||
|
None,
|
||||||
|
));
|
||||||
|
|
||||||
|
let lifetime = if !is_variant && has_flatten {
|
||||||
|
Some(quote!(<'de>))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
quote_block! {
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
#[doc(hidden)]
|
||||||
|
enum __Field #lifetime {
|
||||||
|
#(#field_idents,)*
|
||||||
|
#ignore_variant
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
struct __FieldVisitor;
|
||||||
|
|
||||||
|
#[automatically_derived]
|
||||||
|
impl<'de> _serde::de::Visitor<'de> for __FieldVisitor {
|
||||||
|
type Value = __Field #lifetime;
|
||||||
|
|
||||||
|
#visitor_impl
|
||||||
|
}
|
||||||
|
|
||||||
|
#[automatically_derived]
|
||||||
|
impl<'de> _serde::Deserialize<'de> for __Field #lifetime {
|
||||||
|
#[inline]
|
||||||
|
fn deserialize<__D>(__deserializer: __D) -> _serde::#private::Result<Self, __D::Error>
|
||||||
|
where
|
||||||
|
__D: _serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
_serde::Deserializer::deserialize_identifier(__deserializer, __FieldVisitor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_identifier(
|
||||||
|
this_value: &TokenStream,
|
||||||
|
deserialized_fields: &[FieldWithAliases],
|
||||||
|
is_variant: bool,
|
||||||
|
fallthrough: Option<TokenStream>,
|
||||||
|
fallthrough_borrowed: Option<TokenStream>,
|
||||||
|
collect_other_fields: bool,
|
||||||
|
expecting: Option<&str>,
|
||||||
|
) -> Fragment {
|
||||||
|
let str_mapping = deserialized_fields.iter().map(|field| {
|
||||||
|
let ident = &field.ident;
|
||||||
|
let aliases = field.aliases;
|
||||||
|
let private2 = private;
|
||||||
|
// `aliases` also contains a main name
|
||||||
|
quote! {
|
||||||
|
#(
|
||||||
|
#aliases => _serde::#private2::Ok(#this_value::#ident),
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let bytes_mapping = deserialized_fields.iter().map(|field| {
|
||||||
|
let ident = &field.ident;
|
||||||
|
// `aliases` also contains a main name
|
||||||
|
let aliases = field
|
||||||
|
.aliases
|
||||||
|
.iter()
|
||||||
|
.map(|alias| Literal::byte_string(alias.value.as_bytes()));
|
||||||
|
let private2 = private;
|
||||||
|
quote! {
|
||||||
|
#(
|
||||||
|
#aliases => _serde::#private2::Ok(#this_value::#ident),
|
||||||
|
)*
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let expecting = expecting.unwrap_or(if is_variant {
|
||||||
|
"variant identifier"
|
||||||
|
} else {
|
||||||
|
"field identifier"
|
||||||
|
});
|
||||||
|
|
||||||
|
let bytes_to_str = if fallthrough.is_some() || collect_other_fields {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(quote! {
|
||||||
|
let __value = &_serde::#private::from_utf8_lossy(__value);
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
let (
|
||||||
|
value_as_str_content,
|
||||||
|
value_as_borrowed_str_content,
|
||||||
|
value_as_bytes_content,
|
||||||
|
value_as_borrowed_bytes_content,
|
||||||
|
) = if collect_other_fields {
|
||||||
|
(
|
||||||
|
Some(quote! {
|
||||||
|
let __value = _serde::#private::de::Content::String(_serde::#private::ToString::to_string(__value));
|
||||||
|
}),
|
||||||
|
Some(quote! {
|
||||||
|
let __value = _serde::#private::de::Content::Str(__value);
|
||||||
|
}),
|
||||||
|
Some(quote! {
|
||||||
|
let __value = _serde::#private::de::Content::ByteBuf(__value.to_vec());
|
||||||
|
}),
|
||||||
|
Some(quote! {
|
||||||
|
let __value = _serde::#private::de::Content::Bytes(__value);
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(None, None, None, None)
|
||||||
|
};
|
||||||
|
|
||||||
|
let fallthrough_arm_tokens;
|
||||||
|
let fallthrough_arm = if let Some(fallthrough) = &fallthrough {
|
||||||
|
fallthrough
|
||||||
|
} else if is_variant {
|
||||||
|
fallthrough_arm_tokens = quote! {
|
||||||
|
_serde::#private::Err(_serde::de::Error::unknown_variant(__value, VARIANTS))
|
||||||
|
};
|
||||||
|
&fallthrough_arm_tokens
|
||||||
|
} else {
|
||||||
|
fallthrough_arm_tokens = quote! {
|
||||||
|
_serde::#private::Err(_serde::de::Error::unknown_field(__value, FIELDS))
|
||||||
|
};
|
||||||
|
&fallthrough_arm_tokens
|
||||||
|
};
|
||||||
|
|
||||||
|
let visit_other = if collect_other_fields {
|
||||||
|
quote! {
|
||||||
|
fn visit_bool<__E>(self, __value: bool) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
_serde::#private::Ok(__Field::__other(_serde::#private::de::Content::Bool(__value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_i8<__E>(self, __value: i8) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
_serde::#private::Ok(__Field::__other(_serde::#private::de::Content::I8(__value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_i16<__E>(self, __value: i16) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
_serde::#private::Ok(__Field::__other(_serde::#private::de::Content::I16(__value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_i32<__E>(self, __value: i32) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
_serde::#private::Ok(__Field::__other(_serde::#private::de::Content::I32(__value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_i64<__E>(self, __value: i64) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
_serde::#private::Ok(__Field::__other(_serde::#private::de::Content::I64(__value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_u8<__E>(self, __value: u8) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
_serde::#private::Ok(__Field::__other(_serde::#private::de::Content::U8(__value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_u16<__E>(self, __value: u16) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
_serde::#private::Ok(__Field::__other(_serde::#private::de::Content::U16(__value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_u32<__E>(self, __value: u32) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
_serde::#private::Ok(__Field::__other(_serde::#private::de::Content::U32(__value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_u64<__E>(self, __value: u64) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
_serde::#private::Ok(__Field::__other(_serde::#private::de::Content::U64(__value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_f32<__E>(self, __value: f32) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
_serde::#private::Ok(__Field::__other(_serde::#private::de::Content::F32(__value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_f64<__E>(self, __value: f64) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
_serde::#private::Ok(__Field::__other(_serde::#private::de::Content::F64(__value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_char<__E>(self, __value: char) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
_serde::#private::Ok(__Field::__other(_serde::#private::de::Content::Char(__value)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_unit<__E>(self) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
_serde::#private::Ok(__Field::__other(_serde::#private::de::Content::Unit))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let u64_mapping = deserialized_fields.iter().enumerate().map(|(i, field)| {
|
||||||
|
let i = i as u64;
|
||||||
|
let ident = &field.ident;
|
||||||
|
quote!(#i => _serde::#private::Ok(#this_value::#ident))
|
||||||
|
});
|
||||||
|
|
||||||
|
let u64_fallthrough_arm_tokens;
|
||||||
|
let u64_fallthrough_arm = if let Some(fallthrough) = &fallthrough {
|
||||||
|
fallthrough
|
||||||
|
} else {
|
||||||
|
let index_expecting = if is_variant { "variant" } else { "field" };
|
||||||
|
let fallthrough_msg = format!(
|
||||||
|
"{} index 0 <= i < {}",
|
||||||
|
index_expecting,
|
||||||
|
deserialized_fields.len(),
|
||||||
|
);
|
||||||
|
u64_fallthrough_arm_tokens = quote! {
|
||||||
|
_serde::#private::Err(_serde::de::Error::invalid_value(
|
||||||
|
_serde::de::Unexpected::Unsigned(__value),
|
||||||
|
&#fallthrough_msg,
|
||||||
|
))
|
||||||
|
};
|
||||||
|
&u64_fallthrough_arm_tokens
|
||||||
|
};
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
fn visit_u64<__E>(self, __value: u64) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
match __value {
|
||||||
|
#(#u64_mapping,)*
|
||||||
|
_ => #u64_fallthrough_arm,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let visit_borrowed = if fallthrough_borrowed.is_some() || collect_other_fields {
|
||||||
|
let str_mapping = str_mapping.clone();
|
||||||
|
let bytes_mapping = bytes_mapping.clone();
|
||||||
|
let fallthrough_borrowed_arm = fallthrough_borrowed.as_ref().unwrap_or(fallthrough_arm);
|
||||||
|
Some(quote! {
|
||||||
|
fn visit_borrowed_str<__E>(self, __value: &'de str) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
match __value {
|
||||||
|
#(#str_mapping)*
|
||||||
|
_ => {
|
||||||
|
#value_as_borrowed_str_content
|
||||||
|
#fallthrough_borrowed_arm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_borrowed_bytes<__E>(self, __value: &'de [u8]) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
match __value {
|
||||||
|
#(#bytes_mapping)*
|
||||||
|
_ => {
|
||||||
|
#bytes_to_str
|
||||||
|
#value_as_borrowed_bytes_content
|
||||||
|
#fallthrough_borrowed_arm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
quote_block! {
|
||||||
|
fn expecting(&self, __formatter: &mut _serde::#private::Formatter) -> _serde::#private::fmt::Result {
|
||||||
|
_serde::#private::Formatter::write_str(__formatter, #expecting)
|
||||||
|
}
|
||||||
|
|
||||||
|
#visit_other
|
||||||
|
|
||||||
|
fn visit_str<__E>(self, __value: &str) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
match __value {
|
||||||
|
#(#str_mapping)*
|
||||||
|
_ => {
|
||||||
|
#value_as_str_content
|
||||||
|
#fallthrough_arm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn visit_bytes<__E>(self, __value: &[u8]) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
match __value {
|
||||||
|
#(#bytes_mapping)*
|
||||||
|
_ => {
|
||||||
|
#bytes_to_str
|
||||||
|
#value_as_bytes_content
|
||||||
|
#fallthrough_arm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#visit_borrowed
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,697 @@
|
|||||||
|
use crate::de::identifier;
|
||||||
|
use crate::de::{
|
||||||
|
deserialize_seq, expr_is_missing, field_i, has_flatten, wrap_deserialize_field_with,
|
||||||
|
FieldWithAliases, Parameters, StructForm,
|
||||||
|
};
|
||||||
|
#[cfg(feature = "deserialize_in_place")]
|
||||||
|
use crate::de::{deserialize_seq_in_place, place_lifetime};
|
||||||
|
use crate::fragment::{Expr, Fragment, Match, Stmts};
|
||||||
|
use crate::internals::ast::Field;
|
||||||
|
use crate::internals::attr;
|
||||||
|
use crate::private;
|
||||||
|
use proc_macro2::TokenStream;
|
||||||
|
use quote::{quote, quote_spanned};
|
||||||
|
use syn::spanned::Spanned;
|
||||||
|
|
||||||
|
/// Generates `Deserialize::deserialize` body for a `struct Struct {...}`
|
||||||
|
pub(super) fn deserialize(
|
||||||
|
params: &Parameters,
|
||||||
|
fields: &[Field],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
form: StructForm,
|
||||||
|
) -> Fragment {
|
||||||
|
let this_type = ¶ms.this_type;
|
||||||
|
let this_value = ¶ms.this_value;
|
||||||
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||||
|
params.generics_with_de_lifetime();
|
||||||
|
let delife = params.borrowed.de_lifetime();
|
||||||
|
|
||||||
|
// If there are getters (implying private fields), construct the local type
|
||||||
|
// and use an `Into` conversion to get the remote type. If there are no
|
||||||
|
// getters then construct the target type directly.
|
||||||
|
let construct = if params.has_getter {
|
||||||
|
let local = ¶ms.local;
|
||||||
|
quote!(#local)
|
||||||
|
} else {
|
||||||
|
quote!(#this_value)
|
||||||
|
};
|
||||||
|
|
||||||
|
let type_path = match form {
|
||||||
|
StructForm::Struct => construct,
|
||||||
|
StructForm::ExternallyTagged(variant_ident)
|
||||||
|
| StructForm::InternallyTagged(variant_ident)
|
||||||
|
| StructForm::Untagged(variant_ident) => quote!(#construct::#variant_ident),
|
||||||
|
};
|
||||||
|
let expecting = match form {
|
||||||
|
StructForm::Struct => format!("struct {}", params.type_name()),
|
||||||
|
StructForm::ExternallyTagged(variant_ident)
|
||||||
|
| StructForm::InternallyTagged(variant_ident)
|
||||||
|
| StructForm::Untagged(variant_ident) => {
|
||||||
|
format!("struct variant {}::{}", params.type_name(), variant_ident)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
||||||
|
|
||||||
|
let deserialized_fields: Vec<_> = fields
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
// Skip fields that shouldn't be deserialized or that were flattened,
|
||||||
|
// so they don't appear in the storage in their literal form
|
||||||
|
.filter(|&(_, field)| !field.attrs.skip_deserializing() && !field.attrs.flatten())
|
||||||
|
.map(|(i, field)| FieldWithAliases {
|
||||||
|
ident: field_i(i),
|
||||||
|
aliases: field.attrs.aliases(),
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let has_flatten = has_flatten(fields);
|
||||||
|
let field_visitor = deserialize_field_identifier(&deserialized_fields, cattrs, has_flatten);
|
||||||
|
|
||||||
|
// untagged struct variants do not get a visit_seq method. The same applies to
|
||||||
|
// structs that only have a map representation.
|
||||||
|
let visit_seq = match form {
|
||||||
|
StructForm::Untagged(_) => None,
|
||||||
|
_ if has_flatten => None,
|
||||||
|
_ => {
|
||||||
|
let mut_seq = if deserialized_fields.is_empty() {
|
||||||
|
quote!(_)
|
||||||
|
} else {
|
||||||
|
quote!(mut __seq)
|
||||||
|
};
|
||||||
|
|
||||||
|
let visit_seq = Stmts(deserialize_seq(
|
||||||
|
&type_path, params, fields, true, cattrs, expecting,
|
||||||
|
));
|
||||||
|
|
||||||
|
Some(quote! {
|
||||||
|
#[inline]
|
||||||
|
fn visit_seq<__A>(self, #mut_seq: __A) -> _serde::#private::Result<Self::Value, __A::Error>
|
||||||
|
where
|
||||||
|
__A: _serde::de::SeqAccess<#delife>,
|
||||||
|
{
|
||||||
|
#visit_seq
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let visit_map = Stmts(deserialize_map(
|
||||||
|
&type_path,
|
||||||
|
params,
|
||||||
|
fields,
|
||||||
|
cattrs,
|
||||||
|
has_flatten,
|
||||||
|
));
|
||||||
|
|
||||||
|
let visitor_seed = match form {
|
||||||
|
StructForm::ExternallyTagged(..) if has_flatten => Some(quote! {
|
||||||
|
#[automatically_derived]
|
||||||
|
impl #de_impl_generics _serde::de::DeserializeSeed<#delife> for __Visitor #de_ty_generics #where_clause {
|
||||||
|
type Value = #this_type #ty_generics;
|
||||||
|
|
||||||
|
fn deserialize<__D>(self, __deserializer: __D) -> _serde::#private::Result<Self::Value, __D::Error>
|
||||||
|
where
|
||||||
|
__D: _serde::Deserializer<#delife>,
|
||||||
|
{
|
||||||
|
_serde::Deserializer::deserialize_map(__deserializer, self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let fields_stmt = if has_flatten {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
let field_names = deserialized_fields.iter().flat_map(|field| field.aliases);
|
||||||
|
|
||||||
|
Some(quote! {
|
||||||
|
#[doc(hidden)]
|
||||||
|
const FIELDS: &'static [&'static str] = &[ #(#field_names),* ];
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
let visitor_expr = quote! {
|
||||||
|
__Visitor {
|
||||||
|
marker: _serde::#private::PhantomData::<#this_type #ty_generics>,
|
||||||
|
lifetime: _serde::#private::PhantomData,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let dispatch = match form {
|
||||||
|
StructForm::Struct if has_flatten => quote! {
|
||||||
|
_serde::Deserializer::deserialize_map(__deserializer, #visitor_expr)
|
||||||
|
},
|
||||||
|
StructForm::Struct => {
|
||||||
|
let type_name = cattrs.name().deserialize_name();
|
||||||
|
quote! {
|
||||||
|
_serde::Deserializer::deserialize_struct(__deserializer, #type_name, FIELDS, #visitor_expr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
StructForm::ExternallyTagged(_) if has_flatten => quote! {
|
||||||
|
_serde::de::VariantAccess::newtype_variant_seed(__variant, #visitor_expr)
|
||||||
|
},
|
||||||
|
StructForm::ExternallyTagged(_) => quote! {
|
||||||
|
_serde::de::VariantAccess::struct_variant(__variant, FIELDS, #visitor_expr)
|
||||||
|
},
|
||||||
|
StructForm::InternallyTagged(_) => quote! {
|
||||||
|
_serde::Deserializer::deserialize_any(__deserializer, #visitor_expr)
|
||||||
|
},
|
||||||
|
StructForm::Untagged(_) => quote! {
|
||||||
|
_serde::Deserializer::deserialize_any(__deserializer, #visitor_expr)
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
quote_block! {
|
||||||
|
#field_visitor
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
struct __Visitor #de_impl_generics #where_clause {
|
||||||
|
marker: _serde::#private::PhantomData<#this_type #ty_generics>,
|
||||||
|
lifetime: _serde::#private::PhantomData<&#delife ()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[automatically_derived]
|
||||||
|
impl #de_impl_generics _serde::de::Visitor<#delife> for __Visitor #de_ty_generics #where_clause {
|
||||||
|
type Value = #this_type #ty_generics;
|
||||||
|
|
||||||
|
fn expecting(&self, __formatter: &mut _serde::#private::Formatter) -> _serde::#private::fmt::Result {
|
||||||
|
_serde::#private::Formatter::write_str(__formatter, #expecting)
|
||||||
|
}
|
||||||
|
|
||||||
|
#visit_seq
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_map<__A>(self, mut __map: __A) -> _serde::#private::Result<Self::Value, __A::Error>
|
||||||
|
where
|
||||||
|
__A: _serde::de::MapAccess<#delife>,
|
||||||
|
{
|
||||||
|
#visit_map
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#visitor_seed
|
||||||
|
|
||||||
|
#fields_stmt
|
||||||
|
|
||||||
|
#dispatch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_map(
|
||||||
|
struct_path: &TokenStream,
|
||||||
|
params: &Parameters,
|
||||||
|
fields: &[Field],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
has_flatten: bool,
|
||||||
|
) -> Fragment {
|
||||||
|
// Create the field names for the fields.
|
||||||
|
let fields_names: Vec<_> = fields
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, field)| (field, field_i(i)))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
// Declare each field that will be deserialized.
|
||||||
|
let let_values = fields_names
|
||||||
|
.iter()
|
||||||
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing() && !field.attrs.flatten())
|
||||||
|
.map(|(field, name)| {
|
||||||
|
let field_ty = field.ty;
|
||||||
|
quote! {
|
||||||
|
let mut #name: _serde::#private::Option<#field_ty> = _serde::#private::None;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Collect contents for flatten fields into a buffer
|
||||||
|
let let_collect = if has_flatten {
|
||||||
|
Some(quote! {
|
||||||
|
let mut __collect = _serde::#private::Vec::<_serde::#private::Option<(
|
||||||
|
_serde::#private::de::Content,
|
||||||
|
_serde::#private::de::Content
|
||||||
|
)>>::new();
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
// Match arms to extract a value for a field.
|
||||||
|
let value_arms = fields_names
|
||||||
|
.iter()
|
||||||
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing() && !field.attrs.flatten())
|
||||||
|
.map(|(field, name)| {
|
||||||
|
let deser_name = field.attrs.name().deserialize_name();
|
||||||
|
|
||||||
|
let visit = match field.attrs.deserialize_with() {
|
||||||
|
None => {
|
||||||
|
let field_ty = field.ty;
|
||||||
|
let span = field.original.span();
|
||||||
|
let func =
|
||||||
|
quote_spanned!(span=> _serde::de::MapAccess::next_value::<#field_ty>);
|
||||||
|
quote! {
|
||||||
|
#func(&mut __map)?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(path) => {
|
||||||
|
let (wrapper, wrapper_ty) = wrap_deserialize_field_with(params, field.ty, path);
|
||||||
|
quote!({
|
||||||
|
#wrapper
|
||||||
|
match _serde::de::MapAccess::next_value::<#wrapper_ty>(&mut __map) {
|
||||||
|
_serde::#private::Ok(__wrapper) => __wrapper.value,
|
||||||
|
_serde::#private::Err(__err) => {
|
||||||
|
return _serde::#private::Err(__err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
quote! {
|
||||||
|
__Field::#name => {
|
||||||
|
if _serde::#private::Option::is_some(&#name) {
|
||||||
|
return _serde::#private::Err(<__A::Error as _serde::de::Error>::duplicate_field(#deser_name));
|
||||||
|
}
|
||||||
|
#name = _serde::#private::Some(#visit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Visit ignored values to consume them
|
||||||
|
let ignored_arm = if has_flatten {
|
||||||
|
Some(quote! {
|
||||||
|
__Field::__other(__name) => {
|
||||||
|
__collect.push(_serde::#private::Some((
|
||||||
|
__name,
|
||||||
|
_serde::de::MapAccess::next_value_seed(&mut __map, _serde::#private::de::ContentVisitor::new())?)));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else if cattrs.deny_unknown_fields() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(quote! {
|
||||||
|
_ => { let _ = _serde::de::MapAccess::next_value::<_serde::de::IgnoredAny>(&mut __map)?; }
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing());
|
||||||
|
let match_keys = if cattrs.deny_unknown_fields() && all_skipped {
|
||||||
|
quote! {
|
||||||
|
// FIXME: Once feature(exhaustive_patterns) is stable:
|
||||||
|
// let _serde::#private::None::<__Field> = _serde::de::MapAccess::next_key(&mut __map)?;
|
||||||
|
_serde::#private::Option::map(
|
||||||
|
_serde::de::MapAccess::next_key::<__Field>(&mut __map)?,
|
||||||
|
|__impossible| match __impossible {});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
quote! {
|
||||||
|
while let _serde::#private::Some(__key) = _serde::de::MapAccess::next_key::<__Field>(&mut __map)? {
|
||||||
|
match __key {
|
||||||
|
#(#value_arms)*
|
||||||
|
#ignored_arm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let extract_values = fields_names
|
||||||
|
.iter()
|
||||||
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing() && !field.attrs.flatten())
|
||||||
|
.map(|(field, name)| {
|
||||||
|
let missing_expr = Match(expr_is_missing(field, cattrs));
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
let #name = match #name {
|
||||||
|
_serde::#private::Some(#name) => #name,
|
||||||
|
_serde::#private::None => #missing_expr
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let extract_collected = fields_names
|
||||||
|
.iter()
|
||||||
|
.filter(|&&(field, _)| field.attrs.flatten() && !field.attrs.skip_deserializing())
|
||||||
|
.map(|(field, name)| {
|
||||||
|
let field_ty = field.ty;
|
||||||
|
let func = match field.attrs.deserialize_with() {
|
||||||
|
None => {
|
||||||
|
let span = field.original.span();
|
||||||
|
quote_spanned!(span=> _serde::de::Deserialize::deserialize)
|
||||||
|
}
|
||||||
|
Some(path) => quote!(#path),
|
||||||
|
};
|
||||||
|
quote! {
|
||||||
|
let #name: #field_ty = #func(
|
||||||
|
_serde::#private::de::FlatMapDeserializer(
|
||||||
|
&mut __collect,
|
||||||
|
_serde::#private::PhantomData))?;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let collected_deny_unknown_fields = if has_flatten && cattrs.deny_unknown_fields() {
|
||||||
|
Some(quote! {
|
||||||
|
if let _serde::#private::Some(_serde::#private::Some((__key, _))) =
|
||||||
|
__collect.into_iter().filter(_serde::#private::Option::is_some).next()
|
||||||
|
{
|
||||||
|
if let _serde::#private::Some(__key) = _serde::#private::de::content_as_str(&__key) {
|
||||||
|
return _serde::#private::Err(
|
||||||
|
_serde::de::Error::custom(format_args!("unknown field `{}`", &__key)));
|
||||||
|
} else {
|
||||||
|
return _serde::#private::Err(
|
||||||
|
_serde::de::Error::custom(format_args!("unexpected map key")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = fields_names.iter().map(|(field, name)| {
|
||||||
|
let member = &field.member;
|
||||||
|
if field.attrs.skip_deserializing() {
|
||||||
|
let value = Expr(expr_is_missing(field, cattrs));
|
||||||
|
quote!(#member: #value)
|
||||||
|
} else {
|
||||||
|
quote!(#member: #name)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let let_default = match cattrs.default() {
|
||||||
|
attr::Default::Default => Some(quote!(
|
||||||
|
let __default: Self::Value = _serde::#private::Default::default();
|
||||||
|
)),
|
||||||
|
// If #path returns wrong type, error will be reported here (^^^^^).
|
||||||
|
// We attach span of the path to the function so it will be reported
|
||||||
|
// on the #[serde(default = "...")]
|
||||||
|
// ^^^^^
|
||||||
|
attr::Default::Path(path) => Some(quote_spanned!(path.span()=>
|
||||||
|
let __default: Self::Value = #path();
|
||||||
|
)),
|
||||||
|
attr::Default::None => {
|
||||||
|
// We don't need the default value, to prevent an unused variable warning
|
||||||
|
// we'll leave the line empty.
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut result = quote!(#struct_path { #(#result),* });
|
||||||
|
if params.has_getter {
|
||||||
|
let this_type = ¶ms.this_type;
|
||||||
|
let (_, ty_generics, _) = params.generics.split_for_impl();
|
||||||
|
result = quote! {
|
||||||
|
_serde::#private::Into::<#this_type #ty_generics>::into(#result)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
quote_block! {
|
||||||
|
#(#let_values)*
|
||||||
|
|
||||||
|
#let_collect
|
||||||
|
|
||||||
|
#match_keys
|
||||||
|
|
||||||
|
#let_default
|
||||||
|
|
||||||
|
#(#extract_values)*
|
||||||
|
|
||||||
|
#(#extract_collected)*
|
||||||
|
|
||||||
|
#collected_deny_unknown_fields
|
||||||
|
|
||||||
|
_serde::#private::Ok(#result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates `Deserialize::deserialize_in_place` body for a `struct Struct {...}`
|
||||||
|
#[cfg(feature = "deserialize_in_place")]
|
||||||
|
pub(super) fn deserialize_in_place(
|
||||||
|
params: &Parameters,
|
||||||
|
fields: &[Field],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
) -> Option<Fragment> {
|
||||||
|
// for now we do not support in_place deserialization for structs that
|
||||||
|
// are represented as map.
|
||||||
|
if has_flatten(fields) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let this_type = ¶ms.this_type;
|
||||||
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||||
|
params.generics_with_de_lifetime();
|
||||||
|
let delife = params.borrowed.de_lifetime();
|
||||||
|
|
||||||
|
let expecting = format!("struct {}", params.type_name());
|
||||||
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
||||||
|
|
||||||
|
let deserialized_fields: Vec<_> = fields
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter(|&(_, field)| !field.attrs.skip_deserializing())
|
||||||
|
.map(|(i, field)| FieldWithAliases {
|
||||||
|
ident: field_i(i),
|
||||||
|
aliases: field.attrs.aliases(),
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let field_visitor = deserialize_field_identifier(&deserialized_fields, cattrs, false);
|
||||||
|
|
||||||
|
let mut_seq = if deserialized_fields.is_empty() {
|
||||||
|
quote!(_)
|
||||||
|
} else {
|
||||||
|
quote!(mut __seq)
|
||||||
|
};
|
||||||
|
let visit_seq = Stmts(deserialize_seq_in_place(params, fields, cattrs, expecting));
|
||||||
|
let visit_map = Stmts(deserialize_map_in_place(params, fields, cattrs));
|
||||||
|
let field_names = deserialized_fields.iter().flat_map(|field| field.aliases);
|
||||||
|
let type_name = cattrs.name().deserialize_name();
|
||||||
|
|
||||||
|
let in_place_impl_generics = de_impl_generics.in_place();
|
||||||
|
let in_place_ty_generics = de_ty_generics.in_place();
|
||||||
|
let place_life = place_lifetime();
|
||||||
|
|
||||||
|
Some(quote_block! {
|
||||||
|
#field_visitor
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
struct __Visitor #in_place_impl_generics #where_clause {
|
||||||
|
place: &#place_life mut #this_type #ty_generics,
|
||||||
|
lifetime: _serde::#private::PhantomData<&#delife ()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[automatically_derived]
|
||||||
|
impl #in_place_impl_generics _serde::de::Visitor<#delife> for __Visitor #in_place_ty_generics #where_clause {
|
||||||
|
type Value = ();
|
||||||
|
|
||||||
|
fn expecting(&self, __formatter: &mut _serde::#private::Formatter) -> _serde::#private::fmt::Result {
|
||||||
|
_serde::#private::Formatter::write_str(__formatter, #expecting)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_seq<__A>(self, #mut_seq: __A) -> _serde::#private::Result<Self::Value, __A::Error>
|
||||||
|
where
|
||||||
|
__A: _serde::de::SeqAccess<#delife>,
|
||||||
|
{
|
||||||
|
#visit_seq
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_map<__A>(self, mut __map: __A) -> _serde::#private::Result<Self::Value, __A::Error>
|
||||||
|
where
|
||||||
|
__A: _serde::de::MapAccess<#delife>,
|
||||||
|
{
|
||||||
|
#visit_map
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
const FIELDS: &'static [&'static str] = &[ #(#field_names),* ];
|
||||||
|
|
||||||
|
_serde::Deserializer::deserialize_struct(__deserializer, #type_name, FIELDS, __Visitor {
|
||||||
|
place: __place,
|
||||||
|
lifetime: _serde::#private::PhantomData,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "deserialize_in_place")]
|
||||||
|
fn deserialize_map_in_place(
|
||||||
|
params: &Parameters,
|
||||||
|
fields: &[Field],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
) -> Fragment {
|
||||||
|
assert!(
|
||||||
|
!has_flatten(fields),
|
||||||
|
"inplace deserialization of maps does not support flatten fields"
|
||||||
|
);
|
||||||
|
|
||||||
|
// Create the field names for the fields.
|
||||||
|
let fields_names: Vec<_> = fields
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(i, field)| (field, field_i(i)))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
// For deserialize_in_place, declare booleans for each field that will be
|
||||||
|
// deserialized.
|
||||||
|
let let_flags = fields_names
|
||||||
|
.iter()
|
||||||
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
||||||
|
.map(|(_, name)| {
|
||||||
|
quote! {
|
||||||
|
let mut #name: bool = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Match arms to extract a value for a field.
|
||||||
|
let value_arms_from = fields_names
|
||||||
|
.iter()
|
||||||
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
||||||
|
.map(|(field, name)| {
|
||||||
|
let deser_name = field.attrs.name().deserialize_name();
|
||||||
|
let member = &field.member;
|
||||||
|
|
||||||
|
let visit = match field.attrs.deserialize_with() {
|
||||||
|
None => {
|
||||||
|
quote! {
|
||||||
|
_serde::de::MapAccess::next_value_seed(&mut __map, _serde::#private::de::InPlaceSeed(&mut self.place.#member))?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(path) => {
|
||||||
|
let (wrapper, wrapper_ty) = wrap_deserialize_field_with(params, field.ty, path);
|
||||||
|
quote!({
|
||||||
|
#wrapper
|
||||||
|
self.place.#member = match _serde::de::MapAccess::next_value::<#wrapper_ty>(&mut __map) {
|
||||||
|
_serde::#private::Ok(__wrapper) => __wrapper.value,
|
||||||
|
_serde::#private::Err(__err) => {
|
||||||
|
return _serde::#private::Err(__err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
quote! {
|
||||||
|
__Field::#name => {
|
||||||
|
if #name {
|
||||||
|
return _serde::#private::Err(<__A::Error as _serde::de::Error>::duplicate_field(#deser_name));
|
||||||
|
}
|
||||||
|
#visit;
|
||||||
|
#name = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Visit ignored values to consume them
|
||||||
|
let ignored_arm = if cattrs.deny_unknown_fields() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(quote! {
|
||||||
|
_ => { let _ = _serde::de::MapAccess::next_value::<_serde::de::IgnoredAny>(&mut __map)?; }
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing());
|
||||||
|
|
||||||
|
let match_keys = if cattrs.deny_unknown_fields() && all_skipped {
|
||||||
|
quote! {
|
||||||
|
// FIXME: Once feature(exhaustive_patterns) is stable:
|
||||||
|
// let _serde::#private::None::<__Field> = _serde::de::MapAccess::next_key(&mut __map)?;
|
||||||
|
_serde::#private::Option::map(
|
||||||
|
_serde::de::MapAccess::next_key::<__Field>(&mut __map)?,
|
||||||
|
|__impossible| match __impossible {});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
quote! {
|
||||||
|
while let _serde::#private::Some(__key) = _serde::de::MapAccess::next_key::<__Field>(&mut __map)? {
|
||||||
|
match __key {
|
||||||
|
#(#value_arms_from)*
|
||||||
|
#ignored_arm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let check_flags = fields_names
|
||||||
|
.iter()
|
||||||
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
||||||
|
.map(|(field, name)| {
|
||||||
|
let missing_expr = expr_is_missing(field, cattrs);
|
||||||
|
// If missing_expr unconditionally returns an error, don't try
|
||||||
|
// to assign its value to self.place.
|
||||||
|
if field.attrs.default().is_none()
|
||||||
|
&& cattrs.default().is_none()
|
||||||
|
&& field.attrs.deserialize_with().is_some()
|
||||||
|
{
|
||||||
|
let missing_expr = Stmts(missing_expr);
|
||||||
|
quote! {
|
||||||
|
if !#name {
|
||||||
|
#missing_expr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let member = &field.member;
|
||||||
|
let missing_expr = Expr(missing_expr);
|
||||||
|
quote! {
|
||||||
|
if !#name {
|
||||||
|
self.place.#member = #missing_expr;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let this_type = ¶ms.this_type;
|
||||||
|
let (_, ty_generics, _) = params.generics.split_for_impl();
|
||||||
|
|
||||||
|
let let_default = match cattrs.default() {
|
||||||
|
attr::Default::Default => Some(quote!(
|
||||||
|
let __default: #this_type #ty_generics = _serde::#private::Default::default();
|
||||||
|
)),
|
||||||
|
// If #path returns wrong type, error will be reported here (^^^^^).
|
||||||
|
// We attach span of the path to the function so it will be reported
|
||||||
|
// on the #[serde(default = "...")]
|
||||||
|
// ^^^^^
|
||||||
|
attr::Default::Path(path) => Some(quote_spanned!(path.span()=>
|
||||||
|
let __default: #this_type #ty_generics = #path();
|
||||||
|
)),
|
||||||
|
attr::Default::None => {
|
||||||
|
// We don't need the default value, to prevent an unused variable warning
|
||||||
|
// we'll leave the line empty.
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
quote_block! {
|
||||||
|
#(#let_flags)*
|
||||||
|
|
||||||
|
#match_keys
|
||||||
|
|
||||||
|
#let_default
|
||||||
|
|
||||||
|
#(#check_flags)*
|
||||||
|
|
||||||
|
_serde::#private::Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates enum and its `Deserialize` implementation that represents each
|
||||||
|
/// non-skipped field of the struct
|
||||||
|
fn deserialize_field_identifier(
|
||||||
|
deserialized_fields: &[FieldWithAliases],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
has_flatten: bool,
|
||||||
|
) -> Stmts {
|
||||||
|
let (ignore_variant, fallthrough) = if has_flatten {
|
||||||
|
let ignore_variant = quote!(__other(_serde::#private::de::Content<'de>),);
|
||||||
|
let fallthrough = quote!(_serde::#private::Ok(__Field::__other(__value)));
|
||||||
|
(Some(ignore_variant), Some(fallthrough))
|
||||||
|
} else if cattrs.deny_unknown_fields() {
|
||||||
|
(None, None)
|
||||||
|
} else {
|
||||||
|
let ignore_variant = quote!(__ignore,);
|
||||||
|
let fallthrough = quote!(_serde::#private::Ok(__Field::__ignore));
|
||||||
|
(Some(ignore_variant), Some(fallthrough))
|
||||||
|
};
|
||||||
|
|
||||||
|
Stmts(identifier::deserialize_generated(
|
||||||
|
deserialized_fields,
|
||||||
|
has_flatten,
|
||||||
|
false,
|
||||||
|
ignore_variant,
|
||||||
|
fallthrough,
|
||||||
|
))
|
||||||
|
}
|
||||||
@@ -0,0 +1,283 @@
|
|||||||
|
use crate::de::{deserialize_seq, has_flatten, Parameters, TupleForm};
|
||||||
|
#[cfg(feature = "deserialize_in_place")]
|
||||||
|
use crate::de::{deserialize_seq_in_place, place_lifetime};
|
||||||
|
use crate::fragment::{Fragment, Stmts};
|
||||||
|
use crate::internals::ast::Field;
|
||||||
|
use crate::internals::attr;
|
||||||
|
use crate::private;
|
||||||
|
use proc_macro2::TokenStream;
|
||||||
|
use quote::{quote, quote_spanned};
|
||||||
|
use syn::spanned::Spanned;
|
||||||
|
|
||||||
|
/// Generates `Deserialize::deserialize` body for a `struct Tuple(...);` including `struct Newtype(T);`
|
||||||
|
pub(super) fn deserialize(
|
||||||
|
params: &Parameters,
|
||||||
|
fields: &[Field],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
form: TupleForm,
|
||||||
|
) -> Fragment {
|
||||||
|
assert!(
|
||||||
|
!has_flatten(fields),
|
||||||
|
"tuples and tuple variants cannot have flatten fields"
|
||||||
|
);
|
||||||
|
|
||||||
|
let field_count = fields
|
||||||
|
.iter()
|
||||||
|
.filter(|field| !field.attrs.skip_deserializing())
|
||||||
|
.count();
|
||||||
|
|
||||||
|
let this_type = ¶ms.this_type;
|
||||||
|
let this_value = ¶ms.this_value;
|
||||||
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||||
|
params.generics_with_de_lifetime();
|
||||||
|
let delife = params.borrowed.de_lifetime();
|
||||||
|
|
||||||
|
// If there are getters (implying private fields), construct the local type
|
||||||
|
// and use an `Into` conversion to get the remote type. If there are no
|
||||||
|
// getters then construct the target type directly.
|
||||||
|
let construct = if params.has_getter {
|
||||||
|
let local = ¶ms.local;
|
||||||
|
quote!(#local)
|
||||||
|
} else {
|
||||||
|
quote!(#this_value)
|
||||||
|
};
|
||||||
|
|
||||||
|
let type_path = match form {
|
||||||
|
TupleForm::Tuple => construct,
|
||||||
|
TupleForm::ExternallyTagged(variant_ident) | TupleForm::Untagged(variant_ident) => {
|
||||||
|
quote!(#construct::#variant_ident)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let expecting = match form {
|
||||||
|
TupleForm::Tuple => format!("tuple struct {}", params.type_name()),
|
||||||
|
TupleForm::ExternallyTagged(variant_ident) | TupleForm::Untagged(variant_ident) => {
|
||||||
|
format!("tuple variant {}::{}", params.type_name(), variant_ident)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
||||||
|
|
||||||
|
let nfields = fields.len();
|
||||||
|
|
||||||
|
let visit_newtype_struct = match form {
|
||||||
|
TupleForm::Tuple if nfields == 1 => {
|
||||||
|
Some(deserialize_newtype_struct(&type_path, params, &fields[0]))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let visit_seq = Stmts(deserialize_seq(
|
||||||
|
&type_path, params, fields, false, cattrs, expecting,
|
||||||
|
));
|
||||||
|
|
||||||
|
let visitor_expr = quote! {
|
||||||
|
__Visitor {
|
||||||
|
marker: _serde::#private::PhantomData::<#this_type #ty_generics>,
|
||||||
|
lifetime: _serde::#private::PhantomData,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let dispatch = match form {
|
||||||
|
TupleForm::Tuple if nfields == 1 => {
|
||||||
|
let type_name = cattrs.name().deserialize_name();
|
||||||
|
quote! {
|
||||||
|
_serde::Deserializer::deserialize_newtype_struct(__deserializer, #type_name, #visitor_expr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TupleForm::Tuple => {
|
||||||
|
let type_name = cattrs.name().deserialize_name();
|
||||||
|
quote! {
|
||||||
|
_serde::Deserializer::deserialize_tuple_struct(__deserializer, #type_name, #field_count, #visitor_expr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TupleForm::ExternallyTagged(_) => quote! {
|
||||||
|
_serde::de::VariantAccess::tuple_variant(__variant, #field_count, #visitor_expr)
|
||||||
|
},
|
||||||
|
TupleForm::Untagged(_) => quote! {
|
||||||
|
_serde::Deserializer::deserialize_tuple(__deserializer, #field_count, #visitor_expr)
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let visitor_var = if field_count == 0 {
|
||||||
|
quote!(_)
|
||||||
|
} else {
|
||||||
|
quote!(mut __seq)
|
||||||
|
};
|
||||||
|
|
||||||
|
quote_block! {
|
||||||
|
#[doc(hidden)]
|
||||||
|
struct __Visitor #de_impl_generics #where_clause {
|
||||||
|
marker: _serde::#private::PhantomData<#this_type #ty_generics>,
|
||||||
|
lifetime: _serde::#private::PhantomData<&#delife ()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[automatically_derived]
|
||||||
|
impl #de_impl_generics _serde::de::Visitor<#delife> for __Visitor #de_ty_generics #where_clause {
|
||||||
|
type Value = #this_type #ty_generics;
|
||||||
|
|
||||||
|
fn expecting(&self, __formatter: &mut _serde::#private::Formatter) -> _serde::#private::fmt::Result {
|
||||||
|
_serde::#private::Formatter::write_str(__formatter, #expecting)
|
||||||
|
}
|
||||||
|
|
||||||
|
#visit_newtype_struct
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_seq<__A>(self, #visitor_var: __A) -> _serde::#private::Result<Self::Value, __A::Error>
|
||||||
|
where
|
||||||
|
__A: _serde::de::SeqAccess<#delife>,
|
||||||
|
{
|
||||||
|
#visit_seq
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#dispatch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn deserialize_newtype_struct(
|
||||||
|
type_path: &TokenStream,
|
||||||
|
params: &Parameters,
|
||||||
|
field: &Field,
|
||||||
|
) -> TokenStream {
|
||||||
|
let delife = params.borrowed.de_lifetime();
|
||||||
|
let field_ty = field.ty;
|
||||||
|
let deserializer_var = quote!(__e);
|
||||||
|
|
||||||
|
let value = match field.attrs.deserialize_with() {
|
||||||
|
None => {
|
||||||
|
let span = field.original.span();
|
||||||
|
let func = quote_spanned!(span=> <#field_ty as _serde::Deserialize>::deserialize);
|
||||||
|
quote! {
|
||||||
|
#func(#deserializer_var)?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(path) => {
|
||||||
|
// If #path returns wrong type, error will be reported here (^^^^^).
|
||||||
|
// We attach span of the path to the function so it will be reported
|
||||||
|
// on the #[serde(with = "...")]
|
||||||
|
// ^^^^^
|
||||||
|
quote_spanned! {path.span()=>
|
||||||
|
#path(#deserializer_var)?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut result = quote!(#type_path(__field0));
|
||||||
|
if params.has_getter {
|
||||||
|
let this_type = ¶ms.this_type;
|
||||||
|
let (_, ty_generics, _) = params.generics.split_for_impl();
|
||||||
|
result = quote! {
|
||||||
|
_serde::#private::Into::<#this_type #ty_generics>::into(#result)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
#[inline]
|
||||||
|
fn visit_newtype_struct<__E>(self, #deserializer_var: __E) -> _serde::#private::Result<Self::Value, __E::Error>
|
||||||
|
where
|
||||||
|
__E: _serde::Deserializer<#delife>,
|
||||||
|
{
|
||||||
|
let __field0: #field_ty = #value;
|
||||||
|
_serde::#private::Ok(#result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates `Deserialize::deserialize_in_place` body for a `struct Tuple(...);` including `struct Newtype(T);`
|
||||||
|
#[cfg(feature = "deserialize_in_place")]
|
||||||
|
pub(super) fn deserialize_in_place(
|
||||||
|
params: &Parameters,
|
||||||
|
fields: &[Field],
|
||||||
|
cattrs: &attr::Container,
|
||||||
|
) -> Fragment {
|
||||||
|
assert!(
|
||||||
|
!has_flatten(fields),
|
||||||
|
"tuples and tuple variants cannot have flatten fields"
|
||||||
|
);
|
||||||
|
|
||||||
|
let field_count = fields
|
||||||
|
.iter()
|
||||||
|
.filter(|field| !field.attrs.skip_deserializing())
|
||||||
|
.count();
|
||||||
|
|
||||||
|
let this_type = ¶ms.this_type;
|
||||||
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||||
|
params.generics_with_de_lifetime();
|
||||||
|
let delife = params.borrowed.de_lifetime();
|
||||||
|
|
||||||
|
let expecting = format!("tuple struct {}", params.type_name());
|
||||||
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
||||||
|
|
||||||
|
let nfields = fields.len();
|
||||||
|
|
||||||
|
let visit_newtype_struct = if nfields == 1 {
|
||||||
|
// We do not generate deserialize_in_place if every field has a
|
||||||
|
// deserialize_with.
|
||||||
|
assert!(fields[0].attrs.deserialize_with().is_none());
|
||||||
|
|
||||||
|
Some(quote! {
|
||||||
|
#[inline]
|
||||||
|
fn visit_newtype_struct<__E>(self, __e: __E) -> _serde::#private::Result<Self::Value, __E::Error>
|
||||||
|
where
|
||||||
|
__E: _serde::Deserializer<#delife>,
|
||||||
|
{
|
||||||
|
_serde::Deserialize::deserialize_in_place(__e, &mut self.place.0)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let visit_seq = Stmts(deserialize_seq_in_place(params, fields, cattrs, expecting));
|
||||||
|
|
||||||
|
let visitor_expr = quote! {
|
||||||
|
__Visitor {
|
||||||
|
place: __place,
|
||||||
|
lifetime: _serde::#private::PhantomData,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let type_name = cattrs.name().deserialize_name();
|
||||||
|
let dispatch = if nfields == 1 {
|
||||||
|
quote!(_serde::Deserializer::deserialize_newtype_struct(__deserializer, #type_name, #visitor_expr))
|
||||||
|
} else {
|
||||||
|
quote!(_serde::Deserializer::deserialize_tuple_struct(__deserializer, #type_name, #field_count, #visitor_expr))
|
||||||
|
};
|
||||||
|
|
||||||
|
let visitor_var = if field_count == 0 {
|
||||||
|
quote!(_)
|
||||||
|
} else {
|
||||||
|
quote!(mut __seq)
|
||||||
|
};
|
||||||
|
|
||||||
|
let in_place_impl_generics = de_impl_generics.in_place();
|
||||||
|
let in_place_ty_generics = de_ty_generics.in_place();
|
||||||
|
let place_life = place_lifetime();
|
||||||
|
|
||||||
|
quote_block! {
|
||||||
|
#[doc(hidden)]
|
||||||
|
struct __Visitor #in_place_impl_generics #where_clause {
|
||||||
|
place: &#place_life mut #this_type #ty_generics,
|
||||||
|
lifetime: _serde::#private::PhantomData<&#delife ()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[automatically_derived]
|
||||||
|
impl #in_place_impl_generics _serde::de::Visitor<#delife> for __Visitor #in_place_ty_generics #where_clause {
|
||||||
|
type Value = ();
|
||||||
|
|
||||||
|
fn expecting(&self, __formatter: &mut _serde::#private::Formatter) -> _serde::#private::fmt::Result {
|
||||||
|
_serde::#private::Formatter::write_str(__formatter, #expecting)
|
||||||
|
}
|
||||||
|
|
||||||
|
#visit_newtype_struct
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_seq<__A>(self, #visitor_var: __A) -> _serde::#private::Result<Self::Value, __A::Error>
|
||||||
|
where
|
||||||
|
__A: _serde::de::SeqAccess<#delife>,
|
||||||
|
{
|
||||||
|
#visit_seq
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#dispatch
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
use crate::de::Parameters;
|
||||||
|
use crate::fragment::Fragment;
|
||||||
|
use crate::internals::attr;
|
||||||
|
use crate::private;
|
||||||
|
use quote::quote;
|
||||||
|
|
||||||
|
/// Generates `Deserialize::deserialize` body for a `struct Unit;`
|
||||||
|
pub(super) fn deserialize(params: &Parameters, cattrs: &attr::Container) -> Fragment {
|
||||||
|
let this_type = ¶ms.this_type;
|
||||||
|
let this_value = ¶ms.this_value;
|
||||||
|
let type_name = cattrs.name().deserialize_name();
|
||||||
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||||
|
params.generics_with_de_lifetime();
|
||||||
|
let delife = params.borrowed.de_lifetime();
|
||||||
|
|
||||||
|
let expecting = format!("unit struct {}", params.type_name());
|
||||||
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
||||||
|
|
||||||
|
quote_block! {
|
||||||
|
#[doc(hidden)]
|
||||||
|
struct __Visitor #de_impl_generics #where_clause {
|
||||||
|
marker: _serde::#private::PhantomData<#this_type #ty_generics>,
|
||||||
|
lifetime: _serde::#private::PhantomData<&#delife ()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[automatically_derived]
|
||||||
|
impl #de_impl_generics _serde::de::Visitor<#delife> for __Visitor #de_ty_generics #where_clause {
|
||||||
|
type Value = #this_type #ty_generics;
|
||||||
|
|
||||||
|
fn expecting(&self, __formatter: &mut _serde::#private::Formatter) -> _serde::#private::fmt::Result {
|
||||||
|
_serde::#private::Formatter::write_str(__formatter, #expecting)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn visit_unit<__E>(self) -> _serde::#private::Result<Self::Value, __E>
|
||||||
|
where
|
||||||
|
__E: _serde::de::Error,
|
||||||
|
{
|
||||||
|
_serde::#private::Ok(#this_value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_serde::Deserializer::deserialize_unit_struct(
|
||||||
|
__deserializer,
|
||||||
|
#type_name,
|
||||||
|
__Visitor {
|
||||||
|
marker: _serde::#private::PhantomData::<#this_type #ty_generics>,
|
||||||
|
lifetime: _serde::#private::PhantomData,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,7 +13,7 @@
|
|||||||
//!
|
//!
|
||||||
//! [https://serde.rs/derive.html]: https://serde.rs/derive.html
|
//! [https://serde.rs/derive.html]: https://serde.rs/derive.html
|
||||||
|
|
||||||
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.226")]
|
#![doc(html_root_url = "https://docs.rs/serde_derive/1.0.228")]
|
||||||
#![cfg_attr(not(check_cfg), allow(unexpected_cfgs))]
|
#![cfg_attr(not(check_cfg), allow(unexpected_cfgs))]
|
||||||
// Ignored clippy lints
|
// Ignored clippy lints
|
||||||
#![allow(
|
#![allow(
|
||||||
|
|||||||
Reference in New Issue
Block a user