use quote::ToTokens;
use struct_data::{Field, StructData};
use syn;
pub fn clone_fields(struct_data: &StructData, to_ty: &syn::Path) -> impl ToTokens {
let from_ty = &struct_data.name;
let fields = &struct_data.field_idents;
let (impl_generics, ty_generics, where_clause) = struct_data.generics.split_for_impl();
let direct_clones = fields
.iter()
.map(|Field { id, .. }| quote!( #id: CloneInto::clone_into(&self.#id) ));
let reverse_clones = fields
.iter()
.map(|Field { id, .. }| quote!( #id: CloneFrom::clone_from(&other.#id) ));
quote!(
impl #impl_generics CloneInto<#to_ty #ty_generics> for #from_ty #ty_generics #where_clause {
fn clone_into(&self) -> #to_ty #ty_generics {
#to_ty {
#( #direct_clones ),*
}
}
}
impl #impl_generics CloneFrom<#to_ty #ty_generics> for #from_ty #ty_generics #where_clause {
fn clone_from(other: &#to_ty #ty_generics) -> Self {
#from_ty {
#( #reverse_clones ),*
}
}
}
)
}