From c8f0a78c5c066be649d0a39aebac9f2a0a4ad5ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Wo=C5=BAniak?= Date: Tue, 19 Nov 2024 17:13:22 +0100 Subject: [PATCH] chore: Minor tweaks --- sylvia-derive/src/contract.rs | 19 +++++++++---------- .../src/contract/communication/reply.rs | 17 +++++++++++++---- sylvia-derive/src/lib.rs | 4 ++-- sylvia/src/ctx.rs | 2 ++ 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/sylvia-derive/src/contract.rs b/sylvia-derive/src/contract.rs index 91b66bec..229bab92 100644 --- a/sylvia-derive/src/contract.rs +++ b/sylvia-derive/src/contract.rs @@ -189,28 +189,27 @@ impl<'a> ContractInput<'a> { } fn emit_reply(&self) -> TokenStream { - if self.sv_features.replies { - let variants = MsgVariants::new(self.item.as_variants(), MsgType::Reply, &[], &None); - - Reply::new(self.item, &self.generics, &variants).emit() - } else { - quote! {} + if !self.sv_features.replies { + return quote! {}; } + + let variants = MsgVariants::new(self.item.as_variants(), MsgType::Reply, &[], &None); + + Reply::new(self.item, &self.generics, &variants).emit() } fn emit_instantiate_builder_trait(&self) -> TokenStream { - let item = self.item; let variants = MsgVariants::new( - item.as_variants(), + self.item.as_variants(), MsgType::Instantiate, &self.generics, - &item.generics.where_clause, + &self.item.generics.where_clause, ); let where_clause = variants.where_clause(); match variants.get_only_variant() { Some(variant) => InstantiateBuilder::new( - *item.self_ty.clone(), + *self.item.self_ty.clone(), variants.used_generics(), &where_clause, variant, diff --git a/sylvia-derive/src/contract/communication/reply.rs b/sylvia-derive/src/contract/communication/reply.rs index 95234e39..e5c1041c 100644 --- a/sylvia-derive/src/contract/communication/reply.rs +++ b/sylvia-derive/src/contract/communication/reply.rs @@ -1,3 +1,10 @@ +//! Module responsible for generating `Reply` related code. +//! +//! Based on methods marked with the `#[sv::msg(reply)]` attribute, this module generates: +//! - reply ids for every unique handler, +//! - dispatch method that matches over every generated `ReplyId` and dispatches depending on the `ReplyOn`, +//! - `SubMsgMethods` trait with method for every reply id. + use convert_case::{Case, Casing}; use proc_macro2::TokenStream; use proc_macro_error::emit_error; @@ -11,8 +18,8 @@ use crate::types::msg_field::MsgField; use crate::types::msg_variant::{MsgVariant, MsgVariants}; use crate::utils::emit_turbofish; -const NO_ALLOWED_DATA_FIELDS: usize = 1; -const NO_ALLOWED_RAW_PAYLOAD_FIELDS: usize = 1; +const NUMBER_OF_ALLOWED_DATA_FIELDS: usize = 1; +const NUMBER_OF_ALLOWED_RAW_PAYLOAD_FIELDS: usize = 1; /// Make sure that there are no additional parameters between ones marked /// with `sv::data` and `sv::payload` and after the one marked with `sv::payload`. @@ -23,7 +30,7 @@ fn assert_no_redundant_params(payload: &[&MsgField]) { .is_some() }); - if payload.len() == NO_ALLOWED_RAW_PAYLOAD_FIELDS { + if payload.len() == NUMBER_OF_ALLOWED_RAW_PAYLOAD_FIELDS { return; } @@ -237,7 +244,9 @@ impl<'a> ReplyData<'a> { variant.validate_fields_attributes(); let payload = variant.fields().iter(); let payload = if data.is_some() || variant.msg_attr().reply_on() != ReplyOn::Success { - payload.skip(NO_ALLOWED_DATA_FIELDS).collect::>() + payload + .skip(NUMBER_OF_ALLOWED_DATA_FIELDS) + .collect::>() } else { payload.collect::>() }; diff --git a/sylvia-derive/src/lib.rs b/sylvia-derive/src/lib.rs index 951e944f..8f708ff9 100644 --- a/sylvia-derive/src/lib.rs +++ b/sylvia-derive/src/lib.rs @@ -311,7 +311,7 @@ fn interface_impl(_attr: TokenStream2, item: TokenStream2) -> TokenStream2 { /// } /// /// #[sv::msg(reply)] -/// fn reply(&self, ctx: ReplyCtx, result: SubMsgResult, payload: Binary) -> Result { +/// fn reply(&self, ctx: ReplyCtx, result: SubMsgResult, #[sv::payload(raw)] payload: Binary) -> Result { /// # Ok(Response::new()) /// } /// @@ -767,7 +767,7 @@ fn contract_impl(attr: TokenStream2, item: TokenStream2) -> TokenStream2 { /// # } /// # /// # #[sv::msg(reply)] -/// # fn reply(&self, ctx: ReplyCtx, result: SubMsgResult, payload: Binary) -> StdResult { +/// # fn reply(&self, ctx: ReplyCtx, result: SubMsgResult, #[sv::payload(raw)] payload: Binary) -> StdResult { /// # Ok(Response::new()) /// # } /// # diff --git a/sylvia/src/ctx.rs b/sylvia/src/ctx.rs index 113aa255..7c96d9fa 100644 --- a/sylvia/src/ctx.rs +++ b/sylvia/src/ctx.rs @@ -1,3 +1,5 @@ +//! Module for context types used in method signatures. + use cosmwasm_std::{Deps, DepsMut, Empty, Env, Event, MessageInfo, MsgResponse}; /// Represantation of `reply` context received in entry point.