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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// TODO: In the future, we would like to converge this macro (or something that provides its
// functionality) with the Scrypto blueprint macro such that they provide similar functionalities
// such as schema generation, generation of stubs, etc...
macro_rules! define_invocation {
    (
        blueprint_name: $blueprint_name: ident,
        function_name: $function_name: ident,
        input: struct { $($input_ident: ident: $input_type: ty),* $(,)? },
        output: struct { $($output_ident: ident: $output_type: ty),* $(,)? } $(,manifest_input: struct { $($manifest_input_ident: ident: $manifest_input_type: ty),* $(,)? } )?
    ) => {
        paste::paste! {
            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _IDENT >]: &'static str = stringify!($function_name);
            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _EXPORT_NAME >]: &'static str = stringify!([<$function_name _ $blueprint_name:snake>]);

            $crate::blueprints::macros::resolve_struct_definition! {
                [< $blueprint_name:camel $function_name:camel Input >],
                radix_common::ScryptoSbor,
                $($input_ident: $input_type),*
            }

            $crate::blueprints::macros::resolve_struct_definition! {
                [< $blueprint_name:camel $function_name:camel Output >],
                radix_common::ScryptoSbor,
                $($output_ident: $output_type),*
            }

            $(
                $crate::blueprints::macros::resolve_struct_definition! {
                    [< $blueprint_name:camel $function_name:camel ManifestInput >],
                    radix_common::ManifestSbor,
                    $($manifest_input_ident: $manifest_input_type),*
                }
            )?
        }
    };
    (
        blueprint_name: $blueprint_name: ident,
        function_name: $function_name: ident,
        input: struct { $($input_ident: ident: $input_type: ty),* $(,)? },
        output: type $output_type:ty $(,manifest_input: struct { $($manifest_input_ident: ident: $manifest_input_type: ty),* $(,)? } )?
    ) => {
        paste::paste! {
            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _IDENT >]: &'static str = stringify!($function_name);
            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _EXPORT_NAME >]: &'static str = stringify!([<$function_name _ $blueprint_name:snake>]);

            $crate::blueprints::macros::resolve_struct_definition! {
                [< $blueprint_name:camel $function_name:camel Input >],
                radix_common::ScryptoSbor,
                $($input_ident: $input_type),*
            }

            $crate::blueprints::macros::resolve_type_definition! {
                [< $blueprint_name:camel $function_name:camel Output >],
                $output_type
            }

            $(
                $crate::blueprints::macros::resolve_struct_definition! {
                    [< $blueprint_name:camel $function_name:camel ManifestInput >],
                    radix_common::ManifestSbor,
                    $($manifest_input_ident: $manifest_input_type),*
                }
            )?
        }
    };
    (
        blueprint_name: $blueprint_name: ident,
        function_name: $function_name: ident,
        input: type $input_type:ty,
        output: struct { $($output_ident: ident: $output_type: ty),* $(,)? } $(,manifest_input: struct { $($manifest_input_ident: ident: $manifest_input_type: ty),* $(,)? } )?
    ) => {
        paste::paste! {
            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _IDENT >]: &'static str = stringify!($function_name);
            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _EXPORT_NAME >]: &'static str = stringify!([<$function_name _ $blueprint_name:snake>]);

            $crate::blueprints::macros::resolve_type_definition! {
                [< $blueprint_name:camel $function_name:camel Input >],
                $input_type
            }

            $crate::blueprints::macros::resolve_struct_definition! {
                [< $blueprint_name:camel $function_name:camel Output >],
                radix_common::ScryptoSbor,
                $($output_ident: $output_type),*
            }

            $(
                $crate::blueprints::macros::resolve_struct_definition! {
                    [< $blueprint_name:camel $function_name:camel ManifestInput >],
                    radix_common::ManifestSbor,
                    $($manifest_input_ident: $manifest_input_type),*
                }
            )?
        }
    };
    (
        blueprint_name: $blueprint_name: ident,
        function_name: $function_name: ident,
        input: type $input_type:ty,
        output: type $output_type:ty $(,manifest_input: struct { $($manifest_input_ident: ident: $manifest_input_type: ty),* $(,)? } )?
    ) => {
        paste::paste! {
            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _IDENT >]: &'static str = stringify!($function_name);
            pub const [< $blueprint_name:snake:upper _ $function_name:snake:upper _EXPORT_NAME >]: &'static str = stringify!([<$function_name _ $blueprint_name:snake>]);

            $crate::blueprints::macros::resolve_type_definition! {
                [< $blueprint_name:camel $function_name:camel Input >],
                $input_type
            }

            $crate::blueprints::macros::resolve_type_definition! {
                [< $blueprint_name:camel $function_name:camel Output >],
                $output_type
            }

            $(
                $crate::blueprints::macros::resolve_struct_definition! {
                    [< $blueprint_name:camel $function_name:camel ManifestInput >],
                    radix_common::ManifestSbor,
                    $($manifest_input_ident: $manifest_input_type),*
                }
            )?
        }
    };
}

macro_rules! resolve_struct_definition {
    ($name: ident, $derive: ty$(,)?) => {
        #[derive(sbor::rust::fmt::Debug, Eq, PartialEq, $derive)]
        pub struct $name;
    };
    ($name: ident, $derive: ty, $($ident: ident: $type: ty),* $(,)?) => {
        #[derive(sbor::rust::fmt::Debug, Eq, PartialEq, $derive)]
        pub struct $name {
            $(
                pub $ident: $type,
            )*
        }
    };
}

macro_rules! resolve_type_definition {
    ($name: ident, $type: ty) => {
        pub type $name = $type;
    };
}

pub(crate) use {define_invocation, resolve_struct_definition, resolve_type_definition};