clockwork_anchor_generate_cpi_crate/
lib.rs

1//! Generates a crate for cross-program invocations to an Anchor program from a JSON IDL.
2//!
3//! # Usage
4//!
5//! In a new crate, write:
6//!
7//! ```skip
8//! anchor_gen::generate_cpi_crate!("../../examples/govern-cpi/idl.json");
9//!
10//! declare_id!("GjphYQcbP1m3FuDyCTUJf2mUMxKPE3j6feWU1rxvC7Ps");
11//! ```
12//!
13//! This will generate a fully functional Rust CPI client for your IDL.
14//!
15//! More examples can be found in the [examples/](https://github.com/saber-hq/anchor-gen/tree/master/examples) directory.
16
17use clockwork_anchor_idl::GeneratorOptions;
18use syn::{parse_macro_input, LitStr};
19
20/// Generates an Anchor CPI crate from a JSON file.
21///
22/// # Arguments
23///
24/// * `input` - Path to a JSON IDL relative to the crate's the Cargo.toml.
25///
26/// # Examples
27///
28/// ```
29/// anchor_generate_cpi_crate::generate_cpi_crate!("../../examples/govern-cpi/idl.json");
30/// declare_id!("GjphYQcbP1m3FuDyCTUJf2mUMxKPE3j6feWU1rxvC7Ps");
31/// # fn main() -> Result<()> {
32/// let _my_governor = GovernanceParameters {
33///     quorum_votes: 0,
34///     timelock_delay_seconds: 0,
35///     voting_period: 0,
36///     voting_delay: 0,
37/// };
38/// #   Ok(())
39/// # }
40/// ```
41#[proc_macro]
42pub fn generate_cpi_crate(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
43    let id_literal = parse_macro_input!(input as LitStr);
44    let opts = GeneratorOptions {
45        idl_path: id_literal.value(),
46        ..Default::default()
47    };
48    opts.to_generator().generate_cpi_interface().into()
49}