2017-04-13 19:34:42 -05:00
|
|
|
// Copyright 2017 Serde Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-01-08 23:49:09 -06:00
|
|
|
use proc_macro2::Span;
|
2018-04-13 00:58:24 -05:00
|
|
|
use quote::Tokens;
|
|
|
|
use syn::spanned::Spanned;
|
|
|
|
use syn::{self, Ident, Index, Member};
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2016-04-13 02:34:29 -05:00
|
|
|
use bound;
|
2017-12-23 22:13:08 -06:00
|
|
|
use fragment::{Fragment, Match, Stmts};
|
2018-04-13 00:58:24 -05:00
|
|
|
use internals::ast::{Container, Data, Field, Style, Variant};
|
2017-04-14 18:09:00 -05:00
|
|
|
use internals::{attr, Ctxt};
|
2016-02-13 00:05:02 -06:00
|
|
|
|
2017-04-09 15:50:38 -05:00
|
|
|
use std::u32;
|
|
|
|
|
2017-04-14 17:52:56 -05:00
|
|
|
pub fn expand_derive_serialize(input: &syn::DeriveInput) -> Result<Tokens, String> {
|
2017-04-14 18:09:00 -05:00
|
|
|
let ctxt = Ctxt::new();
|
2017-04-14 17:52:56 -05:00
|
|
|
let cont = Container::from_ast(&ctxt, input);
|
2017-04-14 18:09:00 -05:00
|
|
|
precondition(&ctxt, &cont);
|
2016-09-27 02:11:37 -05:00
|
|
|
try!(ctxt.check());
|
2016-06-19 22:15:49 -05:00
|
|
|
|
2017-04-14 17:52:56 -05:00
|
|
|
let ident = &cont.ident;
|
|
|
|
let params = Parameters::new(&cont);
|
2017-04-09 00:42:42 -05:00
|
|
|
let (impl_generics, ty_generics, where_clause) = params.generics.split_for_impl();
|
2018-03-28 03:42:53 -05:00
|
|
|
let dummy_const = Ident::new(&format!("_IMPL_SERIALIZE_FOR_{}", ident), Span::call_site());
|
2017-04-14 17:52:56 -05:00
|
|
|
let body = Stmts(serialize_body(&cont, ¶ms));
|
2016-04-24 12:59:57 -05:00
|
|
|
|
2017-04-14 17:52:56 -05:00
|
|
|
let impl_block = if let Some(remote) = cont.attrs.remote() {
|
2017-07-26 01:52:06 -05:00
|
|
|
let vis = &input.vis;
|
2018-01-10 00:22:08 -06:00
|
|
|
let fun = quote_spanned!(Span::call_site()=> serialize);
|
2017-04-09 00:42:42 -05:00
|
|
|
quote! {
|
|
|
|
impl #impl_generics #ident #ty_generics #where_clause {
|
2018-01-10 00:22:08 -06:00
|
|
|
#vis fn #fun<__S>(__self: &#remote #ty_generics, __serializer: __S) -> _serde::export::Result<__S::Ok, __S::Error>
|
2017-04-09 00:42:42 -05:00
|
|
|
where __S: _serde::Serializer
|
|
|
|
{
|
|
|
|
#body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quote! {
|
2016-04-24 12:59:57 -05:00
|
|
|
#[automatically_derived]
|
2017-02-20 13:50:24 -06:00
|
|
|
impl #impl_generics _serde::Serialize for #ident #ty_generics #where_clause {
|
2017-03-18 12:33:22 -05:00
|
|
|
fn serialize<__S>(&self, __serializer: __S) -> _serde::export::Result<__S::Ok, __S::Error>
|
2017-01-08 02:47:37 -06:00
|
|
|
where __S: _serde::Serializer
|
2016-09-10 23:53:14 -05:00
|
|
|
{
|
|
|
|
#body
|
|
|
|
}
|
2015-03-14 16:57:05 -05:00
|
|
|
}
|
2017-04-09 00:42:42 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-19 17:56:22 -05:00
|
|
|
let generated = quote! {
|
2017-04-09 00:42:42 -05:00
|
|
|
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
|
|
|
|
const #dummy_const: () = {
|
|
|
|
extern crate serde as _serde;
|
2017-04-14 17:52:56 -05:00
|
|
|
#impl_block
|
2016-04-24 12:59:57 -05:00
|
|
|
};
|
2017-04-19 17:56:22 -05:00
|
|
|
};
|
|
|
|
Ok(generated)
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
2017-04-14 18:09:00 -05:00
|
|
|
fn precondition(cx: &Ctxt, cont: &Container) {
|
|
|
|
match cont.attrs.identifier() {
|
|
|
|
attr::Identifier::No => {}
|
|
|
|
attr::Identifier::Field => {
|
|
|
|
cx.error("field identifiers cannot be serialized");
|
|
|
|
}
|
|
|
|
attr::Identifier::Variant => {
|
|
|
|
cx.error("variant identifiers cannot be serialized");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-09 00:42:42 -05:00
|
|
|
struct Parameters {
|
|
|
|
/// Variable holding the value being serialized. Either `self` for local
|
|
|
|
/// types or `__self` for remote types.
|
|
|
|
self_var: Ident,
|
|
|
|
|
|
|
|
/// Path to the type the impl is for. Either a single `Ident` for local
|
|
|
|
/// types or `some::remote::Ident` for remote types. Does not include
|
|
|
|
/// generic parameters.
|
|
|
|
this: syn::Path,
|
|
|
|
|
|
|
|
/// Generics including any explicit and inferred bounds for the impl.
|
|
|
|
generics: syn::Generics,
|
2017-04-10 14:12:00 -05:00
|
|
|
|
|
|
|
/// Type has a `serde(remote = "...")` attribute.
|
|
|
|
is_remote: bool,
|
2017-04-09 00:42:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Parameters {
|
2017-04-14 17:52:56 -05:00
|
|
|
fn new(cont: &Container) -> Self {
|
|
|
|
let is_remote = cont.attrs.remote().is_some();
|
2017-04-10 14:12:00 -05:00
|
|
|
let self_var = if is_remote {
|
2018-03-28 03:42:53 -05:00
|
|
|
Ident::new("__self", Span::call_site())
|
2017-04-09 00:42:42 -05:00
|
|
|
} else {
|
2018-03-28 03:42:53 -05:00
|
|
|
Ident::new("self", Span::call_site())
|
2017-04-09 00:42:42 -05:00
|
|
|
};
|
|
|
|
|
2017-04-14 17:52:56 -05:00
|
|
|
let this = match cont.attrs.remote() {
|
2017-04-09 00:42:42 -05:00
|
|
|
Some(remote) => remote.clone(),
|
2018-01-09 21:34:35 -06:00
|
|
|
None => cont.ident.into(),
|
2017-04-09 00:42:42 -05:00
|
|
|
};
|
|
|
|
|
2017-04-14 17:52:56 -05:00
|
|
|
let generics = build_generics(cont);
|
2017-04-09 00:42:42 -05:00
|
|
|
|
|
|
|
Parameters {
|
|
|
|
self_var: self_var,
|
|
|
|
this: this,
|
|
|
|
generics: generics,
|
2017-04-10 14:12:00 -05:00
|
|
|
is_remote: is_remote,
|
2017-04-09 00:42:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Type name to use in error messages and `&'static str` arguments to
|
|
|
|
/// various Serializer methods.
|
|
|
|
fn type_name(&self) -> &str {
|
2018-01-08 23:49:09 -06:00
|
|
|
self.this.segments.last().unwrap().value().ident.as_ref()
|
2017-04-09 00:42:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-29 23:53:58 -06:00
|
|
|
// All the generics in the input, plus a bound `T: Serialize` for each generic
|
|
|
|
// field type that will be serialized by us.
|
2017-04-14 17:52:56 -05:00
|
|
|
fn build_generics(cont: &Container) -> syn::Generics {
|
|
|
|
let generics = bound::without_defaults(cont.generics);
|
feat(codegen): Inhibit generic bounds if skip_serializing
The generated code for a struct like:
struct Test<A, B, C> {
a: X<A>
#[serde(skip_serializing)]
b: B
#[serde(serialize_with="...")]
c: C
}
Used to be:
impl<A, B, C> Serialize for Test<A, B, C>
where A: Serialize,
B: Serialize,
C: Serialize,
{ ... }
Now it is:
impl<A, B, C> Serialize for Test<A, B, C>
where X<A>: Serialize,
{ ... }
Both `skip_serializing` and `serialize_with` mean the type does not need to
implement `Serialize`.
2016-02-28 14:17:29 -06:00
|
|
|
|
2018-04-13 00:39:26 -05:00
|
|
|
let generics =
|
|
|
|
bound::with_where_predicates_from_fields(cont, &generics, attr::Field::ser_bound);
|
2016-05-21 00:03:20 -05:00
|
|
|
|
2017-04-14 17:52:56 -05:00
|
|
|
match cont.attrs.ser_bound() {
|
2017-02-12 23:59:04 -06:00
|
|
|
Some(predicates) => bound::with_where_predicates(&generics, predicates),
|
2017-12-23 22:13:08 -06:00
|
|
|
None => bound::with_bound(
|
|
|
|
cont,
|
|
|
|
&generics,
|
|
|
|
needs_serialize_bound,
|
2018-04-13 00:39:26 -05:00
|
|
|
&parse_quote!(_serde::Serialize),
|
2017-12-23 22:13:08 -06:00
|
|
|
),
|
feat(codegen): Inhibit generic bounds if skip_serializing
The generated code for a struct like:
struct Test<A, B, C> {
a: X<A>
#[serde(skip_serializing)]
b: B
#[serde(serialize_with="...")]
c: C
}
Used to be:
impl<A, B, C> Serialize for Test<A, B, C>
where A: Serialize,
B: Serialize,
C: Serialize,
{ ... }
Now it is:
impl<A, B, C> Serialize for Test<A, B, C>
where X<A>: Serialize,
{ ... }
Both `skip_serializing` and `serialize_with` mean the type does not need to
implement `Serialize`.
2016-02-28 14:17:29 -06:00
|
|
|
}
|
2016-05-21 00:03:20 -05:00
|
|
|
}
|
|
|
|
|
2017-08-07 19:22:26 -05:00
|
|
|
// Fields with a `skip_serializing` or `serialize_with` attribute, or which
|
|
|
|
// belong to a variant with a `serialize_with` attribute, are not serialized 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:
|
|
|
|
// Serialize` bound where T is the type of the field.
|
|
|
|
fn needs_serialize_bound(field: &attr::Field, variant: Option<&attr::Variant>) -> bool {
|
2017-12-23 22:13:08 -06:00
|
|
|
!field.skip_serializing() && field.serialize_with().is_none() && field.ser_bound().is_none()
|
|
|
|
&& variant.map_or(true, |variant| variant.serialize_with().is_none())
|
feat(codegen): Inhibit generic bounds if skip_serializing
The generated code for a struct like:
struct Test<A, B, C> {
a: X<A>
#[serde(skip_serializing)]
b: B
#[serde(serialize_with="...")]
c: C
}
Used to be:
impl<A, B, C> Serialize for Test<A, B, C>
where A: Serialize,
B: Serialize,
C: Serialize,
{ ... }
Now it is:
impl<A, B, C> Serialize for Test<A, B, C>
where X<A>: Serialize,
{ ... }
Both `skip_serializing` and `serialize_with` mean the type does not need to
implement `Serialize`.
2016-02-28 14:17:29 -06:00
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:56 -05:00
|
|
|
fn serialize_body(cont: &Container, params: &Parameters) -> Fragment {
|
2018-01-09 21:21:01 -06:00
|
|
|
if let Some(type_into) = cont.attrs.type_into() {
|
|
|
|
serialize_into(params, type_into)
|
2017-03-18 11:22:27 -05:00
|
|
|
} else {
|
2018-01-08 23:49:09 -06:00
|
|
|
match cont.data {
|
|
|
|
Data::Enum(ref variants) => serialize_enum(params, variants, &cont.attrs),
|
|
|
|
Data::Struct(Style::Struct, ref fields) => {
|
2017-03-18 11:22:27 -05:00
|
|
|
if fields.iter().any(|field| field.ident.is_none()) {
|
|
|
|
panic!("struct has unnamed fields");
|
|
|
|
}
|
2017-04-14 17:52:56 -05:00
|
|
|
serialize_struct(params, fields, &cont.attrs)
|
2015-10-17 21:44:07 -05:00
|
|
|
}
|
2018-01-08 23:49:09 -06:00
|
|
|
Data::Struct(Style::Tuple, ref fields) => {
|
2017-03-18 11:22:27 -05:00
|
|
|
if fields.iter().any(|field| field.ident.is_some()) {
|
|
|
|
panic!("tuple struct has named fields");
|
|
|
|
}
|
2017-04-14 17:52:56 -05:00
|
|
|
serialize_tuple_struct(params, fields, &cont.attrs)
|
2017-03-18 11:22:27 -05:00
|
|
|
}
|
2018-01-08 23:49:09 -06:00
|
|
|
Data::Struct(Style::Newtype, ref fields) => {
|
2017-04-14 17:52:56 -05:00
|
|
|
serialize_newtype_struct(params, &fields[0], &cont.attrs)
|
2017-03-18 11:22:27 -05:00
|
|
|
}
|
2018-01-08 23:49:09 -06:00
|
|
|
Data::Struct(Style::Unit, _) => serialize_unit_struct(&cont.attrs),
|
2015-03-14 16:57:05 -05:00
|
|
|
}
|
2017-03-18 11:22:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 21:21:01 -06:00
|
|
|
fn serialize_into(params: &Parameters, type_into: &syn::Type) -> Fragment {
|
2017-04-09 00:42:42 -05:00
|
|
|
let self_var = ¶ms.self_var;
|
2017-03-18 11:22:27 -05:00
|
|
|
quote_block! {
|
2017-03-18 12:33:22 -05:00
|
|
|
_serde::Serialize::serialize(
|
2018-01-09 21:21:01 -06:00
|
|
|
&_serde::export::Into::<#type_into>::into(_serde::export::Clone::clone(#self_var)),
|
2017-03-18 12:33:22 -05:00
|
|
|
__serializer)
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:56 -05:00
|
|
|
fn serialize_unit_struct(cattrs: &attr::Container) -> Fragment {
|
|
|
|
let type_name = cattrs.name().serialize_name();
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_expr! {
|
2017-03-18 12:33:22 -05:00
|
|
|
_serde::Serializer::serialize_unit_struct(__serializer, #type_name)
|
2016-09-10 23:53:14 -05:00
|
|
|
}
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_newtype_struct(
|
|
|
|
params: &Parameters,
|
|
|
|
field: &Field,
|
2017-04-14 17:52:56 -05:00
|
|
|
cattrs: &attr::Container,
|
2017-04-13 14:28:23 -05:00
|
|
|
) -> Fragment {
|
2017-04-14 17:52:56 -05:00
|
|
|
let type_name = cattrs.name().serialize_name();
|
2015-07-30 11:38:09 -05:00
|
|
|
|
2018-04-13 00:58:24 -05:00
|
|
|
let mut field_expr = get_member(
|
|
|
|
params,
|
|
|
|
field,
|
|
|
|
&Member::Unnamed(Index {
|
|
|
|
index: 0,
|
|
|
|
span: Span::call_site(),
|
|
|
|
}),
|
|
|
|
);
|
2016-06-15 01:37:20 -05:00
|
|
|
if let Some(path) = field.attrs.serialize_with() {
|
2018-01-09 21:34:35 -06:00
|
|
|
field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr);
|
2016-05-15 17:54:20 -05:00
|
|
|
}
|
|
|
|
|
2018-03-31 16:44:50 -05:00
|
|
|
let span = field.original.span();
|
2018-01-10 21:22:07 -06:00
|
|
|
let func = quote_spanned!(span=> _serde::Serializer::serialize_newtype_struct);
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_expr! {
|
2018-01-10 21:22:07 -06:00
|
|
|
#func(__serializer, #type_name, #field_expr)
|
2016-09-10 23:53:14 -05:00
|
|
|
}
|
2015-07-30 11:38:09 -05:00
|
|
|
}
|
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_tuple_struct(
|
|
|
|
params: &Parameters,
|
|
|
|
fields: &[Field],
|
2017-04-14 17:52:56 -05:00
|
|
|
cattrs: &attr::Container,
|
2017-04-13 14:28:23 -05:00
|
|
|
) -> Fragment {
|
2018-04-13 00:58:24 -05:00
|
|
|
let serialize_stmts =
|
|
|
|
serialize_tuple_struct_visitor(fields, params, false, &TupleTrait::SerializeTupleStruct);
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2017-04-14 17:52:56 -05:00
|
|
|
let type_name = cattrs.name().serialize_name();
|
2016-07-12 08:44:19 -05:00
|
|
|
let len = serialize_stmts.len();
|
2016-09-10 23:53:14 -05:00
|
|
|
let let_mut = mut_if(len > 0);
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_block! {
|
2017-03-18 12:33:22 -05:00
|
|
|
let #let_mut __serde_state = try!(_serde::Serializer::serialize_tuple_struct(__serializer, #type_name, #len));
|
2016-10-03 23:09:52 -05:00
|
|
|
#(#serialize_stmts)*
|
2017-01-14 23:39:37 -06:00
|
|
|
_serde::ser::SerializeTupleStruct::end(__serde_state)
|
2016-09-27 00:13:53 -05:00
|
|
|
}
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
2017-04-14 17:52:56 -05:00
|
|
|
fn serialize_struct(params: &Parameters, fields: &[Field], cattrs: &attr::Container) -> Fragment {
|
2018-01-09 21:34:35 -06:00
|
|
|
assert!(fields.len() as u64 <= u64::from(u32::MAX));
|
2017-04-09 15:50:38 -05:00
|
|
|
|
2018-03-18 12:30:46 -05:00
|
|
|
if cattrs.has_flatten() {
|
|
|
|
serialize_struct_as_map(params, fields, cattrs)
|
|
|
|
} else {
|
|
|
|
serialize_struct_as_struct(params, fields, cattrs)
|
2018-03-14 07:00:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-13 00:58:24 -05:00
|
|
|
fn serialize_struct_as_struct(
|
|
|
|
params: &Parameters,
|
|
|
|
fields: &[Field],
|
|
|
|
cattrs: &attr::Container,
|
|
|
|
) -> Fragment {
|
|
|
|
let serialize_fields =
|
|
|
|
serialize_struct_visitor(fields, params, false, &StructTrait::SerializeStruct);
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2017-04-14 17:52:56 -05:00
|
|
|
let type_name = cattrs.name().serialize_name();
|
2016-09-02 13:42:26 -05:00
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
let mut serialized_fields = fields
|
|
|
|
.iter()
|
2016-07-12 08:44:19 -05:00
|
|
|
.filter(|&field| !field.attrs.skip_serializing())
|
2016-09-05 10:22:17 -05:00
|
|
|
.peekable();
|
2016-09-02 13:42:26 -05:00
|
|
|
|
2016-09-10 23:53:14 -05:00
|
|
|
let let_mut = mut_if(serialized_fields.peek().is_some());
|
2016-09-05 10:22:17 -05:00
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
let len = serialized_fields
|
2017-12-23 22:13:08 -06:00
|
|
|
.map(|field| match field.attrs.skip_serializing_if() {
|
|
|
|
None => quote!(1),
|
|
|
|
Some(path) => {
|
2018-01-09 21:34:35 -06:00
|
|
|
let ident = field.ident.expect("struct has unnamed fields");
|
|
|
|
let field_expr = get_member(params, field, &Member::Named(ident));
|
2017-12-23 22:13:08 -06:00
|
|
|
quote!(if #path(#field_expr) { 0 } else { 1 })
|
|
|
|
}
|
|
|
|
})
|
2016-09-10 23:53:14 -05:00
|
|
|
.fold(quote!(0), |sum, expr| quote!(#sum + #expr));
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_block! {
|
2017-03-18 12:33:22 -05:00
|
|
|
let #let_mut __serde_state = try!(_serde::Serializer::serialize_struct(__serializer, #type_name, #len));
|
2016-10-03 23:09:52 -05:00
|
|
|
#(#serialize_fields)*
|
2017-01-14 23:39:37 -06:00
|
|
|
_serde::ser::SerializeStruct::end(__serde_state)
|
2016-09-27 00:13:53 -05:00
|
|
|
}
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
2018-04-13 00:58:24 -05:00
|
|
|
fn serialize_struct_as_map(
|
|
|
|
params: &Parameters,
|
|
|
|
fields: &[Field],
|
|
|
|
cattrs: &attr::Container,
|
|
|
|
) -> Fragment {
|
|
|
|
let serialize_fields =
|
|
|
|
serialize_struct_visitor(fields, params, false, &StructTrait::SerializeMap);
|
2018-03-14 07:00:16 -05:00
|
|
|
|
|
|
|
let mut serialized_fields = fields
|
|
|
|
.iter()
|
|
|
|
.filter(|&field| !field.attrs.skip_serializing())
|
|
|
|
.peekable();
|
|
|
|
|
|
|
|
let let_mut = mut_if(serialized_fields.peek().is_some());
|
|
|
|
|
2018-03-15 08:30:38 -05:00
|
|
|
let len = if cattrs.has_flatten() {
|
2018-03-20 07:35:16 -05:00
|
|
|
quote!(_serde::export::None)
|
2018-03-14 07:00:16 -05:00
|
|
|
} else {
|
|
|
|
let len = serialized_fields
|
|
|
|
.map(|field| match field.attrs.skip_serializing_if() {
|
|
|
|
None => quote!(1),
|
|
|
|
Some(path) => {
|
|
|
|
let ident = field.ident.expect("struct has unnamed fields");
|
|
|
|
let field_expr = get_member(params, field, &Member::Named(ident));
|
|
|
|
quote!(if #path(#field_expr) { 0 } else { 1 })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.fold(quote!(0), |sum, expr| quote!(#sum + #expr));
|
2018-03-20 07:35:16 -05:00
|
|
|
quote!(_serde::export::Some(#len))
|
2018-03-14 07:00:16 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
quote_block! {
|
|
|
|
let #let_mut __serde_state = try!(_serde::Serializer::serialize_map(__serializer, #len));
|
|
|
|
#(#serialize_fields)*
|
|
|
|
_serde::ser::SerializeMap::end(__serde_state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 16:23:21 -05:00
|
|
|
fn serialize_enum(params: &Parameters, variants: &[Variant], cattrs: &attr::Container) -> Fragment {
|
2018-01-09 21:34:35 -06:00
|
|
|
assert!(variants.len() as u64 <= u64::from(u32::MAX));
|
2017-04-09 15:50:38 -05:00
|
|
|
|
2017-04-09 00:42:42 -05:00
|
|
|
let self_var = ¶ms.self_var;
|
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
let arms: Vec<_> = variants
|
|
|
|
.iter()
|
2017-02-12 23:59:04 -06:00
|
|
|
.enumerate()
|
2017-12-23 22:13:08 -06:00
|
|
|
.map(|(variant_index, variant)| {
|
|
|
|
serialize_variant(params, variant, variant_index as u32, cattrs)
|
|
|
|
})
|
2017-02-12 23:59:04 -06:00
|
|
|
.collect();
|
2015-03-14 16:57:05 -05:00
|
|
|
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_expr! {
|
2017-04-09 00:42:42 -05:00
|
|
|
match *#self_var {
|
2016-10-03 23:09:52 -05:00
|
|
|
#(#arms)*
|
2015-03-14 16:57:05 -05:00
|
|
|
}
|
2016-09-10 23:53:14 -05:00
|
|
|
}
|
2015-03-14 16:57:05 -05:00
|
|
|
}
|
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_variant(
|
|
|
|
params: &Parameters,
|
|
|
|
variant: &Variant,
|
|
|
|
variant_index: u32,
|
2017-04-14 17:52:56 -05:00
|
|
|
cattrs: &attr::Container,
|
2017-04-13 14:28:23 -05:00
|
|
|
) -> Tokens {
|
2017-04-09 00:42:42 -05:00
|
|
|
let this = ¶ms.this;
|
2018-01-09 21:34:35 -06:00
|
|
|
let variant_ident = variant.ident;
|
2016-12-24 11:16:28 -06:00
|
|
|
|
2016-12-24 00:37:23 -06:00
|
|
|
if variant.attrs.skip_serializing() {
|
2017-04-13 14:28:23 -05:00
|
|
|
let skipped_msg = format!(
|
|
|
|
"the enum variant {}::{} cannot be serialized",
|
|
|
|
params.type_name(),
|
|
|
|
variant_ident
|
|
|
|
);
|
2016-12-24 12:10:06 -06:00
|
|
|
let skipped_err = quote! {
|
2017-02-01 11:42:12 -06:00
|
|
|
_serde::export::Err(_serde::ser::Error::custom(#skipped_msg))
|
2016-12-24 12:10:06 -06:00
|
|
|
};
|
|
|
|
let fields_pat = match variant.style {
|
|
|
|
Style::Unit => quote!(),
|
2017-04-13 14:28:23 -05:00
|
|
|
Style::Newtype | Style::Tuple => quote!((..)),
|
2017-12-23 22:13:08 -06:00
|
|
|
Style::Struct => quote!({ .. }),
|
2016-12-24 12:10:06 -06:00
|
|
|
};
|
|
|
|
quote! {
|
2017-04-09 00:42:42 -05:00
|
|
|
#this::#variant_ident #fields_pat => #skipped_err,
|
2015-03-14 16:57:05 -05:00
|
|
|
}
|
2017-02-12 23:59:04 -06:00
|
|
|
} else {
|
|
|
|
// variant wasn't skipped
|
2017-02-02 14:10:07 -06:00
|
|
|
let case = match variant.style {
|
2016-12-24 00:37:23 -06:00
|
|
|
Style::Unit => {
|
|
|
|
quote! {
|
2017-04-09 00:42:42 -05:00
|
|
|
#this::#variant_ident
|
2016-12-24 00:37:23 -06:00
|
|
|
}
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
2016-12-24 00:37:23 -06:00
|
|
|
Style::Newtype => {
|
|
|
|
quote! {
|
2017-04-09 00:42:42 -05:00
|
|
|
#this::#variant_ident(ref __field0)
|
2016-12-24 00:37:23 -06:00
|
|
|
}
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
2016-12-24 00:37:23 -06:00
|
|
|
Style::Tuple => {
|
2018-04-13 00:58:24 -05:00
|
|
|
let field_names = (0..variant.fields.len())
|
|
|
|
.map(|i| Ident::new(&format!("__field{}", i), Span::call_site()));
|
2016-12-24 00:37:23 -06:00
|
|
|
quote! {
|
2017-04-09 00:42:42 -05:00
|
|
|
#this::#variant_ident(#(ref #field_names),*)
|
2016-12-24 00:37:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Style::Struct => {
|
2017-04-13 14:28:23 -05:00
|
|
|
let fields = variant
|
|
|
|
.fields
|
2017-02-12 23:59:04 -06:00
|
|
|
.iter()
|
2018-01-09 21:34:35 -06:00
|
|
|
.map(|f| f.ident.expect("struct variant has unnamed fields"));
|
2017-02-02 14:10:07 -06:00
|
|
|
quote! {
|
2017-04-09 00:42:42 -05:00
|
|
|
#this::#variant_ident { #(ref #fields),* }
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-03-14 16:57:05 -05:00
|
|
|
|
2017-12-23 22:13:08 -06:00
|
|
|
let body = Match(match *cattrs.tag() {
|
|
|
|
attr::EnumTag::External => {
|
|
|
|
serialize_externally_tagged_variant(params, variant, variant_index, cattrs)
|
|
|
|
}
|
|
|
|
attr::EnumTag::Internal { ref tag } => {
|
|
|
|
serialize_internally_tagged_variant(params, variant, cattrs, tag)
|
|
|
|
}
|
|
|
|
attr::EnumTag::Adjacent {
|
|
|
|
ref tag,
|
|
|
|
ref content,
|
|
|
|
} => serialize_adjacently_tagged_variant(params, variant, cattrs, tag, content),
|
|
|
|
attr::EnumTag::None => serialize_untagged_variant(params, variant, cattrs),
|
|
|
|
});
|
2017-02-02 14:10:07 -06:00
|
|
|
|
|
|
|
quote! {
|
|
|
|
#case => #body
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_externally_tagged_variant(
|
|
|
|
params: &Parameters,
|
|
|
|
variant: &Variant,
|
|
|
|
variant_index: u32,
|
2017-04-14 17:52:56 -05:00
|
|
|
cattrs: &attr::Container,
|
2017-04-13 14:28:23 -05:00
|
|
|
) -> Fragment {
|
2017-04-14 17:52:56 -05:00
|
|
|
let type_name = cattrs.name().serialize_name();
|
2017-02-02 14:10:07 -06:00
|
|
|
let variant_name = variant.attrs.name().serialize_name();
|
|
|
|
|
2017-08-07 19:22:26 -05:00
|
|
|
if let Some(path) = variant.attrs.serialize_with() {
|
2018-01-09 21:34:35 -06:00
|
|
|
let ser = wrap_serialize_variant_with(params, path, variant);
|
2017-08-07 19:22:26 -05:00
|
|
|
return quote_expr! {
|
|
|
|
_serde::Serializer::serialize_newtype_variant(
|
|
|
|
__serializer,
|
|
|
|
#type_name,
|
|
|
|
#variant_index,
|
|
|
|
#variant_name,
|
|
|
|
#ser,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-02 14:10:07 -06:00
|
|
|
match variant.style {
|
|
|
|
Style::Unit => {
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_expr! {
|
2017-02-02 14:10:07 -06:00
|
|
|
_serde::Serializer::serialize_unit_variant(
|
2017-03-18 12:33:22 -05:00
|
|
|
__serializer,
|
2017-02-02 14:10:07 -06:00
|
|
|
#type_name,
|
|
|
|
#variant_index,
|
|
|
|
#variant_name,
|
2017-02-20 16:43:51 -06:00
|
|
|
)
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Style::Newtype => {
|
|
|
|
let field = &variant.fields[0];
|
2017-02-20 16:16:54 -06:00
|
|
|
let mut field_expr = quote!(__field0);
|
2017-02-02 14:10:07 -06:00
|
|
|
if let Some(path) = field.attrs.serialize_with() {
|
2018-01-09 21:34:35 -06:00
|
|
|
field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr);
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
|
|
|
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_expr! {
|
2017-02-02 14:10:07 -06:00
|
|
|
_serde::Serializer::serialize_newtype_variant(
|
2017-03-18 12:33:22 -05:00
|
|
|
__serializer,
|
2017-02-02 14:10:07 -06:00
|
|
|
#type_name,
|
|
|
|
#variant_index,
|
|
|
|
#variant_name,
|
|
|
|
#field_expr,
|
2017-02-20 16:43:51 -06:00
|
|
|
)
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
|
|
|
}
|
2017-12-23 22:13:08 -06:00
|
|
|
Style::Tuple => serialize_tuple_variant(
|
|
|
|
TupleVariant::ExternallyTagged {
|
|
|
|
type_name: type_name,
|
|
|
|
variant_index: variant_index,
|
|
|
|
variant_name: variant_name,
|
|
|
|
},
|
|
|
|
params,
|
|
|
|
&variant.fields,
|
|
|
|
),
|
|
|
|
Style::Struct => serialize_struct_variant(
|
|
|
|
StructVariant::ExternallyTagged {
|
|
|
|
variant_index: variant_index,
|
|
|
|
variant_name: variant_name,
|
|
|
|
},
|
|
|
|
params,
|
|
|
|
&variant.fields,
|
|
|
|
&type_name,
|
|
|
|
),
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_internally_tagged_variant(
|
|
|
|
params: &Parameters,
|
|
|
|
variant: &Variant,
|
2017-04-14 17:52:56 -05:00
|
|
|
cattrs: &attr::Container,
|
2017-04-13 14:28:23 -05:00
|
|
|
tag: &str,
|
|
|
|
) -> Fragment {
|
2017-04-14 17:52:56 -05:00
|
|
|
let type_name = cattrs.name().serialize_name();
|
2017-02-02 14:10:07 -06:00
|
|
|
let variant_name = variant.attrs.name().serialize_name();
|
|
|
|
|
2017-04-09 00:42:42 -05:00
|
|
|
let enum_ident_str = params.type_name();
|
2017-02-20 13:50:24 -06:00
|
|
|
let variant_ident_str = variant.ident.as_ref();
|
|
|
|
|
2017-08-07 19:22:26 -05:00
|
|
|
if let Some(path) = variant.attrs.serialize_with() {
|
2018-01-09 21:34:35 -06:00
|
|
|
let ser = wrap_serialize_variant_with(params, path, variant);
|
2017-08-07 19:22:26 -05:00
|
|
|
return quote_expr! {
|
|
|
|
_serde::private::ser::serialize_tagged_newtype(
|
|
|
|
__serializer,
|
|
|
|
#enum_ident_str,
|
|
|
|
#variant_ident_str,
|
|
|
|
#tag,
|
|
|
|
#variant_name,
|
|
|
|
#ser,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-02 14:10:07 -06:00
|
|
|
match variant.style {
|
|
|
|
Style::Unit => {
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_block! {
|
2017-02-02 14:10:07 -06:00
|
|
|
let mut __struct = try!(_serde::Serializer::serialize_struct(
|
2017-03-18 12:33:22 -05:00
|
|
|
__serializer, #type_name, 1));
|
2017-02-02 14:10:07 -06:00
|
|
|
try!(_serde::ser::SerializeStruct::serialize_field(
|
|
|
|
&mut __struct, #tag, #variant_name));
|
|
|
|
_serde::ser::SerializeStruct::end(__struct)
|
2017-02-20 16:43:51 -06:00
|
|
|
}
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
|
|
|
Style::Newtype => {
|
|
|
|
let field = &variant.fields[0];
|
2017-02-20 16:16:54 -06:00
|
|
|
let mut field_expr = quote!(__field0);
|
2017-02-02 14:10:07 -06:00
|
|
|
if let Some(path) = field.attrs.serialize_with() {
|
2018-01-09 21:34:35 -06:00
|
|
|
field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr);
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
|
|
|
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_expr! {
|
2017-04-07 11:46:45 -05:00
|
|
|
_serde::private::ser::serialize_tagged_newtype(
|
2017-03-18 12:33:22 -05:00
|
|
|
__serializer,
|
2017-02-20 13:50:24 -06:00
|
|
|
#enum_ident_str,
|
|
|
|
#variant_ident_str,
|
2017-02-02 14:10:07 -06:00
|
|
|
#tag,
|
|
|
|
#variant_name,
|
|
|
|
#field_expr,
|
2017-02-20 16:43:51 -06:00
|
|
|
)
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
|
|
|
}
|
2017-12-23 22:13:08 -06:00
|
|
|
Style::Struct => serialize_struct_variant(
|
|
|
|
StructVariant::InternallyTagged {
|
|
|
|
tag: tag,
|
|
|
|
variant_name: variant_name,
|
|
|
|
},
|
|
|
|
params,
|
|
|
|
&variant.fields,
|
|
|
|
&type_name,
|
|
|
|
),
|
2017-04-14 17:42:27 -05:00
|
|
|
Style::Tuple => unreachable!("checked in serde_derive_internals"),
|
2016-05-15 17:54:20 -05:00
|
|
|
}
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
2016-05-15 17:54:20 -05:00
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_adjacently_tagged_variant(
|
|
|
|
params: &Parameters,
|
|
|
|
variant: &Variant,
|
2017-04-14 17:52:56 -05:00
|
|
|
cattrs: &attr::Container,
|
2017-04-13 14:28:23 -05:00
|
|
|
tag: &str,
|
|
|
|
content: &str,
|
|
|
|
) -> Fragment {
|
2017-04-09 00:42:42 -05:00
|
|
|
let this = ¶ms.this;
|
2017-04-14 17:52:56 -05:00
|
|
|
let type_name = cattrs.name().serialize_name();
|
2017-02-17 23:44:15 -06:00
|
|
|
let variant_name = variant.attrs.name().serialize_name();
|
|
|
|
|
2017-12-23 22:13:08 -06:00
|
|
|
let inner = Stmts(if let Some(path) = variant.attrs.serialize_with() {
|
2018-01-09 21:34:35 -06:00
|
|
|
let ser = wrap_serialize_variant_with(params, path, variant);
|
2017-12-23 22:13:08 -06:00
|
|
|
quote_expr! {
|
|
|
|
_serde::Serialize::serialize(#ser, __serializer)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match variant.style {
|
|
|
|
Style::Unit => {
|
|
|
|
return quote_block! {
|
|
|
|
let mut __struct = try!(_serde::Serializer::serialize_struct(
|
|
|
|
__serializer, #type_name, 1));
|
|
|
|
try!(_serde::ser::SerializeStruct::serialize_field(
|
|
|
|
&mut __struct, #tag, #variant_name));
|
|
|
|
_serde::ser::SerializeStruct::end(__struct)
|
|
|
|
};
|
2017-02-19 18:04:39 -06:00
|
|
|
}
|
2017-12-23 22:13:08 -06:00
|
|
|
Style::Newtype => {
|
|
|
|
let field = &variant.fields[0];
|
|
|
|
let mut field_expr = quote!(__field0);
|
|
|
|
if let Some(path) = field.attrs.serialize_with() {
|
2018-01-09 21:34:35 -06:00
|
|
|
field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr);
|
2017-04-13 14:28:23 -05:00
|
|
|
}
|
2017-02-19 18:04:39 -06:00
|
|
|
|
2017-12-23 22:13:08 -06:00
|
|
|
quote_expr! {
|
|
|
|
_serde::Serialize::serialize(#field_expr, __serializer)
|
2017-04-19 17:56:22 -05:00
|
|
|
}
|
2017-04-13 14:28:23 -05:00
|
|
|
}
|
2017-12-23 22:13:08 -06:00
|
|
|
Style::Tuple => {
|
|
|
|
serialize_tuple_variant(TupleVariant::Untagged, params, &variant.fields)
|
|
|
|
}
|
|
|
|
Style::Struct => serialize_struct_variant(
|
|
|
|
StructVariant::Untagged,
|
|
|
|
params,
|
|
|
|
&variant.fields,
|
|
|
|
&variant_name,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
});
|
2017-02-17 23:44:15 -06:00
|
|
|
|
2017-02-19 18:04:39 -06:00
|
|
|
let fields_ty = variant.fields.iter().map(|f| &f.ty);
|
2018-01-09 21:34:35 -06:00
|
|
|
let fields_ident: &Vec<_> = &match variant.style {
|
2017-08-07 19:22:26 -05:00
|
|
|
Style::Unit => {
|
|
|
|
if variant.attrs.serialize_with().is_some() {
|
|
|
|
vec![]
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
2018-03-28 03:42:53 -05:00
|
|
|
Style::Newtype => vec![Ident::new("__field0", Span::call_site())],
|
2017-12-23 22:13:08 -06:00
|
|
|
Style::Tuple => (0..variant.fields.len())
|
2018-03-28 03:42:53 -05:00
|
|
|
.map(|i| Ident::new(&format!("__field{}", i), Span::call_site()))
|
2017-12-23 22:13:08 -06:00
|
|
|
.collect(),
|
|
|
|
Style::Struct => variant
|
|
|
|
.fields
|
|
|
|
.iter()
|
2018-01-09 21:34:35 -06:00
|
|
|
.map(|f| f.ident.expect("struct variant has unnamed fields"))
|
2017-12-23 22:13:08 -06:00
|
|
|
.collect(),
|
2017-02-19 18:04:39 -06:00
|
|
|
};
|
2017-02-17 23:44:15 -06:00
|
|
|
|
2017-04-09 00:42:42 -05:00
|
|
|
let (_, ty_generics, where_clause) = params.generics.split_for_impl();
|
2017-02-19 18:04:39 -06:00
|
|
|
|
2017-08-07 19:22:26 -05:00
|
|
|
let wrapper_generics = if let Style::Unit = variant.style {
|
|
|
|
params.generics.clone()
|
|
|
|
} else {
|
|
|
|
bound::with_lifetime_bound(¶ms.generics, "'__a")
|
|
|
|
};
|
2017-02-20 13:50:24 -06:00
|
|
|
let (wrapper_impl_generics, wrapper_ty_generics, _) = wrapper_generics.split_for_impl();
|
2017-02-19 18:04:39 -06:00
|
|
|
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_block! {
|
2017-02-19 18:04:39 -06:00
|
|
|
struct __AdjacentlyTagged #wrapper_generics #where_clause {
|
|
|
|
data: (#(&'__a #fields_ty,)*),
|
2017-04-09 00:42:42 -05:00
|
|
|
phantom: _serde::export::PhantomData<#this #ty_generics>,
|
2017-02-17 23:44:15 -06:00
|
|
|
}
|
2017-02-19 18:04:39 -06:00
|
|
|
|
2017-02-20 13:50:24 -06:00
|
|
|
impl #wrapper_impl_generics _serde::Serialize for __AdjacentlyTagged #wrapper_ty_generics #where_clause {
|
2017-03-18 12:33:22 -05:00
|
|
|
fn serialize<__S>(&self, __serializer: __S) -> _serde::export::Result<__S::Ok, __S::Error>
|
2017-02-19 18:04:39 -06:00
|
|
|
where __S: _serde::Serializer
|
|
|
|
{
|
|
|
|
let (#(#fields_ident,)*) = self.data;
|
|
|
|
#inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut __struct = try!(_serde::Serializer::serialize_struct(
|
2017-03-18 12:33:22 -05:00
|
|
|
__serializer, #type_name, 2));
|
2017-02-19 18:04:39 -06:00
|
|
|
try!(_serde::ser::SerializeStruct::serialize_field(
|
|
|
|
&mut __struct, #tag, #variant_name));
|
|
|
|
try!(_serde::ser::SerializeStruct::serialize_field(
|
|
|
|
&mut __struct, #content, &__AdjacentlyTagged {
|
|
|
|
data: (#(#fields_ident,)*),
|
2017-04-09 00:42:42 -05:00
|
|
|
phantom: _serde::export::PhantomData::<#this #ty_generics>,
|
2017-02-19 18:04:39 -06:00
|
|
|
}));
|
|
|
|
_serde::ser::SerializeStruct::end(__struct)
|
2017-02-20 16:43:51 -06:00
|
|
|
}
|
2017-02-17 23:44:15 -06:00
|
|
|
}
|
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_untagged_variant(
|
|
|
|
params: &Parameters,
|
|
|
|
variant: &Variant,
|
2017-04-14 17:52:56 -05:00
|
|
|
cattrs: &attr::Container,
|
2017-04-13 14:28:23 -05:00
|
|
|
) -> Fragment {
|
2017-08-07 19:22:26 -05:00
|
|
|
if let Some(path) = variant.attrs.serialize_with() {
|
2018-01-09 21:34:35 -06:00
|
|
|
let ser = wrap_serialize_variant_with(params, path, variant);
|
2017-08-07 19:22:26 -05:00
|
|
|
return quote_expr! {
|
|
|
|
_serde::Serialize::serialize(#ser, __serializer)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-02-02 14:10:07 -06:00
|
|
|
match variant.style {
|
|
|
|
Style::Unit => {
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_expr! {
|
2017-03-18 12:33:22 -05:00
|
|
|
_serde::Serializer::serialize_unit(__serializer)
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Style::Newtype => {
|
|
|
|
let field = &variant.fields[0];
|
2017-02-20 16:16:54 -06:00
|
|
|
let mut field_expr = quote!(__field0);
|
2017-02-02 14:10:07 -06:00
|
|
|
if let Some(path) = field.attrs.serialize_with() {
|
2018-01-09 21:34:35 -06:00
|
|
|
field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr);
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
|
|
|
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_expr! {
|
2017-03-18 12:33:22 -05:00
|
|
|
_serde::Serialize::serialize(#field_expr, __serializer)
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
|
|
|
}
|
2017-04-13 14:28:23 -05:00
|
|
|
Style::Tuple => serialize_tuple_variant(TupleVariant::Untagged, params, &variant.fields),
|
2017-02-02 14:10:07 -06:00
|
|
|
Style::Struct => {
|
2017-04-14 17:52:56 -05:00
|
|
|
let type_name = cattrs.name().serialize_name();
|
2017-04-13 14:28:23 -05:00
|
|
|
serialize_struct_variant(StructVariant::Untagged, params, &variant.fields, &type_name)
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
2016-09-10 23:53:14 -05:00
|
|
|
}
|
2016-05-15 17:54:20 -05:00
|
|
|
}
|
|
|
|
|
2017-02-02 14:10:07 -06:00
|
|
|
enum TupleVariant {
|
|
|
|
ExternallyTagged {
|
|
|
|
type_name: String,
|
2017-04-09 15:50:38 -05:00
|
|
|
variant_index: u32,
|
2017-02-02 14:10:07 -06:00
|
|
|
variant_name: String,
|
|
|
|
},
|
|
|
|
Untagged,
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_tuple_variant(
|
|
|
|
context: TupleVariant,
|
|
|
|
params: &Parameters,
|
|
|
|
fields: &[Field],
|
|
|
|
) -> Fragment {
|
2018-01-10 21:22:07 -06:00
|
|
|
let tuple_trait = match context {
|
|
|
|
TupleVariant::ExternallyTagged { .. } => TupleTrait::SerializeTupleVariant,
|
|
|
|
TupleVariant::Untagged => TupleTrait::SerializeTuple,
|
2017-02-02 14:10:07 -06:00
|
|
|
};
|
|
|
|
|
2018-01-10 21:22:07 -06:00
|
|
|
let serialize_stmts = serialize_tuple_struct_visitor(fields, params, true, &tuple_trait);
|
2015-06-18 10:45:03 -05:00
|
|
|
|
2016-07-12 08:44:19 -05:00
|
|
|
let len = serialize_stmts.len();
|
2016-09-10 23:53:14 -05:00
|
|
|
let let_mut = mut_if(len > 0);
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2017-02-02 14:10:07 -06:00
|
|
|
match context {
|
2017-04-13 14:28:23 -05:00
|
|
|
TupleVariant::ExternallyTagged {
|
|
|
|
type_name,
|
|
|
|
variant_index,
|
|
|
|
variant_name,
|
|
|
|
} => {
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_block! {
|
2017-02-02 14:10:07 -06:00
|
|
|
let #let_mut __serde_state = try!(_serde::Serializer::serialize_tuple_variant(
|
2017-03-18 12:33:22 -05:00
|
|
|
__serializer,
|
2017-02-02 14:10:07 -06:00
|
|
|
#type_name,
|
|
|
|
#variant_index,
|
|
|
|
#variant_name,
|
|
|
|
#len));
|
|
|
|
#(#serialize_stmts)*
|
|
|
|
_serde::ser::SerializeTupleVariant::end(__serde_state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TupleVariant::Untagged => {
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_block! {
|
2017-02-02 14:10:07 -06:00
|
|
|
let #let_mut __serde_state = try!(_serde::Serializer::serialize_tuple(
|
2017-03-18 12:33:22 -05:00
|
|
|
__serializer,
|
2017-02-02 14:10:07 -06:00
|
|
|
#len));
|
|
|
|
#(#serialize_stmts)*
|
|
|
|
_serde::ser::SerializeTuple::end(__serde_state)
|
|
|
|
}
|
|
|
|
}
|
2016-09-27 00:13:53 -05:00
|
|
|
}
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
2017-02-02 14:10:07 -06:00
|
|
|
enum StructVariant<'a> {
|
|
|
|
ExternallyTagged {
|
2017-04-09 15:50:38 -05:00
|
|
|
variant_index: u32,
|
2017-02-02 14:10:07 -06:00
|
|
|
variant_name: String,
|
|
|
|
},
|
2017-12-23 22:13:08 -06:00
|
|
|
InternallyTagged {
|
|
|
|
tag: &'a str,
|
|
|
|
variant_name: String,
|
|
|
|
},
|
2017-02-02 14:10:07 -06:00
|
|
|
Untagged,
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_struct_variant<'a>(
|
|
|
|
context: StructVariant<'a>,
|
|
|
|
params: &Parameters,
|
|
|
|
fields: &[Field],
|
|
|
|
name: &str,
|
|
|
|
) -> Fragment {
|
2018-01-10 21:22:07 -06:00
|
|
|
let struct_trait = match context {
|
2018-04-13 00:58:24 -05:00
|
|
|
StructVariant::ExternallyTagged { .. } => (StructTrait::SerializeStructVariant),
|
|
|
|
StructVariant::InternallyTagged { .. } | StructVariant::Untagged => {
|
|
|
|
(StructTrait::SerializeStruct)
|
|
|
|
}
|
2017-02-02 14:10:07 -06:00
|
|
|
};
|
|
|
|
|
2018-01-10 21:22:07 -06:00
|
|
|
let serialize_fields = serialize_struct_visitor(fields, params, true, &struct_trait);
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
let mut serialized_fields = fields
|
|
|
|
.iter()
|
2016-07-12 08:44:19 -05:00
|
|
|
.filter(|&field| !field.attrs.skip_serializing())
|
2016-09-05 10:22:17 -05:00
|
|
|
.peekable();
|
2016-09-02 13:42:26 -05:00
|
|
|
|
2016-09-10 23:53:14 -05:00
|
|
|
let let_mut = mut_if(serialized_fields.peek().is_some());
|
2016-09-05 10:22:17 -05:00
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
let len = serialized_fields
|
2017-12-23 22:13:08 -06:00
|
|
|
.map(|field| {
|
2018-01-09 21:34:35 -06:00
|
|
|
let ident = field.ident.expect("struct has unnamed fields");
|
2016-07-12 08:44:19 -05:00
|
|
|
|
2017-12-23 22:13:08 -06:00
|
|
|
match field.attrs.skip_serializing_if() {
|
|
|
|
Some(path) => quote!(if #path(#ident) { 0 } else { 1 }),
|
|
|
|
None => quote!(1),
|
|
|
|
}
|
|
|
|
})
|
2016-09-10 23:53:14 -05:00
|
|
|
.fold(quote!(0), |sum, expr| quote!(#sum + #expr));
|
|
|
|
|
2017-02-02 14:10:07 -06:00
|
|
|
match context {
|
2017-04-13 14:28:23 -05:00
|
|
|
StructVariant::ExternallyTagged {
|
|
|
|
variant_index,
|
|
|
|
variant_name,
|
|
|
|
} => {
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_block! {
|
2017-02-02 14:10:07 -06:00
|
|
|
let #let_mut __serde_state = try!(_serde::Serializer::serialize_struct_variant(
|
2017-03-18 12:33:22 -05:00
|
|
|
__serializer,
|
2017-02-19 18:04:39 -06:00
|
|
|
#name,
|
2017-02-02 14:10:07 -06:00
|
|
|
#variant_index,
|
|
|
|
#variant_name,
|
|
|
|
#len,
|
|
|
|
));
|
|
|
|
#(#serialize_fields)*
|
|
|
|
_serde::ser::SerializeStructVariant::end(__serde_state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StructVariant::InternallyTagged { tag, variant_name } => {
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_block! {
|
2017-02-02 14:10:07 -06:00
|
|
|
let mut __serde_state = try!(_serde::Serializer::serialize_struct(
|
2017-03-18 12:33:22 -05:00
|
|
|
__serializer,
|
2017-02-19 18:04:39 -06:00
|
|
|
#name,
|
2017-02-02 14:10:07 -06:00
|
|
|
#len + 1,
|
|
|
|
));
|
|
|
|
try!(_serde::ser::SerializeStruct::serialize_field(
|
|
|
|
&mut __serde_state,
|
|
|
|
#tag,
|
|
|
|
#variant_name,
|
|
|
|
));
|
|
|
|
#(#serialize_fields)*
|
|
|
|
_serde::ser::SerializeStruct::end(__serde_state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StructVariant::Untagged => {
|
2017-02-20 16:43:51 -06:00
|
|
|
quote_block! {
|
2017-02-02 14:10:07 -06:00
|
|
|
let #let_mut __serde_state = try!(_serde::Serializer::serialize_struct(
|
2017-03-18 12:33:22 -05:00
|
|
|
__serializer,
|
2017-02-19 18:04:39 -06:00
|
|
|
#name,
|
2017-02-02 14:10:07 -06:00
|
|
|
#len,
|
|
|
|
));
|
|
|
|
#(#serialize_fields)*
|
|
|
|
_serde::ser::SerializeStruct::end(__serde_state)
|
|
|
|
}
|
|
|
|
}
|
2016-09-27 00:13:53 -05:00
|
|
|
}
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_tuple_struct_visitor(
|
|
|
|
fields: &[Field],
|
|
|
|
params: &Parameters,
|
|
|
|
is_enum: bool,
|
2018-01-10 21:22:07 -06:00
|
|
|
tuple_trait: &TupleTrait,
|
2017-04-13 14:28:23 -05:00
|
|
|
) -> Vec<Tokens> {
|
|
|
|
fields
|
|
|
|
.iter()
|
2016-05-15 17:54:20 -05:00
|
|
|
.enumerate()
|
2017-12-23 22:13:08 -06:00
|
|
|
.map(|(i, field)| {
|
|
|
|
let mut field_expr = if is_enum {
|
2018-03-28 03:42:53 -05:00
|
|
|
let id = Ident::new(&format!("__field{}", i), Span::call_site());
|
2017-12-23 22:13:08 -06:00
|
|
|
quote!(#id)
|
|
|
|
} else {
|
2018-04-13 00:58:24 -05:00
|
|
|
get_member(
|
|
|
|
params,
|
|
|
|
field,
|
|
|
|
&Member::Unnamed(Index {
|
|
|
|
index: i as u32,
|
|
|
|
span: Span::call_site(),
|
|
|
|
}),
|
|
|
|
)
|
2017-12-23 22:13:08 -06:00
|
|
|
};
|
2017-04-13 14:28:23 -05:00
|
|
|
|
2017-12-23 22:13:08 -06:00
|
|
|
let skip = field
|
|
|
|
.attrs
|
|
|
|
.skip_serializing_if()
|
|
|
|
.map(|path| quote!(#path(#field_expr)));
|
2017-04-13 14:28:23 -05:00
|
|
|
|
2017-12-23 22:13:08 -06:00
|
|
|
if let Some(path) = field.attrs.serialize_with() {
|
2018-01-09 21:34:35 -06:00
|
|
|
field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr);
|
2017-12-23 22:13:08 -06:00
|
|
|
}
|
2015-03-14 15:09:37 -05:00
|
|
|
|
2018-03-31 16:44:50 -05:00
|
|
|
let span = field.original.span();
|
2018-01-10 21:22:07 -06:00
|
|
|
let func = tuple_trait.serialize_element(span);
|
2017-12-23 22:13:08 -06:00
|
|
|
let ser = quote! {
|
|
|
|
try!(#func(&mut __serde_state, #field_expr));
|
|
|
|
};
|
2016-09-02 19:22:58 -05:00
|
|
|
|
2017-12-23 22:13:08 -06:00
|
|
|
match skip {
|
|
|
|
None => ser,
|
|
|
|
Some(skip) => quote!(if !#skip { #ser }),
|
|
|
|
}
|
|
|
|
})
|
2016-07-12 08:44:19 -05:00
|
|
|
.collect()
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
fn serialize_struct_visitor(
|
|
|
|
fields: &[Field],
|
|
|
|
params: &Parameters,
|
|
|
|
is_enum: bool,
|
2018-01-10 21:22:07 -06:00
|
|
|
struct_trait: &StructTrait,
|
2017-04-13 14:28:23 -05:00
|
|
|
) -> Vec<Tokens> {
|
|
|
|
fields
|
|
|
|
.iter()
|
2016-06-15 01:37:20 -05:00
|
|
|
.filter(|&field| !field.attrs.skip_serializing())
|
2017-12-23 22:13:08 -06:00
|
|
|
.map(|field| {
|
2018-01-09 21:34:35 -06:00
|
|
|
let field_ident = field.ident.expect("struct has unnamed field");
|
2018-03-15 08:17:48 -05:00
|
|
|
|
2017-12-23 22:13:08 -06:00
|
|
|
let mut field_expr = if is_enum {
|
|
|
|
quote!(#field_ident)
|
|
|
|
} else {
|
2018-01-09 21:34:35 -06:00
|
|
|
get_member(params, field, &Member::Named(field_ident))
|
2017-12-23 22:13:08 -06:00
|
|
|
};
|
2017-04-13 14:28:23 -05:00
|
|
|
|
2017-12-23 22:13:08 -06:00
|
|
|
let key_expr = field.attrs.name().serialize_name();
|
2017-04-13 14:28:23 -05:00
|
|
|
|
2017-12-23 22:13:08 -06:00
|
|
|
let skip = field
|
|
|
|
.attrs
|
|
|
|
.skip_serializing_if()
|
|
|
|
.map(|path| quote!(#path(#field_expr)));
|
2016-02-15 19:39:46 -06:00
|
|
|
|
2017-12-23 22:13:08 -06:00
|
|
|
if let Some(path) = field.attrs.serialize_with() {
|
2018-01-09 21:34:35 -06:00
|
|
|
field_expr = wrap_serialize_field_with(params, field.ty, path, &field_expr);
|
2017-12-23 22:13:08 -06:00
|
|
|
}
|
2016-09-02 19:22:58 -05:00
|
|
|
|
2018-03-31 16:44:50 -05:00
|
|
|
let span = field.original.span();
|
2018-03-15 08:17:48 -05:00
|
|
|
let ser = if field.attrs.flatten() {
|
|
|
|
quote! {
|
2018-03-20 07:35:16 -05:00
|
|
|
try!(_serde::Serialize::serialize(&#field_expr, _serde::private::ser::FlatMapSerializer(&mut __serde_state)));
|
2018-03-15 08:17:48 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let func = struct_trait.serialize_field(span);
|
|
|
|
quote! {
|
|
|
|
try!(#func(&mut __serde_state, #key_expr, #field_expr));
|
|
|
|
}
|
2017-12-23 22:13:08 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
match skip {
|
|
|
|
None => ser,
|
|
|
|
Some(skip) => {
|
2018-03-14 07:00:16 -05:00
|
|
|
if let Some(skip_func) = struct_trait.skip_field(span) {
|
|
|
|
quote! {
|
|
|
|
if !#skip {
|
|
|
|
#ser
|
|
|
|
} else {
|
|
|
|
try!(#skip_func(&mut __serde_state, #key_expr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quote! {
|
|
|
|
if !#skip {
|
|
|
|
#ser
|
|
|
|
}
|
2017-08-19 12:29:54 -05:00
|
|
|
}
|
|
|
|
}
|
2017-04-13 14:28:23 -05:00
|
|
|
}
|
2017-12-23 22:13:08 -06:00
|
|
|
}
|
|
|
|
})
|
2016-07-12 08:44:19 -05:00
|
|
|
.collect()
|
2015-03-14 15:09:37 -05:00
|
|
|
}
|
2016-05-15 17:54:20 -05:00
|
|
|
|
2017-08-07 19:22:26 -05:00
|
|
|
fn wrap_serialize_field_with(
|
2017-04-13 14:28:23 -05:00
|
|
|
params: &Parameters,
|
2018-01-08 23:49:09 -06:00
|
|
|
field_ty: &syn::Type,
|
2018-03-15 12:02:40 -05:00
|
|
|
serialize_with: &syn::ExprPath,
|
2018-01-09 21:34:35 -06:00
|
|
|
field_expr: &Tokens,
|
2017-08-07 19:22:26 -05:00
|
|
|
) -> Tokens {
|
2017-12-23 22:13:08 -06:00
|
|
|
wrap_serialize_with(params, serialize_with, &[field_ty], &[quote!(#field_expr)])
|
2017-08-07 19:22:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn wrap_serialize_variant_with(
|
|
|
|
params: &Parameters,
|
2018-03-15 12:02:40 -05:00
|
|
|
serialize_with: &syn::ExprPath,
|
2017-08-07 19:22:26 -05:00
|
|
|
variant: &Variant,
|
|
|
|
) -> Tokens {
|
|
|
|
let field_tys: Vec<_> = variant.fields.iter().map(|field| field.ty).collect();
|
2017-12-23 22:13:08 -06:00
|
|
|
let field_exprs: Vec<_> = variant
|
|
|
|
.fields
|
|
|
|
.iter()
|
2017-08-07 19:22:26 -05:00
|
|
|
.enumerate()
|
|
|
|
.map(|(i, field)| {
|
2017-12-23 22:13:08 -06:00
|
|
|
let id = field
|
|
|
|
.ident
|
2018-03-28 03:42:53 -05:00
|
|
|
.unwrap_or_else(|| Ident::new(&format!("__field{}", i), Span::call_site()));
|
2017-08-07 19:22:26 -05:00
|
|
|
quote!(#id)
|
|
|
|
})
|
|
|
|
.collect();
|
2017-12-23 22:13:08 -06:00
|
|
|
wrap_serialize_with(
|
|
|
|
params,
|
|
|
|
serialize_with,
|
|
|
|
field_tys.as_slice(),
|
|
|
|
field_exprs.as_slice(),
|
|
|
|
)
|
2017-08-07 19:22:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn wrap_serialize_with(
|
|
|
|
params: &Parameters,
|
2018-03-15 12:02:40 -05:00
|
|
|
serialize_with: &syn::ExprPath,
|
2018-01-08 23:49:09 -06:00
|
|
|
field_tys: &[&syn::Type],
|
2017-08-07 19:22:26 -05:00
|
|
|
field_exprs: &[Tokens],
|
2017-04-13 14:28:23 -05:00
|
|
|
) -> Tokens {
|
2017-04-09 00:42:42 -05:00
|
|
|
let this = ¶ms.this;
|
|
|
|
let (_, ty_generics, where_clause) = params.generics.split_for_impl();
|
2016-05-15 17:54:20 -05:00
|
|
|
|
2018-01-09 21:34:35 -06:00
|
|
|
let wrapper_generics = if field_exprs.is_empty() {
|
2017-08-07 19:22:26 -05:00
|
|
|
params.generics.clone()
|
|
|
|
} else {
|
|
|
|
bound::with_lifetime_bound(¶ms.generics, "'__a")
|
|
|
|
};
|
2017-02-20 13:50:24 -06:00
|
|
|
let (wrapper_impl_generics, wrapper_ty_generics, _) = wrapper_generics.split_for_impl();
|
2016-05-15 17:54:20 -05:00
|
|
|
|
2018-04-13 00:58:24 -05:00
|
|
|
let field_access = (0..field_exprs.len()).map(|n| {
|
|
|
|
Member::Unnamed(Index {
|
|
|
|
index: n as u32,
|
|
|
|
span: Span::call_site(),
|
|
|
|
})
|
|
|
|
});
|
2017-08-07 19:22:26 -05:00
|
|
|
|
2016-09-28 11:18:18 -05:00
|
|
|
quote!({
|
2017-02-20 13:50:24 -06:00
|
|
|
struct __SerializeWith #wrapper_impl_generics #where_clause {
|
2017-08-07 19:22:26 -05:00
|
|
|
values: (#(&'__a #field_tys, )*),
|
2017-04-09 00:42:42 -05:00
|
|
|
phantom: _serde::export::PhantomData<#this #ty_generics>,
|
2016-09-28 11:18:18 -05:00
|
|
|
}
|
2016-05-15 17:54:20 -05:00
|
|
|
|
2017-02-20 13:50:24 -06:00
|
|
|
impl #wrapper_impl_generics _serde::Serialize for __SerializeWith #wrapper_ty_generics #where_clause {
|
2017-01-23 18:03:06 -06:00
|
|
|
fn serialize<__S>(&self, __s: __S) -> _serde::export::Result<__S::Ok, __S::Error>
|
2017-01-08 02:47:37 -06:00
|
|
|
where __S: _serde::Serializer
|
2016-09-28 11:18:18 -05:00
|
|
|
{
|
2017-08-07 19:22:26 -05:00
|
|
|
#serialize_with(#(self.values.#field_access, )* __s)
|
2016-05-15 17:54:20 -05:00
|
|
|
}
|
2016-09-28 11:18:18 -05:00
|
|
|
}
|
2016-05-15 17:54:20 -05:00
|
|
|
|
2017-01-23 18:21:03 -06:00
|
|
|
&__SerializeWith {
|
2017-08-07 19:22:26 -05:00
|
|
|
values: (#(#field_exprs, )*),
|
2017-04-09 00:42:42 -05:00
|
|
|
phantom: _serde::export::PhantomData::<#this #ty_generics>,
|
2016-05-15 17:54:20 -05:00
|
|
|
}
|
2016-09-28 11:18:18 -05:00
|
|
|
})
|
2016-06-19 22:15:49 -05:00
|
|
|
}
|
2016-09-02 13:42:26 -05:00
|
|
|
|
|
|
|
// Serialization of an empty struct results in code like:
|
|
|
|
//
|
2016-09-26 23:09:13 -05:00
|
|
|
// let mut __serde_state = try!(serializer.serialize_struct("S", 0));
|
2017-01-14 23:39:37 -06:00
|
|
|
// _serde::ser::SerializeStruct::end(__serde_state)
|
2016-09-02 13:42:26 -05:00
|
|
|
//
|
|
|
|
// where we want to omit the `mut` to avoid a warning.
|
2016-09-10 23:53:14 -05:00
|
|
|
fn mut_if(is_mut: bool) -> Option<Tokens> {
|
2017-12-23 22:13:08 -06:00
|
|
|
if is_mut {
|
|
|
|
Some(quote!(mut))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2016-09-02 13:42:26 -05:00
|
|
|
}
|
2017-04-09 00:42:42 -05:00
|
|
|
|
2018-01-09 21:34:35 -06:00
|
|
|
fn get_member(params: &Parameters, field: &Field, member: &Member) -> Tokens {
|
2017-04-09 00:42:42 -05:00
|
|
|
let self_var = ¶ms.self_var;
|
2017-04-10 14:12:00 -05:00
|
|
|
match (params.is_remote, field.attrs.getter()) {
|
2018-04-13 00:58:24 -05:00
|
|
|
(false, None) => quote_spanned!(Span::call_site()=> &#self_var.#member),
|
2017-04-10 14:12:00 -05:00
|
|
|
(true, None) => {
|
2018-01-10 00:22:08 -06:00
|
|
|
let inner = quote_spanned!(Span::call_site()=> &#self_var.#member);
|
2017-04-10 14:12:00 -05:00
|
|
|
let ty = field.ty;
|
2018-01-10 00:22:08 -06:00
|
|
|
quote!(_serde::private::ser::constrain::<#ty>(#inner))
|
2017-04-10 14:12:00 -05:00
|
|
|
}
|
|
|
|
(true, Some(getter)) => {
|
2017-04-09 12:59:54 -05:00
|
|
|
let ty = field.ty;
|
|
|
|
quote!(_serde::private::ser::constrain::<#ty>(&#getter(#self_var)))
|
|
|
|
}
|
2017-04-10 14:12:00 -05:00
|
|
|
(false, Some(_)) => {
|
|
|
|
unreachable!("getter is only allowed for remote impls");
|
|
|
|
}
|
2017-04-09 00:42:42 -05:00
|
|
|
}
|
|
|
|
}
|
2018-01-10 21:22:07 -06:00
|
|
|
|
|
|
|
enum StructTrait {
|
2018-03-14 07:00:16 -05:00
|
|
|
SerializeMap,
|
2018-01-10 21:22:07 -06:00
|
|
|
SerializeStruct,
|
|
|
|
SerializeStructVariant,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StructTrait {
|
|
|
|
fn serialize_field(&self, span: Span) -> Tokens {
|
|
|
|
match *self {
|
2018-03-14 07:00:16 -05:00
|
|
|
StructTrait::SerializeMap => {
|
|
|
|
quote_spanned!(span=> _serde::ser::SerializeMap::serialize_entry)
|
|
|
|
}
|
2018-01-10 21:22:07 -06:00
|
|
|
StructTrait::SerializeStruct => {
|
|
|
|
quote_spanned!(span=> _serde::ser::SerializeStruct::serialize_field)
|
|
|
|
}
|
|
|
|
StructTrait::SerializeStructVariant => {
|
|
|
|
quote_spanned!(span=> _serde::ser::SerializeStructVariant::serialize_field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 07:00:16 -05:00
|
|
|
fn skip_field(&self, span: Span) -> Option<Tokens> {
|
2018-01-10 21:22:07 -06:00
|
|
|
match *self {
|
2018-03-14 07:00:16 -05:00
|
|
|
StructTrait::SerializeMap => None,
|
2018-04-13 00:58:24 -05:00
|
|
|
StructTrait::SerializeStruct => {
|
|
|
|
Some(quote_spanned!(span=> _serde::ser::SerializeStruct::skip_field))
|
|
|
|
}
|
|
|
|
StructTrait::SerializeStructVariant => {
|
|
|
|
Some(quote_spanned!(span=> _serde::ser::SerializeStructVariant::skip_field))
|
|
|
|
}
|
2018-01-10 21:22:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum TupleTrait {
|
|
|
|
SerializeTuple,
|
|
|
|
SerializeTupleStruct,
|
|
|
|
SerializeTupleVariant,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TupleTrait {
|
|
|
|
fn serialize_element(&self, span: Span) -> Tokens {
|
|
|
|
match *self {
|
|
|
|
TupleTrait::SerializeTuple => {
|
|
|
|
quote_spanned!(span=> _serde::ser::SerializeTuple::serialize_element)
|
|
|
|
}
|
|
|
|
TupleTrait::SerializeTupleStruct => {
|
|
|
|
quote_spanned!(span=> _serde::ser::SerializeTupleStruct::serialize_field)
|
|
|
|
}
|
|
|
|
TupleTrait::SerializeTupleVariant => {
|
|
|
|
quote_spanned!(span=> _serde::ser::SerializeTupleVariant::serialize_field)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|