Skip to content

Commit b2bcbcb

Browse files
committed
Make crate used in entry_point configurable
1 parent 1d06b23 commit b2bcbcb

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

packages/derive/src/lib.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,45 @@
11
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+
}
443

544
/// This attribute macro generates the boilerplate required to call into the
645
/// contract-specific logic from the entry-points to the Wasm module.
@@ -50,9 +89,10 @@ use syn::parse_macro_input;
5089
/// where `InstantiateMsg`, `ExecuteMsg`, and `QueryMsg` are contract defined
5190
/// types that implement `DeserializeOwned + JsonSchema`.
5291
#[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 {
5493
let cloned = item.clone();
5594
let function = parse_macro_input!(cloned as syn::ItemFn);
95+
let Options { crate_path } = parse_macro_input!(attr as Options);
5696

5797
// The first argument is `deps`, the rest is region pointers
5898
let args = function.sig.inputs.len() - 1;
@@ -68,7 +108,7 @@ pub fn entry_point(_attr: TokenStream, mut item: TokenStream) -> TokenStream {
68108
mod #wasm_export { // new module to avoid conflict of function name
69109
#[no_mangle]
70110
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 ),*)
72112
}
73113
}
74114
};

0 commit comments

Comments
 (0)