Skip to main content

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),* $(,)? },
10        manifest_input: struct { $($manifest_input_ident: ident: $manifest_input_type: ty),* $(,)? }
11    ) => {
12        paste::paste! {
13            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _IDENT >]: &'static str = stringify!($function_name);
14            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _EXPORT_NAME >]: &'static str = stringify!([<$function_name _ $blueprint_name:snake>]);
15
16            $crate::blueprints::macros::resolve_struct_definition! {
17                [< $blueprint_name:camel $function_name:camel Input >],
18                [radix_common::ScryptoSbor],
19                $($input_ident: $input_type),*
20            }
21
22            $crate::blueprints::macros::resolve_struct_definition! {
23                [< $blueprint_name:camel $function_name:camel Output >],
24                [radix_common::ScryptoSbor],
25                $($output_ident: $output_type),*
26            }
27
28            $crate::blueprints::macros::resolve_struct_definition! {
29                [< $blueprint_name:camel $function_name:camel ManifestInput >],
30                [radix_common::ManifestSbor, $crate::internal_prelude::ScryptoDescribe],
31                $($manifest_input_ident: $manifest_input_type),*
32            }
33        }
34    };
35    (
36        blueprint_name: $blueprint_name: ident,
37        function_name: $function_name: ident,
38        input: struct { $($input_ident: ident: $input_type: ty),* $(,)? },
39        output: type $output_type:ty,
40        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            $crate::blueprints::macros::resolve_struct_definition! {
58                [< $blueprint_name:camel $function_name:camel ManifestInput >],
59                [radix_common::ManifestSbor, $crate::internal_prelude::ScryptoDescribe],
60                $($manifest_input_ident: $manifest_input_type),*
61            }
62        }
63    };
64    (
65        blueprint_name: $blueprint_name: ident,
66        function_name: $function_name: ident,
67        input: type $input_type:ty,
68        output: struct { $($output_ident: ident: $output_type: ty),* $(,)? },
69        manifest_input: struct { $($manifest_input_ident: ident: $manifest_input_type: ty),* $(,)? }
70    ) => {
71        paste::paste! {
72            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _IDENT >]: &'static str = stringify!($function_name);
73            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _EXPORT_NAME >]: &'static str = stringify!([<$function_name _ $blueprint_name:snake>]);
74
75            $crate::blueprints::macros::resolve_type_definition! {
76                [< $blueprint_name:camel $function_name:camel Input >],
77                $input_type
78            }
79
80            $crate::blueprints::macros::resolve_struct_definition! {
81                [< $blueprint_name:camel $function_name:camel Output >],
82                [radix_common::ScryptoSbor],
83                $($output_ident: $output_type),*
84            }
85
86            $crate::blueprints::macros::resolve_struct_definition! {
87                [< $blueprint_name:camel $function_name:camel ManifestInput >],
88                [radix_common::ManifestSbor, $crate::internal_prelude::ScryptoDescribe],
89                $($manifest_input_ident: $manifest_input_type),*
90            }
91        }
92    };
93    (
94        blueprint_name: $blueprint_name: ident,
95        function_name: $function_name: ident,
96        input: type $input_type:ty,
97        output: type $output_type:ty,
98        manifest_input: struct { $($manifest_input_ident: ident: $manifest_input_type: ty),* $(,)? }
99    ) => {
100        paste::paste! {
101            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _IDENT >]: &'static str = stringify!($function_name);
102            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _EXPORT_NAME >]: &'static str = stringify!([<$function_name _ $blueprint_name:snake>]);
103
104            $crate::blueprints::macros::resolve_type_definition! {
105                [< $blueprint_name:camel $function_name:camel Input >],
106                $input_type
107            }
108
109            $crate::blueprints::macros::resolve_type_definition! {
110                [< $blueprint_name:camel $function_name:camel Output >],
111                $output_type
112            }
113
114            $crate::blueprints::macros::resolve_struct_definition! {
115                [< $blueprint_name:camel $function_name:camel ManifestInput >],
116                [radix_common::ManifestSbor, $crate::internal_prelude::ScryptoDescribe],
117                $($manifest_input_ident: $manifest_input_type),*
118            }
119        }
120    };
121}
122
123macro_rules! resolve_struct_definition {
124    ($name: ident, [$($derive: ty),* $(,)?] $(,)?) => {
125        #[derive( $($derive),* )]
126        #[derive(sbor::rust::fmt::Debug, Eq, PartialEq)]
127        pub struct $name;
128    };
129    ($name: ident, [$($derive: ty),* $(,)?], $($ident: ident: $type: ty),* $(,)?) => {
130        #[derive( $($derive),* )]
131        #[derive(sbor::rust::fmt::Debug, Eq, PartialEq)]
132        pub struct $name {
133            $(
134                pub $ident: $type,
135            )*
136        }
137    };
138}
139
140macro_rules! resolve_type_definition {
141    ($name: ident, $type: ty) => {
142        pub type $name = $type;
143    };
144}
145
146pub(crate) use {define_invocation, resolve_struct_definition, resolve_type_definition};