Skip to main content

anchor_parser_macros/
lib.rs

1extern crate proc_macro;
2
3mod codegen;
4mod idl;
5
6use proc_macro::TokenStream;
7use quote::quote;
8
9/// Generate a complete program module from an Anchor IDL JSON file.
10///
11/// Searches for `idls/{name}.json` by walking up from `CARGO_MANIFEST_DIR`.
12///
13/// # Generated items
14///
15/// - **`ID`** — the program's [`Pubkey`](solana_sdk::pubkey::Pubkey)
16/// - **`types`** — shared structs, enums, and type aliases
17/// - **`accounts`** — account structs with discriminator and deserialization
18/// - **`events`** — event structs with `from_logs` / `from_cpi_logs` parsers
19/// - **`instructions`** — builder functions → `Result<`[`Instruction`](solana_sdk::instruction::Instruction)`, io::Error>`
20/// - **`constants`** — program constants
21/// - **`utils`** — `Event` / `Account` wrapper enums
22///
23/// # Example
24///
25/// ```ignore
26/// anchor_parser::declare_program!(my_program);
27///
28/// // Use generated types:
29/// use my_program::accounts::MyAccount;
30/// use my_program::events::MyEvent;
31/// use my_program::instructions;
32/// ```
33#[proc_macro]
34pub fn declare_program(input: TokenStream) -> TokenStream {
35    let name = syn::parse_macro_input!(input as syn::Ident);
36    match codegen::generate(&name) {
37        Ok(tokens) => tokens.into(),
38        Err(err) => {
39            let msg = err.to_string();
40            quote! { compile_error!(#msg); }.into()
41        }
42    }
43}