Skip to content

Commit

Permalink
lang/syn: only consider parsing serializable user types for idl
Browse files Browse the repository at this point in the history
  • Loading branch information
suscd committed Jul 14, 2021
1 parent e1fb610 commit 36ae7ac
Showing 1 changed file with 40 additions and 26 deletions.
66 changes: 40 additions & 26 deletions lang/syn/src/idl/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,35 +343,49 @@ fn parse_account_derives(ctx: &CrateContext) -> HashMap<String, AccountsStruct>
fn parse_ty_defs(ctx: &CrateContext) -> Result<Vec<IdlTypeDefinition>> {
ctx.structs()
.filter_map(|item_strct| {
for attr in &item_strct.attrs {
if attr.tokens.to_string().contains(DERIVE_NAME) {
return None;
}
// Only take serializable types
let serializable = item_strct.attrs.iter().any(|attr| {
let attr_string = attr.tokens.to_string();

attr.path
.get_ident()
.map(|id| id.to_string() == "derive")
.unwrap_or_default()
&& attr_string.contains("AnchorSerialize")
&& attr_string.contains("AnchorDeserialize")
});

if !serializable {
return None;
}
if let syn::Visibility::Public(_) = &item_strct.vis {
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>>>(),
f => panic!("Only named structs are allowed. Got {} {:?}", &name, f),
};

return Some(fields.map(|fields| IdlTypeDefinition {
name,
ty: IdlTypeDefinitionTy::Struct { fields },
}));
// Only take public types
match &item_strct.vis {
syn::Visibility::Public(_) => (),
_ => return None,
}
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().filter_map(|enm| {
let name = enm.ident.to_string();
Expand Down

0 comments on commit 36ae7ac

Please sign in to comment.