multiversx_sc_derive/
lib.rs

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// ensure we don't run out of macro stack
#![recursion_limit = "1024"]

#[macro_use]
extern crate syn;

#[macro_use]
extern crate quote;

mod contract_impl;
mod format;
mod generate;
mod macro_contract;
mod macro_module;
mod macro_proxy;
mod managed_vec_item_derive;
mod model;
mod parse;
mod preprocessing;
mod type_abi_derive;
mod validate;

#[proc_macro_attribute]
pub fn contract(
    args: proc_macro::TokenStream,
    input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    macro_contract::process_contract(args, input)
}

#[proc_macro_attribute]
pub fn module(
    args: proc_macro::TokenStream,
    input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    macro_module::process_module(args, input)
}

#[proc_macro_attribute]
pub fn proxy(
    args: proc_macro::TokenStream,
    input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    macro_proxy::process_proxy(args, input)
}

#[deprecated(
    since = "0.54.4",
    note = "Replace with attribute #[type_abi], which should be placed before all derives. More about this: https://docs.multiversx.com/developers/transactions/tx-migration/#replace-derivetypeabi-with-type_abi"
)]
#[proc_macro_derive(TypeAbi)]
pub fn type_abi_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    type_abi_derive::type_abi_derive(input).into()
}

#[proc_macro_attribute]
pub fn type_abi(
    args: proc_macro::TokenStream,
    input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    assert!(args.is_empty(), "#[type_abi] attribute takes no args");
    type_abi_derive::type_abi_full(input).into()
}

#[proc_macro_derive(ManagedVecItem)]
pub fn managed_vec_item_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let ast = syn::parse(input).unwrap();
    managed_vec_item_derive::managed_vec_item_derive(&ast)
}

#[proc_macro]
pub fn format_receiver_args(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    format::format_receiver_args_macro(input.into()).into()
}

#[proc_macro]
pub fn semver_tuple(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    format::semver_tuple(input.into()).into()
}

#[proc_macro]
pub fn const_managed_decimal(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = parse_macro_input!(input as syn::LitStr);
    let (raw_int, decimals) = format::extract_number_data(input);

    let expanded = quote! {
        multiversx_sc::types::ManagedDecimal::<<Self as ContractBase>::Api, multiversx_sc::types::ConstDecimals<#decimals>>::const_decimals_from_raw(multiversx_sc::types::BigUint::from(#raw_int))
    };

    proc_macro::TokenStream::from(expanded)
}

#[proc_macro]
pub fn managed_decimal(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = parse_macro_input!(input as syn::LitStr);
    let (raw_int, decimals) = format::extract_number_data(input);

    let expanded = quote! {
        multiversx_sc::types::ManagedDecimal::<<Self as ContractBase>::Api, usize>::from_raw_units(multiversx_sc::types::BigUint::from(#raw_int), #decimals)
    };

    proc_macro::TokenStream::from(expanded)
}

#[proc_macro]
pub fn debug_const_managed_decimal(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = parse_macro_input!(input as syn::LitStr);
    let (raw_int, decimals) = format::extract_number_data(input);

    let expanded = quote! {
        multiversx_sc::types::ManagedDecimal::<multiversx_sc_scenario::imports::StaticApi, multiversx_sc::types::ConstDecimals<#decimals>>::const_decimals_from_raw(multiversx_sc::types::BigUint::from(#raw_int))
    };

    proc_macro::TokenStream::from(expanded)
}

#[proc_macro]
pub fn debug_managed_decimal(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = parse_macro_input!(input as syn::LitStr);
    let (raw_int, decimals) = format::extract_number_data(input);

    let expanded = quote! {
        multiversx_sc::types::ManagedDecimal::<multiversx_sc_scenario::imports::StaticApi, usize>::from_raw_units(multiversx_sc::types::BigUint::from(#raw_int), #decimals)
    };

    proc_macro::TokenStream::from(expanded)
}