__client_accounts.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. use crate::{AccountField, AccountsStruct};
  2. use heck::SnakeCase;
  3. use quote::quote;
  4. // Generates the private `__client_accounts` mod implementation, containing
  5. // a generated struct mapping 1-1 to the `Accounts` struct, except with
  6. // `Pubkey`s as the types. This is generated for Rust *clients*.
  7. pub fn generate(accs: &AccountsStruct) -> proc_macro2::TokenStream {
  8. let name = &accs.ident;
  9. let account_mod_name: proc_macro2::TokenStream = format!(
  10. "__client_accounts_{}",
  11. accs.ident.to_string().to_snake_case()
  12. )
  13. .parse()
  14. .unwrap();
  15. let account_struct_fields: Vec<proc_macro2::TokenStream> = accs
  16. .fields
  17. .iter()
  18. .map(|f: &AccountField| match f {
  19. AccountField::CompositeField(s) => {
  20. let name = &s.ident;
  21. let symbol: proc_macro2::TokenStream = format!(
  22. "__client_accounts_{0}::{1}",
  23. s.symbol.to_snake_case(),
  24. s.symbol,
  25. )
  26. .parse()
  27. .unwrap();
  28. quote! {
  29. pub #name: #symbol
  30. }
  31. }
  32. AccountField::Field(f) => {
  33. let name = &f.ident;
  34. quote! {
  35. pub #name: anchor_lang::solana_program::pubkey::Pubkey
  36. }
  37. }
  38. })
  39. .collect();
  40. let account_struct_metas: Vec<proc_macro2::TokenStream> = accs
  41. .fields
  42. .iter()
  43. .map(|f: &AccountField| match f {
  44. AccountField::CompositeField(s) => {
  45. let name = &s.ident;
  46. quote! {
  47. account_metas.extend(self.#name.to_account_metas(None));
  48. }
  49. }
  50. AccountField::Field(f) => {
  51. let is_signer = match f.constraints.is_signer() {
  52. false => quote! {false},
  53. true => quote! {true},
  54. };
  55. let meta = match f.constraints.is_mutable() {
  56. false => quote! { anchor_lang::solana_program::instruction::AccountMeta::new_readonly },
  57. true => quote! { anchor_lang::solana_program::instruction::AccountMeta::new },
  58. };
  59. let name = &f.ident;
  60. quote! {
  61. account_metas.push(#meta(self.#name, #is_signer));
  62. }
  63. }
  64. })
  65. .collect();
  66. // Re-export all composite account structs (i.e. other structs deriving
  67. // accounts embedded into this struct. Required because, these embedded
  68. // structs are *not* visible from the #[program] macro, which is responsible
  69. // for generating the `accounts` mod, which aggregates all the the generated
  70. // accounts used for structs.
  71. let re_exports: Vec<proc_macro2::TokenStream> = {
  72. // First, dedup the exports.
  73. let mut re_exports = std::collections::HashSet::new();
  74. for f in accs.fields.iter().filter_map(|f: &AccountField| match f {
  75. AccountField::CompositeField(s) => Some(s),
  76. AccountField::Field(_) => None,
  77. }) {
  78. re_exports.insert(format!(
  79. "__client_accounts_{0}::{1}",
  80. f.symbol.to_snake_case(),
  81. f.symbol,
  82. ));
  83. }
  84. re_exports
  85. .iter()
  86. .map(|symbol: &String| {
  87. let symbol: proc_macro2::TokenStream = symbol.parse().unwrap();
  88. quote! {
  89. pub use #symbol;
  90. }
  91. })
  92. .collect()
  93. };
  94. quote! {
  95. /// An internal, Anchor generated module. This is used (as an
  96. /// implementation detail), to generate a struct for a given
  97. /// `#[derive(Accounts)]` implementation, where each field is a Pubkey,
  98. /// instead of an `AccountInfo`. This is useful for clients that want
  99. /// to generate a list of accounts, without explicitly knowing the
  100. /// order all the fields should be in.
  101. ///
  102. /// To access the struct in this module, one should use the sibling
  103. /// `accounts` module (also generated), which re-exports this.
  104. mod #account_mod_name {
  105. use super::*;
  106. use anchor_lang::prelude::borsh;
  107. #(#re_exports)*
  108. #[derive(anchor_lang::AnchorSerialize)]
  109. pub struct #name {
  110. #(#account_struct_fields),*
  111. }
  112. impl anchor_lang::ToAccountMetas for #name {
  113. fn to_account_metas(&self, is_signer: Option<bool>) -> Vec<anchor_lang::solana_program::instruction::AccountMeta> {
  114. let mut account_metas = vec![];
  115. #(#account_struct_metas)*
  116. account_metas
  117. }
  118. }
  119. }
  120. }
  121. }