anchor_parser_macros/lib.rs
1extern crate proc_macro;
2
3mod codegen;
4mod idl;
5
6use proc_macro::TokenStream;
7use quote::quote;
8
9/// Declare a program module from an Anchor IDL JSON file.
10///
11/// Looks for `idls/{name}.json` by walking up from `CARGO_MANIFEST_DIR`.
12///
13/// # Example
14/// ```ignore
15/// anchor_parser::declare_program!(my_program);
16/// ```
17///
18/// This generates a module `my_program` containing:
19/// - `ID` constant (`Pubkey`)
20/// - `types` module (struct/enum/alias definitions)
21/// - `accounts` module (account types with discriminators + fetch)
22/// - `events` module (event types with `from_log`)
23/// - `instructions` module (instruction builders → `Instruction`)
24/// - `constants` module
25#[proc_macro]
26pub fn declare_program(input: TokenStream) -> TokenStream {
27 let name = syn::parse_macro_input!(input as syn::Ident);
28 match codegen::generate(&name) {
29 Ok(tokens) => tokens.into(),
30 Err(err) => {
31 let msg = err.to_string();
32 quote! { compile_error!(#msg); }.into()
33 }
34 }
35}