fuels_abigen_macro/lib.rs
1extern crate core;
2
3use fuels_core::code_gen::abigen::Abigen;
4use proc_macro::TokenStream;
5use syn::parse_macro_input;
6
7use crate::{
8 abigen_macro::MacroAbigenTargets,
9 setup_contract_test_macro::{generate_setup_contract_test_code, TestContractCommands},
10};
11
12mod abigen_macro;
13mod parse_utils;
14mod setup_contract_test_macro;
15
16/// Used to generate bindings for Contracts, Scripts and Predicates. Accepts
17/// input in the form of `ProgramType(name="MyBindings", abi=ABI_SOURCE)...`
18///
19/// `ProgramType` is either `Contract`, `Script` or `Predicate`.
20///
21/// `ABI_SOURCE` is a string literal representing either a path to the JSON ABI
22/// file or the contents of the JSON ABI file itself.
23///
24///```text
25/// abigen!(Contract(
26/// name = "MyContract",
27/// abi = "packages/fuels/tests/contracts/token_ops/out/debug/token_ops-abi.json"
28/// ));
29///```
30///
31/// More details can be found in the [`Fuel Rust SDK Book`](https://fuellabs.github.io/fuels-rs/latest)
32#[proc_macro]
33pub fn abigen(input: TokenStream) -> TokenStream {
34 let targets = parse_macro_input!(input as MacroAbigenTargets);
35
36 Abigen::generate(targets.into(), false).unwrap().into()
37}
38
39#[proc_macro]
40pub fn wasm_abigen(input: TokenStream) -> TokenStream {
41 let targets = parse_macro_input!(input as MacroAbigenTargets);
42
43 Abigen::generate(targets.into(), true).unwrap().into()
44}
45
46/// Used to reduce boilerplate in integration tests.
47///
48/// More details can be found in the [`Fuel Rust SDK Book`](https://fuellabs.github.io/fuels-rs/latest)
49///```text
50///setup_contract_test!(
51/// Wallets("wallet"),
52/// Abigen(
53/// name = "FooContract",
54/// abi = "packages/fuels/tests/contracts/foo_contract"
55/// ),
56/// Abigen(
57/// name = "FooCallerContract",
58/// abi = "packages/fuels/tests/contracts/foo_caller_contract"
59/// ),
60/// Deploy(
61/// name = "foo_contract_instance",
62/// contract = "FooContract",
63/// wallet = "wallet"
64/// ),
65/// Deploy(
66/// name = "foo_caller_contract_instance",
67/// contract = "FooCallerContract",
68/// wallet = "my_own_wallet"
69/// ),
70///);
71///```
72#[proc_macro]
73pub fn setup_contract_test(input: TokenStream) -> TokenStream {
74 let test_contract_commands = parse_macro_input!(input as TestContractCommands);
75
76 generate_setup_contract_test_code(test_contract_commands)
77 .unwrap_or_else(|e| e.to_compile_error())
78 .into()
79}