From 59be6951033e8b9a48286e55bcc6152202f931e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Wo=C5=BAniak?= Date: Mon, 23 Oct 2023 13:38:54 +0200 Subject: [PATCH] feat: Move `messages` method out of EnumMsg --- examples/interfaces/cw4/src/lib.rs | 4 ++-- sylvia-derive/src/interfaces.rs | 31 +++++++++-------------------- sylvia-derive/src/message.rs | 32 +++++++++++++++++++----------- sylvia-derive/src/parser.rs | 14 ++++++------- 4 files changed, 38 insertions(+), 43 deletions(-) diff --git a/examples/interfaces/cw4/src/lib.rs b/examples/interfaces/cw4/src/lib.rs index cc730746..4b27dcde 100644 --- a/examples/interfaces/cw4/src/lib.rs +++ b/examples/interfaces/cw4/src/lib.rs @@ -84,7 +84,7 @@ mod tests { #[test] fn exec_msgs() { assert_eq!( - Cw4ExecMsg::messages(), + execute_messages(), ["add_hook", "remove_hook", "update_admin", "update_members"] ); } @@ -92,7 +92,7 @@ mod tests { #[test] fn query_msgs() { assert_eq!( - Cw4QueryMsg::messages(), + query_messages(), ["admin", "hooks", "list_members", "member", "total_weight"] ); } diff --git a/sylvia-derive/src/interfaces.rs b/sylvia-derive/src/interfaces.rs index 09559c70..877edebf 100644 --- a/sylvia-derive/src/interfaces.rs +++ b/sylvia-derive/src/interfaces.rs @@ -106,45 +106,32 @@ impl Interfaces { } pub fn emit_messages_call(&self, msg_ty: &MsgType) -> Vec { - 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 { - 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)), diff --git a/sylvia-derive/src/message.rs b/sylvia-derive/src/message.rs index a067a7c3..4af19680 100644 --- a/sylvia-derive/src/message.rs +++ b/sylvia-derive/src/message.rs @@ -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" => { @@ -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,)*] + } } } } @@ -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! { @@ -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,)*] + } } } } @@ -1265,6 +1273,8 @@ 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() { @@ -1272,7 +1282,7 @@ impl<'a> GlueMessage<'a> { } 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(); @@ -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)), @@ -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,)* diff --git a/sylvia-derive/src/parser.rs b/sylvia-derive/src/parser.rs index 864eb183..836ed6df 100644 --- a/sylvia-derive/src/parser.rs +++ b/sylvia-derive/src/parser.rs @@ -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 }, } }