fn parse_ty_defs(ctx: &CrateContext) -> Result<Vec<IdlTypeDefinition>> {
    ctx.structs()
        .filter_map(|item_strct| {
            // Only take serializable types
            let serializable = item_strct.attrs.iter().any(|attr| {
                let attr_string = attr.tokens.to_string();
                let attr_name = attr.path.segments.last().unwrap().ident.to_string();
                let attr_serializable = ["account", "associated", "event", "zero_copy"];

                let derived_serializable = attr_name == "derive"
                    && attr_string.contains("AnchorSerialize")
                    && attr_string.contains("AnchorDeserialize");

                attr_serializable.iter().any(|a| *a == attr_name) || derived_serializable
            });

            if !serializable {
                return None;
            }

            // Only take public types
            match &item_strct.vis {
                syn::Visibility::Public(_) => (),
                _ => return None,
            }

            let name = item_strct.ident.to_string();
            let fields = match &item_strct.fields {
                syn::Fields::Named(fields) => fields
                    .named
                    .iter()
                    .map(|f: &syn::Field| {
                        let mut tts = proc_macro2::TokenStream::new();
                        f.ty.to_tokens(&mut tts);
                        Ok(IdlField {
                            name: f.ident.as_ref().unwrap().to_string().to_mixed_case(),
                            ty: tts.to_string().parse()?,
                        })
                    })
                    .collect::<Result<Vec<IdlField>>>(),
                _ => panic!("Only named structs are allowed."),
            };

            Some(fields.map(|fields| IdlTypeDefinition {
                name,
                ty: IdlTypeDefinitionTy::Struct { fields },
            }))
        })
        .chain(ctx.enums().map(|enm| {
            let name = enm.ident.to_string();
            let variants = enm
                .variants
                .iter()
                .map(|variant: &syn::Variant| {
                    let name = variant.ident.to_string();
                    let fields = match &variant.fields {
                        syn::Fields::Unit => None,
                        syn::Fields::Unnamed(fields) => {
                            let fields: Vec<IdlType> =
                                fields.unnamed.iter().map(to_idl_type).collect();
                            Some(EnumFields::Tuple(fields))
                        }
                        syn::Fields::Named(fields) => {
                            let fields: Vec<IdlField> = fields
                                .named
                                .iter()
                                .map(|f: &syn::Field| {
                                    let name = f.ident.as_ref().unwrap().to_string();
                                    let ty = to_idl_type(f);
                                    IdlField { name, ty }
                                })
                                .collect();
                            Some(EnumFields::Named(fields))
                        }
                    };
                    IdlEnumVariant { name, fields }
                })
                .collect::<Vec<IdlEnumVariant>>();
            Ok(IdlTypeDefinition {
                name,
                ty: IdlTypeDefinitionTy::Enum { variants },
            })
        }))
        .collect()
}
// Parse all user defined types in the file.
Parse all user defined types in the file.
Parse all user defined types in the file .
fn parse_ty_defs(ctx: &CrateContext) -> Result<Vec<IdlTypeDefinition>> {
    ctx.structs()
        .filter_map(|item_strct| {
            // Only take serializable types
            let serializable = item_strct.attrs.iter().any(|attr| {
                let attr_string = attr.tokens.to_string();
                let attr_name = attr.path.segments.last().unwrap().ident.to_string();
                let attr_serializable = ["account", "associated", "event", "zero_copy"];
                let derived_serializable = attr_name == "derive"
                    && attr_string.contains("AnchorSerialize")
                    && attr_string.contains("AnchorDeserialize");
                attr_serializable.iter().any(|a| *a == attr_name) || derived_serializable
            });
            if !serializable {
                return None;
            }
            // Only take public types
            match &item_strct.vis {
                syn::Visibility::Public(_) => (),
                _ => return None,
            }
            let name = item_strct.ident.to_string();
            let fields = match &item_strct.fields {
                syn::Fields::Named(fields) => fields
                    .named
                    .iter()
                    .map(|f: &syn::Field| {
                        let mut tts = proc_macro2::TokenStream::new();
                        f.ty.to_tokens(&mut tts);
                        Ok(IdlField {
                            name: f.ident.as_ref().unwrap().to_string().to_mixed_case(),
                            ty: tts.to_string().parse()?,
                        })
                    })
                    .collect::<Result<Vec<IdlField>>>(),
                _ => panic!("Only named structs are allowed."),
            };
            Some(fields.map(|fields| IdlTypeDefinition {
                name,
                ty: IdlTypeDefinitionTy::Struct { fields },
            }))
        })
        .chain(ctx.enums().map(|enm| {
            let name = enm.ident.to_string();
            let variants = enm
                .variants
                .iter()
                .map(|variant: &syn::Variant| {
                    let name = variant.ident.to_string();
                    let fields = match &variant.fields {
                        syn::Fields::Unit => None,
                        syn::Fields::Unnamed(fields) => {
                            let fields: Vec<IdlType> =
                                fields.unnamed.iter().map(to_idl_type).collect();
                            Some(EnumFields::Tuple(fields))
                        }
                        syn::Fields::Named(fields) => {
                            let fields: Vec<IdlField> = fields
                                .named
                                .iter()
                                .map(|f: &syn::Field| {
                                    let name = f.ident.as_ref().unwrap().to_string();
                                    let ty = to_idl_type(f);
                                    IdlField { name, ty }
                                })
                                .collect();
                            Some(EnumFields::Named(fields))
                        }
                    };
                    IdlEnumVariant { name, fields }
                })
                .collect::<Vec<IdlEnumVariant>>();
            Ok(IdlTypeDefinition {
                name,
                ty: IdlTypeDefinitionTy::Enum { variants },
            })
        }))
        .collect()
}
fn parse_ty_defs ( ctx : & CrateContext ) -> Result < Vec < IdlTypeDefinition > > { ctx . structs ( ) . filter_map ( | item_strct | { // Only take serializable types let serializable = item_strct . attrs . iter ( ) . any ( | attr | { let attr_string = attr . tokens . to_string ( ) ; let attr_name = attr . path . segments . last ( ) . unwrap ( ) . ident . to_string ( ) ; let attr_serializable = [ " " , " " , " " , " " ] ; let derived_serializable = attr_name == " " && attr_string . contains ( " " ) && attr_string . contains ( " " ) ; attr_serializable . iter ( ) . any ( | a | * a == attr_name ) || derived_serializable } ) ; if ! serializable { return None ; } // Only take public types match & item_strct . vis { syn :: Visibility :: Public ( _ ) => ( ) , _ => return None , } let name = item_strct . ident . to_string ( ) ; let fields = match & item_strct . fields { syn :: Fields :: Named ( fields ) => fields . named . iter ( ) . map ( | f : & syn :: Field | { let mut tts = proc_macro2 :: TokenStream :: new ( ) ; f . ty . to_tokens ( & mut tts ) ; Ok ( IdlField { name : f . ident . as_ref ( ) . unwrap ( ) . to_string ( ) . to_mixed_case ( ) , ty : tts . to_string ( ) . parse ( ) ? , } ) } ) . collect :: < Result < Vec < IdlField > > > ( ) , _ => panic ! ( " " ) , } ; Some ( fields . map ( | fields | IdlTypeDefinition { name , ty : IdlTypeDefinitionTy :: Struct { fields } , } ) ) } ) . chain ( ctx . enums ( ) . map ( | enm | { let name = enm . ident . to_string ( ) ; let variants = enm . variants . iter ( ) . map ( | variant : & syn :: Variant | { let name = variant . ident . to_string ( ) ; let fields = match & variant . fields { syn :: Fields :: Unit => None , syn :: Fields :: Unnamed ( fields ) => { let fields : Vec < IdlType > = fields . unnamed . iter ( ) . map ( to_idl_type ) . collect ( ) ; Some ( EnumFields :: Tuple ( fields ) ) } syn :: Fields :: Named ( fields ) => { let fields : Vec < IdlField > = fields . named . iter ( ) . map ( | f : & syn :: Field | { let name = f . ident . as_ref ( ) . unwrap ( ) . to_string ( ) ; let ty = to_idl_type ( f ) ; IdlField { name , ty } } ) . collect ( ) ; Some ( EnumFields :: Named ( fields ) ) } } ; IdlEnumVariant { name , fields } } ) . collect :: < Vec < IdlEnumVariant > > ( ) ; Ok ( IdlTypeDefinition { name , ty : IdlTypeDefinitionTy :: Enum { variants } , } ) } ) ) . collect ( ) }
Parse all user defined types in the file.
Parse all user defined types in the file .
ctx
&CrateContext