accounts.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. use crate::Program;
  2. use heck::SnakeCase;
  3. use quote::quote;
  4. pub fn generate(program: &Program) -> proc_macro2::TokenStream {
  5. let mut accounts = std::collections::HashMap::new();
  6. // Go through instruction accounts.
  7. for ix in &program.ixs {
  8. let anchor_ident = &ix.anchor_ident;
  9. // TODO: move to fn and share with accounts.rs.
  10. let macro_name = format!(
  11. "__client_accounts_{}",
  12. anchor_ident.to_string().to_snake_case()
  13. );
  14. accounts.insert(macro_name, ix.cfgs.as_slice());
  15. }
  16. // Build the tokens from all accounts
  17. let account_structs: Vec<proc_macro2::TokenStream> = accounts
  18. .iter()
  19. .map(|(macro_name, cfgs)| {
  20. let macro_name: proc_macro2::TokenStream = macro_name.parse().unwrap();
  21. quote! {
  22. #(#cfgs)*
  23. pub use crate::#macro_name::*;
  24. }
  25. })
  26. .collect();
  27. // TODO: calculate the account size and add it as a constant field to
  28. // each struct here. This is convenient for Rust clients.
  29. quote! {
  30. /// An Anchor generated module, providing a set of structs
  31. /// mirroring the structs deriving `Accounts`, where each field is
  32. /// a `Pubkey`. This is useful for specifying accounts for a client.
  33. pub mod accounts {
  34. #(#account_structs)*
  35. }
  36. }
  37. }