fields-converter-derive 0.1.4

Fields-wise type conversions derive macros
Documentation
use quote::ToTokens;
use struct_data::{Field, StructData};
use syn;

pub fn move_fields(struct_data: &StructData, to_ty: &syn::Path) -> impl ToTokens {
    let (impl_generics, ty_generics, where_clause) = struct_data.generics.split_for_impl();
    let into_fields = struct_data
        .field_idents
        .iter()
        .map(|Field { id, .. }| quote!( #id: Into::into(self.#id) ));
    let from_fields = struct_data
        .field_idents
        .iter()
        .map(|Field { id, .. }| quote!( #id: From::from(other.#id) ));
    let from_ty = &struct_data.name;
    quote!(
        impl #impl_generics Into<#to_ty #ty_generics> for #from_ty #ty_generics #where_clause {
            fn into(self) -> #to_ty #ty_generics {
                #to_ty {
                    #( #into_fields ),*
                }
            }
        }

        impl #impl_generics From<#to_ty #ty_generics> for #from_ty #ty_generics #where_clause {
            fn from(other: #to_ty #ty_generics) -> Self {
                #from_ty {
                    #( #from_fields ),*
                }
            }
        }
    )
}