mirror of
https://github.com/serde-rs/serde.git
synced 2025-09-26 20:40:35 +00:00
commit
aa2d9b208a
@ -35,7 +35,7 @@ pub fn expand_derive_deserialize(input: &mut syn::DeriveInput) -> syn::Result<To
|
||||
|
||||
let ident = &cont.ident;
|
||||
let params = Parameters::new(&cont);
|
||||
let (de_impl_generics, _, ty_generics, where_clause) = params.generics();
|
||||
let (de_impl_generics, _, ty_generics, where_clause) = params.generics_with_de_lifetime();
|
||||
let body = Stmts(deserialize_body(&cont, ¶ms));
|
||||
let delife = params.borrowed.de_lifetime();
|
||||
let allow_deprecated = allow_deprecated(input);
|
||||
@ -168,10 +168,10 @@ impl Parameters {
|
||||
self.this_type.segments.last().unwrap().ident.to_string()
|
||||
}
|
||||
|
||||
/// Split a deserialized type's generics into the pieces required for impl'ing
|
||||
/// a `Deserialize` trait for that type. Additionally appends the `'de` lifetime
|
||||
/// to list of impl generics.
|
||||
fn generics(
|
||||
/// Split the data structure's generics into the pieces to use for its
|
||||
/// `Deserialize` impl, augmented with an additional `'de` lifetime for use
|
||||
/// as the `Deserialize` trait's lifetime.
|
||||
fn generics_with_de_lifetime(
|
||||
&self,
|
||||
) -> (
|
||||
DeImplGenerics,
|
||||
@ -312,18 +312,18 @@ fn deserialize_body(cont: &Container, params: &Parameters) -> Fragment {
|
||||
deserialize_try_from(type_try_from)
|
||||
} else if let attr::Identifier::No = cont.attrs.identifier() {
|
||||
match &cont.data {
|
||||
Data::Enum(variants) => enum_::generate_body(params, variants, &cont.attrs),
|
||||
Data::Enum(variants) => enum_::deserialize(params, variants, &cont.attrs),
|
||||
Data::Struct(Style::Struct, fields) => {
|
||||
struct_::generate_body(params, fields, &cont.attrs, StructForm::Struct)
|
||||
struct_::deserialize(params, fields, &cont.attrs, StructForm::Struct)
|
||||
}
|
||||
Data::Struct(Style::Tuple, fields) | Data::Struct(Style::Newtype, fields) => {
|
||||
tuple::generate_body(params, fields, &cont.attrs, TupleForm::Tuple)
|
||||
tuple::deserialize(params, fields, &cont.attrs, TupleForm::Tuple)
|
||||
}
|
||||
Data::Struct(Style::Unit, _) => unit::generate_body(params, &cont.attrs),
|
||||
Data::Struct(Style::Unit, _) => unit::deserialize(params, &cont.attrs),
|
||||
}
|
||||
} else {
|
||||
match &cont.data {
|
||||
Data::Enum(variants) => identifier::generate_body(params, variants, &cont.attrs),
|
||||
Data::Enum(variants) => identifier::deserialize_custom(params, variants, &cont.attrs),
|
||||
Data::Struct(_, _) => unreachable!("checked in serde_derive_internals"),
|
||||
}
|
||||
}
|
||||
@ -349,10 +349,10 @@ fn deserialize_in_place_body(cont: &Container, params: &Parameters) -> Option<St
|
||||
|
||||
let code = match &cont.data {
|
||||
Data::Struct(Style::Struct, fields) => {
|
||||
struct_::generate_body_in_place(params, fields, &cont.attrs)?
|
||||
struct_::deserialize_in_place(params, fields, &cont.attrs)?
|
||||
}
|
||||
Data::Struct(Style::Tuple, fields) | Data::Struct(Style::Newtype, fields) => {
|
||||
tuple::generate_body_in_place(params, fields, &cont.attrs)
|
||||
tuple::deserialize_in_place(params, fields, &cont.attrs)
|
||||
}
|
||||
Data::Enum(_) | Data::Struct(Style::Unit, _) => {
|
||||
return None;
|
||||
@ -664,7 +664,8 @@ fn wrap_deserialize_with(
|
||||
deserialize_with: &syn::ExprPath,
|
||||
) -> (TokenStream, TokenStream) {
|
||||
let this_type = ¶ms.this_type;
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = params.generics();
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||
params.generics_with_de_lifetime();
|
||||
let delife = params.borrowed.de_lifetime();
|
||||
let deserializer_var = quote!(__deserializer);
|
||||
|
||||
|
@ -12,7 +12,7 @@ use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
|
||||
/// Generates `Deserialize::deserialize` body for an `enum Enum {...}`
|
||||
pub(super) fn generate_body(
|
||||
pub(super) fn deserialize(
|
||||
params: &Parameters,
|
||||
variants: &[Variant],
|
||||
cattrs: &attr::Container,
|
||||
@ -25,12 +25,12 @@ pub(super) fn generate_body(
|
||||
// Ignore any error associated with non-untagged deserialization so that we
|
||||
// can fall through to the untagged variants. This may be infallible so we
|
||||
// need to provide the error type.
|
||||
let tagged_frag = quote! {
|
||||
let first_attempt = quote! {
|
||||
if let _serde::#private::Result::<_, __D::Error>::Ok(__ok) = (|| #tagged_frag)() {
|
||||
return _serde::#private::Ok(__ok);
|
||||
}
|
||||
};
|
||||
enum_untagged::generate_body(params, untagged, cattrs, Some(tagged_frag))
|
||||
enum_untagged::deserialize(params, untagged, cattrs, Some(first_attempt))
|
||||
}
|
||||
None => deserialize_homogeneous_enum(params, variants, cattrs),
|
||||
}
|
||||
@ -42,14 +42,14 @@ fn deserialize_homogeneous_enum(
|
||||
cattrs: &attr::Container,
|
||||
) -> Fragment {
|
||||
match cattrs.tag() {
|
||||
attr::TagType::External => enum_externally::generate_body(params, variants, cattrs),
|
||||
attr::TagType::External => enum_externally::deserialize(params, variants, cattrs),
|
||||
attr::TagType::Internal { tag } => {
|
||||
enum_internally::generate_body(params, variants, cattrs, tag)
|
||||
enum_internally::deserialize(params, variants, cattrs, tag)
|
||||
}
|
||||
attr::TagType::Adjacent { tag, content } => {
|
||||
enum_adjacently::generate_body(params, variants, cattrs, tag, content)
|
||||
enum_adjacently::deserialize(params, variants, cattrs, tag, content)
|
||||
}
|
||||
attr::TagType::None => enum_untagged::generate_body(params, variants, cattrs, None),
|
||||
attr::TagType::None => enum_untagged::deserialize(params, variants, cattrs, None),
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,7 +84,7 @@ pub fn prepare_enum_variant_enum(variants: &[Variant]) -> (TokenStream, Stmts) {
|
||||
})
|
||||
.collect();
|
||||
|
||||
let variant_visitor = Stmts(identifier::generate_identifier(
|
||||
let variant_visitor = Stmts(identifier::deserialize_generated(
|
||||
&deserialized_variants,
|
||||
false, // variant identifiers do not depend on the presence of flatten fields
|
||||
true,
|
||||
|
@ -1,4 +1,4 @@
|
||||
//! Generator of the deserialization code for the adjacently tagged enums:
|
||||
//! Deserialization for adjacently tagged enums:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! #[serde(tag = "...", content = "...")]
|
||||
@ -16,7 +16,7 @@ use quote::{quote, quote_spanned};
|
||||
use syn::spanned::Spanned;
|
||||
|
||||
/// Generates `Deserialize::deserialize` body for an `enum Enum {...}` with `#[serde(tag, content)]` attributes
|
||||
pub(super) fn generate_body(
|
||||
pub(super) fn deserialize(
|
||||
params: &Parameters,
|
||||
variants: &[Variant],
|
||||
cattrs: &attr::Container,
|
||||
@ -25,7 +25,8 @@ pub(super) fn generate_body(
|
||||
) -> Fragment {
|
||||
let this_type = ¶ms.this_type;
|
||||
let this_value = ¶ms.this_value;
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = params.generics();
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||
params.generics_with_de_lifetime();
|
||||
let delife = params.borrowed.de_lifetime();
|
||||
|
||||
let (variants_stmt, variant_visitor) = enum_::prepare_enum_variant_enum(variants);
|
||||
@ -37,7 +38,7 @@ pub(super) fn generate_body(
|
||||
.map(|(i, variant)| {
|
||||
let variant_index = field_i(i);
|
||||
|
||||
let block = Match(enum_untagged::generate_variant(params, variant, cattrs));
|
||||
let block = Match(enum_untagged::deserialize_variant(params, variant, cattrs));
|
||||
|
||||
quote! {
|
||||
__Field::#variant_index => #block
|
||||
|
@ -1,4 +1,4 @@
|
||||
//! Generator of the deserialization code for the externally tagged enums:
|
||||
//! Deserialization for externally tagged enums:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! enum Enum {}
|
||||
@ -20,13 +20,14 @@ use quote::{quote, quote_spanned};
|
||||
use syn::spanned::Spanned;
|
||||
|
||||
/// Generates `Deserialize::deserialize` body for an `enum Enum {...}` without additional attributes
|
||||
pub(super) fn generate_body(
|
||||
pub(super) fn deserialize(
|
||||
params: &Parameters,
|
||||
variants: &[Variant],
|
||||
cattrs: &attr::Container,
|
||||
) -> Fragment {
|
||||
let this_type = ¶ms.this_type;
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = params.generics();
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||
params.generics_with_de_lifetime();
|
||||
let delife = params.borrowed.de_lifetime();
|
||||
|
||||
let type_name = cattrs.name().deserialize_name();
|
||||
@ -143,13 +144,13 @@ fn deserialize_externally_tagged_variant(
|
||||
&variant.fields[0],
|
||||
cattrs,
|
||||
),
|
||||
Style::Tuple => tuple::generate_body(
|
||||
Style::Tuple => tuple::deserialize(
|
||||
params,
|
||||
&variant.fields,
|
||||
cattrs,
|
||||
TupleForm::ExternallyTagged(variant_ident),
|
||||
),
|
||||
Style::Struct => struct_::generate_body(
|
||||
Style::Struct => struct_::deserialize(
|
||||
params,
|
||||
&variant.fields,
|
||||
cattrs,
|
||||
|
@ -1,4 +1,4 @@
|
||||
//! Generator of the deserialization code for the internally tagged enums:
|
||||
//! Deserialization for internally tagged enums:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! #[serde(tag = "...")]
|
||||
@ -18,7 +18,7 @@ use crate::private;
|
||||
use quote::quote;
|
||||
|
||||
/// Generates `Deserialize::deserialize` body for an `enum Enum {...}` with `#[serde(tag)]` attribute
|
||||
pub(super) fn generate_body(
|
||||
pub(super) fn deserialize(
|
||||
params: &Parameters,
|
||||
variants: &[Variant],
|
||||
cattrs: &attr::Container,
|
||||
@ -93,9 +93,9 @@ fn deserialize_internally_tagged_variant(
|
||||
}
|
||||
}
|
||||
Style::Newtype => {
|
||||
enum_untagged::generate_newtype_variant(variant_ident, params, &variant.fields[0])
|
||||
enum_untagged::deserialize_newtype_variant(variant_ident, params, &variant.fields[0])
|
||||
}
|
||||
Style::Struct => struct_::generate_body(
|
||||
Style::Struct => struct_::deserialize(
|
||||
params,
|
||||
&variant.fields,
|
||||
cattrs,
|
||||
|
@ -1,4 +1,4 @@
|
||||
//! Generator of the deserialization code for the untagged enums:
|
||||
//! Deserialization for untagged enums:
|
||||
//!
|
||||
//! ```ignore
|
||||
//! #[serde(untagged)]
|
||||
@ -19,7 +19,7 @@ use quote::{quote, quote_spanned};
|
||||
use syn::spanned::Spanned;
|
||||
|
||||
/// Generates `Deserialize::deserialize` body for an `enum Enum {...}` with `#[serde(untagged)]` attribute
|
||||
pub(super) fn generate_body(
|
||||
pub(super) fn deserialize(
|
||||
params: &Parameters,
|
||||
variants: &[Variant],
|
||||
cattrs: &attr::Container,
|
||||
@ -28,7 +28,7 @@ pub(super) fn generate_body(
|
||||
let attempts = variants
|
||||
.iter()
|
||||
.filter(|variant| !variant.attrs.skip_deserializing())
|
||||
.map(|variant| Expr(generate_variant(params, variant, cattrs)));
|
||||
.map(|variant| Expr(deserialize_variant(params, variant, cattrs)));
|
||||
// TODO this message could be better by saving the errors from the failed
|
||||
// attempts. The heuristic used by TOML was to count the number of fields
|
||||
// processed before an error, and use the error that happened after the
|
||||
@ -59,7 +59,7 @@ pub(super) fn generate_body(
|
||||
}
|
||||
|
||||
// Also used by adjacently tagged enums
|
||||
pub(super) fn generate_variant(
|
||||
pub(super) fn deserialize_variant(
|
||||
params: &Parameters,
|
||||
variant: &Variant,
|
||||
cattrs: &attr::Container,
|
||||
@ -92,14 +92,14 @@ pub(super) fn generate_variant(
|
||||
}
|
||||
}
|
||||
}
|
||||
Style::Newtype => generate_newtype_variant(variant_ident, params, &variant.fields[0]),
|
||||
Style::Tuple => tuple::generate_body(
|
||||
Style::Newtype => deserialize_newtype_variant(variant_ident, params, &variant.fields[0]),
|
||||
Style::Tuple => tuple::deserialize(
|
||||
params,
|
||||
&variant.fields,
|
||||
cattrs,
|
||||
TupleForm::Untagged(variant_ident),
|
||||
),
|
||||
Style::Struct => struct_::generate_body(
|
||||
Style::Struct => struct_::deserialize(
|
||||
params,
|
||||
&variant.fields,
|
||||
cattrs,
|
||||
@ -110,7 +110,7 @@ pub(super) fn generate_variant(
|
||||
|
||||
// Also used by internally tagged enums
|
||||
// Implicitly (via `generate_variant`) used by adjacently tagged enums
|
||||
pub(super) fn generate_newtype_variant(
|
||||
pub(super) fn deserialize_newtype_variant(
|
||||
variant_ident: &syn::Ident,
|
||||
params: &Parameters,
|
||||
field: &Field,
|
||||
|
@ -1,4 +1,5 @@
|
||||
//! Contains generators of enums that represents identifiers of fields in structs or variants in enums.
|
||||
//! Deserialization of struct field identifiers and enum variant identifiers by
|
||||
//! way of a Rust enum.
|
||||
|
||||
use crate::de::{FieldWithAliases, Parameters};
|
||||
use crate::fragment::{Fragment, Stmts};
|
||||
@ -10,7 +11,7 @@ use quote::{quote, ToTokens};
|
||||
|
||||
// Generates `Deserialize::deserialize` body for an enum with
|
||||
// `serde(field_identifier)` or `serde(variant_identifier)` attribute.
|
||||
pub(super) fn generate_body(
|
||||
pub(super) fn deserialize_custom(
|
||||
params: &Parameters,
|
||||
variants: &[Variant],
|
||||
cattrs: &attr::Container,
|
||||
@ -84,7 +85,8 @@ pub(super) fn generate_body(
|
||||
Some(fields)
|
||||
};
|
||||
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = params.generics();
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||
params.generics_with_de_lifetime();
|
||||
let delife = params.borrowed.de_lifetime();
|
||||
let visitor_impl = Stmts(deserialize_identifier(
|
||||
&this_value,
|
||||
@ -120,7 +122,7 @@ pub(super) fn generate_body(
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn generate_identifier(
|
||||
pub(super) fn deserialize_generated(
|
||||
deserialized_fields: &[FieldWithAliases],
|
||||
has_flatten: bool,
|
||||
is_variant: bool,
|
||||
|
@ -14,7 +14,7 @@ use quote::{quote, quote_spanned};
|
||||
use syn::spanned::Spanned;
|
||||
|
||||
/// Generates `Deserialize::deserialize` body for a `struct Struct {...}`
|
||||
pub(super) fn generate_body(
|
||||
pub(super) fn deserialize(
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
cattrs: &attr::Container,
|
||||
@ -22,7 +22,8 @@ pub(super) fn generate_body(
|
||||
) -> Fragment {
|
||||
let this_type = ¶ms.this_type;
|
||||
let this_value = ¶ms.this_value;
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = params.generics();
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||
params.generics_with_de_lifetime();
|
||||
let delife = params.borrowed.de_lifetime();
|
||||
|
||||
// If there are getters (implying private fields), construct the local type
|
||||
@ -419,7 +420,7 @@ fn deserialize_map(
|
||||
|
||||
/// Generates `Deserialize::deserialize_in_place` body for a `struct Struct {...}`
|
||||
#[cfg(feature = "deserialize_in_place")]
|
||||
pub(super) fn generate_body_in_place(
|
||||
pub(super) fn deserialize_in_place(
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
cattrs: &attr::Container,
|
||||
@ -431,7 +432,8 @@ pub(super) fn generate_body_in_place(
|
||||
}
|
||||
|
||||
let this_type = ¶ms.this_type;
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = params.generics();
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||
params.generics_with_de_lifetime();
|
||||
let delife = params.borrowed.de_lifetime();
|
||||
|
||||
let expecting = format!("struct {}", params.type_name());
|
||||
@ -633,7 +635,7 @@ fn deserialize_map_in_place(
|
||||
});
|
||||
|
||||
let this_type = ¶ms.this_type;
|
||||
let (_, _, ty_generics, _) = params.generics();
|
||||
let (_, ty_generics, _) = params.generics.split_for_impl();
|
||||
|
||||
let let_default = match cattrs.default() {
|
||||
attr::Default::Default => Some(quote!(
|
||||
@ -685,7 +687,7 @@ fn deserialize_field_identifier(
|
||||
(Some(ignore_variant), Some(fallthrough))
|
||||
};
|
||||
|
||||
Stmts(identifier::generate_identifier(
|
||||
Stmts(identifier::deserialize_generated(
|
||||
deserialized_fields,
|
||||
has_flatten,
|
||||
false,
|
||||
|
@ -10,7 +10,7 @@ use quote::{quote, quote_spanned};
|
||||
use syn::spanned::Spanned;
|
||||
|
||||
/// Generates `Deserialize::deserialize` body for a `struct Tuple(...);` including `struct Newtype(T);`
|
||||
pub(super) fn generate_body(
|
||||
pub(super) fn deserialize(
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
cattrs: &attr::Container,
|
||||
@ -28,7 +28,8 @@ pub(super) fn generate_body(
|
||||
|
||||
let this_type = ¶ms.this_type;
|
||||
let this_value = ¶ms.this_value;
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = params.generics();
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||
params.generics_with_de_lifetime();
|
||||
let delife = params.borrowed.de_lifetime();
|
||||
|
||||
// If there are getters (implying private fields), construct the local type
|
||||
@ -182,7 +183,7 @@ fn deserialize_newtype_struct(
|
||||
|
||||
/// Generates `Deserialize::deserialize_in_place` body for a `struct Tuple(...);` including `struct Newtype(T);`
|
||||
#[cfg(feature = "deserialize_in_place")]
|
||||
pub(super) fn generate_body_in_place(
|
||||
pub(super) fn deserialize_in_place(
|
||||
params: &Parameters,
|
||||
fields: &[Field],
|
||||
cattrs: &attr::Container,
|
||||
@ -198,7 +199,8 @@ pub(super) fn generate_body_in_place(
|
||||
.count();
|
||||
|
||||
let this_type = ¶ms.this_type;
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = params.generics();
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||
params.generics_with_de_lifetime();
|
||||
let delife = params.borrowed.de_lifetime();
|
||||
|
||||
let expecting = format!("tuple struct {}", params.type_name());
|
||||
|
@ -5,11 +5,12 @@ use crate::private;
|
||||
use quote::quote;
|
||||
|
||||
/// Generates `Deserialize::deserialize` body for a `struct Unit;`
|
||||
pub(super) fn generate_body(params: &Parameters, cattrs: &attr::Container) -> Fragment {
|
||||
pub(super) fn deserialize(params: &Parameters, cattrs: &attr::Container) -> Fragment {
|
||||
let this_type = ¶ms.this_type;
|
||||
let this_value = ¶ms.this_value;
|
||||
let type_name = cattrs.name().deserialize_name();
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) = params.generics();
|
||||
let (de_impl_generics, de_ty_generics, ty_generics, where_clause) =
|
||||
params.generics_with_de_lifetime();
|
||||
let delife = params.borrowed.de_lifetime();
|
||||
|
||||
let expecting = format!("unit struct {}", params.type_name());
|
||||
|
Loading…
x
Reference in New Issue
Block a user