cw_orch_fns_derive/lib.rs
1#![recursion_limit = "128"]
2
3mod fns_derive;
4mod helpers;
5mod query_fns;
6
7extern crate proc_macro;
8use helpers::{MsgType, SyncType};
9use proc_macro::TokenStream;
10
11use syn::{parse_macro_input, ItemEnum};
12
13/// Available attributes are :
14///
15/// payable - The Execute function can accept funds
16/// fn_name - Modify the generated function name (useful for query or execute variants for instance)
17/// disable_fields_sorting - By default the fields are sorted on named variants. Disabled this behavior
18/// into - The field can be indicated in the generated function with a type that implements `Into` the field type
19#[proc_macro_derive(ExecuteFns, attributes(cw_orch))]
20pub fn cw_orch_execute(input: TokenStream) -> TokenStream {
21 // We only parse and return the modified code if the flag is activated
22 let ast = parse_macro_input!(input as ItemEnum);
23 fns_derive::fns_derive(MsgType::Execute, SyncType::Sync, ast).into()
24}
25
26/// Available attributes are :
27///
28/// returns - The return type of the query
29/// fn_name - Modify the generated function name (useful for query or execute variants for instance)
30/// disable_fields_sorting - By default the fields are sorted on named variants. Disabled this behavior
31/// into - The field can be indicated in the generated function with a type that implements `Into` the field type
32#[proc_macro_derive(QueryFns, attributes(cw_orch))]
33pub fn cw_orch_query(input: TokenStream) -> TokenStream {
34 let ast = parse_macro_input!(input as ItemEnum);
35 let sync_gen = fns_derive::fns_derive(MsgType::Query, SyncType::Sync, ast.clone());
36 let async_gen = fns_derive::fns_derive(MsgType::Query, SyncType::Async, ast);
37 let tokens = quote::quote! {
38 #sync_gen
39 #async_gen
40 };
41 tokens.into()
42}