Skip to content

Commit

Permalink
feat: Generate migrate entry point if message defined on contract
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Sep 18, 2023
1 parent d1c0d6e commit 694a40a
Showing 1 changed file with 32 additions and 8 deletions.
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 @@ impl<'a> GlueMessage<'a> {
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 @@ impl<'a> EntryPoints<'a> {
.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 @@ impl<'a> EntryPoints<'a> {
},
);

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 @@ impl<'a> EntryPoints<'a> {

#(#entry_points)*

#migrate

#reply_ep
}
}
Expand Down

0 comments on commit 694a40a

Please sign in to comment.