2018-05-20 19:34:52 -07:00
|
|
|
use proc_macro2::{Literal, Span, TokenStream};
|
|
|
|
use quote::ToTokens;
|
2018-01-08 21:49:09 -08:00
|
|
|
use syn::punctuated::Punctuated;
|
2018-01-10 20:59:48 -08:00
|
|
|
use syn::spanned::Spanned;
|
2018-04-12 22:58:24 -07:00
|
|
|
use syn::{self, Ident, Index, Member};
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2016-04-13 00:34:29 -07:00
|
|
|
use bound;
|
2018-12-27 15:20:32 -05:00
|
|
|
use dummy;
|
2017-12-23 20:13:08 -08:00
|
|
|
use fragment::{Expr, Fragment, Match, Stmts};
|
2018-04-12 22:58:24 -07:00
|
|
|
use internals::ast::{Container, Data, Field, Style, Variant};
|
2020-10-24 05:45:12 +09:00
|
|
|
use internals::{attr, replace_receiver, ungroup, Ctxt, Derive};
|
2018-05-02 13:29:44 -07:00
|
|
|
use pretend;
|
2022-11-27 16:08:47 -08:00
|
|
|
use this;
|
2016-06-19 20:15:49 -07:00
|
|
|
|
2017-04-02 21:42:07 -07:00
|
|
|
use std::collections::BTreeSet;
|
2020-10-27 19:18:27 -07:00
|
|
|
use std::ptr;
|
2017-04-02 21:42:07 -07:00
|
|
|
|
2023-05-25 08:09:24 -07:00
|
|
|
pub fn expand_derive_deserialize(input: &mut syn::DeriveInput) -> syn::Result<TokenStream> {
|
2020-10-24 05:45:12 +09:00
|
|
|
replace_receiver(input);
|
|
|
|
|
2018-05-07 11:22:20 -07:00
|
|
|
let ctxt = Ctxt::new();
|
2018-11-29 08:01:17 +02:00
|
|
|
let cont = match Container::from_ast(&ctxt, input, Derive::Deserialize) {
|
|
|
|
Some(cont) => cont,
|
|
|
|
None => return Err(ctxt.check().unwrap_err()),
|
|
|
|
};
|
2018-05-07 11:22:20 -07:00
|
|
|
precondition(&ctxt, &cont);
|
2019-08-16 11:28:25 -07:00
|
|
|
ctxt.check()?;
|
2016-05-20 22:03:20 -07:00
|
|
|
|
2018-05-20 19:34:52 -07:00
|
|
|
let ident = &cont.ident;
|
2017-04-14 15:52:56 -07:00
|
|
|
let params = Parameters::new(&cont);
|
2017-06-29 20:12:44 -07:00
|
|
|
let (de_impl_generics, _, ty_generics, where_clause) = split_with_de_lifetime(¶ms);
|
2017-12-10 23:19:25 -08:00
|
|
|
let body = Stmts(deserialize_body(&cont, ¶ms));
|
2017-09-09 11:34:08 -07:00
|
|
|
let delife = params.borrowed.de_lifetime();
|
2019-04-03 09:16:17 -07:00
|
|
|
let serde = cont.attrs.serde_path();
|
2016-04-24 10:59:57 -07:00
|
|
|
|
2017-04-14 15:52:56 -07:00
|
|
|
let impl_block = if let Some(remote) = cont.attrs.remote() {
|
2017-07-25 23:52:06 -07:00
|
|
|
let vis = &input.vis;
|
2021-08-23 10:17:44 -07:00
|
|
|
let used = pretend::pretend_used(&cont, params.is_packed);
|
2017-04-08 22:42:42 -07:00
|
|
|
quote! {
|
2017-06-29 20:12:44 -07:00
|
|
|
impl #de_impl_generics #ident #ty_generics #where_clause {
|
2021-01-08 19:35:52 -08:00
|
|
|
#vis fn deserialize<__D>(__deserializer: __D) -> #serde::__private::Result<#remote #ty_generics, __D::Error>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
2019-04-03 09:16:17 -07:00
|
|
|
__D: #serde::Deserializer<#delife>,
|
2017-04-08 22:42:42 -07:00
|
|
|
{
|
2018-05-02 13:29:44 -07:00
|
|
|
#used
|
2017-12-10 23:19:25 -08:00
|
|
|
#body
|
2017-04-08 22:42:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-17 10:46:44 -08:00
|
|
|
let fn_deserialize_in_place = deserialize_in_place_body(&cont, ¶ms);
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2017-04-08 22:42:42 -07:00
|
|
|
quote! {
|
2016-04-24 10:59:57 -07:00
|
|
|
#[automatically_derived]
|
2019-04-03 09:16:17 -07:00
|
|
|
impl #de_impl_generics #serde::Deserialize<#delife> for #ident #ty_generics #where_clause {
|
2021-01-08 19:35:52 -08:00
|
|
|
fn deserialize<__D>(__deserializer: __D) -> #serde::__private::Result<Self, __D::Error>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
2019-04-03 09:16:17 -07:00
|
|
|
__D: #serde::Deserializer<#delife>,
|
2017-02-20 14:43:51 -08:00
|
|
|
{
|
2017-12-10 23:19:25 -08:00
|
|
|
#body
|
2017-02-20 14:43:51 -08:00
|
|
|
}
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
#fn_deserialize_in_place
|
2015-03-15 17:47:25 -07:00
|
|
|
}
|
2017-04-08 22:42:42 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-03 09:34:53 -07:00
|
|
|
Ok(dummy::wrap_in_const(
|
|
|
|
cont.attrs.custom_serde_path(),
|
|
|
|
impl_block,
|
|
|
|
))
|
2015-03-15 17:47:25 -07:00
|
|
|
}
|
|
|
|
|
2018-05-07 11:22:20 -07:00
|
|
|
fn precondition(cx: &Ctxt, cont: &Container) {
|
2018-05-07 21:15:21 -07:00
|
|
|
precondition_sized(cx, cont);
|
|
|
|
precondition_no_de_lifetime(cx, cont);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn precondition_sized(cx: &Ctxt, cont: &Container) {
|
2019-09-07 23:01:16 -07:00
|
|
|
if let Data::Struct(_, fields) = &cont.data {
|
2018-05-07 11:22:20 -07:00
|
|
|
if let Some(last) = fields.last() {
|
2020-05-29 17:58:34 -07:00
|
|
|
if let syn::Type::Slice(_) = ungroup(last.ty) {
|
2018-12-10 22:09:02 -08:00
|
|
|
cx.error_spanned_by(
|
|
|
|
cont.original,
|
|
|
|
"cannot deserialize a dynamically sized struct",
|
|
|
|
);
|
2018-05-07 11:22:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-07 21:15:21 -07:00
|
|
|
fn precondition_no_de_lifetime(cx: &Ctxt, cont: &Container) {
|
|
|
|
if let BorrowedLifetimes::Borrowed(_) = borrowed_lifetimes(cont) {
|
|
|
|
for param in cont.generics.lifetimes() {
|
|
|
|
if param.lifetime.to_string() == "'de" {
|
2018-11-29 08:01:17 +02:00
|
|
|
cx.error_spanned_by(
|
|
|
|
¶m.lifetime,
|
|
|
|
"cannot deserialize when there is a lifetime parameter called 'de",
|
|
|
|
);
|
2018-05-07 21:15:21 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-02 21:42:07 -07:00
|
|
|
struct Parameters {
|
2017-04-08 22:42:42 -07:00
|
|
|
/// Name of the type the `derive` is on.
|
|
|
|
local: syn::Ident,
|
|
|
|
|
|
|
|
/// Path to the type the impl is for. Either a single `Ident` for local
|
2022-11-27 16:08:47 -08:00
|
|
|
/// types (does not include generic parameters) or `some::remote::Path` for
|
|
|
|
/// remote types.
|
|
|
|
this_type: syn::Path,
|
|
|
|
|
|
|
|
/// Same as `this_type` but using `::<T>` for generic parameters for use in
|
|
|
|
/// expression position.
|
|
|
|
this_value: syn::Path,
|
2017-04-08 22:42:42 -07:00
|
|
|
|
|
|
|
/// Generics including any explicit and inferred bounds for the impl.
|
2017-04-02 21:42:07 -07:00
|
|
|
generics: syn::Generics,
|
2017-04-08 22:42:42 -07:00
|
|
|
|
|
|
|
/// Lifetimes borrowed from the deserializer. These will become bounds on
|
|
|
|
/// the `'de` lifetime of the deserializer.
|
2017-09-09 11:34:08 -07:00
|
|
|
borrowed: BorrowedLifetimes,
|
2017-04-08 22:42:42 -07:00
|
|
|
|
|
|
|
/// At least one field has a serde(getter) attribute, implying that the
|
|
|
|
/// remote type has a private field.
|
|
|
|
has_getter: bool,
|
2021-08-23 10:17:44 -07:00
|
|
|
|
|
|
|
/// Type has a repr(packed) attribute.
|
|
|
|
is_packed: bool,
|
2017-04-08 22:42:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Parameters {
|
2017-04-14 15:52:56 -07:00
|
|
|
fn new(cont: &Container) -> Self {
|
2018-05-20 19:34:52 -07:00
|
|
|
let local = cont.ident.clone();
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_type = this::this_type(cont);
|
|
|
|
let this_value = this::this_value(cont);
|
2017-04-14 15:52:56 -07:00
|
|
|
let borrowed = borrowed_lifetimes(cont);
|
2017-09-09 11:34:08 -07:00
|
|
|
let generics = build_generics(cont, &borrowed);
|
2018-01-08 21:49:09 -08:00
|
|
|
let has_getter = cont.data.has_getter();
|
2021-08-23 10:17:44 -07:00
|
|
|
let is_packed = cont.attrs.is_packed();
|
2017-04-08 22:42:42 -07:00
|
|
|
|
|
|
|
Parameters {
|
2020-04-05 21:02:37 -07:00
|
|
|
local,
|
2022-11-27 16:08:47 -08:00
|
|
|
this_type,
|
|
|
|
this_value,
|
2020-04-05 21:02:37 -07:00
|
|
|
generics,
|
|
|
|
borrowed,
|
|
|
|
has_getter,
|
2021-08-23 10:17:44 -07:00
|
|
|
is_packed,
|
2017-04-08 22:42:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Type name to use in error messages and `&'static str` arguments to
|
|
|
|
/// various Deserializer methods.
|
2018-05-20 19:34:52 -07:00
|
|
|
fn type_name(&self) -> String {
|
2022-11-27 16:08:47 -08:00
|
|
|
self.this_type.segments.last().unwrap().ident.to_string()
|
2017-04-08 22:42:42 -07:00
|
|
|
}
|
2017-04-02 21:42:07 -07:00
|
|
|
}
|
|
|
|
|
2016-04-13 00:34:29 -07:00
|
|
|
// All the generics in the input, plus a bound `T: Deserialize` for each generic
|
|
|
|
// field type that will be deserialized by us, plus a bound `T: Default` for
|
|
|
|
// each generic field type that will be set to a default value.
|
2017-09-09 11:34:08 -07:00
|
|
|
fn build_generics(cont: &Container, borrowed: &BorrowedLifetimes) -> syn::Generics {
|
2017-04-14 15:52:56 -07:00
|
|
|
let generics = bound::without_defaults(cont.generics);
|
2016-05-20 22:03:20 -07:00
|
|
|
|
2018-04-12 22:39:26 -07:00
|
|
|
let generics = bound::with_where_predicates_from_fields(cont, &generics, attr::Field::de_bound);
|
2016-05-20 22:03:20 -07:00
|
|
|
|
2018-05-08 10:07:17 -07:00
|
|
|
let generics =
|
|
|
|
bound::with_where_predicates_from_variants(cont, &generics, attr::Variant::de_bound);
|
|
|
|
|
2017-04-14 15:52:56 -07:00
|
|
|
match cont.attrs.de_bound() {
|
2017-02-12 21:59:04 -08:00
|
|
|
Some(predicates) => bound::with_where_predicates(&generics, predicates),
|
2016-05-20 22:03:20 -07:00
|
|
|
None => {
|
2017-04-14 15:52:56 -07:00
|
|
|
let generics = match *cont.attrs.default() {
|
2021-01-08 19:35:52 -08:00
|
|
|
attr::Default::Default => bound::with_self_bound(
|
|
|
|
cont,
|
|
|
|
&generics,
|
|
|
|
&parse_quote!(_serde::__private::Default),
|
|
|
|
),
|
2017-12-23 20:13:08 -08:00
|
|
|
attr::Default::None | attr::Default::Path(_) => generics,
|
2017-02-20 17:03:38 -08:00
|
|
|
};
|
|
|
|
|
2018-04-12 22:39:26 -07:00
|
|
|
let delife = borrowed.de_lifetime();
|
2017-04-13 12:28:23 -07:00
|
|
|
let generics = bound::with_bound(
|
2017-04-14 15:52:56 -07:00
|
|
|
cont,
|
2017-04-13 12:28:23 -07:00
|
|
|
&generics,
|
|
|
|
needs_deserialize_bound,
|
2018-04-12 22:39:26 -07:00
|
|
|
&parse_quote!(_serde::Deserialize<#delife>),
|
2017-04-13 12:28:23 -07:00
|
|
|
);
|
2017-02-20 17:03:38 -08:00
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
bound::with_bound(
|
2017-04-14 15:52:56 -07:00
|
|
|
cont,
|
2017-04-13 12:28:23 -07:00
|
|
|
&generics,
|
|
|
|
requires_default,
|
2021-01-08 19:35:52 -08:00
|
|
|
&parse_quote!(_serde::__private::Default),
|
2017-04-13 12:28:23 -07:00
|
|
|
)
|
2016-05-20 22:03:20 -07:00
|
|
|
}
|
|
|
|
}
|
2016-04-13 00:34:29 -07:00
|
|
|
}
|
|
|
|
|
2018-05-07 21:30:00 -07:00
|
|
|
// Fields with a `skip_deserializing` or `deserialize_with` attribute, or which
|
|
|
|
// belong to a variant with a `skip_deserializing` or `deserialize_with`
|
|
|
|
// attribute, are not deserialized by us so we do not generate a bound. Fields
|
|
|
|
// with a `bound` attribute specify their own bound so we do not generate one.
|
|
|
|
// All other fields may need a `T: Deserialize` bound where T is the type of the
|
|
|
|
// field.
|
2017-08-07 17:22:26 -07:00
|
|
|
fn needs_deserialize_bound(field: &attr::Field, variant: Option<&attr::Variant>) -> bool {
|
2018-08-06 22:40:28 -07:00
|
|
|
!field.skip_deserializing()
|
|
|
|
&& field.deserialize_with().is_none()
|
|
|
|
&& field.de_bound().is_none()
|
2018-05-07 21:30:00 -07:00
|
|
|
&& variant.map_or(true, |variant| {
|
2018-05-19 17:33:30 -07:00
|
|
|
!variant.skip_deserializing()
|
|
|
|
&& variant.deserialize_with().is_none()
|
2018-05-08 10:07:17 -07:00
|
|
|
&& variant.de_bound().is_none()
|
2018-05-07 21:30:00 -07:00
|
|
|
})
|
2016-04-13 00:34:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fields with a `default` attribute (not `default=...`), and fields with a
|
|
|
|
// `skip_deserializing` attribute that do not also have `default=...`.
|
2017-08-07 17:22:26 -07:00
|
|
|
fn requires_default(field: &attr::Field, _variant: Option<&attr::Variant>) -> bool {
|
2018-01-08 21:49:09 -08:00
|
|
|
if let attr::Default::Default = *field.default() {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2016-04-13 00:34:29 -07:00
|
|
|
}
|
|
|
|
|
2017-09-09 11:34:08 -07:00
|
|
|
enum BorrowedLifetimes {
|
|
|
|
Borrowed(BTreeSet<syn::Lifetime>),
|
|
|
|
Static,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BorrowedLifetimes {
|
|
|
|
fn de_lifetime(&self) -> syn::Lifetime {
|
|
|
|
match *self {
|
2018-03-31 23:46:25 +02:00
|
|
|
BorrowedLifetimes::Borrowed(_) => syn::Lifetime::new("'de", Span::call_site()),
|
|
|
|
BorrowedLifetimes::Static => syn::Lifetime::new("'static", Span::call_site()),
|
2017-09-09 11:34:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-15 23:44:08 -07:00
|
|
|
fn de_lifetime_param(&self) -> Option<syn::LifetimeParam> {
|
2019-09-07 23:01:16 -07:00
|
|
|
match self {
|
2023-03-15 23:44:08 -07:00
|
|
|
BorrowedLifetimes::Borrowed(bounds) => Some(syn::LifetimeParam {
|
2017-12-23 20:13:08 -08:00
|
|
|
attrs: Vec::new(),
|
2018-03-31 23:46:25 +02:00
|
|
|
lifetime: syn::Lifetime::new("'de", Span::call_site()),
|
2018-01-08 21:49:09 -08:00
|
|
|
colon_token: None,
|
2017-12-23 20:13:08 -08:00
|
|
|
bounds: bounds.iter().cloned().collect(),
|
|
|
|
}),
|
2017-09-09 11:34:08 -07:00
|
|
|
BorrowedLifetimes::Static => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 15:52:56 -07:00
|
|
|
// The union of lifetimes borrowed by each field of the container.
|
2017-04-02 21:42:07 -07:00
|
|
|
//
|
|
|
|
// These turn into bounds on the `'de` lifetime of the Deserialize impl. If
|
|
|
|
// lifetimes `'a` and `'b` are borrowed but `'c` is not, the impl is:
|
|
|
|
//
|
|
|
|
// impl<'de: 'a + 'b, 'a, 'b, 'c> Deserialize<'de> for S<'a, 'b, 'c>
|
2017-09-09 11:34:08 -07:00
|
|
|
//
|
|
|
|
// If any borrowed lifetime is `'static`, then `'de: 'static` would be redundant
|
|
|
|
// and we use plain `'static` instead of `'de`.
|
|
|
|
fn borrowed_lifetimes(cont: &Container) -> BorrowedLifetimes {
|
2017-04-02 21:42:07 -07:00
|
|
|
let mut lifetimes = BTreeSet::new();
|
2018-01-08 21:49:09 -08:00
|
|
|
for field in cont.data.all_fields() {
|
2017-11-03 10:08:02 -07:00
|
|
|
if !field.attrs.skip_deserializing() {
|
|
|
|
lifetimes.extend(field.attrs.borrowed_lifetimes().iter().cloned());
|
|
|
|
}
|
2017-04-02 21:42:07 -07:00
|
|
|
}
|
2018-01-08 21:49:09 -08:00
|
|
|
if lifetimes.iter().any(|b| b.to_string() == "'static") {
|
2017-09-09 11:34:08 -07:00
|
|
|
BorrowedLifetimes::Static
|
|
|
|
} else {
|
|
|
|
BorrowedLifetimes::Borrowed(lifetimes)
|
|
|
|
}
|
2017-04-02 21:42:07 -07:00
|
|
|
}
|
|
|
|
|
2017-04-14 15:52:56 -07:00
|
|
|
fn deserialize_body(cont: &Container, params: &Parameters) -> Fragment {
|
2018-05-20 13:36:17 -07:00
|
|
|
if cont.attrs.transparent() {
|
|
|
|
deserialize_transparent(cont, params)
|
|
|
|
} else if let Some(type_from) = cont.attrs.type_from() {
|
2018-01-09 19:21:01 -08:00
|
|
|
deserialize_from(type_from)
|
2019-05-11 23:26:42 -07:00
|
|
|
} else if let Some(type_try_from) = cont.attrs.type_try_from() {
|
|
|
|
deserialize_try_from(type_try_from)
|
2017-04-14 16:09:00 -07:00
|
|
|
} else if let attr::Identifier::No = cont.attrs.identifier() {
|
2019-09-07 23:01:16 -07:00
|
|
|
match &cont.data {
|
|
|
|
Data::Enum(variants) => deserialize_enum(params, variants, &cont.attrs),
|
|
|
|
Data::Struct(Style::Struct, fields) => {
|
2023-05-07 01:12:49 +05:00
|
|
|
deserialize_struct(params, fields, &cont.attrs, StructForm::Struct)
|
2017-03-18 12:22:27 -04:00
|
|
|
}
|
2019-09-07 23:01:16 -07:00
|
|
|
Data::Struct(Style::Tuple, fields) | Data::Struct(Style::Newtype, fields) => {
|
2023-05-07 01:28:47 +05:00
|
|
|
deserialize_tuple(params, fields, &cont.attrs, TupleForm::Tuple)
|
2015-10-17 19:44:07 -07:00
|
|
|
}
|
2018-01-08 21:49:09 -08:00
|
|
|
Data::Struct(Style::Unit, _) => deserialize_unit_struct(params, &cont.attrs),
|
2017-03-18 12:22:27 -04:00
|
|
|
}
|
2017-04-14 16:09:00 -07:00
|
|
|
} else {
|
2019-09-07 23:01:16 -07:00
|
|
|
match &cont.data {
|
|
|
|
Data::Enum(variants) => deserialize_custom_identifier(params, variants, &cont.attrs),
|
2018-01-08 21:49:09 -08:00
|
|
|
Data::Struct(_, _) => unreachable!("checked in serde_derive_internals"),
|
2017-04-14 16:09:00 -07:00
|
|
|
}
|
2017-03-18 12:22:27 -04:00
|
|
|
}
|
|
|
|
}
|
2015-10-17 19:44:07 -07:00
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
#[cfg(feature = "deserialize_in_place")]
|
|
|
|
fn deserialize_in_place_body(cont: &Container, params: &Parameters) -> Option<Stmts> {
|
|
|
|
// Only remote derives have getters, and we do not generate
|
|
|
|
// deserialize_in_place for remote derives.
|
2017-12-10 22:46:46 -08:00
|
|
|
assert!(!params.has_getter);
|
|
|
|
|
2018-05-20 13:36:17 -07:00
|
|
|
if cont.attrs.transparent()
|
|
|
|
|| cont.attrs.type_from().is_some()
|
2019-05-11 23:26:42 -07:00
|
|
|
|| cont.attrs.type_try_from().is_some()
|
2018-05-20 13:36:17 -07:00
|
|
|
|| cont.attrs.identifier().is_some()
|
2018-05-19 17:33:30 -07:00
|
|
|
|| cont
|
|
|
|
.data
|
2017-12-23 20:13:08 -08:00
|
|
|
.all_fields()
|
|
|
|
.all(|f| f.attrs.deserialize_with().is_some())
|
2017-12-10 23:04:44 -08:00
|
|
|
{
|
2017-12-10 22:55:28 -08:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-09-07 23:01:16 -07:00
|
|
|
let code = match &cont.data {
|
|
|
|
Data::Struct(Style::Struct, fields) => {
|
2020-03-05 19:48:07 -08:00
|
|
|
deserialize_struct_in_place(None, params, fields, &cont.attrs, None)?
|
2017-12-10 23:04:44 -08:00
|
|
|
}
|
2019-09-07 23:01:16 -07:00
|
|
|
Data::Struct(Style::Tuple, fields) | Data::Struct(Style::Newtype, fields) => {
|
2017-12-17 10:46:44 -08:00
|
|
|
deserialize_tuple_in_place(None, params, fields, &cont.attrs, None)
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
2018-01-08 21:49:09 -08:00
|
|
|
Data::Enum(_) | Data::Struct(Style::Unit, _) => {
|
2017-12-10 23:12:54 -08:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let delife = params.borrowed.de_lifetime();
|
|
|
|
let stmts = Stmts(code);
|
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
let fn_deserialize_in_place = quote_block! {
|
2021-01-08 19:35:52 -08:00
|
|
|
fn deserialize_in_place<__D>(__deserializer: __D, __place: &mut Self) -> _serde::__private::Result<(), __D::Error>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__D: _serde::Deserializer<#delife>,
|
2017-12-10 23:12:54 -08:00
|
|
|
{
|
|
|
|
#stmts
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
Some(Stmts(fn_deserialize_in_place))
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
#[cfg(not(feature = "deserialize_in_place"))]
|
|
|
|
fn deserialize_in_place_body(_cont: &Container, _params: &Parameters) -> Option<Stmts> {
|
2017-11-13 15:35:14 -05:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-05-20 13:36:17 -07:00
|
|
|
fn deserialize_transparent(cont: &Container, params: &Parameters) -> Fragment {
|
2019-09-07 23:01:16 -07:00
|
|
|
let fields = match &cont.data {
|
|
|
|
Data::Struct(_, fields) => fields,
|
2018-05-20 13:36:17 -07:00
|
|
|
Data::Enum(_) => unreachable!(),
|
|
|
|
};
|
|
|
|
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_value = ¶ms.this_value;
|
2018-05-20 15:09:51 -07:00
|
|
|
let transparent_field = fields.iter().find(|f| f.attrs.transparent()).unwrap();
|
2018-05-20 13:36:17 -07:00
|
|
|
|
|
|
|
let path = match transparent_field.attrs.deserialize_with() {
|
|
|
|
Some(path) => quote!(#path),
|
2018-11-17 18:13:36 +02:00
|
|
|
None => {
|
|
|
|
let span = transparent_field.original.span();
|
|
|
|
quote_spanned!(span=> _serde::Deserialize::deserialize)
|
2018-12-10 22:09:02 -08:00
|
|
|
}
|
2018-05-20 13:36:17 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
let assign = fields.iter().map(|field| {
|
|
|
|
let member = &field.member;
|
2020-10-27 19:18:27 -07:00
|
|
|
if ptr::eq(field, transparent_field) {
|
2018-05-20 13:36:17 -07:00
|
|
|
quote!(#member: __transparent)
|
|
|
|
} else {
|
2019-09-07 23:01:16 -07:00
|
|
|
let value = match field.attrs.default() {
|
2021-01-08 19:35:52 -08:00
|
|
|
attr::Default::Default => quote!(_serde::__private::Default::default()),
|
2019-09-07 23:01:16 -07:00
|
|
|
attr::Default::Path(path) => quote!(#path()),
|
2021-01-08 19:35:52 -08:00
|
|
|
attr::Default::None => quote!(_serde::__private::PhantomData),
|
2018-05-20 13:36:17 -07:00
|
|
|
};
|
|
|
|
quote!(#member: #value)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
quote_block! {
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Result::map(
|
2018-05-20 13:36:17 -07:00
|
|
|
#path(__deserializer),
|
2022-11-27 16:08:47 -08:00
|
|
|
|__transparent| #this_value { #(#assign),* })
|
2018-05-20 13:36:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 19:21:01 -08:00
|
|
|
fn deserialize_from(type_from: &syn::Type) -> Fragment {
|
2017-03-18 12:22:27 -04:00
|
|
|
quote_block! {
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Result::map(
|
2018-01-09 19:21:01 -08:00
|
|
|
<#type_from as _serde::Deserialize>::deserialize(__deserializer),
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::From::from)
|
2015-03-15 17:47:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-11 23:26:42 -07:00
|
|
|
fn deserialize_try_from(type_try_from: &syn::Type) -> Fragment {
|
|
|
|
quote_block! {
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Result::and_then(
|
2019-05-11 23:26:42 -07:00
|
|
|
<#type_try_from as _serde::Deserialize>::deserialize(__deserializer),
|
2021-01-08 19:35:52 -08:00
|
|
|
|v| _serde::__private::TryFrom::try_from(v).map_err(_serde::de::Error::custom))
|
2019-05-11 23:26:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 15:52:56 -07:00
|
|
|
fn deserialize_unit_struct(params: &Parameters, cattrs: &attr::Container) -> Fragment {
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_type = ¶ms.this_type;
|
|
|
|
let this_value = ¶ms.this_value;
|
2017-04-14 15:52:56 -07:00
|
|
|
let type_name = cattrs.name().deserialize_name();
|
2023-07-10 01:31:40 +02:00
|
|
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
|
|
|
split_with_de_lifetime(params);
|
|
|
|
let delife = params.borrowed.de_lifetime();
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2017-04-08 22:42:42 -07:00
|
|
|
let expecting = format!("unit struct {}", params.type_name());
|
2020-10-23 00:54:13 +05:00
|
|
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
2017-01-11 02:39:16 -08:00
|
|
|
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_block! {
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2023-07-10 01:31:40 +02:00
|
|
|
struct __Visitor #de_impl_generics #where_clause {
|
|
|
|
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
|
|
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
|
|
|
}
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2023-07-10 01:31:40 +02:00
|
|
|
impl #de_impl_generics _serde::de::Visitor<#delife> for __Visitor #de_ty_generics #where_clause {
|
|
|
|
type Value = #this_type #ty_generics;
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn expecting(&self, __formatter: &mut _serde::__private::Formatter) -> _serde::__private::fmt::Result {
|
|
|
|
_serde::__private::Formatter::write_str(__formatter, #expecting)
|
2017-01-11 02:39:16 -08:00
|
|
|
}
|
|
|
|
|
2015-03-14 13:09:37 -07:00
|
|
|
#[inline]
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_unit<__E>(self) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2015-03-14 13:09:37 -07:00
|
|
|
{
|
2022-11-27 16:08:47 -08:00
|
|
|
_serde::__private::Ok(#this_value)
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-09 18:03:58 -07:00
|
|
|
_serde::Deserializer::deserialize_unit_struct(
|
|
|
|
__deserializer,
|
|
|
|
#type_name,
|
|
|
|
__Visitor {
|
|
|
|
marker: _serde::__private::PhantomData::<#this_type #ty_generics>,
|
|
|
|
lifetime: _serde::__private::PhantomData,
|
|
|
|
},
|
|
|
|
)
|
2017-02-20 14:43:51 -08:00
|
|
|
}
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
|
2023-05-07 01:28:47 +05:00
|
|
|
enum TupleForm<'a> {
|
|
|
|
Tuple,
|
|
|
|
/// Contains a variant name
|
|
|
|
ExternallyTagged(&'a syn::Ident),
|
|
|
|
/// Contains a variant name and an intermediate deserializer from which actual
|
|
|
|
/// deserialization will be performed
|
|
|
|
Untagged(&'a syn::Ident, TokenStream),
|
|
|
|
}
|
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
fn deserialize_tuple(
|
|
|
|
params: &Parameters,
|
|
|
|
fields: &[Field],
|
2017-04-14 15:52:56 -07:00
|
|
|
cattrs: &attr::Container,
|
2023-05-07 01:28:47 +05:00
|
|
|
form: TupleForm,
|
2017-04-13 12:28:23 -07:00
|
|
|
) -> Fragment {
|
2023-05-27 17:17:00 +05:00
|
|
|
assert!(!cattrs.has_flatten());
|
|
|
|
|
|
|
|
let field_count = fields
|
|
|
|
.iter()
|
|
|
|
.filter(|field| !field.attrs.skip_deserializing())
|
|
|
|
.count();
|
|
|
|
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_type = ¶ms.this_type;
|
|
|
|
let this_value = ¶ms.this_value;
|
2017-12-23 20:13:08 -08:00
|
|
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
|
|
|
split_with_de_lifetime(params);
|
2017-09-09 11:34:08 -07:00
|
|
|
let delife = params.borrowed.de_lifetime();
|
2015-07-30 09:38:09 -07:00
|
|
|
|
2017-04-08 22:42:42 -07:00
|
|
|
// 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 {
|
2018-05-20 19:34:52 -07:00
|
|
|
let local = ¶ms.local;
|
2017-04-08 22:42:42 -07:00
|
|
|
quote!(#local)
|
|
|
|
} else {
|
2022-11-27 16:08:47 -08:00
|
|
|
quote!(#this_value)
|
2017-04-08 22:42:42 -07:00
|
|
|
};
|
|
|
|
|
2023-05-07 01:28:47 +05:00
|
|
|
let type_path = match form {
|
|
|
|
TupleForm::Tuple => construct,
|
|
|
|
TupleForm::ExternallyTagged(variant_ident) | TupleForm::Untagged(variant_ident, _) => {
|
|
|
|
quote!(#construct::#variant_ident)
|
|
|
|
}
|
2016-05-15 13:32:06 -07:00
|
|
|
};
|
2023-05-07 01:28:47 +05:00
|
|
|
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)
|
|
|
|
}
|
2017-01-11 02:39:16 -08:00
|
|
|
};
|
2020-10-23 00:54:13 +05:00
|
|
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
2015-07-30 09:38:09 -07:00
|
|
|
|
2016-05-15 15:54:20 -07:00
|
|
|
let nfields = fields.len();
|
|
|
|
|
2023-05-07 01:28:47 +05:00
|
|
|
let visit_newtype_struct = match form {
|
|
|
|
TupleForm::Tuple if nfields == 1 => {
|
|
|
|
Some(deserialize_newtype_struct(&type_path, params, &fields[0]))
|
|
|
|
}
|
|
|
|
_ => None,
|
2016-05-15 13:32:06 -07:00
|
|
|
};
|
2015-03-15 17:47:25 -07:00
|
|
|
|
2018-05-08 12:03:35 -07:00
|
|
|
let visit_seq = Stmts(deserialize_seq(
|
Resolve needless_borrow clippy lints
error: this expression borrows a reference (`&syn::Type`) that is immediately dereferenced by the compiler
--> serde_derive/src/internals/check.rs:399:37
|
399 | if let Type::Path(ty) = ungroup(&field.ty) {
| ^^^^^^^^^ help: change this to: `field.ty`
|
note: the lint level is defined here
--> serde_derive/src/lib.rs:18:9
|
18 | #![deny(clippy::all, clippy::pedantic)]
| ^^^^^^^^^^^
= note: `#[deny(clippy::needless_borrow)]` implied by `#[deny(clippy::all)]`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:478:52
|
478 | &type_path, params, fields, false, cattrs, &expecting,
| ^^^^^^^^^^ help: change this to: `expecting`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:564:76
|
564 | let visit_seq = Stmts(deserialize_seq_in_place(params, fields, cattrs, &expecting));
| ^^^^^^^^^^ help: change this to: `expecting`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:925:51
|
925 | &type_path, params, fields, true, cattrs, &expecting,
| ^^^^^^^^^^ help: change this to: `expecting`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:1066:76
|
1066 | let visit_seq = Stmts(deserialize_seq_in_place(params, fields, cattrs, &expecting));
| ^^^^^^^^^^ help: change this to: `expecting`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&proc_macro2::TokenStream`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:2288:80
|
2288 | let fallthrough_borrowed_arm = fallthrough_borrowed.as_ref().unwrap_or(&fallthrough_arm);
| ^^^^^^^^^^^^^^^^ help: change this to: `fallthrough_arm`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&syn::Member`) that is immediately dereferenced by the compiler
--> serde_derive/src/ser.rs:1102:43
|
1102 | get_member(params, field, &member)
| ^^^^^^^ help: change this to: `member`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
2021-06-04 20:56:35 -07:00
|
|
|
&type_path, params, fields, false, cattrs, expecting,
|
2018-05-08 12:03:35 -07:00
|
|
|
));
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2017-02-20 11:50:24 -08:00
|
|
|
let visitor_expr = quote! {
|
2017-03-26 22:56:58 -07:00
|
|
|
__Visitor {
|
2022-11-27 16:08:47 -08:00
|
|
|
marker: _serde::__private::PhantomData::<#this_type #ty_generics>,
|
2021-01-08 19:35:52 -08:00
|
|
|
lifetime: _serde::__private::PhantomData,
|
2017-03-26 22:56:58 -07:00
|
|
|
}
|
2017-02-20 11:50:24 -08:00
|
|
|
};
|
2023-05-07 01:28:47 +05:00
|
|
|
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(_, deserializer) => quote! {
|
|
|
|
_serde::Deserializer::deserialize_tuple(#deserializer, #field_count, #visitor_expr)
|
|
|
|
},
|
2016-05-15 13:32:06 -07:00
|
|
|
};
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2023-05-27 17:17:00 +05:00
|
|
|
let visitor_var = if field_count == 0 {
|
2017-01-13 00:06:50 -08:00
|
|
|
quote!(_)
|
|
|
|
} else {
|
2017-04-14 11:58:29 -07:00
|
|
|
quote!(mut __seq)
|
2017-01-13 00:06:50 -08:00
|
|
|
};
|
|
|
|
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_block! {
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2017-03-26 22:56:58 -07:00
|
|
|
struct __Visitor #de_impl_generics #where_clause {
|
2022-11-27 16:08:47 -08:00
|
|
|
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
2021-01-08 19:35:52 -08:00
|
|
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
2017-02-20 11:50:24 -08:00
|
|
|
}
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2017-09-09 11:34:08 -07:00
|
|
|
impl #de_impl_generics _serde::de::Visitor<#delife> for __Visitor #de_ty_generics #where_clause {
|
2022-11-27 16:08:47 -08:00
|
|
|
type Value = #this_type #ty_generics;
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn expecting(&self, __formatter: &mut _serde::__private::Formatter) -> _serde::__private::fmt::Result {
|
|
|
|
_serde::__private::Formatter::write_str(__formatter, #expecting)
|
2017-01-11 02:39:16 -08:00
|
|
|
}
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
#visit_newtype_struct
|
2016-05-15 13:32:06 -07:00
|
|
|
|
2015-07-30 09:31:16 -07:00
|
|
|
#[inline]
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_seq<__A>(self, #visitor_var: __A) -> _serde::__private::Result<Self::Value, __A::Error>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__A: _serde::de::SeqAccess<#delife>,
|
2016-09-12 00:04:21 -07:00
|
|
|
{
|
|
|
|
#visit_seq
|
|
|
|
}
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
#dispatch
|
2017-02-20 14:43:51 -08:00
|
|
|
}
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
#[cfg(feature = "deserialize_in_place")]
|
|
|
|
fn deserialize_tuple_in_place(
|
2018-05-05 00:45:30 -07:00
|
|
|
variant_ident: Option<syn::Ident>,
|
2017-11-13 15:35:14 -05:00
|
|
|
params: &Parameters,
|
|
|
|
fields: &[Field],
|
|
|
|
cattrs: &attr::Container,
|
2018-05-20 19:34:52 -07:00
|
|
|
deserializer: Option<TokenStream>,
|
2017-12-10 22:46:46 -08:00
|
|
|
) -> Fragment {
|
2023-05-27 17:17:00 +05:00
|
|
|
assert!(!cattrs.has_flatten());
|
|
|
|
|
|
|
|
let field_count = fields
|
|
|
|
.iter()
|
|
|
|
.filter(|field| !field.attrs.skip_deserializing())
|
|
|
|
.count();
|
|
|
|
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_type = ¶ms.this_type;
|
2017-12-23 20:13:08 -08:00
|
|
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
|
|
|
split_with_de_lifetime(params);
|
2017-11-13 15:35:14 -05:00
|
|
|
let delife = params.borrowed.de_lifetime();
|
|
|
|
|
|
|
|
let is_enum = variant_ident.is_some();
|
|
|
|
let expecting = match variant_ident {
|
|
|
|
Some(variant_ident) => format!("tuple variant {}::{}", params.type_name(), variant_ident),
|
|
|
|
None => format!("tuple struct {}", params.type_name()),
|
|
|
|
};
|
2020-10-23 00:54:13 +05:00
|
|
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
2017-11-13 15:35:14 -05:00
|
|
|
|
|
|
|
let nfields = fields.len();
|
|
|
|
|
|
|
|
let visit_newtype_struct = if !is_enum && nfields == 1 {
|
2017-12-17 10:46:44 -08:00
|
|
|
Some(deserialize_newtype_struct_in_place(params, &fields[0]))
|
2017-11-13 15:35:14 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
Resolve needless_borrow clippy lints
error: this expression borrows a reference (`&syn::Type`) that is immediately dereferenced by the compiler
--> serde_derive/src/internals/check.rs:399:37
|
399 | if let Type::Path(ty) = ungroup(&field.ty) {
| ^^^^^^^^^ help: change this to: `field.ty`
|
note: the lint level is defined here
--> serde_derive/src/lib.rs:18:9
|
18 | #![deny(clippy::all, clippy::pedantic)]
| ^^^^^^^^^^^
= note: `#[deny(clippy::needless_borrow)]` implied by `#[deny(clippy::all)]`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:478:52
|
478 | &type_path, params, fields, false, cattrs, &expecting,
| ^^^^^^^^^^ help: change this to: `expecting`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:564:76
|
564 | let visit_seq = Stmts(deserialize_seq_in_place(params, fields, cattrs, &expecting));
| ^^^^^^^^^^ help: change this to: `expecting`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:925:51
|
925 | &type_path, params, fields, true, cattrs, &expecting,
| ^^^^^^^^^^ help: change this to: `expecting`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:1066:76
|
1066 | let visit_seq = Stmts(deserialize_seq_in_place(params, fields, cattrs, &expecting));
| ^^^^^^^^^^ help: change this to: `expecting`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&proc_macro2::TokenStream`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:2288:80
|
2288 | let fallthrough_borrowed_arm = fallthrough_borrowed.as_ref().unwrap_or(&fallthrough_arm);
| ^^^^^^^^^^^^^^^^ help: change this to: `fallthrough_arm`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&syn::Member`) that is immediately dereferenced by the compiler
--> serde_derive/src/ser.rs:1102:43
|
1102 | get_member(params, field, &member)
| ^^^^^^^ help: change this to: `member`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
2021-06-04 20:56:35 -07:00
|
|
|
let visit_seq = Stmts(deserialize_seq_in_place(params, fields, cattrs, expecting));
|
2017-11-13 15:35:14 -05:00
|
|
|
|
|
|
|
let visitor_expr = quote! {
|
|
|
|
__Visitor {
|
2017-12-17 10:46:44 -08:00
|
|
|
place: __place,
|
2021-01-08 19:35:52 -08:00
|
|
|
lifetime: _serde::__private::PhantomData,
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let dispatch = if let Some(deserializer) = deserializer {
|
2023-05-27 17:17:00 +05:00
|
|
|
quote!(_serde::Deserializer::deserialize_tuple(#deserializer, #field_count, #visitor_expr))
|
2017-11-13 15:35:14 -05:00
|
|
|
} else if is_enum {
|
2023-05-27 17:17:00 +05:00
|
|
|
quote!(_serde::de::VariantAccess::tuple_variant(__variant, #field_count, #visitor_expr))
|
2017-11-13 15:35:14 -05:00
|
|
|
} else if nfields == 1 {
|
|
|
|
let type_name = cattrs.name().deserialize_name();
|
|
|
|
quote!(_serde::Deserializer::deserialize_newtype_struct(__deserializer, #type_name, #visitor_expr))
|
|
|
|
} else {
|
|
|
|
let type_name = cattrs.name().deserialize_name();
|
2023-05-27 17:17:00 +05:00
|
|
|
quote!(_serde::Deserializer::deserialize_tuple_struct(__deserializer, #type_name, #field_count, #visitor_expr))
|
2017-11-13 15:35:14 -05:00
|
|
|
};
|
|
|
|
|
2023-05-27 17:17:00 +05:00
|
|
|
let visitor_var = if field_count == 0 {
|
2017-11-13 15:35:14 -05:00
|
|
|
quote!(_)
|
|
|
|
} else {
|
|
|
|
quote!(mut __seq)
|
|
|
|
};
|
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
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();
|
2017-12-10 22:46:46 -08:00
|
|
|
|
|
|
|
quote_block! {
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2017-12-17 10:46:44 -08:00
|
|
|
struct __Visitor #in_place_impl_generics #where_clause {
|
2022-11-27 16:08:47 -08:00
|
|
|
place: &#place_life mut #this_type #ty_generics,
|
2021-01-08 19:35:52 -08:00
|
|
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
2017-12-10 22:46:46 -08:00
|
|
|
}
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
impl #in_place_impl_generics _serde::de::Visitor<#delife> for __Visitor #in_place_ty_generics #where_clause {
|
2017-12-10 22:46:46 -08:00
|
|
|
type Value = ();
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn expecting(&self, __formatter: &mut _serde::__private::Formatter) -> _serde::__private::fmt::Result {
|
|
|
|
_serde::__private::Formatter::write_str(__formatter, #expecting)
|
2017-12-10 22:46:46 -08:00
|
|
|
}
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2017-12-10 22:46:46 -08:00
|
|
|
#visit_newtype_struct
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2017-12-10 22:46:46 -08:00
|
|
|
#[inline]
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_seq<__A>(self, #visitor_var: __A) -> _serde::__private::Result<Self::Value, __A::Error>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__A: _serde::de::SeqAccess<#delife>,
|
2017-12-10 22:46:46 -08:00
|
|
|
{
|
|
|
|
#visit_seq
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
2017-12-10 22:46:46 -08:00
|
|
|
}
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2017-12-10 22:46:46 -08:00
|
|
|
#dispatch
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
fn deserialize_seq(
|
2018-05-20 19:34:52 -07:00
|
|
|
type_path: &TokenStream,
|
2017-04-13 12:28:23 -07:00
|
|
|
params: &Parameters,
|
|
|
|
fields: &[Field],
|
|
|
|
is_struct: bool,
|
2017-04-14 15:52:56 -07:00
|
|
|
cattrs: &attr::Container,
|
2018-05-08 12:03:35 -07:00
|
|
|
expecting: &str,
|
2017-04-13 12:28:23 -07:00
|
|
|
) -> Fragment {
|
2017-01-12 21:41:27 -08:00
|
|
|
let vars = (0..fields.len()).map(field_i as fn(_) -> _);
|
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
let deserialized_count = fields
|
|
|
|
.iter()
|
2017-01-11 02:39:16 -08:00
|
|
|
.filter(|field| !field.attrs.skip_deserializing())
|
|
|
|
.count();
|
2018-05-08 12:03:35 -07:00
|
|
|
let expecting = if deserialized_count == 1 {
|
|
|
|
format!("{} with 1 element", expecting)
|
|
|
|
} else {
|
|
|
|
format!("{} with {} elements", expecting, deserialized_count)
|
|
|
|
};
|
2020-10-23 00:54:13 +05:00
|
|
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
2017-01-11 02:39:16 -08:00
|
|
|
|
2018-05-07 11:02:12 -07:00
|
|
|
let mut index_in_seq = 0_usize;
|
2017-12-23 20:13:08 -08:00
|
|
|
let let_values = vars.clone().zip(fields).map(|(var, field)| {
|
|
|
|
if field.attrs.skip_deserializing() {
|
2018-01-09 19:34:35 -08:00
|
|
|
let default = Expr(expr_is_missing(field, cattrs));
|
2017-12-23 20:13:08 -08:00
|
|
|
quote! {
|
|
|
|
let #var = #default;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let visit = match field.attrs.deserialize_with() {
|
|
|
|
None => {
|
2018-05-05 00:45:30 -07:00
|
|
|
let field_ty = field.ty;
|
2018-03-31 23:44:50 +02:00
|
|
|
let span = field.original.span();
|
2018-04-12 22:58:24 -07:00
|
|
|
let func =
|
|
|
|
quote_spanned!(span=> _serde::de::SeqAccess::next_element::<#field_ty>);
|
2018-01-10 20:59:48 -08:00
|
|
|
quote!(try!(#func(&mut __seq)))
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2017-12-23 20:13:08 -08:00
|
|
|
Some(path) => {
|
|
|
|
let (wrapper, wrapper_ty) = wrap_deserialize_field_with(params, field.ty, path);
|
|
|
|
quote!({
|
|
|
|
#wrapper
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Option::map(
|
2017-12-23 20:13:08 -08:00
|
|
|
try!(_serde::de::SeqAccess::next_element::<#wrapper_ty>(&mut __seq)),
|
|
|
|
|__wrap| __wrap.value)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
2019-09-07 23:01:16 -07:00
|
|
|
let value_if_none = match field.attrs.default() {
|
2021-01-08 19:35:52 -08:00
|
|
|
attr::Default::Default => quote!(_serde::__private::Default::default()),
|
2019-09-07 23:01:16 -07:00
|
|
|
attr::Default::Path(path) => quote!(#path()),
|
2018-12-10 23:43:37 -05:00
|
|
|
attr::Default::None => quote!(
|
2021-01-08 19:35:52 -08:00
|
|
|
return _serde::__private::Err(_serde::de::Error::invalid_length(#index_in_seq, &#expecting));
|
2018-12-10 23:43:37 -05:00
|
|
|
),
|
|
|
|
};
|
2017-12-23 20:13:08 -08:00
|
|
|
let assign = quote! {
|
|
|
|
let #var = match #visit {
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Some(__value) => __value,
|
|
|
|
_serde::__private::None => {
|
2018-12-10 22:08:13 -08:00
|
|
|
#value_if_none
|
2016-05-07 12:33:59 -07:00
|
|
|
}
|
|
|
|
};
|
2017-12-23 20:13:08 -08:00
|
|
|
};
|
|
|
|
index_in_seq += 1;
|
|
|
|
assign
|
|
|
|
}
|
|
|
|
});
|
2015-07-21 21:35:20 -07:00
|
|
|
|
2017-04-08 22:42:42 -07:00
|
|
|
let mut result = if is_struct {
|
2018-05-19 20:47:40 -07:00
|
|
|
let names = fields.iter().map(|f| &f.member);
|
2018-04-21 11:32:57 -07:00
|
|
|
quote! {
|
2018-03-09 00:21:57 -08:00
|
|
|
#type_path { #( #names: #vars ),* }
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2016-05-15 15:54:20 -07:00
|
|
|
} else {
|
2018-04-21 11:32:57 -07:00
|
|
|
quote! {
|
2018-03-09 00:21:57 -08:00
|
|
|
#type_path ( #(#vars),* )
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2016-05-15 15:54:20 -07:00
|
|
|
};
|
2015-07-21 21:35:20 -07:00
|
|
|
|
2017-04-08 22:42:42 -07:00
|
|
|
if params.has_getter {
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_type = ¶ms.this_type;
|
2022-11-27 17:16:00 -08:00
|
|
|
let (_, ty_generics, _) = params.generics.split_for_impl();
|
2017-04-08 22:42:42 -07:00
|
|
|
result = quote! {
|
2022-11-27 17:16:00 -08:00
|
|
|
_serde::__private::Into::<#this_type #ty_generics>::into(#result)
|
2017-04-08 22:42:42 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-07 23:01:16 -07:00
|
|
|
let let_default = match cattrs.default() {
|
2017-12-23 20:13:08 -08:00
|
|
|
attr::Default::Default => Some(quote!(
|
2021-01-08 19:35:52 -08:00
|
|
|
let __default: Self::Value = _serde::__private::Default::default();
|
2017-12-23 20:13:08 -08:00
|
|
|
)),
|
2019-09-07 23:01:16 -07:00
|
|
|
attr::Default::Path(path) => Some(quote!(
|
2017-12-23 20:13:08 -08:00
|
|
|
let __default: Self::Value = #path();
|
|
|
|
)),
|
2017-12-06 21:07:16 +01:00
|
|
|
attr::Default::None => {
|
|
|
|
// We don't need the default value, to prevent an unused variable warning
|
|
|
|
// we'll leave the line empty.
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_block! {
|
2017-12-06 21:07:16 +01:00
|
|
|
#let_default
|
2016-10-03 21:09:52 -07:00
|
|
|
#(#let_values)*
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(#result)
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2015-07-21 21:35:20 -07:00
|
|
|
}
|
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
#[cfg(feature = "deserialize_in_place")]
|
|
|
|
fn deserialize_seq_in_place(
|
2017-11-13 15:35:14 -05:00
|
|
|
params: &Parameters,
|
|
|
|
fields: &[Field],
|
|
|
|
cattrs: &attr::Container,
|
2018-05-08 12:03:35 -07:00
|
|
|
expecting: &str,
|
2017-11-13 15:35:14 -05:00
|
|
|
) -> Fragment {
|
|
|
|
let deserialized_count = fields
|
|
|
|
.iter()
|
|
|
|
.filter(|field| !field.attrs.skip_deserializing())
|
|
|
|
.count();
|
2018-05-08 12:03:35 -07:00
|
|
|
let expecting = if deserialized_count == 1 {
|
|
|
|
format!("{} with 1 element", expecting)
|
|
|
|
} else {
|
|
|
|
format!("{} with {} elements", expecting, deserialized_count)
|
|
|
|
};
|
2020-10-23 00:54:13 +05:00
|
|
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
2017-11-13 15:35:14 -05:00
|
|
|
|
|
|
|
let mut index_in_seq = 0usize;
|
2018-05-20 12:36:42 -07:00
|
|
|
let write_values = fields.iter().map(|field| {
|
|
|
|
let member = &field.member;
|
|
|
|
|
|
|
|
if field.attrs.skip_deserializing() {
|
|
|
|
let default = Expr(expr_is_missing(field, cattrs));
|
|
|
|
quote! {
|
|
|
|
self.place.#member = #default;
|
|
|
|
}
|
|
|
|
} else {
|
2019-09-07 23:01:16 -07:00
|
|
|
let value_if_none = match field.attrs.default() {
|
2018-12-10 22:08:13 -08:00
|
|
|
attr::Default::Default => quote!(
|
2021-01-08 19:35:52 -08:00
|
|
|
self.place.#member = _serde::__private::Default::default();
|
2018-12-10 22:08:13 -08:00
|
|
|
),
|
2019-09-07 23:01:16 -07:00
|
|
|
attr::Default::Path(path) => quote!(
|
2018-12-10 22:08:13 -08:00
|
|
|
self.place.#member = #path();
|
|
|
|
),
|
|
|
|
attr::Default::None => quote!(
|
2021-01-08 19:35:52 -08:00
|
|
|
return _serde::__private::Err(_serde::de::Error::invalid_length(#index_in_seq, &#expecting));
|
2018-12-10 22:08:13 -08:00
|
|
|
),
|
2018-05-20 12:36:42 -07:00
|
|
|
};
|
|
|
|
let write = match field.attrs.deserialize_with() {
|
|
|
|
None => {
|
|
|
|
quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
if let _serde::__private::None = try!(_serde::de::SeqAccess::next_element_seed(&mut __seq,
|
|
|
|
_serde::__private::de::InPlaceSeed(&mut self.place.#member)))
|
2018-05-20 12:36:42 -07:00
|
|
|
{
|
2018-12-10 22:08:13 -08:00
|
|
|
#value_if_none
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
|
|
|
}
|
2018-05-20 12:36:42 -07:00
|
|
|
}
|
|
|
|
Some(path) => {
|
|
|
|
let (wrapper, wrapper_ty) = wrap_deserialize_field_with(params, field.ty, path);
|
|
|
|
quote!({
|
2018-12-10 22:08:13 -08:00
|
|
|
#wrapper
|
|
|
|
match try!(_serde::de::SeqAccess::next_element::<#wrapper_ty>(&mut __seq)) {
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Some(__wrap) => {
|
2018-12-10 22:08:13 -08:00
|
|
|
self.place.#member = __wrap.value;
|
|
|
|
}
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::None => {
|
2018-12-10 22:08:13 -08:00
|
|
|
#value_if_none
|
2017-12-11 18:13:13 -08:00
|
|
|
}
|
2018-12-10 22:08:13 -08:00
|
|
|
}
|
|
|
|
})
|
2018-05-20 12:36:42 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
index_in_seq += 1;
|
|
|
|
write
|
|
|
|
}
|
|
|
|
});
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_type = ¶ms.this_type;
|
2017-12-11 20:59:54 -08:00
|
|
|
let (_, ty_generics, _) = params.generics.split_for_impl();
|
2019-09-07 23:01:16 -07:00
|
|
|
let let_default = match cattrs.default() {
|
2017-12-23 20:13:08 -08:00
|
|
|
attr::Default::Default => Some(quote!(
|
2022-11-27 16:08:47 -08:00
|
|
|
let __default: #this_type #ty_generics = _serde::__private::Default::default();
|
2017-12-23 20:13:08 -08:00
|
|
|
)),
|
2019-09-07 23:01:16 -07:00
|
|
|
attr::Default::Path(path) => Some(quote!(
|
2022-11-27 16:08:47 -08:00
|
|
|
let __default: #this_type #ty_generics = #path();
|
2017-12-23 20:13:08 -08:00
|
|
|
)),
|
2017-12-11 20:59:54 -08:00
|
|
|
attr::Default::None => {
|
|
|
|
// We don't need the default value, to prevent an unused variable warning
|
|
|
|
// we'll leave the line empty.
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-11-13 15:35:14 -05:00
|
|
|
quote_block! {
|
2017-12-11 20:59:54 -08:00
|
|
|
#let_default
|
2017-11-13 15:35:14 -05:00
|
|
|
#(#write_values)*
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(())
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 21:06:32 -07:00
|
|
|
fn deserialize_newtype_struct(
|
|
|
|
type_path: &TokenStream,
|
|
|
|
params: &Parameters,
|
|
|
|
field: &Field,
|
|
|
|
) -> TokenStream {
|
2017-09-09 11:34:08 -07:00
|
|
|
let delife = params.borrowed.de_lifetime();
|
2018-05-05 00:17:00 -07:00
|
|
|
let field_ty = field.ty;
|
2017-09-09 11:34:08 -07:00
|
|
|
|
2016-06-14 23:37:20 -07:00
|
|
|
let value = match field.attrs.deserialize_with() {
|
2016-05-15 15:54:20 -07:00
|
|
|
None => {
|
2018-11-17 18:13:36 +02:00
|
|
|
let span = field.original.span();
|
|
|
|
let func = quote_spanned!(span=> <#field_ty as _serde::Deserialize>::deserialize);
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
2018-11-17 18:13:36 +02:00
|
|
|
try!(#func(__e))
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2016-05-15 15:54:20 -07:00
|
|
|
}
|
|
|
|
Some(path) => {
|
2018-05-05 00:17:00 -07:00
|
|
|
quote! {
|
|
|
|
try!(#path(__e))
|
|
|
|
}
|
2016-05-15 15:54:20 -07:00
|
|
|
}
|
|
|
|
};
|
2017-04-08 22:42:42 -07:00
|
|
|
|
2018-04-21 11:32:57 -07:00
|
|
|
let mut result = quote!(#type_path(__field0));
|
2017-04-08 22:42:42 -07:00
|
|
|
if params.has_getter {
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_type = ¶ms.this_type;
|
2022-11-27 17:16:00 -08:00
|
|
|
let (_, ty_generics, _) = params.generics.split_for_impl();
|
2017-04-08 22:42:42 -07:00
|
|
|
result = quote! {
|
2022-11-27 17:16:00 -08:00
|
|
|
_serde::__private::Into::<#this_type #ty_generics>::into(#result)
|
2017-04-08 22:42:42 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
2016-05-15 15:54:20 -07:00
|
|
|
#[inline]
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_newtype_struct<__E>(self, __e: __E) -> _serde::__private::Result<Self::Value, __E::Error>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::Deserializer<#delife>,
|
2016-05-15 15:54:20 -07:00
|
|
|
{
|
2018-05-05 00:17:00 -07:00
|
|
|
let __field0: #field_ty = #value;
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(#result)
|
2016-05-15 15:54:20 -07:00
|
|
|
}
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2016-05-15 15:54:20 -07:00
|
|
|
}
|
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
#[cfg(feature = "deserialize_in_place")]
|
2018-05-20 19:34:52 -07:00
|
|
|
fn deserialize_newtype_struct_in_place(params: &Parameters, field: &Field) -> TokenStream {
|
2018-08-14 22:32:27 -07:00
|
|
|
// We do not generate deserialize_in_place if every field has a
|
|
|
|
// deserialize_with.
|
2017-12-10 22:55:28 -08:00
|
|
|
assert!(field.attrs.deserialize_with().is_none());
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2017-12-10 22:55:28 -08:00
|
|
|
let delife = params.borrowed.de_lifetime();
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2017-12-10 22:55:28 -08:00
|
|
|
quote! {
|
|
|
|
#[inline]
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_newtype_struct<__E>(self, __e: __E) -> _serde::__private::Result<Self::Value, __E::Error>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::Deserializer<#delife>,
|
2017-12-10 22:55:28 -08:00
|
|
|
{
|
2018-04-21 11:32:57 -07:00
|
|
|
_serde::Deserialize::deserialize_in_place(__e, &mut self.place.0)
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-07 01:12:49 +05:00
|
|
|
enum StructForm<'a> {
|
|
|
|
Struct,
|
|
|
|
/// Contains a variant name
|
|
|
|
ExternallyTagged(&'a syn::Ident),
|
|
|
|
/// Contains a variant name and an intermediate deserializer from which actual
|
|
|
|
/// deserialization will be performed
|
|
|
|
InternallyTagged(&'a syn::Ident, TokenStream),
|
|
|
|
/// Contains a variant name and an intermediate deserializer from which actual
|
|
|
|
/// deserialization will be performed
|
|
|
|
Untagged(&'a syn::Ident, TokenStream),
|
2017-09-17 13:45:07 -07:00
|
|
|
}
|
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
fn deserialize_struct(
|
|
|
|
params: &Parameters,
|
|
|
|
fields: &[Field],
|
2017-04-14 15:52:56 -07:00
|
|
|
cattrs: &attr::Container,
|
2023-05-07 01:12:49 +05:00
|
|
|
form: StructForm,
|
2017-04-13 12:28:23 -07:00
|
|
|
) -> Fragment {
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_type = ¶ms.this_type;
|
|
|
|
let this_value = ¶ms.this_value;
|
2017-12-23 20:13:08 -08:00
|
|
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
|
|
|
split_with_de_lifetime(params);
|
2017-09-09 11:34:08 -07:00
|
|
|
let delife = params.borrowed.de_lifetime();
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2017-04-08 22:42:42 -07:00
|
|
|
// 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 {
|
2018-05-20 19:34:52 -07:00
|
|
|
let local = ¶ms.local;
|
2017-04-08 22:42:42 -07:00
|
|
|
quote!(#local)
|
|
|
|
} else {
|
2022-11-27 16:08:47 -08:00
|
|
|
quote!(#this_value)
|
2017-04-08 22:42:42 -07:00
|
|
|
};
|
|
|
|
|
2023-05-07 01:12:49 +05:00
|
|
|
let type_path = match form {
|
|
|
|
StructForm::Struct => construct,
|
|
|
|
StructForm::ExternallyTagged(variant_ident)
|
|
|
|
| StructForm::InternallyTagged(variant_ident, _)
|
|
|
|
| StructForm::Untagged(variant_ident, _) => quote!(#construct::#variant_ident),
|
2016-05-15 13:32:06 -07:00
|
|
|
};
|
2023-05-07 01:12:49 +05:00
|
|
|
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)
|
|
|
|
}
|
2017-01-11 02:39:16 -08:00
|
|
|
};
|
2020-10-23 00:54:13 +05:00
|
|
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
2015-07-21 21:35:20 -07:00
|
|
|
|
2023-05-01 01:45:37 +05:00
|
|
|
let field_names_idents: 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)| {
|
|
|
|
(
|
|
|
|
field.attrs.name().deserialize_name(),
|
|
|
|
field_i(i),
|
|
|
|
field.attrs.aliases(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
2023-05-01 01:49:11 +05:00
|
|
|
let field_visitor = Stmts(deserialize_generated_identifier(
|
|
|
|
&field_names_idents,
|
|
|
|
cattrs,
|
|
|
|
false,
|
|
|
|
None,
|
2018-05-10 09:11:19 -07:00
|
|
|
));
|
2017-02-12 21:59:04 -08:00
|
|
|
|
2018-08-14 22:32:27 -07:00
|
|
|
// untagged struct variants do not get a visit_seq method. The same applies to
|
|
|
|
// structs that only have a map representation.
|
2023-05-07 01:12:49 +05:00
|
|
|
let visit_seq = match form {
|
|
|
|
StructForm::Untagged(..) => None,
|
|
|
|
_ if cattrs.has_flatten() => None,
|
|
|
|
_ => {
|
|
|
|
let mut_seq = if field_names_idents.is_empty() {
|
|
|
|
quote!(_)
|
|
|
|
} else {
|
|
|
|
quote!(mut __seq)
|
|
|
|
};
|
2023-05-01 02:08:10 +05:00
|
|
|
|
2023-05-07 01:12:49 +05:00
|
|
|
let visit_seq = Stmts(deserialize_seq(
|
|
|
|
&type_path, params, fields, true, cattrs, expecting,
|
|
|
|
));
|
2023-05-01 02:08:10 +05:00
|
|
|
|
2023-05-07 01:12:49 +05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2017-02-02 12:10:07 -08:00
|
|
|
};
|
2020-10-01 13:29:39 +05:00
|
|
|
let visit_map = Stmts(deserialize_map(&type_path, params, fields, cattrs));
|
2017-02-02 12:10:07 -08:00
|
|
|
|
2023-05-07 01:12:49 +05:00
|
|
|
let visitor_seed = match form {
|
|
|
|
StructForm::ExternallyTagged(..) if cattrs.has_flatten() => Some(quote! {
|
2018-05-06 20:39:45 -07:00
|
|
|
impl #de_impl_generics _serde::de::DeserializeSeed<#delife> for __Visitor #de_ty_generics #where_clause {
|
2022-11-27 16:08:47 -08:00
|
|
|
type Value = #this_type #ty_generics;
|
2018-05-06 20:39:45 -07:00
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn deserialize<__D>(self, __deserializer: __D) -> _serde::__private::Result<Self::Value, __D::Error>
|
2018-05-06 20:39:45 -07:00
|
|
|
where
|
2023-02-25 22:17:27 +05:00
|
|
|
__D: _serde::Deserializer<#delife>,
|
2018-05-06 20:39:45 -07:00
|
|
|
{
|
|
|
|
_serde::Deserializer::deserialize_map(__deserializer, self)
|
|
|
|
}
|
|
|
|
}
|
2023-05-07 01:12:49 +05:00
|
|
|
}),
|
|
|
|
_ => None,
|
2018-05-06 20:39:45 -07:00
|
|
|
};
|
|
|
|
|
2023-05-01 01:49:11 +05:00
|
|
|
let fields_stmt = if cattrs.has_flatten() {
|
2018-05-06 20:39:45 -07:00
|
|
|
None
|
2023-05-01 01:49:11 +05:00
|
|
|
} else {
|
|
|
|
let field_names = field_names_idents
|
|
|
|
.iter()
|
|
|
|
.flat_map(|(_, _, aliases)| aliases);
|
|
|
|
|
|
|
|
Some(quote! {
|
|
|
|
#[doc(hidden)]
|
|
|
|
const FIELDS: &'static [&'static str] = &[ #(#field_names),* ];
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2023-05-01 02:43:25 +05:00
|
|
|
let visitor_expr = quote! {
|
|
|
|
__Visitor {
|
|
|
|
marker: _serde::__private::PhantomData::<#this_type #ty_generics>,
|
|
|
|
lifetime: _serde::__private::PhantomData,
|
|
|
|
}
|
|
|
|
};
|
2023-05-07 01:12:49 +05:00
|
|
|
let dispatch = match form {
|
|
|
|
StructForm::Struct if cattrs.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)
|
|
|
|
}
|
2023-05-01 02:43:25 +05:00
|
|
|
}
|
2023-05-07 01:12:49 +05:00
|
|
|
StructForm::ExternallyTagged(_) if cattrs.has_flatten() => quote! {
|
2023-05-01 02:43:25 +05:00
|
|
|
_serde::de::VariantAccess::newtype_variant_seed(__variant, #visitor_expr)
|
2023-05-07 01:12:49 +05:00
|
|
|
},
|
|
|
|
StructForm::ExternallyTagged(_) => quote! {
|
2023-05-01 02:43:25 +05:00
|
|
|
_serde::de::VariantAccess::struct_variant(__variant, FIELDS, #visitor_expr)
|
2023-05-07 01:12:49 +05:00
|
|
|
},
|
|
|
|
StructForm::InternallyTagged(_, deserializer) => quote! {
|
|
|
|
_serde::Deserializer::deserialize_any(#deserializer, #visitor_expr)
|
|
|
|
},
|
|
|
|
StructForm::Untagged(_, deserializer) => quote! {
|
|
|
|
_serde::Deserializer::deserialize_any(#deserializer, #visitor_expr)
|
|
|
|
},
|
2018-05-06 20:39:45 -07:00
|
|
|
};
|
|
|
|
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_block! {
|
2016-09-12 00:04:21 -07:00
|
|
|
#field_visitor
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2017-03-26 22:56:58 -07:00
|
|
|
struct __Visitor #de_impl_generics #where_clause {
|
2022-11-27 16:08:47 -08:00
|
|
|
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
2021-01-08 19:35:52 -08:00
|
|
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
2017-02-20 11:50:24 -08:00
|
|
|
}
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2017-09-09 11:34:08 -07:00
|
|
|
impl #de_impl_generics _serde::de::Visitor<#delife> for __Visitor #de_ty_generics #where_clause {
|
2022-11-27 16:08:47 -08:00
|
|
|
type Value = #this_type #ty_generics;
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn expecting(&self, __formatter: &mut _serde::__private::Formatter) -> _serde::__private::fmt::Result {
|
|
|
|
_serde::__private::Formatter::write_str(__formatter, #expecting)
|
2017-01-11 02:39:16 -08:00
|
|
|
}
|
|
|
|
|
2017-02-02 12:10:07 -08:00
|
|
|
#visit_seq
|
2015-07-21 21:35:20 -07:00
|
|
|
|
2015-03-14 13:09:37 -07:00
|
|
|
#[inline]
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_map<__A>(self, mut __map: __A) -> _serde::__private::Result<Self::Value, __A::Error>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__A: _serde::de::MapAccess<#delife>,
|
2016-09-12 00:04:21 -07:00
|
|
|
{
|
|
|
|
#visit_map
|
|
|
|
}
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
|
2018-05-06 20:39:45 -07:00
|
|
|
#visitor_seed
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
#fields_stmt
|
2015-07-03 19:39:27 -07:00
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
#dispatch
|
2017-02-20 14:43:51 -08:00
|
|
|
}
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
#[cfg(feature = "deserialize_in_place")]
|
|
|
|
fn deserialize_struct_in_place(
|
2018-05-05 00:45:30 -07:00
|
|
|
variant_ident: Option<syn::Ident>,
|
2017-11-13 15:35:14 -05:00
|
|
|
params: &Parameters,
|
|
|
|
fields: &[Field],
|
|
|
|
cattrs: &attr::Container,
|
2018-05-20 19:34:52 -07:00
|
|
|
deserializer: Option<TokenStream>,
|
2018-03-18 18:27:35 +01:00
|
|
|
) -> Option<Fragment> {
|
2017-11-13 15:35:14 -05:00
|
|
|
let is_enum = variant_ident.is_some();
|
|
|
|
|
2018-03-18 18:27:35 +01:00
|
|
|
// for now we do not support in_place deserialization for structs that
|
|
|
|
// are represented as map.
|
2018-03-20 13:43:23 +01:00
|
|
|
if cattrs.has_flatten() {
|
2018-03-18 18:27:35 +01:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_type = ¶ms.this_type;
|
2017-12-23 20:13:08 -08:00
|
|
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
|
|
|
split_with_de_lifetime(params);
|
2017-11-13 15:35:14 -05:00
|
|
|
let delife = params.borrowed.de_lifetime();
|
|
|
|
|
|
|
|
let expecting = match variant_ident {
|
|
|
|
Some(variant_ident) => format!("struct variant {}::{}", params.type_name(), variant_ident),
|
|
|
|
None => format!("struct {}", params.type_name()),
|
|
|
|
};
|
2020-10-23 00:54:13 +05:00
|
|
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
2017-11-13 15:35:14 -05:00
|
|
|
|
Resolve needless_borrow clippy lints
error: this expression borrows a reference (`&syn::Type`) that is immediately dereferenced by the compiler
--> serde_derive/src/internals/check.rs:399:37
|
399 | if let Type::Path(ty) = ungroup(&field.ty) {
| ^^^^^^^^^ help: change this to: `field.ty`
|
note: the lint level is defined here
--> serde_derive/src/lib.rs:18:9
|
18 | #![deny(clippy::all, clippy::pedantic)]
| ^^^^^^^^^^^
= note: `#[deny(clippy::needless_borrow)]` implied by `#[deny(clippy::all)]`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:478:52
|
478 | &type_path, params, fields, false, cattrs, &expecting,
| ^^^^^^^^^^ help: change this to: `expecting`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:564:76
|
564 | let visit_seq = Stmts(deserialize_seq_in_place(params, fields, cattrs, &expecting));
| ^^^^^^^^^^ help: change this to: `expecting`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:925:51
|
925 | &type_path, params, fields, true, cattrs, &expecting,
| ^^^^^^^^^^ help: change this to: `expecting`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:1066:76
|
1066 | let visit_seq = Stmts(deserialize_seq_in_place(params, fields, cattrs, &expecting));
| ^^^^^^^^^^ help: change this to: `expecting`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&proc_macro2::TokenStream`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:2288:80
|
2288 | let fallthrough_borrowed_arm = fallthrough_borrowed.as_ref().unwrap_or(&fallthrough_arm);
| ^^^^^^^^^^^^^^^^ help: change this to: `fallthrough_arm`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&syn::Member`) that is immediately dereferenced by the compiler
--> serde_derive/src/ser.rs:1102:43
|
1102 | get_member(params, field, &member)
| ^^^^^^^ help: change this to: `member`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
2021-06-04 20:56:35 -07:00
|
|
|
let visit_seq = Stmts(deserialize_seq_in_place(params, fields, cattrs, expecting));
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2018-04-12 22:58:24 -07:00
|
|
|
let (field_visitor, fields_stmt, visit_map) =
|
|
|
|
deserialize_struct_as_struct_in_place_visitor(params, fields, cattrs);
|
2018-03-18 18:22:06 +01:00
|
|
|
|
2017-11-13 15:35:14 -05:00
|
|
|
let field_visitor = Stmts(field_visitor);
|
2018-03-25 12:39:20 +02:00
|
|
|
let fields_stmt = Stmts(fields_stmt);
|
2017-11-13 15:35:14 -05:00
|
|
|
let visit_map = Stmts(visit_map);
|
|
|
|
|
|
|
|
let visitor_expr = quote! {
|
|
|
|
__Visitor {
|
2017-12-17 10:46:44 -08:00
|
|
|
place: __place,
|
2021-01-08 19:35:52 -08:00
|
|
|
lifetime: _serde::__private::PhantomData,
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let dispatch = if let Some(deserializer) = deserializer {
|
|
|
|
quote! {
|
|
|
|
_serde::Deserializer::deserialize_any(#deserializer, #visitor_expr)
|
|
|
|
}
|
|
|
|
} else if is_enum {
|
|
|
|
quote! {
|
|
|
|
_serde::de::VariantAccess::struct_variant(__variant, FIELDS, #visitor_expr)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let type_name = cattrs.name().deserialize_name();
|
|
|
|
quote! {
|
|
|
|
_serde::Deserializer::deserialize_struct(__deserializer, #type_name, FIELDS, #visitor_expr)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-20 14:45:14 +01:00
|
|
|
let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing());
|
2017-11-13 15:35:14 -05:00
|
|
|
let visitor_var = if all_skipped {
|
|
|
|
quote!(_)
|
|
|
|
} else {
|
|
|
|
quote!(mut __seq)
|
|
|
|
};
|
|
|
|
|
2018-03-18 18:27:35 +01:00
|
|
|
let visit_seq = quote! {
|
|
|
|
#[inline]
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_seq<__A>(self, #visitor_var: __A) -> _serde::__private::Result<Self::Value, __A::Error>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__A: _serde::de::SeqAccess<#delife>,
|
2018-03-18 18:27:35 +01:00
|
|
|
{
|
|
|
|
#visit_seq
|
|
|
|
}
|
2017-11-13 15:35:14 -05:00
|
|
|
};
|
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
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();
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2018-03-18 18:27:35 +01:00
|
|
|
Some(quote_block! {
|
2017-12-10 22:46:46 -08:00
|
|
|
#field_visitor
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2017-12-17 10:46:44 -08:00
|
|
|
struct __Visitor #in_place_impl_generics #where_clause {
|
2022-11-27 16:08:47 -08:00
|
|
|
place: &#place_life mut #this_type #ty_generics,
|
2021-01-08 19:35:52 -08:00
|
|
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
2017-12-10 22:46:46 -08:00
|
|
|
}
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
impl #in_place_impl_generics _serde::de::Visitor<#delife> for __Visitor #in_place_ty_generics #where_clause {
|
2017-12-10 22:46:46 -08:00
|
|
|
type Value = ();
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn expecting(&self, __formatter: &mut _serde::__private::Formatter) -> _serde::__private::fmt::Result {
|
|
|
|
_serde::__private::Formatter::write_str(__formatter, #expecting)
|
2017-12-10 22:46:46 -08:00
|
|
|
}
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2017-12-10 22:46:46 -08:00
|
|
|
#visit_seq
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2017-12-10 22:46:46 -08:00
|
|
|
#[inline]
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_map<__A>(self, mut __map: __A) -> _serde::__private::Result<Self::Value, __A::Error>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__A: _serde::de::MapAccess<#delife>,
|
2017-12-10 22:46:46 -08:00
|
|
|
{
|
|
|
|
#visit_map
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
2017-12-10 22:46:46 -08:00
|
|
|
}
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2017-12-10 22:46:46 -08:00
|
|
|
#fields_stmt
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2017-12-10 22:46:46 -08:00
|
|
|
#dispatch
|
2018-03-18 18:27:35 +01:00
|
|
|
})
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
|
|
|
|
2017-04-14 15:52:56 -07:00
|
|
|
fn deserialize_enum(
|
2017-04-13 12:28:23 -07:00
|
|
|
params: &Parameters,
|
|
|
|
variants: &[Variant],
|
2017-04-14 15:52:56 -07:00
|
|
|
cattrs: &attr::Container,
|
2023-06-07 20:58:59 -07:00
|
|
|
) -> Fragment {
|
|
|
|
// The variants have already been checked (in ast.rs) that all untagged variants appear at the end
|
2023-06-07 21:23:31 -07:00
|
|
|
match variants.iter().position(|var| var.attrs.untagged()) {
|
|
|
|
Some(variant_idx) => {
|
2023-06-07 20:58:59 -07:00
|
|
|
let (tagged, untagged) = variants.split_at(variant_idx);
|
|
|
|
let tagged_frag = Expr(deserialize_homogeneous_enum(params, tagged, cattrs));
|
2023-06-07 21:33:11 -07:00
|
|
|
deserialize_untagged_enum_after(params, untagged, cattrs, Some(tagged_frag))
|
2023-06-07 20:58:59 -07:00
|
|
|
}
|
|
|
|
None => deserialize_homogeneous_enum(params, variants, cattrs),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_homogeneous_enum(
|
|
|
|
params: &Parameters,
|
|
|
|
variants: &[Variant],
|
|
|
|
cattrs: &attr::Container,
|
2017-04-13 12:28:23 -07:00
|
|
|
) -> Fragment {
|
2019-09-07 23:01:16 -07:00
|
|
|
match cattrs.tag() {
|
2018-12-27 17:57:29 +01:00
|
|
|
attr::TagType::External => deserialize_externally_tagged_enum(params, variants, cattrs),
|
2019-09-07 23:01:16 -07:00
|
|
|
attr::TagType::Internal { tag } => {
|
2017-04-14 15:52:56 -07:00
|
|
|
deserialize_internally_tagged_enum(params, variants, cattrs, tag)
|
2017-02-02 12:10:07 -08:00
|
|
|
}
|
2019-09-07 23:01:16 -07:00
|
|
|
attr::TagType::Adjacent { tag, content } => {
|
|
|
|
deserialize_adjacently_tagged_enum(params, variants, cattrs, tag, content)
|
|
|
|
}
|
2018-12-27 17:57:29 +01:00
|
|
|
attr::TagType::None => deserialize_untagged_enum(params, variants, cattrs),
|
2017-02-02 12:10:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 10:35:09 -06:00
|
|
|
fn prepare_enum_variant_enum(
|
2017-04-13 12:28:23 -07:00
|
|
|
variants: &[Variant],
|
2017-04-14 15:52:56 -07:00
|
|
|
cattrs: &attr::Container,
|
2019-01-15 11:29:43 -06:00
|
|
|
) -> (TokenStream, Stmts) {
|
2020-05-08 15:39:07 -07:00
|
|
|
let mut deserialized_variants = variants
|
2017-04-13 12:28:23 -07:00
|
|
|
.iter()
|
2017-01-12 23:17:45 -08:00
|
|
|
.enumerate()
|
2020-05-08 15:39:07 -07:00
|
|
|
.filter(|&(_, variant)| !variant.attrs.skip_deserializing());
|
|
|
|
|
|
|
|
let variant_names_idents: Vec<_> = deserialized_variants
|
|
|
|
.clone()
|
2019-01-15 10:35:09 -06:00
|
|
|
.map(|(i, variant)| {
|
2019-02-01 21:02:57 -08:00
|
|
|
(
|
|
|
|
variant.attrs.name().deserialize_name(),
|
|
|
|
field_i(i),
|
|
|
|
variant.attrs.aliases(),
|
|
|
|
)
|
2019-01-15 10:35:09 -06:00
|
|
|
})
|
2017-01-12 23:17:45 -08:00
|
|
|
.collect();
|
|
|
|
|
2020-05-08 15:39:07 -07:00
|
|
|
let other_idx = deserialized_variants.position(|(_, variant)| variant.attrs.other());
|
2018-09-10 15:12:15 +00:00
|
|
|
|
2017-01-21 12:18:55 -08:00
|
|
|
let variants_stmt = {
|
2019-09-07 23:01:16 -07:00
|
|
|
let variant_names = variant_names_idents.iter().map(|(name, _, _)| name);
|
2017-01-21 12:18:55 -08:00
|
|
|
quote! {
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2017-01-21 12:18:55 -08:00
|
|
|
const VARIANTS: &'static [&'static str] = &[ #(#variant_names),* ];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-23 20:13:08 -08:00
|
|
|
let variant_visitor = Stmts(deserialize_generated_identifier(
|
2018-01-09 19:34:35 -08:00
|
|
|
&variant_names_idents,
|
2017-12-23 20:13:08 -08:00
|
|
|
cattrs,
|
|
|
|
true,
|
2018-09-11 23:00:08 -07:00
|
|
|
other_idx,
|
2017-12-23 20:13:08 -08:00
|
|
|
));
|
2015-03-15 22:04:17 -07:00
|
|
|
|
2019-01-15 10:35:09 -06:00
|
|
|
(variants_stmt, variant_visitor)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_externally_tagged_enum(
|
|
|
|
params: &Parameters,
|
|
|
|
variants: &[Variant],
|
|
|
|
cattrs: &attr::Container,
|
|
|
|
) -> Fragment {
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_type = ¶ms.this_type;
|
2019-01-15 10:35:09 -06:00
|
|
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
|
|
|
split_with_de_lifetime(params);
|
|
|
|
let delife = params.borrowed.de_lifetime();
|
|
|
|
|
|
|
|
let type_name = cattrs.name().deserialize_name();
|
|
|
|
let expecting = format!("enum {}", params.type_name());
|
2020-10-23 00:54:13 +05:00
|
|
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
2019-01-15 10:35:09 -06:00
|
|
|
|
|
|
|
let (variants_stmt, variant_visitor) = prepare_enum_variant_enum(variants, cattrs);
|
|
|
|
|
2015-03-14 13:09:37 -07:00
|
|
|
// Match arms to extract a variant from a string
|
2017-04-18 14:23:21 -07:00
|
|
|
let variant_arms = variants
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
2017-12-23 20:13:08 -08:00
|
|
|
.map(|(i, variant)| {
|
|
|
|
let variant_name = field_i(i);
|
2016-01-18 12:39:46 -08:00
|
|
|
|
2017-12-23 20:13:08 -08:00
|
|
|
let block = Match(deserialize_externally_tagged_variant(
|
2018-04-12 22:58:24 -07:00
|
|
|
params, variant, cattrs,
|
2017-12-23 20:13:08 -08:00
|
|
|
));
|
2015-03-15 17:47:25 -07:00
|
|
|
|
2017-12-23 20:13:08 -08:00
|
|
|
quote! {
|
|
|
|
(__Field::#variant_name, __variant) => #block
|
|
|
|
}
|
|
|
|
});
|
2017-01-11 11:02:24 -08:00
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
let all_skipped = variants
|
|
|
|
.iter()
|
|
|
|
.all(|variant| variant.attrs.skip_deserializing());
|
2017-01-12 23:35:39 -08:00
|
|
|
let match_variant = if all_skipped {
|
2017-01-11 11:02:24 -08:00
|
|
|
// This is an empty enum like `enum Impossible {}` or an enum in which
|
|
|
|
// all variants have `#[serde(skip_deserializing)]`.
|
|
|
|
quote! {
|
2023-03-08 11:46:26 -08:00
|
|
|
// FIXME: Once feature(exhaustive_patterns) is stable:
|
2021-01-08 19:35:52 -08:00
|
|
|
// let _serde::__private::Err(__err) = _serde::de::EnumAccess::variant::<__Field>(__data);
|
|
|
|
// _serde::__private::Err(__err)
|
|
|
|
_serde::__private::Result::map(
|
2017-04-14 11:58:29 -07:00
|
|
|
_serde::de::EnumAccess::variant::<__Field>(__data),
|
2017-03-18 13:28:42 -07:00
|
|
|
|(__impossible, _)| match __impossible {})
|
2017-01-11 11:02:24 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quote! {
|
2017-04-14 11:58:29 -07:00
|
|
|
match try!(_serde::de::EnumAccess::variant(__data)) {
|
2017-01-11 11:02:24 -08:00
|
|
|
#(#variant_arms)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-01-18 12:39:46 -08:00
|
|
|
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_block! {
|
2016-09-12 00:04:21 -07:00
|
|
|
#variant_visitor
|
2015-03-15 22:04:17 -07:00
|
|
|
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2017-03-26 22:56:58 -07:00
|
|
|
struct __Visitor #de_impl_generics #where_clause {
|
2022-11-27 16:08:47 -08:00
|
|
|
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
2021-01-08 19:35:52 -08:00
|
|
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
2017-02-20 11:50:24 -08:00
|
|
|
}
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2017-09-09 11:34:08 -07:00
|
|
|
impl #de_impl_generics _serde::de::Visitor<#delife> for __Visitor #de_ty_generics #where_clause {
|
2022-11-27 16:08:47 -08:00
|
|
|
type Value = #this_type #ty_generics;
|
2015-03-14 13:09:37 -07:00
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn expecting(&self, __formatter: &mut _serde::__private::Formatter) -> _serde::__private::fmt::Result {
|
|
|
|
_serde::__private::Formatter::write_str(__formatter, #expecting)
|
2017-01-11 02:39:16 -08:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_enum<__A>(self, __data: __A) -> _serde::__private::Result<Self::Value, __A::Error>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__A: _serde::de::EnumAccess<#delife>,
|
2015-03-14 13:09:37 -07:00
|
|
|
{
|
2017-01-11 11:02:24 -08:00
|
|
|
#match_variant
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 00:04:21 -07:00
|
|
|
#variants_stmt
|
2015-07-29 12:04:31 -07:00
|
|
|
|
2018-12-10 22:31:39 -08:00
|
|
|
_serde::Deserializer::deserialize_enum(
|
|
|
|
__deserializer,
|
|
|
|
#type_name,
|
|
|
|
VARIANTS,
|
|
|
|
__Visitor {
|
2022-11-27 16:08:47 -08:00
|
|
|
marker: _serde::__private::PhantomData::<#this_type #ty_generics>,
|
2021-01-08 19:35:52 -08:00
|
|
|
lifetime: _serde::__private::PhantomData,
|
2018-12-10 22:31:39 -08:00
|
|
|
},
|
|
|
|
)
|
2017-02-20 14:43:51 -08:00
|
|
|
}
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
fn deserialize_internally_tagged_enum(
|
|
|
|
params: &Parameters,
|
|
|
|
variants: &[Variant],
|
2017-04-14 15:52:56 -07:00
|
|
|
cattrs: &attr::Container,
|
2017-04-13 12:28:23 -07:00
|
|
|
tag: &str,
|
|
|
|
) -> Fragment {
|
2019-01-15 10:35:09 -06:00
|
|
|
let (variants_stmt, variant_visitor) = prepare_enum_variant_enum(variants, cattrs);
|
2017-02-02 12:10:07 -08:00
|
|
|
|
|
|
|
// Match arms to extract a variant from a string
|
2017-12-23 20:13:08 -08:00
|
|
|
let variant_arms = variants
|
|
|
|
.iter()
|
2017-02-02 12:10:07 -08:00
|
|
|
.enumerate()
|
|
|
|
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
|
|
|
.map(|(i, variant)| {
|
|
|
|
let variant_name = field_i(i);
|
|
|
|
|
2017-02-20 14:43:51 -08:00
|
|
|
let block = Match(deserialize_internally_tagged_variant(
|
2017-04-02 21:42:07 -07:00
|
|
|
params,
|
2017-02-02 12:10:07 -08:00
|
|
|
variant,
|
2017-04-14 15:52:56 -07:00
|
|
|
cattrs,
|
2020-10-26 22:06:00 +05:00
|
|
|
quote!(__deserializer),
|
2017-02-20 14:43:51 -08:00
|
|
|
));
|
2017-02-02 12:10:07 -08:00
|
|
|
|
|
|
|
quote! {
|
|
|
|
__Field::#variant_name => #block
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-10-23 00:54:13 +05:00
|
|
|
let expecting = format!("internally tagged enum {}", params.type_name());
|
|
|
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
|
|
|
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_block! {
|
2017-02-02 12:10:07 -08:00
|
|
|
#variant_visitor
|
|
|
|
|
|
|
|
#variants_stmt
|
|
|
|
|
2022-10-02 19:34:33 +05:00
|
|
|
let (__tag, __content) = try!(_serde::Deserializer::deserialize_any(
|
2017-03-18 10:33:22 -07:00
|
|
|
__deserializer,
|
2021-01-23 13:38:03 -08:00
|
|
|
_serde::__private::de::TaggedContentVisitor::<__Field>::new(#tag, #expecting)));
|
2022-10-02 19:34:33 +05:00
|
|
|
let __deserializer = _serde::__private::de::ContentDeserializer::<__D::Error>::new(__content);
|
2017-02-02 12:10:07 -08:00
|
|
|
|
2022-10-02 19:34:33 +05:00
|
|
|
match __tag {
|
2017-02-02 12:10:07 -08:00
|
|
|
#(#variant_arms)*
|
|
|
|
}
|
2017-02-20 14:43:51 -08:00
|
|
|
}
|
2017-02-02 12:10:07 -08:00
|
|
|
}
|
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
fn deserialize_adjacently_tagged_enum(
|
|
|
|
params: &Parameters,
|
|
|
|
variants: &[Variant],
|
2017-04-14 15:52:56 -07:00
|
|
|
cattrs: &attr::Container,
|
2017-04-13 12:28:23 -07:00
|
|
|
tag: &str,
|
|
|
|
content: &str,
|
|
|
|
) -> Fragment {
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_type = ¶ms.this_type;
|
|
|
|
let this_value = ¶ms.this_value;
|
2017-12-23 20:13:08 -08:00
|
|
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
|
|
|
split_with_de_lifetime(params);
|
2017-09-09 11:34:08 -07:00
|
|
|
let delife = params.borrowed.de_lifetime();
|
2017-02-19 16:04:50 -08:00
|
|
|
|
2019-01-15 10:35:09 -06:00
|
|
|
let (variants_stmt, variant_visitor) = prepare_enum_variant_enum(variants, cattrs);
|
2017-02-19 16:04:50 -08:00
|
|
|
|
2018-01-09 19:34:35 -08:00
|
|
|
let variant_arms: &Vec<_> = &variants
|
2017-04-13 12:28:23 -07:00
|
|
|
.iter()
|
2017-02-19 16:04:50 -08:00
|
|
|
.enumerate()
|
|
|
|
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
2017-12-23 20:13:08 -08:00
|
|
|
.map(|(i, variant)| {
|
|
|
|
let variant_index = field_i(i);
|
2017-02-19 16:04:50 -08:00
|
|
|
|
2017-12-23 20:13:08 -08:00
|
|
|
let block = Match(deserialize_untagged_variant(
|
|
|
|
params,
|
|
|
|
variant,
|
|
|
|
cattrs,
|
|
|
|
quote!(__deserializer),
|
|
|
|
));
|
2017-02-19 16:04:50 -08:00
|
|
|
|
2017-12-23 20:13:08 -08:00
|
|
|
quote! {
|
|
|
|
__Field::#variant_index => #block
|
|
|
|
}
|
2018-11-21 01:13:17 -08:00
|
|
|
})
|
|
|
|
.collect();
|
2017-02-19 16:04:50 -08:00
|
|
|
|
2017-04-08 22:42:42 -07:00
|
|
|
let expecting = format!("adjacently tagged enum {}", params.type_name());
|
2020-10-23 00:54:13 +05:00
|
|
|
let expecting = cattrs.expecting().unwrap_or(&expecting);
|
2017-04-14 15:52:56 -07:00
|
|
|
let type_name = cattrs.name().deserialize_name();
|
2017-04-27 11:24:09 -07:00
|
|
|
let deny_unknown_fields = cattrs.deny_unknown_fields();
|
|
|
|
|
2017-08-05 23:35:09 -07:00
|
|
|
// If unknown fields are allowed, we pick the visitor that can step over
|
|
|
|
// those. Otherwise we pick the visitor that fails on unknown keys.
|
2017-04-27 11:24:09 -07:00
|
|
|
let field_visitor_ty = if deny_unknown_fields {
|
2021-01-08 19:35:52 -08:00
|
|
|
quote! { _serde::__private::de::TagOrContentFieldVisitor }
|
2017-04-27 11:24:09 -07:00
|
|
|
} else {
|
2021-01-08 19:35:52 -08:00
|
|
|
quote! { _serde::__private::de::TagContentOtherFieldVisitor }
|
2017-04-27 11:24:09 -07:00
|
|
|
};
|
2017-02-19 16:04:50 -08:00
|
|
|
|
|
|
|
let tag_or_content = quote! {
|
2017-04-27 11:24:09 -07:00
|
|
|
#field_visitor_ty {
|
2017-02-19 16:04:50 -08:00
|
|
|
tag: #tag,
|
|
|
|
content: #content,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-09 20:55:55 -07:00
|
|
|
let mut missing_content = quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Err(<__A::Error as _serde::de::Error>::missing_field(#content))
|
2020-05-09 20:55:55 -07:00
|
|
|
};
|
2020-05-09 20:55:18 -07:00
|
|
|
let mut missing_content_fallthrough = quote!();
|
2020-05-09 22:32:13 -07:00
|
|
|
let missing_content_arms = variants
|
2020-05-10 04:58:28 +01:00
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter(|&(_, variant)| !variant.attrs.skip_deserializing())
|
|
|
|
.filter_map(|(i, variant)| {
|
|
|
|
let variant_index = field_i(i);
|
|
|
|
let variant_ident = &variant.ident;
|
2017-02-19 16:04:50 -08:00
|
|
|
|
2020-05-10 04:58:28 +01:00
|
|
|
let arm = match variant.style {
|
|
|
|
Style::Unit => quote! {
|
2022-11-27 16:08:47 -08:00
|
|
|
_serde::__private::Ok(#this_value::#variant_ident)
|
2020-05-10 04:58:28 +01:00
|
|
|
},
|
|
|
|
Style::Newtype if variant.attrs.deserialize_with().is_none() => {
|
|
|
|
let span = variant.original.span();
|
2021-01-08 19:35:52 -08:00
|
|
|
let func = quote_spanned!(span=> _serde::__private::de::missing_field);
|
2020-05-10 04:58:28 +01:00
|
|
|
quote! {
|
2022-11-27 16:08:47 -08:00
|
|
|
#func(#content).map(#this_value::#variant_ident)
|
2020-05-10 04:58:28 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-09 20:55:18 -07:00
|
|
|
_ => {
|
2020-05-09 20:55:55 -07:00
|
|
|
missing_content_fallthrough = quote!(_ => #missing_content);
|
2020-05-09 20:55:18 -07:00
|
|
|
return None;
|
|
|
|
}
|
2020-05-10 04:58:28 +01:00
|
|
|
};
|
2017-12-23 20:13:08 -08:00
|
|
|
Some(quote! {
|
2020-05-10 04:58:28 +01:00
|
|
|
__Field::#variant_index => #arm,
|
2017-12-23 20:13:08 -08:00
|
|
|
})
|
2020-05-09 20:55:55 -07:00
|
|
|
})
|
2020-05-09 22:32:13 -07:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
if !missing_content_arms.is_empty() {
|
2020-05-09 20:55:55 -07:00
|
|
|
missing_content = quote! {
|
|
|
|
match __field {
|
|
|
|
#(#missing_content_arms)*
|
|
|
|
#missing_content_fallthrough
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-02-19 16:04:50 -08:00
|
|
|
|
2017-08-05 23:35:09 -07:00
|
|
|
// Advance the map by one key, returning early in case of error.
|
2017-04-27 11:24:09 -07:00
|
|
|
let next_key = quote! {
|
|
|
|
try!(_serde::de::MapAccess::next_key_seed(&mut __map, #tag_or_content))
|
|
|
|
};
|
|
|
|
|
2017-08-05 23:35:09 -07:00
|
|
|
// 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.
|
2017-04-27 11:24:09 -07:00
|
|
|
let next_relevant_key = if deny_unknown_fields {
|
|
|
|
next_key
|
|
|
|
} else {
|
2017-12-23 20:13:08 -08:00
|
|
|
quote!({
|
2021-01-08 19:35:52 -08:00
|
|
|
let mut __rk : _serde::__private::Option<_serde::__private::de::TagOrContentField> = _serde::__private::None;
|
|
|
|
while let _serde::__private::Some(__k) = #next_key {
|
2017-12-23 20:13:08 -08:00
|
|
|
match __k {
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::de::TagContentOtherField::Other => {
|
2021-12-08 18:44:41 -08:00
|
|
|
let _ = try!(_serde::de::MapAccess::next_value::<_serde::de::IgnoredAny>(&mut __map));
|
2017-12-23 20:13:08 -08:00
|
|
|
continue;
|
|
|
|
},
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::de::TagContentOtherField::Tag => {
|
|
|
|
__rk = _serde::__private::Some(_serde::__private::de::TagOrContentField::Tag);
|
2017-12-23 20:13:08 -08:00
|
|
|
break;
|
|
|
|
}
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::de::TagContentOtherField::Content => {
|
|
|
|
__rk = _serde::__private::Some(_serde::__private::de::TagOrContentField::Content);
|
2017-12-23 20:13:08 -08:00
|
|
|
break;
|
2017-04-27 11:24:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-23 20:13:08 -08:00
|
|
|
|
|
|
|
__rk
|
|
|
|
})
|
2017-04-27 11:24:09 -07:00
|
|
|
};
|
|
|
|
|
2017-08-05 23:35:09 -07:00
|
|
|
// 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.
|
2017-04-27 11:24:09 -07:00
|
|
|
let visit_remaining_keys = quote! {
|
|
|
|
match #next_relevant_key {
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Some(_serde::__private::de::TagOrContentField::Tag) => {
|
|
|
|
_serde::__private::Err(<__A::Error as _serde::de::Error>::duplicate_field(#tag))
|
2017-02-19 16:04:50 -08:00
|
|
|
}
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Some(_serde::__private::de::TagOrContentField::Content) => {
|
|
|
|
_serde::__private::Err(<__A::Error as _serde::de::Error>::duplicate_field(#content))
|
2017-02-19 16:04:50 -08:00
|
|
|
}
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::None => _serde::__private::Ok(__ret),
|
2017-02-19 16:04:50 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-07 21:02:42 -07:00
|
|
|
let finish_content_then_tag = if variant_arms.is_empty() {
|
|
|
|
quote! {
|
|
|
|
match try!(_serde::de::MapAccess::next_value::<__Field>(&mut __map)) {}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quote! {
|
|
|
|
let __ret = try!(match try!(_serde::de::MapAccess::next_value(&mut __map)) {
|
|
|
|
// Deserialize the buffered content now that we know the variant.
|
|
|
|
#(#variant_arms)*
|
|
|
|
});
|
|
|
|
// Visit remaining keys, looking for duplicates.
|
|
|
|
#visit_remaining_keys
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-19 16:04:50 -08:00
|
|
|
quote_block! {
|
|
|
|
#variant_visitor
|
|
|
|
|
|
|
|
#variants_stmt
|
|
|
|
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2017-03-26 22:56:58 -07:00
|
|
|
struct __Seed #de_impl_generics #where_clause {
|
2017-02-19 16:04:50 -08:00
|
|
|
field: __Field,
|
2022-11-27 16:08:47 -08:00
|
|
|
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
2021-01-08 19:35:52 -08:00
|
|
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
2017-02-19 16:04:50 -08:00
|
|
|
}
|
|
|
|
|
2017-09-09 11:34:08 -07:00
|
|
|
impl #de_impl_generics _serde::de::DeserializeSeed<#delife> for __Seed #de_ty_generics #where_clause {
|
2022-11-27 16:08:47 -08:00
|
|
|
type Value = #this_type #ty_generics;
|
2017-02-19 16:04:50 -08:00
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn deserialize<__D>(self, __deserializer: __D) -> _serde::__private::Result<Self::Value, __D::Error>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__D: _serde::Deserializer<#delife>,
|
2017-02-19 16:04:50 -08:00
|
|
|
{
|
|
|
|
match self.field {
|
|
|
|
#(#variant_arms)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2017-03-26 22:56:58 -07:00
|
|
|
struct __Visitor #de_impl_generics #where_clause {
|
2022-11-27 16:08:47 -08:00
|
|
|
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
2021-01-08 19:35:52 -08:00
|
|
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
2017-02-19 16:04:50 -08:00
|
|
|
}
|
|
|
|
|
2017-09-09 11:34:08 -07:00
|
|
|
impl #de_impl_generics _serde::de::Visitor<#delife> for __Visitor #de_ty_generics #where_clause {
|
2022-11-27 16:08:47 -08:00
|
|
|
type Value = #this_type #ty_generics;
|
2017-02-19 16:04:50 -08:00
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn expecting(&self, __formatter: &mut _serde::__private::Formatter) -> _serde::__private::fmt::Result {
|
|
|
|
_serde::__private::Formatter::write_str(__formatter, #expecting)
|
2017-02-19 16:04:50 -08:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_map<__A>(self, mut __map: __A) -> _serde::__private::Result<Self::Value, __A::Error>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__A: _serde::de::MapAccess<#delife>,
|
2017-02-19 16:04:50 -08:00
|
|
|
{
|
2017-04-27 11:24:09 -07:00
|
|
|
// Visit the first relevant key.
|
|
|
|
match #next_relevant_key {
|
2017-02-19 16:04:50 -08:00
|
|
|
// First key is the tag.
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Some(_serde::__private::de::TagOrContentField::Tag) => {
|
2017-02-19 16:04:50 -08:00
|
|
|
// Parse the tag.
|
2017-04-14 11:58:29 -07:00
|
|
|
let __field = try!(_serde::de::MapAccess::next_value(&mut __map));
|
2017-02-19 16:04:50 -08:00
|
|
|
// Visit the second key.
|
2017-04-27 11:24:09 -07:00
|
|
|
match #next_relevant_key {
|
2017-02-19 16:04:50 -08:00
|
|
|
// Second key is a duplicate of the tag.
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Some(_serde::__private::de::TagOrContentField::Tag) => {
|
|
|
|
_serde::__private::Err(<__A::Error as _serde::de::Error>::duplicate_field(#tag))
|
2017-02-19 16:04:50 -08:00
|
|
|
}
|
|
|
|
// Second key is the content.
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Some(_serde::__private::de::TagOrContentField::Content) => {
|
2017-04-14 11:58:29 -07:00
|
|
|
let __ret = try!(_serde::de::MapAccess::next_value_seed(&mut __map,
|
2017-03-26 22:56:58 -07:00
|
|
|
__Seed {
|
|
|
|
field: __field,
|
2021-01-08 19:35:52 -08:00
|
|
|
marker: _serde::__private::PhantomData,
|
|
|
|
lifetime: _serde::__private::PhantomData,
|
2017-03-26 22:56:58 -07:00
|
|
|
}));
|
2017-04-27 11:24:09 -07:00
|
|
|
// Visit remaining keys, looking for duplicates.
|
|
|
|
#visit_remaining_keys
|
2017-02-19 16:04:50 -08:00
|
|
|
}
|
|
|
|
// There is no second key; might be okay if the we have a unit variant.
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::None => #missing_content
|
2017-02-19 16:04:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// First key is the content.
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Some(_serde::__private::de::TagOrContentField::Content) => {
|
2017-02-19 16:04:50 -08:00
|
|
|
// Buffer up the content.
|
2021-01-08 19:35:52 -08:00
|
|
|
let __content = try!(_serde::de::MapAccess::next_value::<_serde::__private::de::Content>(&mut __map));
|
2017-02-19 16:04:50 -08:00
|
|
|
// Visit the second key.
|
2017-04-27 11:24:09 -07:00
|
|
|
match #next_relevant_key {
|
2017-02-19 16:04:50 -08:00
|
|
|
// Second key is the tag.
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Some(_serde::__private::de::TagOrContentField::Tag) => {
|
|
|
|
let __deserializer = _serde::__private::de::ContentDeserializer::<__A::Error>::new(__content);
|
2018-05-07 21:02:42 -07:00
|
|
|
#finish_content_then_tag
|
2017-02-19 16:04:50 -08:00
|
|
|
}
|
|
|
|
// Second key is a duplicate of the content.
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Some(_serde::__private::de::TagOrContentField::Content) => {
|
|
|
|
_serde::__private::Err(<__A::Error as _serde::de::Error>::duplicate_field(#content))
|
2017-02-19 16:04:50 -08:00
|
|
|
}
|
|
|
|
// There is no second key.
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::None => {
|
|
|
|
_serde::__private::Err(<__A::Error as _serde::de::Error>::missing_field(#tag))
|
2017-02-19 16:04:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// There is no first key.
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::None => {
|
|
|
|
_serde::__private::Err(<__A::Error as _serde::de::Error>::missing_field(#tag))
|
2017-02-19 16:04:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_seq<__A>(self, mut __seq: __A) -> _serde::__private::Result<Self::Value, __A::Error>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__A: _serde::de::SeqAccess<#delife>,
|
2017-02-19 16:04:50 -08:00
|
|
|
{
|
|
|
|
// Visit the first element - the tag.
|
2017-04-14 11:58:29 -07:00
|
|
|
match try!(_serde::de::SeqAccess::next_element(&mut __seq)) {
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Some(__field) => {
|
2017-02-19 16:04:50 -08:00
|
|
|
// Visit the second element - the content.
|
2018-12-10 22:31:39 -08:00
|
|
|
match try!(_serde::de::SeqAccess::next_element_seed(
|
|
|
|
&mut __seq,
|
|
|
|
__Seed {
|
|
|
|
field: __field,
|
2021-01-08 19:35:52 -08:00
|
|
|
marker: _serde::__private::PhantomData,
|
|
|
|
lifetime: _serde::__private::PhantomData,
|
2018-12-10 22:31:39 -08:00
|
|
|
},
|
|
|
|
)) {
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Some(__ret) => _serde::__private::Ok(__ret),
|
2017-02-19 16:04:50 -08:00
|
|
|
// There is no second element.
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::None => {
|
|
|
|
_serde::__private::Err(_serde::de::Error::invalid_length(1, &self))
|
2017-02-19 16:04:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// There is no first element.
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::None => {
|
|
|
|
_serde::__private::Err(_serde::de::Error::invalid_length(0, &self))
|
2017-02-19 16:04:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2017-02-19 16:04:50 -08:00
|
|
|
const FIELDS: &'static [&'static str] = &[#tag, #content];
|
2018-12-10 22:31:39 -08:00
|
|
|
_serde::Deserializer::deserialize_struct(
|
|
|
|
__deserializer,
|
|
|
|
#type_name,
|
|
|
|
FIELDS,
|
2017-03-26 22:56:58 -07:00
|
|
|
__Visitor {
|
2022-11-27 16:08:47 -08:00
|
|
|
marker: _serde::__private::PhantomData::<#this_type #ty_generics>,
|
2021-01-08 19:35:52 -08:00
|
|
|
lifetime: _serde::__private::PhantomData,
|
2018-12-10 22:31:39 -08:00
|
|
|
},
|
|
|
|
)
|
2017-02-19 16:04:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
fn deserialize_untagged_enum(
|
|
|
|
params: &Parameters,
|
|
|
|
variants: &[Variant],
|
2017-04-14 15:52:56 -07:00
|
|
|
cattrs: &attr::Container,
|
2017-04-13 12:28:23 -07:00
|
|
|
) -> Fragment {
|
2023-06-07 21:33:11 -07:00
|
|
|
let first_attempt = None;
|
|
|
|
deserialize_untagged_enum_after(params, variants, cattrs, first_attempt)
|
2023-06-07 20:58:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_untagged_enum_after(
|
|
|
|
params: &Parameters,
|
|
|
|
variants: &[Variant],
|
|
|
|
cattrs: &attr::Container,
|
2023-06-07 21:33:11 -07:00
|
|
|
first_attempt: Option<Expr>,
|
2023-06-07 20:58:59 -07:00
|
|
|
) -> Fragment {
|
2017-04-13 12:28:23 -07:00
|
|
|
let attempts = variants
|
|
|
|
.iter()
|
2017-02-02 12:10:07 -08:00
|
|
|
.filter(|variant| !variant.attrs.skip_deserializing())
|
2017-12-23 20:13:08 -08:00
|
|
|
.map(|variant| {
|
|
|
|
Expr(deserialize_untagged_variant(
|
2017-04-02 21:42:07 -07:00
|
|
|
params,
|
2017-02-02 12:10:07 -08:00
|
|
|
variant,
|
2017-04-14 15:52:56 -07:00
|
|
|
cattrs,
|
2023-06-07 21:33:11 -07:00
|
|
|
quote!(__deserializer),
|
2017-02-20 14:43:51 -08:00
|
|
|
))
|
2017-12-23 20:13:08 -08:00
|
|
|
});
|
2023-06-07 21:33:11 -07:00
|
|
|
let attempts = first_attempt.into_iter().chain(attempts);
|
2017-02-02 12:10:07 -08:00
|
|
|
// 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.
|
2017-12-23 20:13:08 -08:00
|
|
|
let fallthrough_msg = format!(
|
|
|
|
"data did not match any variant of untagged enum {}",
|
|
|
|
params.type_name()
|
|
|
|
);
|
2020-10-23 00:54:13 +05:00
|
|
|
let fallthrough_msg = cattrs.expecting().unwrap_or(&fallthrough_msg);
|
2017-02-02 12:10:07 -08:00
|
|
|
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_block! {
|
2021-01-08 19:35:52 -08:00
|
|
|
let __content = try!(<_serde::__private::de::Content as _serde::Deserialize>::deserialize(__deserializer));
|
2023-06-07 21:33:11 -07:00
|
|
|
let __deserializer = _serde::__private::de::ContentRefDeserializer::<__D::Error>::new(&__content);
|
2017-02-02 12:10:07 -08:00
|
|
|
|
|
|
|
#(
|
2021-01-08 19:35:52 -08:00
|
|
|
if let _serde::__private::Ok(__ok) = #attempts {
|
|
|
|
return _serde::__private::Ok(__ok);
|
2017-02-02 12:10:07 -08:00
|
|
|
}
|
|
|
|
)*
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Err(_serde::de::Error::custom(#fallthrough_msg))
|
2017-02-20 14:43:51 -08:00
|
|
|
}
|
2017-02-02 12:10:07 -08:00
|
|
|
}
|
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
fn deserialize_externally_tagged_variant(
|
|
|
|
params: &Parameters,
|
|
|
|
variant: &Variant,
|
2017-04-14 15:52:56 -07:00
|
|
|
cattrs: &attr::Container,
|
2017-04-13 12:28:23 -07:00
|
|
|
) -> Fragment {
|
2017-08-14 14:39:29 -07:00
|
|
|
if let Some(path) = variant.attrs.deserialize_with() {
|
2018-04-12 22:58:24 -07:00
|
|
|
let (wrapper, wrapper_ty, unwrap_fn) = wrap_deserialize_variant_with(params, variant, path);
|
2017-08-14 14:39:29 -07:00
|
|
|
return quote_block! {
|
|
|
|
#wrapper
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Result::map(
|
2017-08-14 14:39:29 -07:00
|
|
|
_serde::de::VariantAccess::newtype_variant::<#wrapper_ty>(__variant), #unwrap_fn)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-20 19:34:52 -07:00
|
|
|
let variant_ident = &variant.ident;
|
2015-03-15 17:47:25 -07:00
|
|
|
|
2016-06-14 23:37:20 -07:00
|
|
|
match variant.style {
|
2016-06-21 19:12:08 -07:00
|
|
|
Style::Unit => {
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_value = ¶ms.this_value;
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_block! {
|
2017-04-15 12:35:04 -07:00
|
|
|
try!(_serde::de::VariantAccess::unit_variant(__variant));
|
2022-11-27 16:08:47 -08:00
|
|
|
_serde::__private::Ok(#this_value::#variant_ident)
|
2017-02-20 14:43:51 -08:00
|
|
|
}
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
2019-09-07 20:32:26 -07:00
|
|
|
Style::Newtype => deserialize_externally_tagged_newtype_variant(
|
|
|
|
variant_ident,
|
|
|
|
params,
|
|
|
|
&variant.fields[0],
|
|
|
|
cattrs,
|
|
|
|
),
|
2023-05-07 01:28:47 +05:00
|
|
|
Style::Tuple => deserialize_tuple(
|
|
|
|
params,
|
|
|
|
&variant.fields,
|
|
|
|
cattrs,
|
|
|
|
TupleForm::ExternallyTagged(variant_ident),
|
|
|
|
),
|
2017-12-23 20:13:08 -08:00
|
|
|
Style::Struct => deserialize_struct(
|
|
|
|
params,
|
|
|
|
&variant.fields,
|
|
|
|
cattrs,
|
2023-05-07 01:12:49 +05:00
|
|
|
StructForm::ExternallyTagged(variant_ident),
|
2017-12-23 20:13:08 -08:00
|
|
|
),
|
2017-02-02 12:10:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 12:46:13 -07:00
|
|
|
// Generates significant part of the visit_seq and visit_map bodies of visitors
|
|
|
|
// for the variants of internally tagged enum.
|
2017-04-13 12:28:23 -07:00
|
|
|
fn deserialize_internally_tagged_variant(
|
|
|
|
params: &Parameters,
|
|
|
|
variant: &Variant,
|
2017-04-14 15:52:56 -07:00
|
|
|
cattrs: &attr::Container,
|
2018-05-20 19:34:52 -07:00
|
|
|
deserializer: TokenStream,
|
2017-04-13 12:28:23 -07:00
|
|
|
) -> Fragment {
|
2017-08-14 14:39:29 -07:00
|
|
|
if variant.attrs.deserialize_with().is_some() {
|
|
|
|
return deserialize_untagged_variant(params, variant, cattrs, deserializer);
|
|
|
|
}
|
|
|
|
|
2018-05-20 19:34:52 -07:00
|
|
|
let variant_ident = &variant.ident;
|
2017-02-02 12:10:07 -08:00
|
|
|
|
2019-09-07 20:32:26 -07:00
|
|
|
match effective_style(variant) {
|
2017-02-02 12:10:07 -08:00
|
|
|
Style::Unit => {
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_value = ¶ms.this_value;
|
2017-04-08 22:42:42 -07:00
|
|
|
let type_name = params.type_name();
|
2018-05-20 19:34:52 -07:00
|
|
|
let variant_name = variant.ident.to_string();
|
2019-09-07 20:32:26 -07:00
|
|
|
let default = variant.fields.get(0).map(|field| {
|
|
|
|
let default = Expr(expr_is_missing(field, cattrs));
|
|
|
|
quote!((#default))
|
|
|
|
});
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_block! {
|
2021-01-08 19:35:52 -08:00
|
|
|
try!(_serde::Deserializer::deserialize_any(#deserializer, _serde::__private::de::InternallyTaggedUnitVisitor::new(#type_name, #variant_name)));
|
2022-11-27 16:08:47 -08:00
|
|
|
_serde::__private::Ok(#this_value::#variant_ident #default)
|
2017-02-20 14:43:51 -08:00
|
|
|
}
|
2017-02-02 12:10:07 -08:00
|
|
|
}
|
2017-12-23 20:13:08 -08:00
|
|
|
Style::Newtype => deserialize_untagged_newtype_variant(
|
|
|
|
variant_ident,
|
|
|
|
params,
|
|
|
|
&variant.fields[0],
|
2018-01-09 19:34:35 -08:00
|
|
|
&deserializer,
|
2017-12-23 20:13:08 -08:00
|
|
|
),
|
|
|
|
Style::Struct => deserialize_struct(
|
|
|
|
params,
|
|
|
|
&variant.fields,
|
|
|
|
cattrs,
|
2023-05-07 01:12:49 +05:00
|
|
|
StructForm::InternallyTagged(variant_ident, deserializer),
|
2017-12-23 20:13:08 -08:00
|
|
|
),
|
2017-04-14 15:42:27 -07:00
|
|
|
Style::Tuple => unreachable!("checked in serde_derive_internals"),
|
2017-02-02 12:10:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
fn deserialize_untagged_variant(
|
|
|
|
params: &Parameters,
|
|
|
|
variant: &Variant,
|
2017-04-14 15:52:56 -07:00
|
|
|
cattrs: &attr::Container,
|
2018-05-20 19:34:52 -07:00
|
|
|
deserializer: TokenStream,
|
2017-04-13 12:28:23 -07:00
|
|
|
) -> Fragment {
|
2017-08-14 14:39:29 -07:00
|
|
|
if let Some(path) = variant.attrs.deserialize_with() {
|
2021-02-28 17:43:54 +05:00
|
|
|
let unwrap_fn = unwrap_to_variant_closure(params, variant, false);
|
2017-08-14 14:39:29 -07:00
|
|
|
return quote_block! {
|
2021-02-27 18:52:32 +05:00
|
|
|
_serde::__private::Result::map(#path(#deserializer), #unwrap_fn)
|
2017-08-14 14:39:29 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-20 19:34:52 -07:00
|
|
|
let variant_ident = &variant.ident;
|
2017-02-02 12:10:07 -08:00
|
|
|
|
2019-09-07 20:32:26 -07:00
|
|
|
match effective_style(variant) {
|
2017-02-02 12:10:07 -08:00
|
|
|
Style::Unit => {
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_value = ¶ms.this_value;
|
2017-04-08 22:42:42 -07:00
|
|
|
let type_name = params.type_name();
|
2018-05-20 19:34:52 -07:00
|
|
|
let variant_name = variant.ident.to_string();
|
2019-09-07 20:32:26 -07:00
|
|
|
let default = variant.fields.get(0).map(|field| {
|
|
|
|
let default = Expr(expr_is_missing(field, cattrs));
|
|
|
|
quote!((#default))
|
|
|
|
});
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_expr! {
|
2018-04-21 13:25:24 -07:00
|
|
|
match _serde::Deserializer::deserialize_any(
|
|
|
|
#deserializer,
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::de::UntaggedUnitVisitor::new(#type_name, #variant_name)
|
2018-04-21 13:25:24 -07:00
|
|
|
) {
|
2022-11-27 16:08:47 -08:00
|
|
|
_serde::__private::Ok(()) => _serde::__private::Ok(#this_value::#variant_ident #default),
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Err(__err) => _serde::__private::Err(__err),
|
2018-04-21 13:25:24 -07:00
|
|
|
}
|
2017-02-02 12:10:07 -08:00
|
|
|
}
|
|
|
|
}
|
2017-12-23 20:13:08 -08:00
|
|
|
Style::Newtype => deserialize_untagged_newtype_variant(
|
|
|
|
variant_ident,
|
|
|
|
params,
|
|
|
|
&variant.fields[0],
|
2018-01-09 19:34:35 -08:00
|
|
|
&deserializer,
|
2017-12-23 20:13:08 -08:00
|
|
|
),
|
|
|
|
Style::Tuple => deserialize_tuple(
|
|
|
|
params,
|
|
|
|
&variant.fields,
|
|
|
|
cattrs,
|
2023-05-07 01:28:47 +05:00
|
|
|
TupleForm::Untagged(variant_ident, deserializer),
|
2017-12-23 20:13:08 -08:00
|
|
|
),
|
|
|
|
Style::Struct => deserialize_struct(
|
|
|
|
params,
|
|
|
|
&variant.fields,
|
|
|
|
cattrs,
|
2023-05-07 01:12:49 +05:00
|
|
|
StructForm::Untagged(variant_ident, deserializer),
|
2017-12-23 20:13:08 -08:00
|
|
|
),
|
2015-03-14 13:09:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
fn deserialize_externally_tagged_newtype_variant(
|
2018-05-20 19:34:52 -07:00
|
|
|
variant_ident: &syn::Ident,
|
2017-04-13 12:28:23 -07:00
|
|
|
params: &Parameters,
|
|
|
|
field: &Field,
|
2019-09-07 20:32:26 -07:00
|
|
|
cattrs: &attr::Container,
|
2017-04-13 12:28:23 -07:00
|
|
|
) -> Fragment {
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_value = ¶ms.this_value;
|
2019-09-07 20:32:26 -07:00
|
|
|
|
|
|
|
if field.attrs.skip_deserializing() {
|
|
|
|
let default = Expr(expr_is_missing(field, cattrs));
|
|
|
|
return quote_block! {
|
|
|
|
try!(_serde::de::VariantAccess::unit_variant(__variant));
|
2022-11-27 16:08:47 -08:00
|
|
|
_serde::__private::Ok(#this_value::#variant_ident(#default))
|
2019-09-07 20:32:26 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-02 12:10:07 -08:00
|
|
|
match field.attrs.deserialize_with() {
|
2016-05-15 15:54:20 -07:00
|
|
|
None => {
|
2018-05-05 00:45:30 -07:00
|
|
|
let field_ty = field.ty;
|
2018-11-17 18:13:36 +02:00
|
|
|
let span = field.original.span();
|
2018-12-10 22:09:02 -08:00
|
|
|
let func =
|
|
|
|
quote_spanned!(span=> _serde::de::VariantAccess::newtype_variant::<#field_ty>);
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_expr! {
|
2022-11-27 16:08:47 -08:00
|
|
|
_serde::__private::Result::map(#func(__variant), #this_value::#variant_ident)
|
2017-01-14 13:35:52 -08:00
|
|
|
}
|
2016-05-15 15:54:20 -07:00
|
|
|
}
|
|
|
|
Some(path) => {
|
2017-08-14 14:39:29 -07:00
|
|
|
let (wrapper, wrapper_ty) = wrap_deserialize_field_with(params, field.ty, path);
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_block! {
|
2016-09-12 00:04:21 -07:00
|
|
|
#wrapper
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Result::map(
|
2017-04-15 12:35:04 -07:00
|
|
|
_serde::de::VariantAccess::newtype_variant::<#wrapper_ty>(__variant),
|
2022-11-27 16:08:47 -08:00
|
|
|
|__wrapper| #this_value::#variant_ident(__wrapper.value))
|
2017-02-20 14:43:51 -08:00
|
|
|
}
|
2017-02-02 12:10:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
fn deserialize_untagged_newtype_variant(
|
2018-05-20 19:34:52 -07:00
|
|
|
variant_ident: &syn::Ident,
|
2017-04-13 12:28:23 -07:00
|
|
|
params: &Parameters,
|
|
|
|
field: &Field,
|
2018-05-20 19:34:52 -07:00
|
|
|
deserializer: &TokenStream,
|
2017-04-13 12:28:23 -07:00
|
|
|
) -> Fragment {
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_value = ¶ms.this_value;
|
2018-05-05 00:25:27 -07:00
|
|
|
let field_ty = field.ty;
|
2017-02-02 12:10:07 -08:00
|
|
|
match field.attrs.deserialize_with() {
|
|
|
|
None => {
|
2018-11-17 18:13:36 +02:00
|
|
|
let span = field.original.span();
|
|
|
|
let func = quote_spanned!(span=> <#field_ty as _serde::Deserialize>::deserialize);
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_expr! {
|
2022-11-27 16:08:47 -08:00
|
|
|
_serde::__private::Result::map(#func(#deserializer), #this_value::#variant_ident)
|
2017-02-20 14:43:51 -08:00
|
|
|
}
|
2017-02-02 12:10:07 -08:00
|
|
|
}
|
|
|
|
Some(path) => {
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_block! {
|
2021-01-08 19:35:52 -08:00
|
|
|
let __value: _serde::__private::Result<#field_ty, _> = #path(#deserializer);
|
2022-11-27 16:08:47 -08:00
|
|
|
_serde::__private::Result::map(__value, #this_value::#variant_ident)
|
2017-02-20 14:43:51 -08:00
|
|
|
}
|
2016-05-15 15:54:20 -07:00
|
|
|
}
|
2016-09-28 09:18:18 -07:00
|
|
|
}
|
2016-05-15 15:54:20 -07:00
|
|
|
}
|
|
|
|
|
2017-04-14 16:09:00 -07:00
|
|
|
fn deserialize_generated_identifier(
|
2019-01-15 10:35:09 -06:00
|
|
|
fields: &[(String, Ident, Vec<String>)],
|
2017-04-14 15:52:56 -07:00
|
|
|
cattrs: &attr::Container,
|
2018-04-12 22:58:24 -07:00
|
|
|
is_variant: bool,
|
2018-09-11 23:00:08 -07:00
|
|
|
other_idx: Option<usize>,
|
2017-04-13 12:28:23 -07:00
|
|
|
) -> Fragment {
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_value = quote!(__Field);
|
2019-09-07 23:01:16 -07:00
|
|
|
let field_idents: &Vec<_> = &fields.iter().map(|(_, ident, _)| ident).collect();
|
2015-03-15 18:37:26 -07:00
|
|
|
|
2018-05-06 20:39:45 -07:00
|
|
|
let (ignore_variant, fallthrough) = if !is_variant && cattrs.has_flatten() {
|
2021-01-08 19:35:52 -08:00
|
|
|
let ignore_variant = quote!(__other(_serde::__private::de::Content<'de>),);
|
|
|
|
let fallthrough = quote!(_serde::__private::Ok(__Field::__other(__value)));
|
2021-01-23 20:21:39 -08:00
|
|
|
(Some(ignore_variant), Some(fallthrough))
|
2018-09-11 17:12:37 +00:00
|
|
|
} else if let Some(other_idx) = other_idx {
|
|
|
|
let ignore_variant = fields[other_idx].1.clone();
|
2021-01-08 19:35:52 -08:00
|
|
|
let fallthrough = quote!(_serde::__private::Ok(__Field::#ignore_variant));
|
2021-01-23 20:21:39 -08:00
|
|
|
(None, Some(fallthrough))
|
2018-03-15 16:17:54 +01:00
|
|
|
} else if is_variant || cattrs.deny_unknown_fields() {
|
2018-03-18 21:07:08 +01:00
|
|
|
(None, None)
|
2017-04-14 16:09:00 -07:00
|
|
|
} else {
|
|
|
|
let ignore_variant = quote!(__ignore,);
|
2021-01-08 19:35:52 -08:00
|
|
|
let fallthrough = quote!(_serde::__private::Ok(__Field::__ignore));
|
2021-01-23 20:21:39 -08:00
|
|
|
(Some(ignore_variant), Some(fallthrough))
|
2017-04-14 16:09:00 -07:00
|
|
|
};
|
|
|
|
|
2017-12-23 20:13:08 -08:00
|
|
|
let visitor_impl = Stmts(deserialize_identifier(
|
2022-11-27 16:08:47 -08:00
|
|
|
&this_value,
|
2018-01-09 19:34:35 -08:00
|
|
|
fields,
|
2017-12-23 20:13:08 -08:00
|
|
|
is_variant,
|
|
|
|
fallthrough,
|
2021-01-23 20:21:39 -08:00
|
|
|
None,
|
2018-05-06 20:39:45 -07:00
|
|
|
!is_variant && cattrs.has_flatten(),
|
2020-10-23 00:54:13 +05:00
|
|
|
None,
|
2017-12-23 20:13:08 -08:00
|
|
|
));
|
2017-04-14 16:09:00 -07:00
|
|
|
|
2018-05-06 20:39:45 -07:00
|
|
|
let lifetime = if !is_variant && cattrs.has_flatten() {
|
2018-03-18 21:07:08 +01:00
|
|
|
Some(quote!(<'de>))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2017-04-14 16:09:00 -07:00
|
|
|
quote_block! {
|
|
|
|
#[allow(non_camel_case_types)]
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2018-03-18 21:07:08 +01:00
|
|
|
enum __Field #lifetime {
|
2017-04-14 16:09:00 -07:00
|
|
|
#(#field_idents,)*
|
|
|
|
#ignore_variant
|
|
|
|
}
|
|
|
|
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2017-04-14 16:09:00 -07:00
|
|
|
struct __FieldVisitor;
|
|
|
|
|
|
|
|
impl<'de> _serde::de::Visitor<'de> for __FieldVisitor {
|
2018-03-18 21:07:08 +01:00
|
|
|
type Value = __Field #lifetime;
|
2017-04-14 16:09:00 -07:00
|
|
|
|
|
|
|
#visitor_impl
|
|
|
|
}
|
|
|
|
|
2018-03-18 21:07:08 +01:00
|
|
|
impl<'de> _serde::Deserialize<'de> for __Field #lifetime {
|
2017-04-14 16:09:00 -07:00
|
|
|
#[inline]
|
2021-01-08 19:35:52 -08:00
|
|
|
fn deserialize<__D>(__deserializer: __D) -> _serde::__private::Result<Self, __D::Error>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__D: _serde::Deserializer<'de>,
|
2017-04-14 16:09:00 -07:00
|
|
|
{
|
|
|
|
_serde::Deserializer::deserialize_identifier(__deserializer, __FieldVisitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-23 19:45:15 -08:00
|
|
|
// Generates `Deserialize::deserialize` body for an enum with
|
|
|
|
// `serde(field_identifier)` or `serde(variant_identifier)` attribute.
|
2017-04-14 16:09:00 -07:00
|
|
|
fn deserialize_custom_identifier(
|
|
|
|
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!(),
|
|
|
|
};
|
|
|
|
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_type = params.this_type.to_token_stream();
|
|
|
|
let this_value = params.this_value.to_token_stream();
|
2017-04-14 16:09:00 -07:00
|
|
|
|
2021-01-23 20:21:39 -08:00
|
|
|
let (ordinary, fallthrough, fallthrough_borrowed) = if let Some(last) = variants.last() {
|
2018-05-20 19:34:52 -07:00
|
|
|
let last_ident = &last.ident;
|
2017-04-14 16:09:00 -07:00
|
|
|
if last.attrs.other() {
|
2021-01-23 19:45:15 -08:00
|
|
|
// Process `serde(other)` attribute. It would always be found on the
|
|
|
|
// last variant (checked in `check_identifier`), so all preceding
|
|
|
|
// are ordinary variants.
|
2017-04-14 16:09:00 -07:00
|
|
|
let ordinary = &variants[..variants.len() - 1];
|
2022-11-27 16:08:47 -08:00
|
|
|
let fallthrough = quote!(_serde::__private::Ok(#this_value::#last_ident));
|
2021-01-23 20:21:39 -08:00
|
|
|
(ordinary, Some(fallthrough), None)
|
2017-04-14 16:09:00 -07:00
|
|
|
} else if let Style::Newtype = last.style {
|
|
|
|
let ordinary = &variants[..variants.len() - 1];
|
2021-01-23 20:00:07 -08:00
|
|
|
let fallthrough = |value| {
|
2021-01-23 12:29:20 -08:00
|
|
|
quote! {
|
|
|
|
_serde::__private::Result::map(
|
|
|
|
_serde::Deserialize::deserialize(
|
2021-01-23 20:00:07 -08:00
|
|
|
_serde::__private::de::IdentifierDeserializer::from(#value)
|
2021-01-23 12:29:20 -08:00
|
|
|
),
|
2022-11-27 16:08:47 -08:00
|
|
|
#this_value::#last_ident)
|
2021-01-23 12:29:20 -08:00
|
|
|
}
|
2017-04-14 16:09:00 -07:00
|
|
|
};
|
2020-10-23 16:12:06 +05:00
|
|
|
(
|
|
|
|
ordinary,
|
2021-01-23 20:21:39 -08:00
|
|
|
Some(fallthrough(quote!(__value))),
|
|
|
|
Some(fallthrough(quote!(_serde::__private::de::Borrowed(
|
|
|
|
__value
|
|
|
|
)))),
|
2020-10-23 16:12:06 +05:00
|
|
|
)
|
2017-04-14 16:09:00 -07:00
|
|
|
} else {
|
2021-01-23 20:21:39 -08:00
|
|
|
(variants, None, None)
|
2017-04-14 16:09:00 -07:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-23 20:21:39 -08:00
|
|
|
(variants, None, None)
|
2017-04-14 16:09:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
let names_idents: Vec<_> = ordinary
|
|
|
|
.iter()
|
2018-05-22 21:06:32 -07:00
|
|
|
.map(|variant| {
|
|
|
|
(
|
|
|
|
variant.attrs.name().deserialize_name(),
|
|
|
|
variant.ident.clone(),
|
2019-01-15 10:35:09 -06:00
|
|
|
variant.attrs.aliases(),
|
2018-05-22 21:06:32 -07:00
|
|
|
)
|
2018-11-21 01:13:17 -08:00
|
|
|
})
|
|
|
|
.collect();
|
2017-04-14 16:09:00 -07:00
|
|
|
|
2019-09-07 23:01:16 -07:00
|
|
|
let names = names_idents.iter().map(|(name, _, _)| name);
|
2017-04-14 16:09:00 -07:00
|
|
|
|
|
|
|
let names_const = if fallthrough.is_some() {
|
2016-04-10 19:54:54 -07:00
|
|
|
None
|
2017-04-14 16:09:00 -07:00
|
|
|
} else if is_variant {
|
|
|
|
let variants = quote! {
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2017-04-14 16:09:00 -07:00
|
|
|
const VARIANTS: &'static [&'static str] = &[ #(#names),* ];
|
|
|
|
};
|
|
|
|
Some(variants)
|
2016-04-10 19:54:54 -07:00
|
|
|
} else {
|
2017-04-14 16:09:00 -07:00
|
|
|
let fields = quote! {
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2017-04-14 16:09:00 -07:00
|
|
|
const FIELDS: &'static [&'static str] = &[ #(#names),* ];
|
|
|
|
};
|
|
|
|
Some(fields)
|
|
|
|
};
|
|
|
|
|
2017-12-23 20:13:08 -08:00
|
|
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
|
|
|
split_with_de_lifetime(params);
|
2017-09-09 11:34:08 -07:00
|
|
|
let delife = params.borrowed.de_lifetime();
|
2017-12-23 20:13:08 -08:00
|
|
|
let visitor_impl = Stmts(deserialize_identifier(
|
2022-11-27 16:08:47 -08:00
|
|
|
&this_value,
|
2017-12-23 20:13:08 -08:00
|
|
|
&names_idents,
|
|
|
|
is_variant,
|
|
|
|
fallthrough,
|
2021-01-23 20:21:39 -08:00
|
|
|
fallthrough_borrowed,
|
2018-03-14 14:15:45 +01:00
|
|
|
false,
|
2020-10-23 00:54:13 +05:00
|
|
|
cattrs.expecting(),
|
2017-12-23 20:13:08 -08:00
|
|
|
));
|
2017-04-14 16:09:00 -07:00
|
|
|
|
|
|
|
quote_block! {
|
|
|
|
#names_const
|
|
|
|
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2017-04-14 16:09:00 -07:00
|
|
|
struct __FieldVisitor #de_impl_generics #where_clause {
|
2022-11-27 16:08:47 -08:00
|
|
|
marker: _serde::__private::PhantomData<#this_type #ty_generics>,
|
2021-01-08 19:35:52 -08:00
|
|
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
2017-04-14 16:09:00 -07:00
|
|
|
}
|
|
|
|
|
2017-09-09 11:34:08 -07:00
|
|
|
impl #de_impl_generics _serde::de::Visitor<#delife> for __FieldVisitor #de_ty_generics #where_clause {
|
2022-11-27 16:08:47 -08:00
|
|
|
type Value = #this_type #ty_generics;
|
2017-04-14 16:09:00 -07:00
|
|
|
|
|
|
|
#visitor_impl
|
|
|
|
}
|
|
|
|
|
|
|
|
let __visitor = __FieldVisitor {
|
2022-11-27 16:08:47 -08:00
|
|
|
marker: _serde::__private::PhantomData::<#this_type #ty_generics>,
|
2021-01-08 19:35:52 -08:00
|
|
|
lifetime: _serde::__private::PhantomData,
|
2017-04-14 16:09:00 -07:00
|
|
|
};
|
|
|
|
_serde::Deserializer::deserialize_identifier(__deserializer, __visitor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deserialize_identifier(
|
2022-11-27 16:08:47 -08:00
|
|
|
this_value: &TokenStream,
|
2019-01-15 10:35:09 -06:00
|
|
|
fields: &[(String, Ident, Vec<String>)],
|
2017-04-14 16:09:00 -07:00
|
|
|
is_variant: bool,
|
2021-01-23 20:21:39 -08:00
|
|
|
fallthrough: Option<TokenStream>,
|
|
|
|
fallthrough_borrowed: Option<TokenStream>,
|
2018-04-12 22:58:24 -07:00
|
|
|
collect_other_fields: bool,
|
2020-10-23 00:54:13 +05:00
|
|
|
expecting: Option<&str>,
|
2017-04-14 16:09:00 -07:00
|
|
|
) -> Fragment {
|
2019-01-15 10:35:09 -06:00
|
|
|
let mut flat_fields = Vec::new();
|
2019-09-07 23:01:16 -07:00
|
|
|
for (_, ident, aliases) in fields {
|
Resolve semicolon_if_nothing_returned clippy lints
error: consider adding a `;` to the last statement for consistent formatting
--> serde_derive/src/internals/attr.rs:559:25
|
559 | serde_path.set(&m.path, path)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add a `;` here: `serde_path.set(&m.path, path);`
|
note: the lint level is defined here
--> serde_derive/src/lib.rs:18:22
|
18 | #![deny(clippy::all, clippy::pedantic)]
| ^^^^^^^^^^^^^^^^
= note: `#[deny(clippy::semicolon_if_nothing_returned)]` implied by `#[deny(clippy::pedantic)]`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned
error: consider adding a `;` to the last statement for consistent formatting
--> serde_derive/src/internals/attr.rs:1612:9
|
1612 | cx.error_spanned_by(lit, format!("failed to parse path: {:?}", string.value()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add a `;` here: `cx.error_spanned_by(lit, format!("failed to parse path: {:?}", string.value()));`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned
error: consider adding a `;` to the last statement for consistent formatting
--> serde_derive/src/internals/attr.rs:1623:9
|
1623 | cx.error_spanned_by(lit, format!("failed to parse path: {:?}", string.value()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add a `;` here: `cx.error_spanned_by(lit, format!("failed to parse path: {:?}", string.value()));`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned
error: consider adding a `;` to the last statement for consistent formatting
--> serde_derive/src/internals/attr.rs:1649:9
|
1649 | / cx.error_spanned_by(
1650 | | lit,
1651 | | format!("failed to parse type: {} = {:?}", attr_name, string.value()),
1652 | | )
| |_________^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned
help: add a `;` here
|
1649 | cx.error_spanned_by(
1650 | lit,
1651 | format!("failed to parse type: {} = {:?}", attr_name, string.value()),
1652 | );
|
error: consider adding a `;` to the last statement for consistent formatting
--> serde_derive/src/internals/check.rs:260:9
|
260 | / cx.error_spanned_by(
261 | | cont.original,
262 | | format!("variant field name `{}` conflicts with internal tag", tag),
263 | | )
| |_________^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned
help: add a `;` here
|
260 | cx.error_spanned_by(
261 | cont.original,
262 | format!("variant field name `{}` conflicts with internal tag", tag),
263 | );
|
error: consider adding a `;` to the last statement for consistent formatting
--> serde_derive/src/de.rs:2090:9
|
2090 | flat_fields.extend(aliases.iter().map(|alias| (alias, ident)))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add a `;` here: `flat_fields.extend(aliases.iter().map(|alias| (alias, ident)));`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#semicolon_if_nothing_returned
2021-06-04 20:55:15 -07:00
|
|
|
flat_fields.extend(aliases.iter().map(|alias| (alias, ident)));
|
2019-01-15 10:35:09 -06:00
|
|
|
}
|
|
|
|
|
2020-10-23 16:12:06 +05:00
|
|
|
let field_strs: &Vec<_> = &flat_fields.iter().map(|(name, _)| name).collect();
|
|
|
|
let field_bytes: &Vec<_> = &flat_fields
|
2018-04-12 22:58:24 -07:00
|
|
|
.iter()
|
2020-10-23 16:12:06 +05:00
|
|
|
.map(|(name, _)| Literal::byte_string(name.as_bytes()))
|
|
|
|
.collect();
|
2017-04-14 16:09:00 -07:00
|
|
|
|
2019-01-15 10:35:09 -06:00
|
|
|
let constructors: &Vec<_> = &flat_fields
|
2017-12-23 20:13:08 -08:00
|
|
|
.iter()
|
2022-11-27 16:08:47 -08:00
|
|
|
.map(|(_, ident)| quote!(#this_value::#ident))
|
2017-12-23 20:13:08 -08:00
|
|
|
.collect();
|
2019-01-15 10:35:09 -06:00
|
|
|
let main_constructors: &Vec<_> = &fields
|
|
|
|
.iter()
|
2022-11-27 16:08:47 -08:00
|
|
|
.map(|(_, ident, _)| quote!(#this_value::#ident))
|
2019-01-15 10:35:09 -06:00
|
|
|
.collect();
|
2017-04-14 16:09:00 -07:00
|
|
|
|
2020-10-23 00:54:13 +05:00
|
|
|
let expecting = expecting.unwrap_or(if is_variant {
|
2017-04-14 16:09:00 -07:00
|
|
|
"variant identifier"
|
|
|
|
} else {
|
|
|
|
"field identifier"
|
2020-10-23 00:54:13 +05:00
|
|
|
});
|
2016-01-10 19:34:48 -08:00
|
|
|
|
2017-12-23 20:13:08 -08:00
|
|
|
let index_expecting = if is_variant { "variant" } else { "field" };
|
2017-09-05 21:55:06 -07:00
|
|
|
|
2018-04-13 00:17:10 -07:00
|
|
|
let bytes_to_str = if fallthrough.is_some() || collect_other_fields {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
let __value = &_serde::__private::from_utf8_lossy(__value);
|
2018-04-13 00:17:10 -07:00
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
let (
|
|
|
|
value_as_str_content,
|
|
|
|
value_as_borrowed_str_content,
|
|
|
|
value_as_bytes_content,
|
|
|
|
value_as_borrowed_bytes_content,
|
2018-05-07 11:02:12 -07:00
|
|
|
) = if collect_other_fields {
|
2018-04-13 00:17:10 -07:00
|
|
|
(
|
|
|
|
Some(quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
let __value = _serde::__private::de::Content::String(_serde::__private::ToString::to_string(__value));
|
2018-04-13 00:17:10 -07:00
|
|
|
}),
|
|
|
|
Some(quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
let __value = _serde::__private::de::Content::Str(__value);
|
2018-04-13 00:17:10 -07:00
|
|
|
}),
|
|
|
|
Some(quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
let __value = _serde::__private::de::Content::ByteBuf(__value.to_vec());
|
2018-04-13 00:17:10 -07:00
|
|
|
}),
|
|
|
|
Some(quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
let __value = _serde::__private::de::Content::Bytes(__value);
|
2018-04-13 00:17:10 -07:00
|
|
|
}),
|
|
|
|
)
|
2018-05-07 11:02:12 -07:00
|
|
|
} else {
|
|
|
|
(None, None, None, None)
|
2018-04-13 00:17:10 -07:00
|
|
|
};
|
|
|
|
|
2021-01-23 20:41:43 -08:00
|
|
|
let fallthrough_arm_tokens;
|
|
|
|
let fallthrough_arm = if let Some(fallthrough) = &fallthrough {
|
2018-04-13 00:17:10 -07:00
|
|
|
fallthrough
|
|
|
|
} else if is_variant {
|
2021-01-23 20:41:43 -08:00
|
|
|
fallthrough_arm_tokens = quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Err(_serde::de::Error::unknown_variant(__value, VARIANTS))
|
2021-01-23 20:41:43 -08:00
|
|
|
};
|
|
|
|
&fallthrough_arm_tokens
|
2018-04-13 00:17:10 -07:00
|
|
|
} else {
|
2021-01-23 20:41:43 -08:00
|
|
|
fallthrough_arm_tokens = quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Err(_serde::de::Error::unknown_field(__value, FIELDS))
|
2021-01-23 20:41:43 -08:00
|
|
|
};
|
|
|
|
&fallthrough_arm_tokens
|
2018-04-13 00:17:10 -07:00
|
|
|
};
|
|
|
|
|
2021-01-23 20:41:43 -08:00
|
|
|
let u64_fallthrough_arm_tokens;
|
|
|
|
let u64_fallthrough_arm = if let Some(fallthrough) = &fallthrough {
|
2020-10-22 10:18:27 +05:00
|
|
|
fallthrough
|
|
|
|
} else {
|
|
|
|
let fallthrough_msg = format!("{} index 0 <= i < {}", index_expecting, fields.len());
|
2021-01-23 20:41:43 -08:00
|
|
|
u64_fallthrough_arm_tokens = quote! {
|
2021-01-23 19:45:15 -08:00
|
|
|
_serde::__private::Err(_serde::de::Error::invalid_value(
|
2020-10-22 10:18:27 +05:00
|
|
|
_serde::de::Unexpected::Unsigned(__value),
|
|
|
|
&#fallthrough_msg,
|
|
|
|
))
|
2021-01-23 20:41:43 -08:00
|
|
|
};
|
|
|
|
&u64_fallthrough_arm_tokens
|
2020-10-22 10:18:27 +05:00
|
|
|
};
|
|
|
|
|
2018-05-07 11:02:12 -07:00
|
|
|
let variant_indices = 0_u64..;
|
2018-03-19 00:57:58 +01:00
|
|
|
let visit_other = if collect_other_fields {
|
2018-04-13 00:12:30 -07:00
|
|
|
quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_bool<__E>(self, __value: bool) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2018-03-19 00:57:58 +01:00
|
|
|
{
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(__Field::__other(_serde::__private::de::Content::Bool(__value)))
|
2018-03-19 00:57:58 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_i8<__E>(self, __value: i8) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2018-03-19 00:57:58 +01:00
|
|
|
{
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(__Field::__other(_serde::__private::de::Content::I8(__value)))
|
2018-03-19 00:57:58 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_i16<__E>(self, __value: i16) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2018-03-19 00:57:58 +01:00
|
|
|
{
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(__Field::__other(_serde::__private::de::Content::I16(__value)))
|
2018-03-19 00:57:58 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_i32<__E>(self, __value: i32) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2018-03-19 00:57:58 +01:00
|
|
|
{
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(__Field::__other(_serde::__private::de::Content::I32(__value)))
|
2018-03-19 00:57:58 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_i64<__E>(self, __value: i64) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2018-03-19 00:57:58 +01:00
|
|
|
{
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(__Field::__other(_serde::__private::de::Content::I64(__value)))
|
2018-03-19 00:57:58 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_u8<__E>(self, __value: u8) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2018-03-19 00:57:58 +01:00
|
|
|
{
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(__Field::__other(_serde::__private::de::Content::U8(__value)))
|
2018-03-19 00:57:58 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_u16<__E>(self, __value: u16) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2018-03-19 00:57:58 +01:00
|
|
|
{
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(__Field::__other(_serde::__private::de::Content::U16(__value)))
|
2018-03-19 00:57:58 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_u32<__E>(self, __value: u32) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2018-03-19 00:57:58 +01:00
|
|
|
{
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(__Field::__other(_serde::__private::de::Content::U32(__value)))
|
2018-03-19 00:57:58 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_u64<__E>(self, __value: u64) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2018-03-19 00:57:58 +01:00
|
|
|
{
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(__Field::__other(_serde::__private::de::Content::U64(__value)))
|
2018-03-19 00:57:58 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_f32<__E>(self, __value: f32) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2018-03-19 00:57:58 +01:00
|
|
|
{
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(__Field::__other(_serde::__private::de::Content::F32(__value)))
|
2018-03-19 00:57:58 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_f64<__E>(self, __value: f64) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2018-03-19 00:57:58 +01:00
|
|
|
{
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(__Field::__other(_serde::__private::de::Content::F64(__value)))
|
2018-03-19 00:57:58 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_char<__E>(self, __value: char) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2018-03-19 00:57:58 +01:00
|
|
|
{
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(__Field::__other(_serde::__private::de::Content::Char(__value)))
|
2018-03-19 00:57:58 +01:00
|
|
|
}
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_unit<__E>(self) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2018-03-19 00:57:58 +01:00
|
|
|
{
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(__Field::__other(_serde::__private::de::Content::Unit))
|
2018-03-19 00:57:58 +01:00
|
|
|
}
|
2018-04-13 00:12:30 -07:00
|
|
|
}
|
2018-03-14 14:15:45 +01:00
|
|
|
} else {
|
2018-04-13 00:12:30 -07:00
|
|
|
quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_u64<__E>(self, __value: u64) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2018-03-14 14:15:45 +01:00
|
|
|
{
|
|
|
|
match __value {
|
|
|
|
#(
|
2021-01-08 19:35:52 -08:00
|
|
|
#variant_indices => _serde::__private::Ok(#main_constructors),
|
2018-03-14 14:15:45 +01:00
|
|
|
)*
|
2021-01-23 19:45:15 -08:00
|
|
|
_ => #u64_fallthrough_arm,
|
2018-03-14 14:15:45 +01:00
|
|
|
}
|
2017-09-05 21:55:06 -07:00
|
|
|
}
|
2018-04-13 00:12:30 -07:00
|
|
|
}
|
2017-01-17 23:52:06 -08:00
|
|
|
};
|
|
|
|
|
2021-01-23 20:21:39 -08:00
|
|
|
let visit_borrowed = if fallthrough_borrowed.is_some() || collect_other_fields {
|
Resolve needless_borrow clippy lints
error: this expression borrows a reference (`&syn::Type`) that is immediately dereferenced by the compiler
--> serde_derive/src/internals/check.rs:399:37
|
399 | if let Type::Path(ty) = ungroup(&field.ty) {
| ^^^^^^^^^ help: change this to: `field.ty`
|
note: the lint level is defined here
--> serde_derive/src/lib.rs:18:9
|
18 | #![deny(clippy::all, clippy::pedantic)]
| ^^^^^^^^^^^
= note: `#[deny(clippy::needless_borrow)]` implied by `#[deny(clippy::all)]`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:478:52
|
478 | &type_path, params, fields, false, cattrs, &expecting,
| ^^^^^^^^^^ help: change this to: `expecting`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:564:76
|
564 | let visit_seq = Stmts(deserialize_seq_in_place(params, fields, cattrs, &expecting));
| ^^^^^^^^^^ help: change this to: `expecting`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:925:51
|
925 | &type_path, params, fields, true, cattrs, &expecting,
| ^^^^^^^^^^ help: change this to: `expecting`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&str`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:1066:76
|
1066 | let visit_seq = Stmts(deserialize_seq_in_place(params, fields, cattrs, &expecting));
| ^^^^^^^^^^ help: change this to: `expecting`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&proc_macro2::TokenStream`) that is immediately dereferenced by the compiler
--> serde_derive/src/de.rs:2288:80
|
2288 | let fallthrough_borrowed_arm = fallthrough_borrowed.as_ref().unwrap_or(&fallthrough_arm);
| ^^^^^^^^^^^^^^^^ help: change this to: `fallthrough_arm`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
error: this expression borrows a reference (`&syn::Member`) that is immediately dereferenced by the compiler
--> serde_derive/src/ser.rs:1102:43
|
1102 | get_member(params, field, &member)
| ^^^^^^^ help: change this to: `member`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
2021-06-04 20:56:35 -07:00
|
|
|
let fallthrough_borrowed_arm = fallthrough_borrowed.as_ref().unwrap_or(fallthrough_arm);
|
2021-01-23 20:21:39 -08:00
|
|
|
Some(quote! {
|
|
|
|
fn visit_borrowed_str<__E>(self, __value: &'de str) -> _serde::__private::Result<Self::Value, __E>
|
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
|
|
|
{
|
|
|
|
match __value {
|
|
|
|
#(
|
|
|
|
#field_strs => _serde::__private::Ok(#constructors),
|
|
|
|
)*
|
|
|
|
_ => {
|
|
|
|
#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 {
|
|
|
|
#(
|
|
|
|
#field_bytes => _serde::__private::Ok(#constructors),
|
|
|
|
)*
|
|
|
|
_ => {
|
|
|
|
#bytes_to_str
|
|
|
|
#value_as_borrowed_bytes_content
|
|
|
|
#fallthrough_borrowed_arm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_block! {
|
2021-01-08 19:35:52 -08:00
|
|
|
fn expecting(&self, __formatter: &mut _serde::__private::Formatter) -> _serde::__private::fmt::Result {
|
|
|
|
_serde::__private::Formatter::write_str(__formatter, #expecting)
|
2016-02-07 22:03:01 -08:00
|
|
|
}
|
2015-04-27 18:05:54 -04:00
|
|
|
|
2018-03-19 00:57:58 +01:00
|
|
|
#visit_other
|
2017-01-17 23:52:06 -08:00
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_str<__E>(self, __value: &str) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2017-04-14 16:09:00 -07:00
|
|
|
{
|
|
|
|
match __value {
|
|
|
|
#(
|
2021-01-08 19:35:52 -08:00
|
|
|
#field_strs => _serde::__private::Ok(#constructors),
|
2017-04-14 16:09:00 -07:00
|
|
|
)*
|
2018-03-19 00:57:58 +01:00
|
|
|
_ => {
|
|
|
|
#value_as_str_content
|
|
|
|
#fallthrough_arm
|
|
|
|
}
|
2017-04-14 16:09:00 -07:00
|
|
|
}
|
|
|
|
}
|
2017-01-18 21:11:51 -08:00
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
fn visit_bytes<__E>(self, __value: &[u8]) -> _serde::__private::Result<Self::Value, __E>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__E: _serde::de::Error,
|
2017-04-14 16:09:00 -07:00
|
|
|
{
|
|
|
|
match __value {
|
|
|
|
#(
|
2021-01-08 19:35:52 -08:00
|
|
|
#field_bytes => _serde::__private::Ok(#constructors),
|
2017-04-14 16:09:00 -07:00
|
|
|
)*
|
|
|
|
_ => {
|
|
|
|
#bytes_to_str
|
2018-03-19 00:57:58 +01:00
|
|
|
#value_as_bytes_content
|
2017-04-14 16:09:00 -07:00
|
|
|
#fallthrough_arm
|
2015-03-15 15:31:31 -07:00
|
|
|
}
|
|
|
|
}
|
2015-05-17 23:14:38 -07:00
|
|
|
}
|
2020-10-23 16:12:06 +05:00
|
|
|
|
2021-01-23 20:21:39 -08:00
|
|
|
#visit_borrowed
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2015-03-15 15:31:31 -07:00
|
|
|
}
|
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
fn deserialize_map(
|
2018-05-20 19:34:52 -07:00
|
|
|
struct_path: &TokenStream,
|
2017-04-13 12:28:23 -07:00
|
|
|
params: &Parameters,
|
|
|
|
fields: &[Field],
|
2017-04-14 15:52:56 -07:00
|
|
|
cattrs: &attr::Container,
|
2017-04-13 12:28:23 -07:00
|
|
|
) -> Fragment {
|
2015-03-15 18:38:52 -07:00
|
|
|
// Create the field names for the fields.
|
2017-04-13 12:28:23 -07:00
|
|
|
let fields_names: Vec<_> = fields
|
|
|
|
.iter()
|
2016-03-06 23:27:12 -08:00
|
|
|
.enumerate()
|
2017-01-12 21:49:20 -08:00
|
|
|
.map(|(i, field)| (field, field_i(i)))
|
2017-01-12 21:52:56 -08:00
|
|
|
.collect();
|
2016-02-15 20:43:11 -08:00
|
|
|
|
2016-03-06 23:27:12 -08:00
|
|
|
// Declare each field that will be deserialized.
|
2017-04-13 12:28:23 -07:00
|
|
|
let let_values = fields_names
|
|
|
|
.iter()
|
2018-03-20 14:45:14 +01:00
|
|
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing() && !field.attrs.flatten())
|
2019-09-07 23:01:16 -07:00
|
|
|
.map(|(field, name)| {
|
2018-05-05 00:45:30 -07:00
|
|
|
let field_ty = field.ty;
|
2017-12-23 20:13:08 -08:00
|
|
|
quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
let mut #name: _serde::__private::Option<#field_ty> = _serde::__private::None;
|
2017-12-23 20:13:08 -08:00
|
|
|
}
|
|
|
|
});
|
2015-03-15 18:38:52 -07:00
|
|
|
|
2018-03-15 15:49:40 +01:00
|
|
|
// Collect contents for flatten fields into a buffer
|
|
|
|
let let_collect = if cattrs.has_flatten() {
|
|
|
|
Some(quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
let mut __collect = _serde::__private::Vec::<_serde::__private::Option<(
|
|
|
|
_serde::__private::de::Content,
|
|
|
|
_serde::__private::de::Content
|
2018-03-18 21:07:08 +01:00
|
|
|
)>>::new();
|
2018-03-15 15:49:40 +01:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2018-03-14 14:15:45 +01:00
|
|
|
|
2015-03-15 18:38:52 -07:00
|
|
|
// Match arms to extract a value for a field.
|
2017-12-23 20:13:08 -08:00
|
|
|
let value_arms = fields_names
|
|
|
|
.iter()
|
2018-03-20 14:45:14 +01:00
|
|
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing() && !field.attrs.flatten())
|
2019-09-07 23:01:16 -07:00
|
|
|
.map(|(field, name)| {
|
2016-06-14 23:37:20 -07:00
|
|
|
let deser_name = field.attrs.name().deserialize_name();
|
2016-05-10 09:52:51 -07:00
|
|
|
|
2016-06-14 23:37:20 -07:00
|
|
|
let visit = match field.attrs.deserialize_with() {
|
2016-05-07 12:33:59 -07:00
|
|
|
None => {
|
2018-05-05 00:45:30 -07:00
|
|
|
let field_ty = field.ty;
|
2018-03-31 23:44:50 +02:00
|
|
|
let span = field.original.span();
|
2018-04-12 22:58:24 -07:00
|
|
|
let func =
|
|
|
|
quote_spanned!(span=> _serde::de::MapAccess::next_value::<#field_ty>);
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
2018-01-10 20:59:48 -08:00
|
|
|
try!(#func(&mut __map))
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2016-05-07 12:33:59 -07:00
|
|
|
}
|
|
|
|
Some(path) => {
|
2017-12-23 20:13:08 -08:00
|
|
|
let (wrapper, wrapper_ty) = wrap_deserialize_field_with(params, field.ty, path);
|
2016-09-28 09:18:18 -07:00
|
|
|
quote!({
|
2016-09-12 00:04:21 -07:00
|
|
|
#wrapper
|
2019-02-13 09:12:19 -08:00
|
|
|
match _serde::de::MapAccess::next_value::<#wrapper_ty>(&mut __map) {
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(__wrapper) => __wrapper.value,
|
|
|
|
_serde::__private::Err(__err) => {
|
|
|
|
return _serde::__private::Err(__err);
|
2019-02-13 09:12:19 -08:00
|
|
|
}
|
|
|
|
}
|
2016-09-28 09:18:18 -07:00
|
|
|
})
|
2016-05-07 12:33:59 -07:00
|
|
|
}
|
|
|
|
};
|
2016-09-12 00:04:21 -07:00
|
|
|
quote! {
|
|
|
|
__Field::#name => {
|
2021-01-08 19:35:52 -08:00
|
|
|
if _serde::__private::Option::is_some(&#name) {
|
|
|
|
return _serde::__private::Err(<__A::Error as _serde::de::Error>::duplicate_field(#deser_name));
|
2016-05-10 09:52:51 -07:00
|
|
|
}
|
2021-01-08 19:35:52 -08:00
|
|
|
#name = _serde::__private::Some(#visit);
|
2016-03-06 23:27:12 -08:00
|
|
|
}
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2017-01-12 21:52:56 -08:00
|
|
|
});
|
2016-03-06 23:27:12 -08:00
|
|
|
|
|
|
|
// Visit ignored values to consume them
|
2018-03-15 16:17:54 +01:00
|
|
|
let ignored_arm = if cattrs.has_flatten() {
|
2018-03-14 14:15:45 +01:00
|
|
|
Some(quote! {
|
|
|
|
__Field::__other(__name) => {
|
2021-01-08 19:35:52 -08:00
|
|
|
__collect.push(_serde::__private::Some((
|
2018-03-18 21:07:08 +01:00
|
|
|
__name,
|
|
|
|
try!(_serde::de::MapAccess::next_value(&mut __map)))));
|
2018-03-14 14:15:45 +01:00
|
|
|
}
|
|
|
|
})
|
2018-03-15 16:17:54 +01:00
|
|
|
} else if cattrs.deny_unknown_fields() {
|
|
|
|
None
|
2016-04-19 12:43:57 -05:00
|
|
|
} else {
|
2016-09-12 00:04:21 -07:00
|
|
|
Some(quote! {
|
2017-04-14 11:58:29 -07:00
|
|
|
_ => { let _ = try!(_serde::de::MapAccess::next_value::<_serde::de::IgnoredAny>(&mut __map)); }
|
2016-09-12 00:04:21 -07:00
|
|
|
})
|
2016-03-06 23:27:12 -08:00
|
|
|
};
|
|
|
|
|
2018-03-20 14:45:14 +01:00
|
|
|
let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing());
|
2017-04-14 15:52:56 -07:00
|
|
|
let match_keys = if cattrs.deny_unknown_fields() && all_skipped {
|
2017-01-12 23:17:45 -08:00
|
|
|
quote! {
|
2023-03-08 11:46:26 -08:00
|
|
|
// FIXME: Once feature(exhaustive_patterns) is stable:
|
2021-01-08 19:35:52 -08:00
|
|
|
// let _serde::__private::None::<__Field> = try!(_serde::de::MapAccess::next_key(&mut __map));
|
|
|
|
_serde::__private::Option::map(
|
2017-04-14 11:58:29 -07:00
|
|
|
try!(_serde::de::MapAccess::next_key::<__Field>(&mut __map)),
|
2017-03-18 13:28:42 -07:00
|
|
|
|__impossible| match __impossible {});
|
2017-01-12 23:17:45 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
while let _serde::__private::Some(__key) = try!(_serde::de::MapAccess::next_key::<__Field>(&mut __map)) {
|
2017-03-18 10:33:22 -07:00
|
|
|
match __key {
|
2017-01-12 23:17:45 -08:00
|
|
|
#(#value_arms)*
|
|
|
|
#ignored_arm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-13 12:28:23 -07:00
|
|
|
let extract_values = fields_names
|
|
|
|
.iter()
|
2018-03-20 14:45:14 +01:00
|
|
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing() && !field.attrs.flatten())
|
2019-09-07 23:01:16 -07:00
|
|
|
.map(|(field, name)| {
|
2018-01-09 19:34:35 -08:00
|
|
|
let missing_expr = Match(expr_is_missing(field, cattrs));
|
2015-03-15 18:38:52 -07:00
|
|
|
|
2017-12-23 20:13:08 -08:00
|
|
|
quote! {
|
|
|
|
let #name = match #name {
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Some(#name) => #name,
|
|
|
|
_serde::__private::None => #missing_expr
|
2017-12-23 20:13:08 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
2017-04-13 12:28:23 -07:00
|
|
|
|
2018-03-15 15:49:40 +01:00
|
|
|
let extract_collected = fields_names
|
|
|
|
.iter()
|
2019-02-15 18:32:13 -08:00
|
|
|
.filter(|&&(field, _)| field.attrs.flatten() && !field.attrs.skip_deserializing())
|
2019-09-07 23:01:16 -07:00
|
|
|
.map(|(field, name)| {
|
2018-03-15 15:49:40 +01:00
|
|
|
let field_ty = field.ty;
|
2018-05-01 22:25:06 -07:00
|
|
|
let func = match field.attrs.deserialize_with() {
|
2018-11-17 18:13:36 +02:00
|
|
|
None => {
|
|
|
|
let span = field.original.span();
|
|
|
|
quote_spanned!(span=> _serde::de::Deserialize::deserialize)
|
2018-12-10 22:09:02 -08:00
|
|
|
}
|
2018-05-01 22:25:06 -07:00
|
|
|
Some(path) => quote!(#path),
|
|
|
|
};
|
2018-03-15 15:49:40 +01:00
|
|
|
quote! {
|
2018-05-01 22:25:06 -07:00
|
|
|
let #name: #field_ty = try!(#func(
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::de::FlatMapDeserializer(
|
2018-03-15 15:49:40 +01:00
|
|
|
&mut __collect,
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::PhantomData)));
|
2018-03-15 15:49:40 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-15 16:17:54 +01:00
|
|
|
let collected_deny_unknown_fields = if cattrs.has_flatten() && cattrs.deny_unknown_fields() {
|
2018-03-15 15:49:40 +01:00
|
|
|
Some(quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
if let _serde::__private::Some(_serde::__private::Some((__key, _))) =
|
|
|
|
__collect.into_iter().filter(_serde::__private::Option::is_some).next()
|
2019-02-28 16:35:29 -08:00
|
|
|
{
|
2021-01-08 19:35:52 -08:00
|
|
|
if let _serde::__private::Some(__key) = __key.as_str() {
|
|
|
|
return _serde::__private::Err(
|
2018-03-18 21:07:08 +01:00
|
|
|
_serde::de::Error::custom(format_args!("unknown field `{}`", &__key)));
|
|
|
|
} else {
|
2021-01-08 19:35:52 -08:00
|
|
|
return _serde::__private::Err(
|
2018-03-18 21:07:08 +01:00
|
|
|
_serde::de::Error::custom(format_args!("unexpected map key")));
|
|
|
|
}
|
2018-03-15 15:49:40 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2019-09-07 23:01:16 -07:00
|
|
|
let result = fields_names.iter().map(|(field, name)| {
|
2018-05-19 20:47:40 -07:00
|
|
|
let member = &field.member;
|
2018-03-20 14:45:14 +01:00
|
|
|
if field.attrs.skip_deserializing() {
|
2018-01-09 19:34:35 -08:00
|
|
|
let value = Expr(expr_is_missing(field, cattrs));
|
2018-05-19 20:47:40 -07:00
|
|
|
quote!(#member: #value)
|
2017-12-23 20:13:08 -08:00
|
|
|
} else {
|
2018-05-19 20:47:40 -07:00
|
|
|
quote!(#member: #name)
|
2017-12-23 20:13:08 -08:00
|
|
|
}
|
|
|
|
});
|
2015-03-15 18:38:52 -07:00
|
|
|
|
2019-09-07 23:01:16 -07:00
|
|
|
let let_default = match cattrs.default() {
|
2017-12-23 20:13:08 -08:00
|
|
|
attr::Default::Default => Some(quote!(
|
2021-01-08 19:35:52 -08:00
|
|
|
let __default: Self::Value = _serde::__private::Default::default();
|
2017-12-23 20:13:08 -08:00
|
|
|
)),
|
2019-09-07 23:01:16 -07:00
|
|
|
attr::Default::Path(path) => Some(quote!(
|
2017-12-23 20:13:08 -08:00
|
|
|
let __default: Self::Value = #path();
|
|
|
|
)),
|
2017-02-20 17:03:38 -08:00
|
|
|
attr::Default::None => {
|
|
|
|
// We don't need the default value, to prevent an unused variable warning
|
|
|
|
// we'll leave the line empty.
|
|
|
|
None
|
|
|
|
}
|
2017-02-20 22:34:52 +01:00
|
|
|
};
|
|
|
|
|
2018-04-21 11:32:57 -07:00
|
|
|
let mut result = quote!(#struct_path { #(#result),* });
|
2017-04-08 22:42:42 -07:00
|
|
|
if params.has_getter {
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_type = ¶ms.this_type;
|
2022-11-27 17:16:00 -08:00
|
|
|
let (_, ty_generics, _) = params.generics.split_for_impl();
|
2017-04-08 22:42:42 -07:00
|
|
|
result = quote! {
|
2022-11-27 17:16:00 -08:00
|
|
|
_serde::__private::Into::<#this_type #ty_generics>::into(#result)
|
2017-04-08 22:42:42 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_block! {
|
2016-10-03 21:09:52 -07:00
|
|
|
#(#let_values)*
|
2015-03-15 18:38:52 -07:00
|
|
|
|
2018-03-14 14:15:45 +01:00
|
|
|
#let_collect
|
|
|
|
|
2017-01-12 23:17:45 -08:00
|
|
|
#match_keys
|
2015-03-15 18:38:52 -07:00
|
|
|
|
2017-02-20 17:03:38 -08:00
|
|
|
#let_default
|
2017-02-20 22:34:52 +01:00
|
|
|
|
2016-10-03 21:09:52 -07:00
|
|
|
#(#extract_values)*
|
2016-07-05 01:42:38 -07:00
|
|
|
|
2018-03-15 15:49:40 +01:00
|
|
|
#(#extract_collected)*
|
|
|
|
|
|
|
|
#collected_deny_unknown_fields
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(#result)
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2015-03-15 18:38:52 -07:00
|
|
|
}
|
2016-03-06 23:27:12 -08:00
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
#[cfg(feature = "deserialize_in_place")]
|
2018-03-18 18:22:06 +01:00
|
|
|
fn deserialize_struct_as_struct_in_place_visitor(
|
2017-11-13 15:35:14 -05:00
|
|
|
params: &Parameters,
|
|
|
|
fields: &[Field],
|
|
|
|
cattrs: &attr::Container,
|
2018-03-25 12:39:20 +02:00
|
|
|
) -> (Fragment, Fragment, Fragment) {
|
2018-03-25 12:32:06 +02:00
|
|
|
assert!(!cattrs.has_flatten());
|
2018-03-20 14:45:14 +01:00
|
|
|
|
2017-11-13 15:35:14 -05:00
|
|
|
let field_names_idents: Vec<_> = fields
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter(|&(_, field)| !field.attrs.skip_deserializing())
|
2019-01-15 10:35:09 -06:00
|
|
|
.map(|(i, field)| {
|
2019-02-01 21:02:57 -08:00
|
|
|
(
|
|
|
|
field.attrs.name().deserialize_name(),
|
|
|
|
field_i(i),
|
|
|
|
field.attrs.aliases(),
|
|
|
|
)
|
2019-01-15 10:35:09 -06:00
|
|
|
})
|
2017-11-13 15:35:14 -05:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
let fields_stmt = {
|
2019-09-07 23:01:16 -07:00
|
|
|
let field_names = field_names_idents.iter().map(|(name, _, _)| name);
|
2017-11-13 15:35:14 -05:00
|
|
|
quote_block! {
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2017-11-13 15:35:14 -05:00
|
|
|
const FIELDS: &'static [&'static str] = &[ #(#field_names),* ];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-09-10 15:12:15 +00:00
|
|
|
let field_visitor = deserialize_generated_identifier(&field_names_idents, cattrs, false, None);
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
let visit_map = deserialize_map_in_place(params, fields, cattrs);
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2018-03-25 12:39:20 +02:00
|
|
|
(field_visitor, fields_stmt, visit_map)
|
2018-03-18 18:22:06 +01:00
|
|
|
}
|
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
#[cfg(feature = "deserialize_in_place")]
|
|
|
|
fn deserialize_map_in_place(
|
2017-11-13 15:35:14 -05:00
|
|
|
params: &Parameters,
|
|
|
|
fields: &[Field],
|
|
|
|
cattrs: &attr::Container,
|
|
|
|
) -> Fragment {
|
2018-03-25 12:32:06 +02:00
|
|
|
assert!(!cattrs.has_flatten());
|
2018-03-20 14:45:14 +01:00
|
|
|
|
2017-11-13 15:35:14 -05:00
|
|
|
// Create the field names for the fields.
|
|
|
|
let fields_names: Vec<_> = fields
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, field)| (field, field_i(i)))
|
|
|
|
.collect();
|
|
|
|
|
2018-08-14 22:32:27 -07:00
|
|
|
// For deserialize_in_place, declare booleans for each field that will be
|
|
|
|
// deserialized.
|
2017-11-13 15:35:14 -05:00
|
|
|
let let_flags = fields_names
|
|
|
|
.iter()
|
|
|
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
2019-09-07 23:01:16 -07:00
|
|
|
.map(|(_, name)| {
|
2017-12-23 20:13:08 -08:00
|
|
|
quote! {
|
|
|
|
let mut #name: bool = false;
|
|
|
|
}
|
|
|
|
});
|
2017-11-13 15:35:14 -05:00
|
|
|
|
|
|
|
// Match arms to extract a value for a field.
|
2017-12-23 20:13:08 -08:00
|
|
|
let value_arms_from = fields_names
|
|
|
|
.iter()
|
2017-11-13 15:35:14 -05:00
|
|
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
2019-09-07 23:01:16 -07:00
|
|
|
.map(|(field, name)| {
|
2017-11-13 15:35:14 -05:00
|
|
|
let deser_name = field.attrs.name().deserialize_name();
|
2018-05-19 20:47:40 -07:00
|
|
|
let member = &field.member;
|
2017-11-13 15:35:14 -05:00
|
|
|
|
|
|
|
let visit = match field.attrs.deserialize_with() {
|
|
|
|
None => {
|
|
|
|
quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
try!(_serde::de::MapAccess::next_value_seed(&mut __map, _serde::__private::de::InPlaceSeed(&mut self.place.#member)))
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(path) => {
|
2017-12-23 20:13:08 -08:00
|
|
|
let (wrapper, wrapper_ty) = wrap_deserialize_field_with(params, field.ty, path);
|
2017-11-13 15:35:14 -05:00
|
|
|
quote!({
|
|
|
|
#wrapper
|
2019-02-13 09:12:19 -08:00
|
|
|
self.place.#member = match _serde::de::MapAccess::next_value::<#wrapper_ty>(&mut __map) {
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(__wrapper) => __wrapper.value,
|
|
|
|
_serde::__private::Err(__err) => {
|
|
|
|
return _serde::__private::Err(__err);
|
2019-02-13 09:12:19 -08:00
|
|
|
}
|
|
|
|
};
|
2017-11-13 15:35:14 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
quote! {
|
|
|
|
__Field::#name => {
|
|
|
|
if #name {
|
2021-01-08 19:35:52 -08:00
|
|
|
return _serde::__private::Err(<__A::Error as _serde::de::Error>::duplicate_field(#deser_name));
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
|
|
|
#visit;
|
|
|
|
#name = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Visit ignored values to consume them
|
2018-03-25 12:42:36 +02:00
|
|
|
let ignored_arm = if cattrs.deny_unknown_fields() {
|
2017-11-13 15:35:14 -05:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(quote! {
|
|
|
|
_ => { let _ = try!(_serde::de::MapAccess::next_value::<_serde::de::IgnoredAny>(&mut __map)); }
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2018-03-25 12:42:36 +02:00
|
|
|
let all_skipped = fields.iter().all(|field| field.attrs.skip_deserializing());
|
2017-11-13 15:35:14 -05:00
|
|
|
|
|
|
|
let match_keys = if cattrs.deny_unknown_fields() && all_skipped {
|
|
|
|
quote! {
|
2023-03-08 11:46:26 -08:00
|
|
|
// FIXME: Once feature(exhaustive_patterns) is stable:
|
2021-01-08 19:35:52 -08:00
|
|
|
// let _serde::__private::None::<__Field> = try!(_serde::de::MapAccess::next_key(&mut __map));
|
|
|
|
_serde::__private::Option::map(
|
2017-11-13 15:35:14 -05:00
|
|
|
try!(_serde::de::MapAccess::next_key::<__Field>(&mut __map)),
|
|
|
|
|__impossible| match __impossible {});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quote! {
|
2021-01-08 19:35:52 -08:00
|
|
|
while let _serde::__private::Some(__key) = try!(_serde::de::MapAccess::next_key::<__Field>(&mut __map)) {
|
2017-11-13 15:35:14 -05:00
|
|
|
match __key {
|
|
|
|
#(#value_arms_from)*
|
|
|
|
#ignored_arm
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let check_flags = fields_names
|
|
|
|
.iter()
|
|
|
|
.filter(|&&(field, _)| !field.attrs.skip_deserializing())
|
2019-09-07 23:01:16 -07:00
|
|
|
.map(|(field, name)| {
|
2018-01-09 19:34:35 -08:00
|
|
|
let missing_expr = expr_is_missing(field, cattrs);
|
2017-12-23 20:13:08 -08:00
|
|
|
// If missing_expr unconditionally returns an error, don't try
|
2018-05-06 22:20:07 -07:00
|
|
|
// to assign its value to self.place.
|
2018-05-19 17:33:30 -07:00
|
|
|
if field.attrs.default().is_none()
|
|
|
|
&& cattrs.default().is_none()
|
2018-05-06 22:20:07 -07:00
|
|
|
&& field.attrs.deserialize_with().is_some()
|
2018-04-12 22:58:24 -07:00
|
|
|
{
|
2017-12-23 20:13:08 -08:00
|
|
|
let missing_expr = Stmts(missing_expr);
|
|
|
|
quote! {
|
|
|
|
if !#name {
|
|
|
|
#missing_expr;
|
2017-12-11 17:55:23 -08:00
|
|
|
}
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
2017-12-23 20:13:08 -08:00
|
|
|
} else {
|
2018-05-19 20:47:40 -07:00
|
|
|
let member = &field.member;
|
2017-12-23 20:13:08 -08:00
|
|
|
let missing_expr = Expr(missing_expr);
|
|
|
|
quote! {
|
|
|
|
if !#name {
|
2018-05-19 20:47:40 -07:00
|
|
|
self.place.#member = #missing_expr;
|
2017-12-23 20:13:08 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_type = ¶ms.this_type;
|
2017-12-23 20:13:08 -08:00
|
|
|
let (_, _, ty_generics, _) = split_with_de_lifetime(params);
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2019-09-07 23:01:16 -07:00
|
|
|
let let_default = match cattrs.default() {
|
2017-12-23 20:13:08 -08:00
|
|
|
attr::Default::Default => Some(quote!(
|
2022-11-27 16:08:47 -08:00
|
|
|
let __default: #this_type #ty_generics = _serde::__private::Default::default();
|
2017-12-23 20:13:08 -08:00
|
|
|
)),
|
2019-09-07 23:01:16 -07:00
|
|
|
attr::Default::Path(path) => Some(quote!(
|
2022-11-27 16:08:47 -08:00
|
|
|
let __default: #this_type #ty_generics = #path();
|
2017-12-23 20:13:08 -08:00
|
|
|
)),
|
2017-11-13 15:35:14 -05:00
|
|
|
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)*
|
|
|
|
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(())
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 21:41:27 -08:00
|
|
|
fn field_i(i: usize) -> Ident {
|
2018-03-28 10:42:53 +02:00
|
|
|
Ident::new(&format!("__field{}", i), Span::call_site())
|
2017-01-12 21:41:27 -08:00
|
|
|
}
|
|
|
|
|
2017-04-06 14:22:03 -07:00
|
|
|
/// This function wraps the expression in `#[serde(deserialize_with = "...")]`
|
|
|
|
/// in a trait to prevent it from accessing the internal `Deserialize` state.
|
2017-04-13 12:28:23 -07:00
|
|
|
fn wrap_deserialize_with(
|
|
|
|
params: &Parameters,
|
2018-05-20 19:34:52 -07:00
|
|
|
value_ty: &TokenStream,
|
2018-03-15 10:02:40 -07:00
|
|
|
deserialize_with: &syn::ExprPath,
|
2018-05-20 19:34:52 -07:00
|
|
|
) -> (TokenStream, TokenStream) {
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_type = ¶ms.this_type;
|
2017-12-23 20:13:08 -08:00
|
|
|
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
|
|
|
split_with_de_lifetime(params);
|
2017-09-09 11:34:08 -07:00
|
|
|
let delife = params.borrowed.de_lifetime();
|
2017-02-20 11:50:24 -08:00
|
|
|
|
|
|
|
let wrapper = quote! {
|
2023-04-10 21:41:50 -07:00
|
|
|
#[doc(hidden)]
|
2017-03-26 22:56:58 -07:00
|
|
|
struct __DeserializeWith #de_impl_generics #where_clause {
|
2017-08-14 14:39:29 -07:00
|
|
|
value: #value_ty,
|
2022-11-27 16:08:47 -08:00
|
|
|
phantom: _serde::__private::PhantomData<#this_type #ty_generics>,
|
2021-01-08 19:35:52 -08:00
|
|
|
lifetime: _serde::__private::PhantomData<&#delife ()>,
|
2017-02-20 11:50:24 -08:00
|
|
|
}
|
|
|
|
|
2017-09-09 11:34:08 -07:00
|
|
|
impl #de_impl_generics _serde::Deserialize<#delife> for __DeserializeWith #de_ty_generics #where_clause {
|
2021-01-08 19:35:52 -08:00
|
|
|
fn deserialize<__D>(__deserializer: __D) -> _serde::__private::Result<Self, __D::Error>
|
2018-05-05 18:24:16 -07:00
|
|
|
where
|
|
|
|
__D: _serde::Deserializer<#delife>,
|
2017-02-20 11:50:24 -08:00
|
|
|
{
|
2021-01-08 19:35:52 -08:00
|
|
|
_serde::__private::Ok(__DeserializeWith {
|
2017-03-18 10:33:22 -07:00
|
|
|
value: try!(#deserialize_with(__deserializer)),
|
2021-01-08 19:35:52 -08:00
|
|
|
phantom: _serde::__private::PhantomData,
|
|
|
|
lifetime: _serde::__private::PhantomData,
|
2017-02-20 11:50:24 -08:00
|
|
|
})
|
2016-05-07 12:33:59 -07:00
|
|
|
}
|
2017-02-20 11:50:24 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-26 22:56:58 -07:00
|
|
|
let wrapper_ty = quote!(__DeserializeWith #de_ty_generics);
|
2017-02-20 11:50:24 -08:00
|
|
|
|
|
|
|
(wrapper, wrapper_ty)
|
2016-05-07 12:33:59 -07:00
|
|
|
}
|
2016-05-07 19:57:44 -07:00
|
|
|
|
2017-08-14 14:39:29 -07:00
|
|
|
fn wrap_deserialize_field_with(
|
|
|
|
params: &Parameters,
|
2018-01-08 21:49:09 -08:00
|
|
|
field_ty: &syn::Type,
|
2018-03-15 10:02:40 -07:00
|
|
|
deserialize_with: &syn::ExprPath,
|
2018-05-20 19:34:52 -07:00
|
|
|
) -> (TokenStream, TokenStream) {
|
2018-01-09 19:34:35 -08:00
|
|
|
wrap_deserialize_with(params, "e!(#field_ty), deserialize_with)
|
2017-08-14 14:39:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn wrap_deserialize_variant_with(
|
|
|
|
params: &Parameters,
|
|
|
|
variant: &Variant,
|
2018-03-15 10:02:40 -07:00
|
|
|
deserialize_with: &syn::ExprPath,
|
2018-05-20 19:34:52 -07:00
|
|
|
) -> (TokenStream, TokenStream, TokenStream) {
|
2017-08-14 14:39:29 -07:00
|
|
|
let field_tys = variant.fields.iter().map(|field| field.ty);
|
|
|
|
let (wrapper, wrapper_ty) =
|
2018-01-09 19:34:35 -08:00
|
|
|
wrap_deserialize_with(params, "e!((#(#field_tys),*)), deserialize_with);
|
2017-08-14 14:39:29 -07:00
|
|
|
|
2021-02-28 17:43:54 +05:00
|
|
|
let unwrap_fn = unwrap_to_variant_closure(params, variant, true);
|
2021-02-27 18:52:32 +05:00
|
|
|
|
|
|
|
(wrapper, wrapper_ty, unwrap_fn)
|
|
|
|
}
|
|
|
|
|
2021-08-21 12:46:13 -07:00
|
|
|
// Generates closure that converts single input parameter to the final value.
|
2021-02-27 18:52:32 +05:00
|
|
|
fn unwrap_to_variant_closure(
|
|
|
|
params: &Parameters,
|
|
|
|
variant: &Variant,
|
2021-02-28 17:43:54 +05:00
|
|
|
with_wrapper: bool,
|
2021-02-27 18:52:32 +05:00
|
|
|
) -> TokenStream {
|
2022-11-27 16:08:47 -08:00
|
|
|
let this_value = ¶ms.this_value;
|
2021-02-27 18:52:32 +05:00
|
|
|
let variant_ident = &variant.ident;
|
|
|
|
|
2021-02-28 17:43:54 +05:00
|
|
|
let (arg, wrapper) = if with_wrapper {
|
2021-08-21 12:49:00 -07:00
|
|
|
(quote! { __wrap }, quote! { __wrap.value })
|
2021-02-28 17:43:54 +05:00
|
|
|
} else {
|
|
|
|
let field_tys = variant.fields.iter().map(|field| field.ty);
|
2021-08-21 12:49:00 -07:00
|
|
|
(quote! { __wrap: (#(#field_tys),*) }, quote! { __wrap })
|
2021-02-28 17:43:54 +05:00
|
|
|
};
|
|
|
|
|
2018-04-12 22:58:24 -07:00
|
|
|
let field_access = (0..variant.fields.len()).map(|n| {
|
|
|
|
Member::Unnamed(Index {
|
|
|
|
index: n as u32,
|
|
|
|
span: Span::call_site(),
|
|
|
|
})
|
|
|
|
});
|
2021-02-27 18:52:32 +05:00
|
|
|
|
|
|
|
match variant.style {
|
2018-04-12 23:44:53 -07:00
|
|
|
Style::Struct if variant.fields.len() == 1 => {
|
2018-05-19 20:47:40 -07:00
|
|
|
let member = &variant.fields[0].member;
|
2018-04-12 23:48:38 -07:00
|
|
|
quote! {
|
2022-11-27 16:08:47 -08:00
|
|
|
|#arg| #this_value::#variant_ident { #member: #wrapper }
|
2018-04-12 23:48:38 -07:00
|
|
|
}
|
2018-04-12 23:44:53 -07:00
|
|
|
}
|
2017-08-14 14:39:29 -07:00
|
|
|
Style::Struct => {
|
2018-05-19 20:47:40 -07:00
|
|
|
let members = variant.fields.iter().map(|field| &field.member);
|
2018-04-12 23:48:38 -07:00
|
|
|
quote! {
|
2022-11-27 16:08:47 -08:00
|
|
|
|#arg| #this_value::#variant_ident { #(#members: #wrapper.#field_access),* }
|
2018-04-12 23:48:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Style::Tuple => quote! {
|
2022-11-27 16:08:47 -08:00
|
|
|
|#arg| #this_value::#variant_ident(#(#wrapper.#field_access),*)
|
2018-04-12 23:48:38 -07:00
|
|
|
},
|
|
|
|
Style::Newtype => quote! {
|
2022-11-27 16:08:47 -08:00
|
|
|
|#arg| #this_value::#variant_ident(#wrapper)
|
2018-04-12 23:48:38 -07:00
|
|
|
},
|
|
|
|
Style::Unit => quote! {
|
2022-11-27 16:08:47 -08:00
|
|
|
|#arg| #this_value::#variant_ident
|
2018-04-12 23:48:38 -07:00
|
|
|
},
|
2021-02-27 18:52:32 +05:00
|
|
|
}
|
2017-08-14 14:39:29 -07:00
|
|
|
}
|
|
|
|
|
2017-04-14 15:52:56 -07:00
|
|
|
fn expr_is_missing(field: &Field, cattrs: &attr::Container) -> Fragment {
|
2019-09-07 23:01:16 -07:00
|
|
|
match field.attrs.default() {
|
2017-02-20 17:03:38 -08:00
|
|
|
attr::Default::Default => {
|
2018-11-17 18:13:36 +02:00
|
|
|
let span = field.original.span();
|
2021-01-08 19:35:52 -08:00
|
|
|
let func = quote_spanned!(span=> _serde::__private::Default::default);
|
2018-11-17 18:13:36 +02:00
|
|
|
return quote_expr!(#func());
|
2016-06-09 23:21:42 -07:00
|
|
|
}
|
2019-09-07 23:01:16 -07:00
|
|
|
attr::Default::Path(path) => {
|
2017-02-20 14:43:51 -08:00
|
|
|
return quote_expr!(#path());
|
2016-06-09 23:21:42 -07:00
|
|
|
}
|
2017-02-20 17:03:38 -08:00
|
|
|
attr::Default::None => { /* below */ }
|
2016-05-07 19:57:44 -07:00
|
|
|
}
|
2016-06-09 23:21:42 -07:00
|
|
|
|
2017-04-14 15:52:56 -07:00
|
|
|
match *cattrs.default() {
|
2017-12-23 20:13:08 -08:00
|
|
|
attr::Default::Default | attr::Default::Path(_) => {
|
2018-05-19 20:47:40 -07:00
|
|
|
let member = &field.member;
|
|
|
|
return quote_expr!(__default.#member);
|
2017-02-20 17:03:38 -08:00
|
|
|
}
|
|
|
|
attr::Default::None => { /* below */ }
|
2017-02-20 22:34:52 +01:00
|
|
|
}
|
|
|
|
|
2017-02-20 17:03:38 -08:00
|
|
|
let name = field.attrs.name().deserialize_name();
|
|
|
|
match field.attrs.deserialize_with() {
|
2016-05-07 19:57:44 -07:00
|
|
|
None => {
|
2018-03-31 23:44:50 +02:00
|
|
|
let span = field.original.span();
|
2021-01-08 19:35:52 -08:00
|
|
|
let func = quote_spanned!(span=> _serde::__private::de::missing_field);
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_expr! {
|
2018-01-10 20:59:48 -08:00
|
|
|
try!(#func(#name))
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2016-05-07 19:57:44 -07:00
|
|
|
}
|
|
|
|
Some(_) => {
|
2017-02-20 14:43:51 -08:00
|
|
|
quote_expr! {
|
2021-01-08 19:35:52 -08:00
|
|
|
return _serde::__private::Err(<__A::Error as _serde::de::Error>::missing_field(#name))
|
2016-09-12 00:04:21 -07:00
|
|
|
}
|
2016-05-07 19:57:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-11 21:27:49 -07:00
|
|
|
|
2019-09-07 20:32:26 -07:00
|
|
|
fn effective_style(variant: &Variant) -> Style {
|
|
|
|
match variant.style {
|
|
|
|
Style::Newtype if variant.fields[0].attrs.skip_deserializing() => Style::Unit,
|
|
|
|
other => other,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-02 21:42:07 -07:00
|
|
|
struct DeImplGenerics<'a>(&'a Parameters);
|
2017-12-17 10:46:44 -08:00
|
|
|
#[cfg(feature = "deserialize_in_place")]
|
|
|
|
struct InPlaceImplGenerics<'a>(&'a Parameters);
|
2017-03-26 22:56:58 -07:00
|
|
|
|
|
|
|
impl<'a> ToTokens for DeImplGenerics<'a> {
|
2018-05-20 19:34:52 -07:00
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
2017-04-02 21:42:07 -07:00
|
|
|
let mut generics = self.0.generics.clone();
|
2023-03-15 23:44:08 -07:00
|
|
|
if let Some(de_lifetime) = self.0.borrowed.de_lifetime_param() {
|
2018-01-08 21:49:09 -08:00
|
|
|
generics.params = Some(syn::GenericParam::Lifetime(de_lifetime))
|
|
|
|
.into_iter()
|
|
|
|
.chain(generics.params)
|
|
|
|
.collect();
|
2017-09-09 11:34:08 -07:00
|
|
|
}
|
2017-03-26 22:56:58 -07:00
|
|
|
let (impl_generics, _, _) = generics.split_for_impl();
|
|
|
|
impl_generics.to_tokens(tokens);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
#[cfg(feature = "deserialize_in_place")]
|
|
|
|
impl<'a> ToTokens for InPlaceImplGenerics<'a> {
|
2018-05-20 19:34:52 -07:00
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
2017-12-17 10:46:44 -08:00
|
|
|
let place_lifetime = place_lifetime();
|
2017-11-13 15:35:14 -05:00
|
|
|
let mut generics = self.0.generics.clone();
|
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
// Add lifetime for `&'place mut Self, and `'a: 'place`
|
2018-01-08 21:49:09 -08:00
|
|
|
for param in &mut generics.params {
|
2019-09-07 23:01:16 -07:00
|
|
|
match param {
|
|
|
|
syn::GenericParam::Lifetime(param) => {
|
2018-05-20 19:34:52 -07:00
|
|
|
param.bounds.push(place_lifetime.lifetime.clone());
|
2018-01-08 21:49:09 -08:00
|
|
|
}
|
2019-09-07 23:01:16 -07:00
|
|
|
syn::GenericParam::Type(param) => {
|
2018-05-22 21:06:32 -07:00
|
|
|
param.bounds.push(syn::TypeParamBound::Lifetime(
|
|
|
|
place_lifetime.lifetime.clone(),
|
|
|
|
));
|
2018-01-08 21:49:09 -08:00
|
|
|
}
|
|
|
|
syn::GenericParam::Const(_) => {}
|
|
|
|
}
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
2018-01-08 21:49:09 -08:00
|
|
|
generics.params = Some(syn::GenericParam::Lifetime(place_lifetime))
|
|
|
|
.into_iter()
|
|
|
|
.chain(generics.params)
|
|
|
|
.collect();
|
2023-03-15 23:44:08 -07:00
|
|
|
if let Some(de_lifetime) = self.0.borrowed.de_lifetime_param() {
|
2018-01-08 21:49:09 -08:00
|
|
|
generics.params = Some(syn::GenericParam::Lifetime(de_lifetime))
|
|
|
|
.into_iter()
|
|
|
|
.chain(generics.params)
|
|
|
|
.collect();
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
|
|
|
let (impl_generics, _, _) = generics.split_for_impl();
|
|
|
|
impl_generics.to_tokens(tokens);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
#[cfg(feature = "deserialize_in_place")]
|
2017-11-13 15:35:14 -05:00
|
|
|
impl<'a> DeImplGenerics<'a> {
|
2017-12-17 10:46:44 -08:00
|
|
|
fn in_place(self) -> InPlaceImplGenerics<'a> {
|
|
|
|
InPlaceImplGenerics(self.0)
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 21:49:09 -08:00
|
|
|
struct DeTypeGenerics<'a>(&'a Parameters);
|
2017-12-17 10:46:44 -08:00
|
|
|
#[cfg(feature = "deserialize_in_place")]
|
2018-01-08 21:49:09 -08:00
|
|
|
struct InPlaceTypeGenerics<'a>(&'a Parameters);
|
2017-03-26 22:56:58 -07:00
|
|
|
|
2023-05-10 00:45:48 -07:00
|
|
|
fn de_type_generics_to_tokens(
|
|
|
|
mut generics: syn::Generics,
|
|
|
|
borrowed: &BorrowedLifetimes,
|
|
|
|
tokens: &mut TokenStream,
|
|
|
|
) {
|
2020-10-12 00:23:45 +05:00
|
|
|
if borrowed.de_lifetime_param().is_some() {
|
|
|
|
let def = syn::LifetimeParam {
|
|
|
|
attrs: Vec::new(),
|
|
|
|
lifetime: syn::Lifetime::new("'de", Span::call_site()),
|
|
|
|
colon_token: None,
|
|
|
|
bounds: Punctuated::new(),
|
|
|
|
};
|
|
|
|
// Prepend 'de lifetime to list of generics
|
|
|
|
generics.params = Some(syn::GenericParam::Lifetime(def))
|
|
|
|
.into_iter()
|
|
|
|
.chain(generics.params)
|
|
|
|
.collect();
|
|
|
|
}
|
|
|
|
let (_, ty_generics, _) = generics.split_for_impl();
|
|
|
|
ty_generics.to_tokens(tokens);
|
|
|
|
}
|
|
|
|
|
2018-01-08 21:49:09 -08:00
|
|
|
impl<'a> ToTokens for DeTypeGenerics<'a> {
|
2018-05-20 19:34:52 -07:00
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
2023-05-10 00:45:48 -07:00
|
|
|
de_type_generics_to_tokens(self.0.generics.clone(), &self.0.borrowed, tokens);
|
2017-03-26 22:56:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
#[cfg(feature = "deserialize_in_place")]
|
2018-01-08 21:49:09 -08:00
|
|
|
impl<'a> ToTokens for InPlaceTypeGenerics<'a> {
|
2018-05-20 19:34:52 -07:00
|
|
|
fn to_tokens(&self, tokens: &mut TokenStream) {
|
2017-11-13 15:35:14 -05:00
|
|
|
let mut generics = self.0.generics.clone();
|
2018-01-08 21:49:09 -08:00
|
|
|
generics.params = Some(syn::GenericParam::Lifetime(place_lifetime()))
|
|
|
|
.into_iter()
|
|
|
|
.chain(generics.params)
|
|
|
|
.collect();
|
2017-11-13 15:35:14 -05:00
|
|
|
|
2023-05-10 00:45:48 -07:00
|
|
|
de_type_generics_to_tokens(generics, &self.0.borrowed, tokens);
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
#[cfg(feature = "deserialize_in_place")]
|
2018-01-08 21:49:09 -08:00
|
|
|
impl<'a> DeTypeGenerics<'a> {
|
|
|
|
fn in_place(self) -> InPlaceTypeGenerics<'a> {
|
|
|
|
InPlaceTypeGenerics(self.0)
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 10:46:44 -08:00
|
|
|
#[cfg(feature = "deserialize_in_place")]
|
2023-03-15 23:44:08 -07:00
|
|
|
fn place_lifetime() -> syn::LifetimeParam {
|
|
|
|
syn::LifetimeParam {
|
2018-01-08 21:49:09 -08:00
|
|
|
attrs: Vec::new(),
|
2018-03-31 23:46:25 +02:00
|
|
|
lifetime: syn::Lifetime::new("'place", Span::call_site()),
|
2018-01-08 21:49:09 -08:00
|
|
|
colon_token: None,
|
|
|
|
bounds: Punctuated::new(),
|
|
|
|
}
|
2017-11-13 15:35:14 -05:00
|
|
|
}
|
|
|
|
|
2017-12-23 20:13:08 -08:00
|
|
|
fn split_with_de_lifetime(
|
|
|
|
params: &Parameters,
|
|
|
|
) -> (
|
|
|
|
DeImplGenerics,
|
2018-01-08 21:49:09 -08:00
|
|
|
DeTypeGenerics,
|
|
|
|
syn::TypeGenerics,
|
|
|
|
Option<&syn::WhereClause>,
|
2017-12-23 20:13:08 -08:00
|
|
|
) {
|
2018-01-09 19:34:35 -08:00
|
|
|
let de_impl_generics = DeImplGenerics(params);
|
|
|
|
let de_ty_generics = DeTypeGenerics(params);
|
2017-04-02 21:42:07 -07:00
|
|
|
let (_, ty_generics, where_clause) = params.generics.split_for_impl();
|
2017-03-26 22:56:58 -07:00
|
|
|
(de_impl_generics, de_ty_generics, ty_generics, where_clause)
|
|
|
|
}
|