|
1 | | -use crate::*; |
| 1 | +use std::{ |
| 2 | + collections::{BTreeMap, HashSet}, |
| 3 | + env, fs, |
| 4 | + path::PathBuf, |
| 5 | +}; |
| 6 | + |
| 7 | +use darling::{util::PathList, FromMeta}; |
2 | 8 | use proc_macro2::{Ident, TokenStream}; |
3 | 9 | use quote::{format_ident, quote}; |
4 | 10 |
|
5 | | -/// Generates all CPI helpers. |
6 | | -pub fn generate_cpi_helpers(idl: &anchor_syn::idl::Idl) -> TokenStream { |
7 | | - let program_name: Ident = format_ident!("{}", idl.name); |
| 11 | +use crate::{ |
| 12 | + generate_accounts, generate_ix_handlers, generate_ix_structs, generate_typedefs, GEN_VERSION, |
| 13 | +}; |
| 14 | + |
| 15 | +#[derive(Default, FromMeta)] |
| 16 | +pub struct GeneratorOptions { |
| 17 | + /// Path to the IDL. |
| 18 | + pub idl_path: String, |
| 19 | + /// List of zero copy structs. |
| 20 | + pub zero_copy: Option<PathList>, |
| 21 | + /// List of `repr(packed)` structs. |
| 22 | + pub packed: Option<PathList>, |
| 23 | +} |
| 24 | + |
| 25 | +fn path_list_to_string(list: Option<&PathList>) -> HashSet<String> { |
| 26 | + list.map(|el| { |
| 27 | + el.iter() |
| 28 | + .map(|el| el.get_ident().unwrap().to_string()) |
| 29 | + .collect() |
| 30 | + }) |
| 31 | + .unwrap_or_default() |
| 32 | +} |
| 33 | + |
| 34 | +impl GeneratorOptions { |
| 35 | + pub fn to_generator(&self) -> Generator { |
| 36 | + let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); |
| 37 | + let path = PathBuf::from(cargo_manifest_dir).join(&self.idl_path); |
| 38 | + let idl_contents = fs::read_to_string(&path).unwrap(); |
| 39 | + let idl: anchor_syn::idl::Idl = serde_json::from_str(&idl_contents).unwrap(); |
| 40 | + |
| 41 | + let zero_copy = path_list_to_string(self.zero_copy.as_ref()); |
| 42 | + let packed = path_list_to_string(self.packed.as_ref()); |
| 43 | + |
| 44 | + let mut struct_opts: BTreeMap<String, StructOpts> = BTreeMap::new(); |
| 45 | + let all_structs: HashSet<&String> = zero_copy.union(&packed).collect::<HashSet<_>>(); |
| 46 | + all_structs.into_iter().for_each(|name| { |
| 47 | + struct_opts.insert( |
| 48 | + name.to_string(), |
| 49 | + StructOpts { |
| 50 | + zero_copy: zero_copy.contains(name), |
| 51 | + packed: packed.contains(name), |
| 52 | + }, |
| 53 | + ); |
| 54 | + }); |
8 | 55 |
|
9 | | - let accounts = generate_accounts(&idl.types, &idl.accounts); |
10 | | - let typedefs = generate_typedefs(&idl.types); |
11 | | - let ix_handlers = generate_ix_handlers(&idl.instructions); |
12 | | - let ix_structs = generate_ix_structs(&idl.instructions); |
| 56 | + Generator { idl, struct_opts } |
| 57 | + } |
| 58 | +} |
13 | 59 |
|
14 | | - let docs = format!( |
| 60 | +#[derive(Clone, Copy, Default)] |
| 61 | +pub struct StructOpts { |
| 62 | + pub packed: bool, |
| 63 | + pub zero_copy: bool, |
| 64 | +} |
| 65 | + |
| 66 | +pub struct Generator { |
| 67 | + pub idl: anchor_syn::idl::Idl, |
| 68 | + pub struct_opts: BTreeMap<String, StructOpts>, |
| 69 | +} |
| 70 | + |
| 71 | +impl Generator { |
| 72 | + pub fn generate_cpi_interface(&self) -> TokenStream { |
| 73 | + let idl = &self.idl; |
| 74 | + let program_name: Ident = format_ident!("{}", idl.name); |
| 75 | + |
| 76 | + let accounts = generate_accounts(&idl.types, &idl.accounts, &self.struct_opts); |
| 77 | + let typedefs = generate_typedefs(&idl.types, &self.struct_opts); |
| 78 | + let ix_handlers = generate_ix_handlers(&idl.instructions); |
| 79 | + let ix_structs = generate_ix_structs(&idl.instructions); |
| 80 | + |
| 81 | + let docs = format!( |
15 | 82 | " Anchor CPI crate generated from {} v{} using [anchor-gen](https://crates.io/crates/anchor-gen) v{}.", |
16 | 83 | &idl.name, |
17 | 84 | &idl.version, |
18 | 85 | &GEN_VERSION.unwrap_or("unknown") |
19 | 86 | ); |
20 | 87 |
|
21 | | - quote! { |
22 | | - use anchor_lang::prelude::*; |
| 88 | + quote! { |
| 89 | + use anchor_lang::prelude::*; |
23 | 90 |
|
24 | | - pub mod typedefs { |
25 | | - //! User-defined types. |
26 | | - use super::*; |
27 | | - #typedefs |
28 | | - } |
| 91 | + pub mod typedefs { |
| 92 | + //! User-defined types. |
| 93 | + use super::*; |
| 94 | + #typedefs |
| 95 | + } |
29 | 96 |
|
30 | | - pub mod state { |
31 | | - //! Structs of accounts which hold state. |
32 | | - use super::*; |
33 | | - #accounts |
34 | | - } |
| 97 | + pub mod state { |
| 98 | + //! Structs of accounts which hold state. |
| 99 | + use super::*; |
| 100 | + #accounts |
| 101 | + } |
35 | 102 |
|
36 | | - pub mod ix_accounts { |
37 | | - //! Accounts used in instructions. |
38 | | - use super::*; |
39 | | - #ix_structs |
40 | | - } |
| 103 | + pub mod ix_accounts { |
| 104 | + //! Accounts used in instructions. |
| 105 | + use super::*; |
| 106 | + #ix_structs |
| 107 | + } |
41 | 108 |
|
42 | | - use ix_accounts::*; |
43 | | - pub use state::*; |
44 | | - pub use typedefs::*; |
| 109 | + use ix_accounts::*; |
| 110 | + pub use state::*; |
| 111 | + pub use typedefs::*; |
45 | 112 |
|
46 | | - #[program] |
47 | | - pub mod #program_name { |
48 | | - #![doc = #docs] |
| 113 | + #[program] |
| 114 | + pub mod #program_name { |
| 115 | + #![doc = #docs] |
49 | 116 |
|
50 | | - use super::*; |
51 | | - #ix_handlers |
| 117 | + use super::*; |
| 118 | + #ix_handlers |
| 119 | + } |
52 | 120 | } |
53 | 121 | } |
54 | 122 | } |
0 commit comments