fuels_macros/
lib.rs

1use fuels_code_gen::Abigen;
2use proc_macro::TokenStream;
3use syn::{parse_macro_input, DeriveInput};
4
5use crate::{
6    abigen::MacroAbigenTargets,
7    derive::{
8        parameterize::generate_parameterize_impl, tokenizable::generate_tokenizable_impl,
9        try_from::generate_try_from_impl,
10    },
11    setup_program_test::{generate_setup_program_test_code, TestProgramCommands},
12};
13
14mod abigen;
15mod derive;
16mod parse_utils;
17mod setup_program_test;
18
19/// Used to generate bindings for Contracts, Scripts and Predicates. Accepts
20/// input in the form of `ProgramType(name="MyBindings", abi=ABI_SOURCE)...`
21///
22/// `ProgramType` is either `Contract`, `Script` or `Predicate`.
23///
24/// `ABI_SOURCE` is a string literal representing either a path to the JSON ABI
25/// file or the contents of the JSON ABI file itself.
26///
27///```text
28/// abigen!(Contract(
29///         name = "MyContract",
30///         abi = "packages/fuels/tests/contracts/token_ops/out/release/token_ops-abi.json"
31///     ));
32///```
33///
34/// More details can be found in the [`Fuel Rust SDK Book`](https://fuellabs.github.io/fuels-rs/latest)
35#[proc_macro]
36pub fn abigen(input: TokenStream) -> TokenStream {
37    let targets = parse_macro_input!(input as MacroAbigenTargets);
38
39    Abigen::generate(targets.into(), false)
40        .expect("abigen generation failed")
41        .into()
42}
43
44#[proc_macro]
45pub fn wasm_abigen(input: TokenStream) -> TokenStream {
46    let targets = parse_macro_input!(input as MacroAbigenTargets);
47
48    Abigen::generate(targets.into(), true)
49        .expect("abigen generation failed")
50        .into()
51}
52
53/// Used to reduce boilerplate in integration tests.
54///
55/// More details can be found in the [`Fuel Rust SDK Book`](https://fuellabs.github.io/fuels-rs/latest)
56#[proc_macro]
57pub fn setup_program_test(input: TokenStream) -> TokenStream {
58    let test_program_commands = parse_macro_input!(input as TestProgramCommands);
59
60    generate_setup_program_test_code(test_program_commands)
61        .unwrap_or_else(|e| e.to_compile_error())
62        .into()
63}
64
65#[proc_macro_derive(Parameterize, attributes(FuelsTypesPath, FuelsCorePath, NoStd, Ignore))]
66pub fn parameterize(stream: TokenStream) -> TokenStream {
67    let input = parse_macro_input!(stream as DeriveInput);
68
69    generate_parameterize_impl(input)
70        .unwrap_or_else(|e| e.to_compile_error())
71        .into()
72}
73
74#[proc_macro_derive(Tokenizable, attributes(FuelsTypesPath, FuelsCorePath, NoStd, Ignore))]
75pub fn tokenizable(stream: TokenStream) -> TokenStream {
76    let input = parse_macro_input!(stream as DeriveInput);
77
78    generate_tokenizable_impl(input)
79        .unwrap_or_else(|e| e.to_compile_error())
80        .into()
81}
82
83#[proc_macro_derive(TryFrom, attributes(FuelsTypesPath, FuelsCorePath, NoStd))]
84pub fn try_from(stream: TokenStream) -> TokenStream {
85    let input = parse_macro_input!(stream as DeriveInput);
86
87    generate_try_from_impl(input)
88        .unwrap_or_else(|e| e.to_compile_error())
89        .into()
90}