radix_engine_interface/blueprints/
macros.rs

1// TODO: In the future, we would like to converge this macro (or something that provides its
2// functionality) with the Scrypto blueprint macro such that they provide similar functionalities
3// such as schema generation, generation of stubs, etc...
4macro_rules! define_invocation {
5    (
6        blueprint_name: $blueprint_name: ident,
7        function_name: $function_name: ident,
8        input: struct { $($input_ident: ident: $input_type: ty),* $(,)? },
9        output: struct { $($output_ident: ident: $output_type: ty),* $(,)? } $(,manifest_input: struct { $($manifest_input_ident: ident: $manifest_input_type: ty),* $(,)? } )?
10    ) => {
11        paste::paste! {
12            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _IDENT >]: &'static str = stringify!($function_name);
13            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _EXPORT_NAME >]: &'static str = stringify!([<$function_name _ $blueprint_name:snake>]);
14
15            $crate::blueprints::macros::resolve_struct_definition! {
16                [< $blueprint_name:camel $function_name:camel Input >],
17                radix_common::ScryptoSbor,
18                $($input_ident: $input_type),*
19            }
20
21            $crate::blueprints::macros::resolve_struct_definition! {
22                [< $blueprint_name:camel $function_name:camel Output >],
23                radix_common::ScryptoSbor,
24                $($output_ident: $output_type),*
25            }
26
27            $(
28                $crate::blueprints::macros::resolve_struct_definition! {
29                    [< $blueprint_name:camel $function_name:camel ManifestInput >],
30                    radix_common::ManifestSbor,
31                    $($manifest_input_ident: $manifest_input_type),*
32                }
33            )?
34        }
35    };
36    (
37        blueprint_name: $blueprint_name: ident,
38        function_name: $function_name: ident,
39        input: struct { $($input_ident: ident: $input_type: ty),* $(,)? },
40        output: type $output_type:ty $(,manifest_input: struct { $($manifest_input_ident: ident: $manifest_input_type: ty),* $(,)? } )?
41    ) => {
42        paste::paste! {
43            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _IDENT >]: &'static str = stringify!($function_name);
44            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _EXPORT_NAME >]: &'static str = stringify!([<$function_name _ $blueprint_name:snake>]);
45
46            $crate::blueprints::macros::resolve_struct_definition! {
47                [< $blueprint_name:camel $function_name:camel Input >],
48                radix_common::ScryptoSbor,
49                $($input_ident: $input_type),*
50            }
51
52            $crate::blueprints::macros::resolve_type_definition! {
53                [< $blueprint_name:camel $function_name:camel Output >],
54                $output_type
55            }
56
57            $(
58                $crate::blueprints::macros::resolve_struct_definition! {
59                    [< $blueprint_name:camel $function_name:camel ManifestInput >],
60                    radix_common::ManifestSbor,
61                    $($manifest_input_ident: $manifest_input_type),*
62                }
63            )?
64        }
65    };
66    (
67        blueprint_name: $blueprint_name: ident,
68        function_name: $function_name: ident,
69        input: type $input_type:ty,
70        output: struct { $($output_ident: ident: $output_type: ty),* $(,)? } $(,manifest_input: struct { $($manifest_input_ident: ident: $manifest_input_type: ty),* $(,)? } )?
71    ) => {
72        paste::paste! {
73            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _IDENT >]: &'static str = stringify!($function_name);
74            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _EXPORT_NAME >]: &'static str = stringify!([<$function_name _ $blueprint_name:snake>]);
75
76            $crate::blueprints::macros::resolve_type_definition! {
77                [< $blueprint_name:camel $function_name:camel Input >],
78                $input_type
79            }
80
81            $crate::blueprints::macros::resolve_struct_definition! {
82                [< $blueprint_name:camel $function_name:camel Output >],
83                radix_common::ScryptoSbor,
84                $($output_ident: $output_type),*
85            }
86
87            $(
88                $crate::blueprints::macros::resolve_struct_definition! {
89                    [< $blueprint_name:camel $function_name:camel ManifestInput >],
90                    radix_common::ManifestSbor,
91                    $($manifest_input_ident: $manifest_input_type),*
92                }
93            )?
94        }
95    };
96    (
97        blueprint_name: $blueprint_name: ident,
98        function_name: $function_name: ident,
99        input: type $input_type:ty,
100        output: type $output_type:ty $(,manifest_input: struct { $($manifest_input_ident: ident: $manifest_input_type: ty),* $(,)? } )?
101    ) => {
102        paste::paste! {
103            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _IDENT >]: &'static str = stringify!($function_name);
104            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _EXPORT_NAME >]: &'static str = stringify!([<$function_name _ $blueprint_name:snake>]);
105
106            $crate::blueprints::macros::resolve_type_definition! {
107                [< $blueprint_name:camel $function_name:camel Input >],
108                $input_type
109            }
110
111            $crate::blueprints::macros::resolve_type_definition! {
112                [< $blueprint_name:camel $function_name:camel Output >],
113                $output_type
114            }
115
116            $(
117                $crate::blueprints::macros::resolve_struct_definition! {
118                    [< $blueprint_name:camel $function_name:camel ManifestInput >],
119                    radix_common::ManifestSbor,
120                    $($manifest_input_ident: $manifest_input_type),*
121                }
122            )?
123        }
124    };
125}
126
127macro_rules! resolve_struct_definition {
128    ($name: ident, $derive: ty$(,)?) => {
129        #[derive(sbor::rust::fmt::Debug, Eq, PartialEq, $derive)]
130        pub struct $name;
131    };
132    ($name: ident, $derive: ty, $($ident: ident: $type: ty),* $(,)?) => {
133        #[derive(sbor::rust::fmt::Debug, Eq, PartialEq, $derive)]
134        pub struct $name {
135            $(
136                pub $ident: $type,
137            )*
138        }
139    };
140}
141
142macro_rules! resolve_type_definition {
143    ($name: ident, $type: ty) => {
144        pub type $name = $type;
145    };
146}
147
148pub(crate) use {define_invocation, resolve_struct_definition, resolve_type_definition};