Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Generate migrate entry point if message defined on contract #222

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions sylvia-derive/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,9 +1041,9 @@
pub struct EntryPoints<'a> {
name: Type,
error: Type,
reply: Option<Ident>,
custom: Custom<'a>,
override_entry_points: OverrideEntryPoints,
variants: MsgVariants<'a>,
}

impl<'a> EntryPoints<'a> {
Expand All @@ -1068,34 +1068,36 @@
.unwrap_or_else(|| parse_quote! { #sylvia ::cw_std::StdError });

let generics: Vec<_> = source.generics.params.iter().collect();
let reply = MsgVariants::new(source.as_variants(), &generics)
.0
.into_iter()
.find(|variant| variant.msg_type == MsgType::Reply)
.map(|variant| variant.function_name.clone());

let variants = MsgVariants::new(source.as_variants(), &generics);

Check warning on line 1072 in sylvia-derive/src/message.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/message.rs#L1072

Added line #L1072 was not covered by tests
let custom = Custom::new(&source.attrs);

Self {
name,
error,
reply,
custom,
override_entry_points,
variants,
}
}

pub fn emit(&self) -> TokenStream {
let Self {
name,
error,
reply,
custom,
override_entry_points,
variants,

Check warning on line 1090 in sylvia-derive/src/message.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/message.rs#L1090

Added line #L1090 was not covered by tests
} = self;
let sylvia = crate_module();

let custom_msg = custom.msg_or_default();
let custom_query = custom.query_or_default();
let reply = variants
.0

Check warning on line 1097 in sylvia-derive/src/message.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/message.rs#L1096-L1097

Added lines #L1096 - L1097 were not covered by tests
.iter()
.find(|variant| variant.msg_type == MsgType::Reply)
.map(|variant| variant.function_name.clone());

Check warning on line 1100 in sylvia-derive/src/message.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/message.rs#L1099-L1100

Added lines #L1099 - L1100 were not covered by tests

#[cfg(not(tarpaulin_include))]
{
Expand All @@ -1114,6 +1116,26 @@
},
);

let migrate_not_overridden = override_entry_points
.get_entry_point(MsgType::Migrate)
.is_none();
let migrate_msg_defined = variants
.0
.iter()
.any(|variant| variant.msg_type == MsgType::Migrate);

let migrate = if migrate_not_overridden && migrate_msg_defined {
OverrideEntryPoint::emit_default_entry_point(
&custom_msg,
&custom_query,
name,
error,
MsgType::Migrate,
)
} else {
quote! {}
};

let reply_ep = override_entry_points
.get_entry_point(MsgType::Reply)
.map(|_| quote! {})
Expand All @@ -1137,6 +1159,8 @@

#(#entry_points)*

#migrate

#reply_ep
}
}
Expand Down
24 changes: 22 additions & 2 deletions sylvia/tests/messages_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ mod interface {
}

mod contract {
use cosmwasm_std::{Addr, Response, StdResult};
use cosmwasm_std::{Addr, Reply, Response, StdResult};
use sylvia::contract;
use sylvia::types::{ExecCtx, InstantiateCtx, MigrateCtx, QueryCtx};
use sylvia::types::{ExecCtx, InstantiateCtx, MigrateCtx, QueryCtx, ReplyCtx};
use sylvia_derive::entry_points;

use crate::{MyQuery, QueryResult};

pub struct Contract {}

#[cfg(not(tarpaulin_include))]
#[entry_points]
#[contract]
#[allow(dead_code)]
#[sv::custom(query=MyQuery)]
Expand Down Expand Up @@ -100,6 +102,11 @@ mod contract {
fn argumented_query(&self, _ctx: QueryCtx<MyQuery>, _user: Addr) -> StdResult<QueryResult> {
Ok(QueryResult {})
}

#[msg(reply)]
fn my_reply(&self, _ctx: ReplyCtx<MyQuery>, _reply: Reply) -> StdResult<Response> {
Ok(Response::new())
}
}
}

Expand Down Expand Up @@ -154,3 +161,16 @@ fn contract_messages_constructible() {
contract::QueryMsg::ArgumentedQuery { .. } => (),
}
}

#[test]
fn entry_points_generation() {
use contract::entry_points;

let _ = cw_multi_test::ContractWrapper::new(
entry_points::execute,
entry_points::instantiate,
entry_points::query,
)
.with_migrate(entry_points::migrate)
.with_reply(entry_points::reply);
}
Loading