Skip to content

Commit

Permalink
feat: Override entry_points in multitest helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Jul 7, 2023
1 parent 95024bc commit 74a3645
Show file tree
Hide file tree
Showing 5 changed files with 336 additions and 41 deletions.
6 changes: 5 additions & 1 deletion sylvia-derive/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::crate_module;
use crate::interfaces::Interfaces;
use crate::message::{ContractEnumMessage, EnumMessage, GlueMessage, MsgVariants, StructMessage};
use crate::multitest::{MultitestHelpers, TraitMultitestHelpers};
use crate::parser::{ContractArgs, ContractErrorAttr, Custom, MsgType};
use crate::parser::{ContractArgs, ContractErrorAttr, Custom, MsgType, OverrideEntryPoints};
use crate::remote::Remote;
use crate::variant_descs::AsVariantDescs;

Expand All @@ -27,6 +27,7 @@ pub struct ImplInput<'a> {
item: &'a ItemImpl,
generics: Vec<&'a GenericParam>,
custom: Custom<'a>,
override_entry_points: OverrideEntryPoints,
}

impl<'a> TraitInput<'a> {
Expand Down Expand Up @@ -126,13 +127,15 @@ impl<'a> ImplInput<'a> {
.unwrap_or_else(|| parse_quote! { #sylvia ::cw_std::StdError });

let custom = Custom::new(&item.attrs);
let override_entry_points = OverrideEntryPoints::new(&item.attrs);

Self {
attributes,
item,
generics,
error,
custom,
override_entry_points,
}
}

Expand All @@ -145,6 +148,7 @@ impl<'a> ImplInput<'a> {
&self.error,
&self.generics,
&self.custom,
&self.override_entry_points,
)
.emit()
} else {
Expand Down
8 changes: 4 additions & 4 deletions sylvia-derive/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ impl<'a> MsgVariant<'a> {
/// Emits match leg dispatching against this variant. Assumes enum variants are imported into the
/// scope. Dispatching is performed by calling the function this variant is build from on the
/// `contract` variable, with `ctx` as its first argument - both of them should be in scope.
pub fn emit_dispatch_leg(&self, msg_attr: MsgType) -> TokenStream {
pub fn emit_dispatch_leg(&self, msg_type: MsgType) -> TokenStream {
use MsgType::*;

let Self {
Expand All @@ -568,7 +568,7 @@ impl<'a> MsgVariant<'a> {
.map(|(field, num_field)| quote!(#field : #num_field));

#[cfg(not(tarpaulin_include))]
match msg_attr {
match msg_type {
Exec => quote! {
#name {
#(#fields,)*
Expand All @@ -579,8 +579,8 @@ impl<'a> MsgVariant<'a> {
#(#fields,)*
} => #sylvia ::cw_std::to_binary(&contract.#function_name(Into::into(ctx), #(#args),*)?).map_err(Into::into)
},
Instantiate | Migrate | Reply => {
emit_error!(name.span(), "Instantiation, Reply and Migrate messages not supported on traits, they should be defined on contracts directly");
Instantiate | Migrate | Reply | Sudo => {
emit_error!(name.span(), "Instantiation, Reply, Migrate and Sudo messages not supported on traits, they should be defined on contracts directly");
quote! {}
}
}
Expand Down
100 changes: 66 additions & 34 deletions sylvia-derive/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use syn::{FnArg, GenericParam, ImplItem, ItemImpl, ItemTrait, Pat, PatType, Path
use crate::check_generics::CheckGenerics;
use crate::crate_module;
use crate::message::MsgField;
use crate::parser::{parse_struct_message, ContractMessageAttr, Custom, MsgAttr, MsgType};
use crate::parser::{
parse_struct_message, ContractMessageAttr, Custom, MsgAttr, MsgType, OverrideEntryPoint,
OverrideEntryPoints,
};
use crate::utils::{extract_return_type, process_fields};

struct MessageSignature<'a> {
Expand All @@ -32,6 +35,7 @@ pub struct MultitestHelpers<'a> {
contract_name: &'a Ident,
proxy_name: Ident,
custom: &'a Custom<'a>,
override_entry_points: &'a OverrideEntryPoints,
}

fn interface_name(source: &ItemImpl) -> &Ident {
Expand Down Expand Up @@ -60,6 +64,7 @@ impl<'a> MultitestHelpers<'a> {
contract_error: &'a Type,
generics: &'a [&'a GenericParam],
custom: &'a Custom,
override_entry_points: &'a OverrideEntryPoints,
) -> Self {
let mut is_migrate = false;
let mut reply = None;
Expand Down Expand Up @@ -197,6 +202,7 @@ impl<'a> MultitestHelpers<'a> {
contract_name,
proxy_name,
custom,
override_entry_points,
}
}

Expand Down Expand Up @@ -696,32 +702,64 @@ impl<'a> MultitestHelpers<'a> {

fn generate_impl_contract(&self) -> TokenStream {
let Self {
contract, custom, ..
contract,
custom,
override_entry_points,
..
} = self;
let sylvia = crate_module();

#[cfg(not(tarpaulin_include))]
let migrate_body = if self.is_migrate {
quote! {
#sylvia ::cw_std::from_slice::<MigrateMsg>(&msg)?
.dispatch(self, (deps, env).into())
.map_err(Into::into)
let instantiate_body = override_entry_points
.get_entry_point(MsgType::Instantiate)
.map(OverrideEntryPoint::emit_multitest_dispatch)
.unwrap_or_else(|| {
OverrideEntryPoint::emit_multitest_default_dispatch(MsgType::Instantiate)
});

let exec_body = override_entry_points
.get_entry_point(MsgType::Exec)
.map(OverrideEntryPoint::emit_multitest_dispatch)
.unwrap_or_else(|| OverrideEntryPoint::emit_multitest_default_dispatch(MsgType::Exec));

let query_body = override_entry_points
.get_entry_point(MsgType::Query)
.map(OverrideEntryPoint::emit_multitest_dispatch)
.unwrap_or_else(|| OverrideEntryPoint::emit_multitest_default_dispatch(MsgType::Query));

let sudo_body = override_entry_points
.get_entry_point(MsgType::Sudo)
.map(OverrideEntryPoint::emit_multitest_dispatch)
.unwrap_or_else(|| {
quote! {
#sylvia ::anyhow::bail!("sudo not implemented for contract")
}
});

let migrate_body = match override_entry_points.get_entry_point(MsgType::Migrate) {
Some(entry_point) => entry_point.emit_multitest_dispatch(),
None if self.is_migrate => {
OverrideEntryPoint::emit_multitest_default_dispatch(MsgType::Migrate)
}
} else {
quote! {
None => quote! {
#sylvia ::anyhow::bail!("migrate not implemented for contract")
}
},
};

#[cfg(not(tarpaulin_include))]
let reply = if let Some(reply) = self.reply.as_ref() {
quote! {
self. #reply((deps, env).into(), msg).map_err(Into::into)
}
} else {
quote! {
#sylvia ::anyhow::bail!("reply not implemented for contract")
}
let reply_body = match override_entry_points.get_entry_point(MsgType::Reply) {
Some(entry_point) => entry_point.emit_multitest_dispatch(),
None => self
.reply
.as_ref()
.map(|reply| {
quote! {
self. #reply((deps, env).into(), msg).map_err(Into::into)
}
})
.unwrap_or_else(|| {
quote! {
#sylvia ::anyhow::bail!("reply not implemented for contract")
}
}),
};

let custom_msg = custom.msg();
Expand All @@ -737,9 +775,7 @@ impl<'a> MultitestHelpers<'a> {
info: #sylvia ::cw_std::MessageInfo,
msg: Vec<u8>,
) -> #sylvia ::anyhow::Result<#sylvia ::cw_std::Response<#custom_msg>> {
#sylvia ::cw_std::from_slice::<ContractExecMsg>(&msg)?
.dispatch(self, (deps, env, info))
.map_err(Into::into)
#exec_body
}

fn instantiate(
Expand All @@ -749,9 +785,7 @@ impl<'a> MultitestHelpers<'a> {
info: #sylvia ::cw_std::MessageInfo,
msg: Vec<u8>,
) -> #sylvia ::anyhow::Result<#sylvia ::cw_std::Response<#custom_msg>> {
#sylvia ::cw_std::from_slice::<InstantiateMsg>(&msg)?
.dispatch(self, (deps, env, info))
.map_err(Into::into)
#instantiate_body
}

fn query(
Expand All @@ -760,18 +794,16 @@ impl<'a> MultitestHelpers<'a> {
env: #sylvia ::cw_std::Env,
msg: Vec<u8>,
) -> #sylvia ::anyhow::Result<#sylvia ::cw_std::Binary> {
#sylvia ::cw_std::from_slice::<ContractQueryMsg>(&msg)?
.dispatch(self, (deps, env))
.map_err(Into::into)
#query_body
}

fn sudo(
&self,
_deps: #sylvia ::cw_std::DepsMut<#sylvia ::cw_std::Empty>,
_env: #sylvia ::cw_std::Env,
_msg: Vec<u8>,
deps: #sylvia ::cw_std::DepsMut<#sylvia ::cw_std::Empty>,
env: #sylvia ::cw_std::Env,
msg: Vec<u8>,
) -> #sylvia ::anyhow::Result<#sylvia ::cw_std::Response<#custom_msg>> {
#sylvia ::anyhow::bail!("sudo not implemented for contract")
#sudo_body
}

fn reply(
Expand All @@ -780,7 +812,7 @@ impl<'a> MultitestHelpers<'a> {
env: #sylvia ::cw_std::Env,
msg: #sylvia ::cw_std::Reply,
) -> #sylvia ::anyhow::Result<#sylvia ::cw_std::Response<#custom_msg>> {
#reply
#reply_body
}

fn migrate(
Expand Down
Loading

0 comments on commit 74a3645

Please sign in to comment.