Skip to content

Commit

Permalink
feat: Move messages method out of EnumMsg
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Oct 23, 2023
1 parent 1c6cb24 commit dfa4f54
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 43 deletions.
4 changes: 2 additions & 2 deletions examples/interfaces/cw4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ mod tests {
#[test]
fn exec_msgs() {
assert_eq!(
Cw4ExecMsg::messages(),
execute_messages(),
["add_hook", "remove_hook", "update_admin", "update_members"]
);
}

#[test]
fn query_msgs() {
assert_eq!(
Cw4QueryMsg::messages(),
query_messages(),
["admin", "hooks", "list_members", "member", "total_weight"]
);
}
Expand Down
31 changes: 9 additions & 22 deletions sylvia-derive/src/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,45 +106,32 @@ impl Interfaces {
}

pub fn emit_messages_call(&self, msg_ty: &MsgType) -> Vec<TokenStream> {
let sylvia = crate_module();

self.interfaces
.iter()
.map(|interface| {
let ContractMessageAttr {
module, generics, ..
} = interface;
let generics = if !generics.is_empty() {
quote! { < #generics > }
} else {
quote! {}
};
let type_name = msg_ty.as_accessor_name();
let ContractMessageAttr { module, .. } = interface;

let ep_name = msg_ty.emit_ep_name();
let messages_fn_name = Ident::new(&format!("{}_messages", ep_name), module.span());
quote! {
&<#module :: InterfaceTypes #generics as #sylvia ::types::InterfaceMessages> :: #type_name :: messages()
&#module :: #messages_fn_name()
}
})
.collect()
}

pub fn emit_deserialization_attempts(&self, msg_ty: &MsgType) -> Vec<TokenStream> {
let sylvia = crate_module();

self.interfaces
.iter()
.map(|interface| {
let ContractMessageAttr {
module, variant, generics, ..
module, variant, ..
} = interface;
let generics = if !generics.is_empty() {
quote! { < #generics > }
} else {
quote! {}
};
let ep_name = msg_ty.emit_ep_name();
let messages_fn_name = Ident::new(&format!("{}_messages", ep_name), module.span());

let type_name = msg_ty.as_accessor_name();
quote! {
let msgs = &<#module :: InterfaceTypes #generics as #sylvia ::types::InterfaceMessages> :: #type_name :: messages();
let msgs = &#module :: #messages_fn_name();
if msgs.into_iter().any(|msg| msg == &recv_msg_name) {
match val.deserialize_into() {
Ok(msg) => return Ok(Self:: #variant (msg)),
Expand Down
32 changes: 20 additions & 12 deletions sylvia-derive/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ impl<'a> EnumMessage<'a> {

let unique_enum_name = Ident::new(&format!("{}{}", trait_name, name), name.span());

let ep_name = msg_ty.emit_ep_name();
let messages_fn_name = Ident::new(&format!("{}_messages", ep_name), name.span());

#[cfg(not(tarpaulin_include))]
let enum_declaration = match name.to_string().as_str() {
"QueryMsg" => {
Expand Down Expand Up @@ -336,11 +339,12 @@ impl<'a> EnumMessage<'a> {
#match_arms
}
}
pub const fn messages() -> [&'static str; #msgs_cnt] {
[#(#msgs,)*]
}
#(#variants_constructors)*
}

pub const fn #messages_fn_name () -> [&'static str; #msgs_cnt] {
[#(#msgs,)*]
}
}
}
}
Expand Down Expand Up @@ -411,6 +415,9 @@ impl<'a> ContractEnumMessage<'a> {
_ => quote! {},
};

let ep_name = msg_ty.emit_ep_name();
let messages_fn_name = Ident::new(&format!("{}_messages", ep_name), contract.span());

#[cfg(not(tarpaulin_include))]
{
quote! {
Expand All @@ -429,12 +436,13 @@ impl<'a> ContractEnumMessage<'a> {
#(#match_arms,)*
}
}
pub const fn messages() -> [&'static str; #variants_cnt] {
[#(#variant_names,)*]
}

#(#variants_constructors)*
}

pub const fn #messages_fn_name () -> [&'static str; #variants_cnt] {
[#(#variant_names,)*]
}
}
}
}
Expand Down Expand Up @@ -1265,14 +1273,16 @@ impl<'a> GlueMessage<'a> {

let variants = interfaces.emit_glue_message_variants(msg_ty);

let ep_name = msg_ty.emit_ep_name();
let messages_fn_name = Ident::new(&format!("{}_messages", ep_name), contract.span());
let contract_variant = quote! { #contract_name ( #enum_name #bracketed_used_generics ) };
let mut messages_call = interfaces.emit_messages_call(msg_ty);
let prefixed_used_generics = if !used_generics.is_empty() {
quote! { :: #bracketed_used_generics }
} else {
quote! {}
};
messages_call.push(quote! { &#enum_name #prefixed_used_generics :: messages() });
messages_call.push(quote! { &#messages_fn_name() });

let variants_cnt = messages_call.len();

Expand Down Expand Up @@ -1310,7 +1320,7 @@ impl<'a> GlueMessage<'a> {

#[cfg(not(tarpaulin_include))]
let contract_deserialization_attempt = quote! {
let msgs = &#enum_name #prefixed_used_generics :: messages();
let msgs = &#messages_fn_name();
if msgs.into_iter().any(|msg| msg == &recv_msg_name) {
match val.deserialize_into() {
Ok(msg) => return Ok(Self:: #contract_name (msg)),
Expand Down Expand Up @@ -1363,12 +1373,10 @@ impl<'a> GlueMessage<'a> {
contract: &#contract,
ctx: #ctx_type,
) -> #ret_type #full_where_clause {
const fn assert_no_intersection #bracketed_used_generics () #where_clause {
const _: () = {
let msgs: [&[&str]; #variants_cnt] = [#(#messages_call),*];
#sylvia ::utils::assert_no_intersection(msgs);
}

assert_no_intersection #prefixed_used_generics ();
};

match self {
#(#dispatch_arms,)*
Expand Down
14 changes: 7 additions & 7 deletions sylvia-derive/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,16 @@ impl MsgType {
}

#[cfg(not(tarpaulin_include))]
pub fn emit_ep_name(self) -> TokenStream {
pub fn emit_ep_name(self) -> Ident {
use MsgType::*;

match self {
Exec => quote! { execute },
Instantiate => quote! { instantiate },
Migrate => quote! { migrate },
Sudo => quote! { sudo },
Reply => quote! { reply },
Query => quote! { query },
Exec => parse_quote! { execute },
Instantiate => parse_quote! { instantiate },
Migrate => parse_quote! { migrate },
Sudo => parse_quote! { sudo },
Reply => parse_quote! { reply },
Query => parse_quote! { query },
}
}

Expand Down

0 comments on commit dfa4f54

Please sign in to comment.