Skip to content

Commit

Permalink
chore: Rename ReplyOn::Failure to match variant from std
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Oct 22, 2024
1 parent fb28493 commit b6cb2ca
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 39 deletions.
28 changes: 14 additions & 14 deletions sylvia-derive/src/contract/communication/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,25 +245,25 @@ impl<'a> ReplyData<'a> {
self.handlers.push((new_function_name, new_reply_on));
}

/// Emits success and failure match arms for a single `ReplyId`.
/// Emits success and error match arms for a single `ReplyId`.
fn emit_match_arms(&self, contract: &Type, generics: &[&GenericParam]) -> TokenStream {
let reply_id = &self.reply_id;
let contract_turbofish = emit_turbofish(contract, generics);
let success_match_arm = self.emit_success_match_arm(&contract_turbofish);
let failure_match_arm = self.emit_failure_match_arm(&contract_turbofish);
let error_match_arm = self.emit_error_match_arm(&contract_turbofish);

quote! {
#reply_id => {
match result {
#success_match_arm
#failure_match_arm
#error_match_arm
}
}
}
}

/// Emits [cosmwasm_std::ReplyOn] value to be put in the `cosmwasm_std::SubMsg`.
/// If both `Success` and `Failure` is defined for given `reply_id`, `cosmwasm_std::ReplyOn::Always` is returned.
/// If both `Success` and `Error` is defined for given `reply_id`, `cosmwasm_std::ReplyOn::Always` is returned.
fn emit_cw_reply_on(&self) -> TokenStream {
let sylvia = crate_module();
let is_always = self
Expand All @@ -274,20 +274,20 @@ impl<'a> ReplyData<'a> {
.handlers
.iter()
.any(|(_, reply_on)| reply_on == &ReplyOn::Success);
let is_failure = self
let is_error = self
.handlers
.iter()
.any(|(_, reply_on)| reply_on == &ReplyOn::Failure);
.any(|(_, reply_on)| reply_on == &ReplyOn::Error);

if is_always || (is_success && is_failure) {
if is_always || (is_success && is_error) {
quote! { #sylvia ::cw_std::ReplyOn::Always }
} else if is_success {
quote! { #sylvia ::cw_std::ReplyOn::Success }
} else if is_failure {
} else if is_error {
quote! { #sylvia ::cw_std::ReplyOn::Error }
} else {
// This should never happen.
// We parse only the `Success`, `Failure` and `Always` values which are covered above.
// We parse only the `Success`, `Error` and `Always` values which are covered above.
// Handling the `Never` value wouldn't make sense as we would create a dead handler.
quote! { #sylvia ::cw_std::ReplyOn::Never }
}
Expand Down Expand Up @@ -416,18 +416,18 @@ impl<'a> ReplyData<'a> {
}
}

/// Emits match arm for [ReplyOn::Failure].
/// In case neither [ReplyOn::Failure] nor [ReplyOn::Always] is present,
/// Emits match arm for [ReplyOn::Error].
/// In case neither [ReplyOn::Error] nor [ReplyOn::Always] is present,
/// the error is forwarded.
fn emit_failure_match_arm(&self, contract_turbofish: &Type) -> TokenStream {
fn emit_error_match_arm(&self, contract_turbofish: &Type) -> TokenStream {
let sylvia = crate_module();

match self
.handlers
.iter()
.find(|(_, reply_on)| reply_on == &ReplyOn::Failure || reply_on == &ReplyOn::Always)
.find(|(_, reply_on)| reply_on == &ReplyOn::Error || reply_on == &ReplyOn::Always)
{
Some((method_name, reply_on)) if reply_on == &ReplyOn::Failure => {
Some((method_name, reply_on)) if reply_on == &ReplyOn::Error => {
let payload_values = self.payload.iter().map(|field| field.name());
let payload_deserialization = self.payload.emit_payload_deserialization();

Expand Down
2 changes: 1 addition & 1 deletion sylvia-derive/src/contract/communication/wrapper_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl<'a> GlueMessage<'a> {
let msgs: [&[&str]; #variants_cnt] = [#(#messages_call),*];
let mut err_msg = msgs.into_iter().flatten().fold(
// It might be better to forward the error or serialization, but we just
// deserialized it from JSON, not reason to expect failure here.
// deserialized it from JSON, not reason to expect error here.
format!(
"Unsupported message received: {}. Messages supported by this contract: ",
#sylvia ::serde_json::to_string(&val).unwrap_or_else(|_| String::new())
Expand Down
8 changes: 4 additions & 4 deletions sylvia-derive/src/parser/attributes/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Parse for ArgumentParser {
#[derive(Copy, Debug, Default, Clone, PartialEq)]
pub enum ReplyOn {
Success,
Failure,
Error,
#[default]
Always,
}
Expand All @@ -80,7 +80,7 @@ impl std::fmt::Display for ReplyOn {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ReplyOn::Success => f.write_str("success"),
ReplyOn::Failure => f.write_str("failure"),
ReplyOn::Error => f.write_str("error"),

Check warning on line 83 in sylvia-derive/src/parser/attributes/msg.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/attributes/msg.rs#L83

Added line #L83 was not covered by tests
ReplyOn::Always => f.write_str("always"),
}
}
Expand All @@ -90,11 +90,11 @@ impl ReplyOn {
pub fn new(reply_on: Ident) -> Result<Self> {
match reply_on.to_string().as_str() {
"success" => Ok(Self::Success),
"failure" => Ok(Self::Failure),
"error" => Ok(Self::Error),
"always" => Ok(Self::Always),
_ => Err(Error::new(
reply_on.span(),
"Invalid argument type, expected one of `success`, `failure` or `always`.",
"Invalid argument type, expected one of `success`, `error` or `always`.",

Check warning on line 97 in sylvia-derive/src/parser/attributes/msg.rs

View check run for this annotation

Codecov / codecov/patch

sylvia-derive/src/parser/attributes/msg.rs#L97

Added line #L97 was not covered by tests
)),
}
}
Expand Down
27 changes: 13 additions & 14 deletions sylvia/tests/reply_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use cw_storage_plus::Item;
use cw_utils::{parse_instantiate_response_data, ParseReplyError};
use noop_contract::sv::{Executor, NoopContractInstantiateBuilder};
use sv::{
SubMsgMethods, ALWAYS_REPLY_ID, FAILURE_REPLY_ID, REMOTE_INSTANTIATED_REPLY_ID,
SUCCESS_REPLY_ID,
SubMsgMethods, ALWAYS_REPLY_ID, ERROR_REPLY_ID, REMOTE_INSTANTIATED_REPLY_ID, SUCCESS_REPLY_ID,
};
use sylvia::builder::instantiate::InstantiateBuilder;
use sylvia::cw_std::{Addr, Binary, Response, StdError, SubMsg};
Expand Down Expand Up @@ -133,7 +132,7 @@ where
}

#[sv::msg(exec)]
pub fn call_remote_failure(
pub fn call_remote_error(
&self,
ctx: ExecCtx<Q>,
should_fail: bool,
Expand All @@ -144,7 +143,7 @@ where
.executor()
.noop(should_fail)?
.build()
.failure(Binary::default())?;
.error(Binary::default())?;

Ok(Response::new().add_submessage(msg))
}
Expand Down Expand Up @@ -244,14 +243,14 @@ where
Ok(Response::new())
}

#[sv::msg(reply, handlers=[failure, both], reply_on=failure)]
fn failure(
#[sv::msg(reply, handlers=[error, both], reply_on=error)]
fn error(
&self,
ctx: ReplyCtx<Q>,
_error: String,
#[sv::payload] _payload: Binary,
) -> Result<Response<M>, ContractError> {
self.last_reply.save(ctx.deps.storage, &FAILURE_REPLY_ID)?;
self.last_reply.save(ctx.deps.storage, &ERROR_REPLY_ID)?;

Ok(Response::new())
}
Expand Down Expand Up @@ -286,7 +285,7 @@ mod tests {
use crate::noop_contract::sv::mt::CodeId as NoopCodeId;
use crate::sv::mt::{CodeId, ContractProxy};
use crate::sv::{
ALWAYS_REPLY_ID, FAILURE_REPLY_ID, REMOTE_INSTANTIATED_REPLY_ID, SUCCESS_REPLY_ID,
ALWAYS_REPLY_ID, ERROR_REPLY_ID, REMOTE_INSTANTIATED_REPLY_ID, SUCCESS_REPLY_ID,
};

use sylvia::cw_multi_test::IntoBech32;
Expand Down Expand Up @@ -321,15 +320,15 @@ mod tests {
let last_reply = contract.last_reply().unwrap();
assert_eq!(last_reply, SUCCESS_REPLY_ID);

// Should not dispatch if expected failure and execution succeeded
contract.call_remote_failure(false).call(&owner).unwrap();
// Should not dispatch if expected error and execution succeeded
contract.call_remote_error(false).call(&owner).unwrap();
let last_reply = contract.last_reply().unwrap();
assert_eq!(last_reply, SUCCESS_REPLY_ID);

// Should dispatch if expected failure and execution failed
contract.call_remote_failure(true).call(&owner).unwrap();
// Should dispatch if expected error and execution failed
contract.call_remote_error(true).call(&owner).unwrap();
let last_reply = contract.last_reply().unwrap();
assert_eq!(last_reply, FAILURE_REPLY_ID);
assert_eq!(last_reply, ERROR_REPLY_ID);

// Should dispatch if expected any result and execution succeeded
contract.call_remote_always(false).call(&owner).unwrap();
Expand All @@ -349,7 +348,7 @@ mod tests {
// Should dispatch if expected both results and execution failed
contract.call_remote_both(true).call(&owner).unwrap();
let last_reply = contract.last_reply().unwrap();
assert_eq!(last_reply, FAILURE_REPLY_ID);
assert_eq!(last_reply, ERROR_REPLY_ID);

// Should send the cosmos message
contract.send_cosmos_messages().call(&owner).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion sylvia/tests/reply_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Contract {
}

#[allow(dead_code)]
#[sv::msg(reply, handlers=[reply_on], reply_on = failure)]
#[sv::msg(reply, handlers=[reply_on], reply_on = error)]
fn both_parameters(
&self,
_ctx: ReplyCtx,
Expand Down
4 changes: 2 additions & 2 deletions sylvia/tests/ui/attributes/msg/overlapping_reply_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ impl Contract {
Ok(Response::new())
}

#[sv::msg(reply, handlers=[handler2], reply_on=failure)]
#[sv::msg(reply, handlers=[handler2], reply_on=error)]
fn some_reply(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult<Response> {
Ok(Response::new())
}

#[sv::msg(reply, reply_on=failure)]
#[sv::msg(reply, reply_on=error)]
fn handler2(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult<Response> {
Ok(Response::new())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ error: Duplicated reply handler.

error: Duplicated reply handler.

= note: Previous definition of handler=`HANDLER_2_REPLY_ID` for reply_on=`failure` defined on `fn some_reply()`
= note: Previous definition of handler=`HANDLER_2_REPLY_ID` for reply_on=`error` defined on `fn some_reply()`

--> tests/ui/attributes/msg/overlapping_reply_handlers.rs:41:8
|
Expand Down
4 changes: 2 additions & 2 deletions sylvia/tests/ui/method_signature/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub mod mismatched_params {
Ok(Response::new())
}

#[sv::msg(reply, handlers=[on_instantiated], reply_on=failure)]
#[sv::msg(reply, handlers=[on_instantiated], reply_on=error)]
fn second_reply(
&self,
_ctx: ReplyCtx,
Expand Down Expand Up @@ -68,7 +68,7 @@ pub mod mismatched_param_arity {
Ok(Response::new())
}

#[sv::msg(reply, handlers=[on_instantiated], reply_on=failure)]
#[sv::msg(reply, handlers=[on_instantiated], reply_on=error)]
fn second_reply(
&self,
_ctx: ReplyCtx,
Expand Down

0 comments on commit b6cb2ca

Please sign in to comment.