1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use fuels_code_gen::Abigen;
use proc_macro::TokenStream;
use syn::{parse_macro_input, DeriveInput};

use crate::{
    abigen::MacroAbigenTargets,
    derive::{
        parameterize::generate_parameterize_impl, tokenizable::generate_tokenizable_impl,
        try_from::generate_try_from_impl,
    },
    setup_program_test::{generate_setup_program_test_code, TestProgramCommands},
};

mod abigen;
mod derive;
mod parse_utils;
mod setup_program_test;

/// Used to generate bindings for Contracts, Scripts and Predicates. Accepts
/// input in the form of `ProgramType(name="MyBindings", abi=ABI_SOURCE)...`
///
/// `ProgramType` is either `Contract`, `Script` or `Predicate`.
///
/// `ABI_SOURCE` is a string literal representing either a path to the JSON ABI
/// file or the contents of the JSON ABI file itself.
///
///```text
/// abigen!(Contract(
///         name = "MyContract",
///         abi = "packages/fuels/tests/contracts/token_ops/out/release/token_ops-abi.json"
///     ));
///```
///
/// More details can be found in the [`Fuel Rust SDK Book`](https://fuellabs.github.io/fuels-rs/latest)
#[proc_macro]
pub fn abigen(input: TokenStream) -> TokenStream {
    let targets = parse_macro_input!(input as MacroAbigenTargets);

    Abigen::generate(targets.into(), false)
        .expect("abigen generation failed")
        .into()
}

#[proc_macro]
pub fn wasm_abigen(input: TokenStream) -> TokenStream {
    let targets = parse_macro_input!(input as MacroAbigenTargets);

    Abigen::generate(targets.into(), true)
        .expect("abigen generation failed")
        .into()
}

/// Used to reduce boilerplate in integration tests.
///
/// More details can be found in the [`Fuel Rust SDK Book`](https://fuellabs.github.io/fuels-rs/latest)
#[proc_macro]
pub fn setup_program_test(input: TokenStream) -> TokenStream {
    let test_program_commands = parse_macro_input!(input as TestProgramCommands);

    generate_setup_program_test_code(test_program_commands)
        .unwrap_or_else(|e| e.to_compile_error())
        .into()
}

#[proc_macro_derive(Parameterize, attributes(FuelsTypesPath, FuelsCorePath, NoStd, Ignore))]
pub fn parameterize(stream: TokenStream) -> TokenStream {
    let input = parse_macro_input!(stream as DeriveInput);

    generate_parameterize_impl(input)
        .unwrap_or_else(|e| e.to_compile_error())
        .into()
}

#[proc_macro_derive(Tokenizable, attributes(FuelsTypesPath, FuelsCorePath, NoStd, Ignore))]
pub fn tokenizable(stream: TokenStream) -> TokenStream {
    let input = parse_macro_input!(stream as DeriveInput);

    generate_tokenizable_impl(input)
        .unwrap_or_else(|e| e.to_compile_error())
        .into()
}

#[proc_macro_derive(TryFrom, attributes(FuelsTypesPath, FuelsCorePath, NoStd))]
pub fn try_from(stream: TokenStream) -> TokenStream {
    let input = parse_macro_input!(stream as DeriveInput);

    generate_try_from_impl(input)
        .unwrap_or_else(|e| e.to_compile_error())
        .into()
}