use quote::quote;
pub struct EventAuthority {
pub name: &'static str,
pub seeds: proc_macro2::TokenStream,
}
impl EventAuthority {
pub fn get() -> Self {
Self {
name: "event_authority",
seeds: quote! {b"__event_authority"},
}
}
pub fn name_token_stream(&self) -> proc_macro2::TokenStream {
let name_token_stream = syn::parse_str::<syn::Expr>(self.name).unwrap();
quote! {#name_token_stream}
}
}
pub fn add_event_cpi_accounts(
accounts_struct: &syn::ItemStruct,
) -> syn::parse::Result<syn::ItemStruct> {
let syn::ItemStruct {
attrs,
vis,
struct_token,
ident,
generics,
fields,
..
} = accounts_struct;
let fields = fields.into_iter().collect::<Vec<_>>();
let info_lifetime = generics
.lifetimes()
.next()
.map(|lifetime| quote! {#lifetime})
.unwrap_or(quote! {'info});
let generics = generics
.lt_token
.map(|_| quote! {#generics})
.unwrap_or(quote! {<'info>});
let authority = EventAuthority::get();
let authority_name = authority.name_token_stream();
let authority_seeds = authority.seeds;
let accounts_struct = quote! {
#(#attrs)*
#vis #struct_token #ident #generics {
#(#fields,)*
#[account(seeds = [#authority_seeds], bump)]
pub #authority_name: AccountInfo<#info_lifetime>,
pub program: AccountInfo<#info_lifetime>,
}
};
syn::parse2(accounts_struct)
}