Skip to content

Commit

Permalink
feat: Handle missing explicite custom types (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
kulikthebird authored Mar 11, 2024
1 parent 5970be8 commit 606a812
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 18 deletions.
1 change: 1 addition & 0 deletions examples/contracts/cw20-base/src/multitest/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use sylvia::types::ExecCtx;
use sylvia::{interface, schemars};

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait Receiver {
type Error: From<StdError>;

Expand Down
1 change: 1 addition & 0 deletions examples/interfaces/cw1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct CanExecuteResp {
}

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait Cw1 {
type Error: From<StdError>;

Expand Down
1 change: 1 addition & 0 deletions examples/interfaces/cw20-allowances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use sylvia::types::{ExecCtx, QueryCtx};
use sylvia::{interface, schemars};

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait Cw20Allowances {
type Error: From<StdError>;

Expand Down
1 change: 1 addition & 0 deletions examples/interfaces/cw20-marketing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum EmbeddedLogo {
}

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait Cw20Marketing {
type Error: From<StdError>;

Expand Down
1 change: 1 addition & 0 deletions examples/interfaces/cw20-minting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use sylvia::types::{ExecCtx, QueryCtx};
use sylvia::{interface, schemars};

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait Cw20Minting {
type Error: From<StdError>;

Expand Down
1 change: 1 addition & 0 deletions examples/interfaces/cw4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use sylvia::types::{ExecCtx, QueryCtx};
use sylvia::{interface, schemars};

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait Cw4 {
type Error: From<StdError>;

Expand Down
1 change: 1 addition & 0 deletions examples/interfaces/generic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use sylvia::types::{CustomMsg, ExecCtx, QueryCtx, SudoCtx};
use sylvia::{interface, schemars};

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait Generic {
type Error: From<StdError>;
type Exec1T: CustomMsg;
Expand Down
1 change: 1 addition & 0 deletions examples/interfaces/whitelist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use sylvia::{interface, schemars};
pub mod responses;

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait Whitelist {
type Error: From<cosmwasm_std::StdError>;

Expand Down
28 changes: 26 additions & 2 deletions sylvia-derive/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use proc_macro2::TokenStream;
use proc_macro_error::emit_error;
use proc_macro_error::{emit_error, emit_warning};
use quote::quote;
use syn::{GenericParam, Ident, ItemImpl, ItemTrait, TraitItem};

use crate::associated_types::{AssociatedTypes, ItemType};
use crate::associated_types::{AssociatedTypes, ItemType, EXEC_TYPE, QUERY_TYPE};
use crate::interfaces::Interfaces;
use crate::message::{
ContractApi, ContractEnumMessage, EnumMessage, GlueMessage, InterfaceApi, MsgVariants,
Expand Down Expand Up @@ -48,6 +48,30 @@ impl<'a> TraitInput<'a> {
.unwrap_or_default();
let associated_types = AssociatedTypes::new(item);

if custom.msg.is_none()
&& !associated_types
.all_names()
.any(|assoc_type| assoc_type == EXEC_TYPE)
{
emit_warning!(
item.ident.span(), "Missing both `{}` type and `#[sv::custom(msg=...)]` defined for the trait.", EXEC_TYPE;
note = "Implicitly it means that the trait could not be implemented for contracts that use CustomMsg different than `cosmwasm_std::Empty`";
note = "If this behaviour is intended, please add `#[sv::custom(msg=sylvia::cw_std::Empty]` attribute.";
);
}

if custom.query.is_none()
&& !associated_types
.all_names()
.any(|assoc_type| assoc_type == QUERY_TYPE)
{
emit_warning!(
item.ident.span(), "Missing both `{}` type and `#[sv::custom(query=...)]` defined for the trait.", QUERY_TYPE;
note = "Implicitly it means that the trait could not be implemented for contracts that use CustomQuery different than `cosmwasm_std::Empty`";
note = "If this behaviour is intended, please add `#[sv::custom(query=sylvia::cw_std::Empty]` attribute.";
);
}

Self {
item,
custom,
Expand Down
6 changes: 4 additions & 2 deletions sylvia-derive/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,14 @@ impl<'a> EnumMessage<'a> {
let associated_query = parse_associated_custom_type(source, QUERY_TYPE);

let resp_type = custom
.msg()
.msg
.clone()
.or(associated_exec)
.unwrap_or_else(Custom::default_type);

let query_type = custom
.query()
.query
.clone()
.or(associated_query)
.unwrap_or_else(Custom::default_type);

Expand Down
12 changes: 2 additions & 10 deletions sylvia-derive/src/parser/attributes/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::crate_module;

#[derive(Debug, Default)]
pub struct Custom {
msg: Option<Type>,
query: Option<Type>,
pub msg: Option<Type>,
pub query: Option<Type>,
}

impl Custom {
Expand All @@ -30,14 +30,6 @@ impl Custom {
self.query.clone().unwrap_or_else(Self::default_type)
}

pub fn msg(&self) -> Option<Type> {
self.msg.clone()
}

pub fn query(&self) -> Option<Type> {
self.query.clone()
}

pub fn default_type() -> Type {
let sylvia = crate_module();
parse_quote! { #sylvia ::cw_std::Empty }
Expand Down
1 change: 1 addition & 0 deletions sylvia/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mod group {
use crate::{Member, MemberResp};

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait Group {
type Error: From<StdError>;

Expand Down
6 changes: 4 additions & 2 deletions sylvia/tests/custom_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod some_interface {
use crate::{MyMsg, SomeResponse};

#[interface]
#[sv::custom(msg=MyMsg)]
#[sv::custom(msg=MyMsg, query=cosmwasm_std::Empty)]
pub trait SomeInterface {
type Error: From<StdError>;

Expand Down Expand Up @@ -74,7 +74,7 @@ mod interface {
use sylvia::types::{ExecCtx, QueryCtx, SudoCtx};

#[interface]
#[sv::custom(msg=MyMsg)]
#[sv::custom(msg=MyMsg, query=cosmwasm_std::Empty)]
pub trait Interface {
type Error: From<StdError>;
type ExecC: CustomMsg;
Expand Down Expand Up @@ -119,6 +119,7 @@ mod other_interface {
use sylvia::types::{ExecCtx, QueryCtx, SudoCtx};

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait OtherInterface {
type Error: From<StdError>;

Expand Down Expand Up @@ -161,6 +162,7 @@ mod associated_interface {
use sylvia::types::{ExecCtx, QueryCtx, SudoCtx};

#[interface]
#[sv::custom(query=cosmwasm_std::Empty)]
pub trait AssociatedInterface {
type Error: From<StdError>;
type ExecC: CustomMsg;
Expand Down
6 changes: 4 additions & 2 deletions sylvia/tests/custom_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod interface {
use crate::{MyQuery, SomeResponse};

#[interface]
#[sv::custom(query=MyQuery)]
#[sv::custom(query=MyQuery, msg=cosmwasm_std::Empty)]
pub trait Interface {
type Error: From<StdError>;
type QueryC: CustomQuery;
Expand Down Expand Up @@ -74,7 +74,7 @@ mod some_interface {
use crate::{MyQuery, SomeResponse};

#[interface]
#[sv::custom(query=MyQuery)]
#[sv::custom(query=MyQuery, msg=cosmwasm_std::Empty)]
pub trait SomeInterface {
type Error: From<StdError>;

Expand Down Expand Up @@ -120,6 +120,7 @@ mod associated_type_interface {
use crate::SomeResponse;

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty)]
pub trait AssociatedTypeInterface {
type Error: From<StdError>;
type QueryC: CustomQuery;
Expand Down Expand Up @@ -167,6 +168,7 @@ mod default_query_interface {
use crate::SomeResponse;

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait DefaultQueryInterface {
type Error: From<StdError>;

Expand Down
1 change: 1 addition & 0 deletions sylvia/tests/dispatching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod interface {
use crate::{EmptyQueryResponse, QueryResponse};

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait Interface {
type Error: From<StdError>;

Expand Down
1 change: 1 addition & 0 deletions sylvia/tests/messages_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod interface {
use crate::QueryResult;

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait Interface {
type Error: From<StdError>;

Expand Down
1 change: 1 addition & 0 deletions sylvia/tests/querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod counter {
use crate::CountResponse;

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait Counter {
type Error: From<StdError>;

Expand Down
1 change: 1 addition & 0 deletions sylvia/tests/query_returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod msg {
use crate::{QueryResponse, QueryResult};

#[interface(module=msg)]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait Interface {
type Error: From<StdError>;

Expand Down
1 change: 1 addition & 0 deletions sylvia/tests/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod some_interface {
use sylvia::interface;

#[interface]
#[sv::custom(msg=cosmwasm_std::Empty, query=cosmwasm_std::Empty)]
pub trait SomeInterface {
type Error: From<StdError>;
}
Expand Down

0 comments on commit 606a812

Please sign in to comment.