Skip to content

Commit

Permalink
chore: Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Nov 20, 2024
1 parent f721860 commit c8f0a78
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
19 changes: 9 additions & 10 deletions sylvia-derive/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 13 additions & 4 deletions sylvia-derive/src/contract/communication/reply.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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`.
Expand All @@ -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;
}

Expand Down Expand Up @@ -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::<Vec<_>>()
payload
.skip(NUMBER_OF_ALLOWED_DATA_FIELDS)
.collect::<Vec<_>>()
} else {
payload.collect::<Vec<_>>()
};
Expand Down
4 changes: 2 additions & 2 deletions sylvia-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response, ContractError> {
/// fn reply(&self, ctx: ReplyCtx, result: SubMsgResult, #[sv::payload(raw)] payload: Binary) -> Result<Response, ContractError> {
/// # Ok(Response::new())
/// }
///
Expand Down Expand Up @@ -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<Response> {
/// # fn reply(&self, ctx: ReplyCtx, result: SubMsgResult, #[sv::payload(raw)] payload: Binary) -> StdResult<Response> {
/// # Ok(Response::new())
/// # }
/// #
Expand Down
2 changes: 2 additions & 0 deletions sylvia/src/ctx.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit c8f0a78

Please sign in to comment.