fn derive_fold_body(type_name: &Ident, data: Data) -> proc_macro2::TokenStream {
    match data {
        Data::Struct(s) => {
            let fields = s.fields.into_iter().map(|f| {
                let name = f.ident.as_ref().expect("Unnamed field in Foldable struct");
                quote! { #name: self.#name.fold_with(folder, binders)? }
            });
            quote! {
                Ok(#type_name {
                    #(#fields),*
                })
            }
        }
        Data::Enum(e) => {
            let matches = e.variants.into_iter().map(|v| {
                let variant = v.ident;
                match &v.fields {
                    syn::Fields::Named(fields) => {
                        let fnames: &Vec<_> = &fields.named.iter().map(|f| &f.ident).collect();
                        let fnames1: &Vec<_> = fnames;
                        quote! {
                            #type_name :: #variant { #(#fnames),* } => {
                                Ok(#type_name :: #variant {
                                    #(#fnames: #fnames1),*
                                })
                            }
                        }
                    }

                    syn::Fields::Unnamed(_fields) => {
                        let names: Vec<_> = (0..v.fields.iter().count())
                            .map(|index| format_ident!("a{}", index))
                            .collect();
                        quote! {
                            #type_name::#variant( #(ref #names),* ) => {
                                Ok(#type_name::#variant( #(#names.fold_with(folder, binders)?),* ))
                            }
                        }
                    }

                    syn::Fields::Unit => {
                        quote! {
                            #type_name::#variant => {
                                Ok(#type_name::#variant)
                            }
                        }
                    }
                }
            });
            quote! {
                match *self {
                    #(#matches)*
                }
            }
        }
        Data::Union(..) => panic!("Fold can not be derived for unions"),
    }
}
/// Generates the body of the Fold impl
Generates the body of the Fold impl
Generates the body of the Fold impl
fn derive_fold_body(type_name: &Ident, data: Data) -> proc_macro2::TokenStream {
    match data {
        Data::Struct(s) => {
            let fields = s.fields.into_iter().map(|f| {
                let name = f.ident.as_ref().expect("Unnamed field in Foldable struct");
                quote! { #name: self.#name.fold_with(folder, binders)? }
            });
            quote! {
                Ok(#type_name {
                    #(#fields),*
                })
            }
        }
        Data::Enum(e) => {
            let matches = e.variants.into_iter().map(|v| {
                let variant = v.ident;
                match &v.fields {
                    syn::Fields::Named(fields) => {
                        let fnames: &Vec<_> = &fields.named.iter().map(|f| &f.ident).collect();
                        let fnames1: &Vec<_> = fnames;
                        quote! {
                            #type_name :: #variant { #(#fnames),* } => {
                                Ok(#type_name :: #variant {
                                    #(#fnames: #fnames1),*
                                })
                            }
                        }
                    }
                    syn::Fields::Unnamed(_fields) => {
                        let names: Vec<_> = (0..v.fields.iter().count())
                            .map(|index| format_ident!("a{}", index))
                            .collect();
                        quote! {
                            #type_name::#variant( #(ref #names),* ) => {
                                Ok(#type_name::#variant( #(#names.fold_with(folder, binders)?),* ))
                            }
                        }
                    }
                    syn::Fields::Unit => {
                        quote! {
                            #type_name::#variant => {
                                Ok(#type_name::#variant)
                            }
                        }
                    }
                }
            });
            quote! {
                match *self {
                    #(#matches)*
                }
            }
        }
        Data::Union(..) => panic!("Fold can not be derived for unions"),
    }
}
fn derive_fold_body ( type_name : & Ident , data : Data ) -> proc_macro2 :: TokenStream { match data { Data :: Struct ( s ) => { let fields = s . fields . into_iter ( ) . map ( | f | { let name = f . ident . as_ref ( ) . expect ( " " ) ; quote ! { name self name fold_with ( folder binders ) } } ) ; quote ! { Ok ( type_name { ( fields ) } ) } } Data :: Enum ( e ) => { let matches = e . variants . into_iter ( ) . map ( | v | { let variant = v . ident ; match & v . fields { syn :: Fields :: Named ( fields ) => { let fnames : & Vec < _ > = & fields . named . iter ( ) . map ( | f | & f . ident ) . collect ( ) ; let fnames1 : & Vec < _ > = fnames ; quote ! { type_name variant { ( fnames ) } { Ok ( type_name variant { ( fnames fnames1 ) } ) } } } syn :: Fields :: Unnamed ( _fields ) => { let names : Vec < _ > = ( 0 .. v . fields . iter ( ) . count ( ) ) . map ( | index | format_ident ! ( " " index ) ) . collect ( ) ; quote ! { type_name variant ( ( ref names ) ) { Ok ( type_name variant ( ( names fold_with ( folder binders ) ) ) ) } } } syn :: Fields :: Unit => { quote ! { type_name variant { Ok ( type_name variant ) } } } } } ) ; quote ! { match self { ( matches ) } } } Data :: Union ( .. ) => panic ! ( " " ) , } }
Generates the body of the Fold impl
Generates the body of the Fold impl
type_name
&Ident
data
Data
type_name
&Ident
data
Data