Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for generic fields in structs #156

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions yaserde/tests/generic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use yaserde::{serialize_and_validate, YaSerialize};

extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;

#[test]
fn parametrized_field() {
#[derive(Debug, PartialEq, YaSerialize)]
#[yaserde(rename = "outer")]
pub struct Outer<T: YaSerialize> {
pub inner: T,
}

#[derive(Debug, PartialEq, YaSerialize)]
pub struct Inner {
#[yaserde(text)]
pub body: String,
}

let content = "<outer><inner>Test</inner></outer>";
let model = Outer { inner: Inner { body: "Test".to_owned() }};
serialize_and_validate!(model, content);
}
3 changes: 2 additions & 1 deletion yaserde_derive/src/ser/expand_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::common::{Field, YaSerdeAttribute, YaSerdeField};
use crate::ser::{implement_serializer::implement_serializer, label::build_label_name};
use proc_macro2::TokenStream;
use quote::quote;
use syn::DataEnum;
use syn::{DataEnum, Generics};
use syn::Fields;
use syn::Ident;

Expand Down Expand Up @@ -88,6 +88,7 @@ pub fn serialize(
name,
root,
root_attributes,
&Generics::default(),
quote!(#variant_matches),
quote!(match self {
#inner_enum_inspector
Expand Down
4 changes: 3 additions & 1 deletion yaserde_derive/src/ser/expand_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use crate::common::{Field, YaSerdeAttribute, YaSerdeField};
use crate::ser::{element::*, implement_serializer::implement_serializer};
use proc_macro2::TokenStream;
use quote::quote;
use syn::DataStruct;
use syn::{DataStruct, Generics};
use syn::Ident;

pub fn serialize(
data_struct: &DataStruct,
name: &Ident,
root: &str,
root_attributes: &YaSerdeAttribute,
generics: &Generics,
) -> TokenStream {
let append_attributes: TokenStream = data_struct
.fields
Expand Down Expand Up @@ -346,6 +347,7 @@ pub fn serialize(
name,
root,
root_attributes,
generics,
append_attributes,
struct_inspector,
)
Expand Down
6 changes: 5 additions & 1 deletion yaserde_derive/src/ser/implement_serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ use crate::ser::namespace::generate_namespaces_definition;
use proc_macro2::Ident;
use proc_macro2::TokenStream;
use quote::quote;
use syn::Generics;

pub fn implement_serializer(
name: &Ident,
root: &str,
attributes: &YaSerdeAttribute,
generics: &Generics,
append_attributes: TokenStream,
inner_inspector: TokenStream,
) -> TokenStream {
let namespaces_definition = generate_namespaces_definition(attributes);
let flatten = attributes.flatten;

let (impl_generics, type_generics, _) = generics.split_for_impl();

quote! {
impl ::yaserde::YaSerialize for #name {
impl #impl_generics ::yaserde::YaSerialize for #name #type_generics {
#[allow(unused_variables)]
fn serialize<W: ::std::io::Write>(
&self,
Expand Down
3 changes: 2 additions & 1 deletion yaserde_derive/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn expand_derive_serialize(ast: &syn::DeriveInput) -> Result<TokenStream, St
let name = &ast.ident;
let attrs = &ast.attrs;
let data = &ast.data;
let generics = &ast.generics;

let root_attributes = YaSerdeAttribute::parse(attrs);

Expand All @@ -24,7 +25,7 @@ pub fn expand_derive_serialize(ast: &syn::DeriveInput) -> Result<TokenStream, St

let impl_block = match *data {
syn::Data::Struct(ref data_struct) => {
expand_struct::serialize(data_struct, name, &root_name, &root_attributes)
expand_struct::serialize(data_struct, name, &root_name, &root_attributes, generics)
}
syn::Data::Enum(ref data_enum) => {
expand_enum::serialize(data_enum, name, &root_name, &root_attributes)
Expand Down