1
1
use proc_macro:: TokenStream ;
2
- use quote:: { format_ident, quote} ;
3
- use syn:: parse_macro_input;
2
+ use quote:: { format_ident, quote, ToTokens } ;
3
+ use syn:: {
4
+ parse:: { Parse , ParseStream } ,
5
+ parse_macro_input, parse_quote,
6
+ punctuated:: Punctuated ,
7
+ Token ,
8
+ } ;
9
+
10
+ struct Options {
11
+ crate_path : syn:: Path ,
12
+ }
13
+
14
+ impl Default for Options {
15
+ fn default ( ) -> Self {
16
+ Self {
17
+ crate_path : parse_quote ! ( :: cosmwasm_std) ,
18
+ }
19
+ }
20
+ }
21
+
22
+ impl Parse for Options {
23
+ fn parse ( input : ParseStream ) -> syn:: Result < Self > {
24
+ let mut ret = Self :: default ( ) ;
25
+ if input. is_empty ( ) {
26
+ return Ok ( ret) ;
27
+ }
28
+
29
+ let attrs = Punctuated :: < syn:: MetaNameValue , Token ! [ , ] > :: parse_terminated ( input) ?;
30
+
31
+ for kv in attrs {
32
+ if kv. path . is_ident ( "crate" ) {
33
+ let path_as_string: syn:: LitStr = syn:: parse2 ( kv. value . to_token_stream ( ) ) ?;
34
+ ret. crate_path = path_as_string. parse ( ) ?
35
+ } else {
36
+ return Err ( syn:: Error :: new_spanned ( kv, "Unknown attribute" ) ) ;
37
+ }
38
+ }
39
+
40
+ Ok ( ret)
41
+ }
42
+ }
4
43
5
44
/// This attribute macro generates the boilerplate required to call into the
6
45
/// contract-specific logic from the entry-points to the Wasm module.
@@ -50,9 +89,10 @@ use syn::parse_macro_input;
50
89
/// where `InstantiateMsg`, `ExecuteMsg`, and `QueryMsg` are contract defined
51
90
/// types that implement `DeserializeOwned + JsonSchema`.
52
91
#[ proc_macro_attribute]
53
- pub fn entry_point ( _attr : TokenStream , mut item : TokenStream ) -> TokenStream {
92
+ pub fn entry_point ( attr : TokenStream , mut item : TokenStream ) -> TokenStream {
54
93
let cloned = item. clone ( ) ;
55
94
let function = parse_macro_input ! ( cloned as syn:: ItemFn ) ;
95
+ let Options { crate_path } = parse_macro_input ! ( attr as Options ) ;
56
96
57
97
// The first argument is `deps`, the rest is region pointers
58
98
let args = function. sig . inputs . len ( ) - 1 ;
@@ -68,7 +108,7 @@ pub fn entry_point(_attr: TokenStream, mut item: TokenStream) -> TokenStream {
68
108
mod #wasm_export { // new module to avoid conflict of function name
69
109
#[ no_mangle]
70
110
extern "C" fn #fn_name( #( #decl_args : u32 ) , * ) -> u32 {
71
- cosmwasm_std :: #do_call( & super :: #fn_name, #( #call_args ) , * )
111
+ #crate_path :: #do_call( & super :: #fn_name, #( #call_args ) , * )
72
112
}
73
113
}
74
114
} ;
0 commit comments