to_account_infos.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. use crate::codegen::accounts::{generics, ParsedGenerics};
  2. use crate::{AccountField, AccountsStruct};
  3. use quote::quote;
  4. // Generates the `ToAccountInfos` trait implementation.
  5. pub fn generate(accs: &AccountsStruct) -> proc_macro2::TokenStream {
  6. let name = &accs.ident;
  7. let ParsedGenerics {
  8. combined_generics,
  9. trait_generics,
  10. struct_generics,
  11. where_clause,
  12. } = generics(accs);
  13. let to_acc_infos: Vec<proc_macro2::TokenStream> = accs
  14. .fields
  15. .iter()
  16. .map(|f: &AccountField| {
  17. let name = match f {
  18. AccountField::CompositeField(s) => &s.ident,
  19. AccountField::Field(f) => &f.ident,
  20. };
  21. quote! {
  22. account_infos.extend(self.#name.to_account_infos());
  23. }
  24. })
  25. .collect();
  26. quote! {
  27. #[automatically_derived]
  28. impl<#combined_generics> anchor_lang::ToAccountInfos<#trait_generics> for #name <#struct_generics> #where_clause{
  29. fn to_account_infos(&self) -> Vec<anchor_lang::solana_program::account_info::AccountInfo<'info>> {
  30. let mut account_infos = vec![];
  31. #(#to_acc_infos)*
  32. account_infos
  33. }
  34. }
  35. }
  36. }