Skip to content

Commit

Permalink
feat: Interfaces generate with associated_types
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Jan 31, 2024
1 parent 4b65245 commit 30a6a0e
Show file tree
Hide file tree
Showing 14 changed files with 583 additions and 400 deletions.
51 changes: 0 additions & 51 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ members = [
"contracts/cw20-base",
"contracts/entry-points-overriding",
"contracts/custom",
"contracts/generic_contract",
"contracts/generics_forwarded",
"contracts/generic_iface_on_contract",
# "contracts/generic_contract",
# "contracts/generics_forwarded",
# "contracts/generic_iface_on_contract",
]
resolver = "2"

Expand Down
104 changes: 48 additions & 56 deletions examples/interfaces/custom-and-generic/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,66 +1,53 @@
use cosmwasm_std::{CosmosMsg, CustomMsg, Response, StdError};
use cosmwasm_std::{CosmosMsg, Response, StdError};

use serde::de::DeserializeOwned;
use serde::Deserialize;
use sylvia::types::{ExecCtx, QueryCtx};
use sylvia::types::{CustomMsg, CustomQuery, ExecCtx, QueryCtx};
use sylvia::{interface, schemars};

#[interface]
#[sv::custom(msg=CustomMsgT, query=CustomQueryT)]
pub trait CustomAndGeneric<
Exec1T,
Exec2T,
Exec3T,
Query1T,
Query2T,
Query3T,
RetT,
CustomMsgT,
CustomQueryT,
> where
for<'msg_de> Exec1T: CustomMsg + Deserialize<'msg_de>,
Exec2T: sylvia::types::CustomMsg,
Exec3T: sylvia::types::CustomMsg,
Query1T: sylvia::types::CustomMsg,
Query2T: sylvia::types::CustomMsg,
Query3T: sylvia::types::CustomMsg,
RetT: CustomMsg + DeserializeOwned,
CustomMsgT: CustomMsg + DeserializeOwned,
CustomQueryT: sylvia::types::CustomQuery + 'static,
{
pub trait CustomAndGeneric {
type Error: From<StdError>;
type Exec1T: CustomMsg;
type Exec2T: CustomMsg;
type Exec3T: CustomMsg;
type Query1T: CustomMsg;
type Query2T: CustomMsg;
type Query3T: CustomMsg;
type RetT: CustomMsg;
type CustomMsgT: CustomMsg;
type CustomQueryT: CustomQuery + 'static;

#[msg(exec)]
fn custom_generic_execute_one(
&self,
ctx: ExecCtx<CustomQueryT>,
msgs1: Vec<CosmosMsg<Exec1T>>,
msgs2: Vec<CosmosMsg<Exec2T>>,
) -> Result<Response<CustomMsgT>, Self::Error>;
ctx: ExecCtx<Self::CustomQueryT>,
msgs1: Vec<CosmosMsg<Self::Exec1T>>,
msgs2: Vec<CosmosMsg<Self::Exec2T>>,
) -> Result<Response<Self::CustomMsgT>, Self::Error>;

#[msg(exec)]
fn custom_generic_execute_two(
&self,
ctx: ExecCtx<CustomQueryT>,
msgs1: Vec<CosmosMsg<Exec2T>>,
msgs2: Vec<CosmosMsg<Exec3T>>,
) -> Result<Response<CustomMsgT>, Self::Error>;
ctx: ExecCtx<Self::CustomQueryT>,
msgs1: Vec<CosmosMsg<Self::Exec2T>>,
msgs2: Vec<CosmosMsg<Self::Exec3T>>,
) -> Result<Response<Self::CustomMsgT>, Self::Error>;

#[msg(query)]
fn custom_generic_query_one(
&self,
ctx: QueryCtx<CustomQueryT>,
param1: Query1T,
param2: Query2T,
) -> Result<RetT, Self::Error>;
ctx: QueryCtx<Self::CustomQueryT>,
param1: Self::Query1T,
param2: Self::Query2T,
) -> Result<Self::RetT, Self::Error>;

#[msg(query)]
fn custom_generic_query_two(
&self,
ctx: QueryCtx<CustomQueryT>,
param1: Query2T,
param2: Query3T,
) -> Result<RetT, Self::Error>;
ctx: QueryCtx<Self::CustomQueryT>,
param1: Self::Query2T,
param2: Self::Query3T,
) -> Result<Self::RetT, Self::Error>;
}

#[cfg(test)]
Expand All @@ -69,6 +56,8 @@ mod tests {
use cosmwasm_std::{Addr, CosmosMsg, Empty, QuerierWrapper};
use sylvia::types::{InterfaceApi, SvCustomMsg, SvCustomQuery};

use crate::sv::Querier;

#[test]
fn construct_messages() {
let contract = Addr::unchecked("contract");
Expand Down Expand Up @@ -96,24 +85,27 @@ mod tests {
let deps = mock_dependencies();
let querier_wrapper: QuerierWrapper = QuerierWrapper::new(&deps.querier);

let querier = super::sv::BoundQuerier::borrowed(&contract, &querier_wrapper);
let querier = super::sv::BoundQuerier::<
_,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomQuery,
>::borrowed(&contract, &querier_wrapper);

let _: Result<SvCustomMsg, _> =
super::sv::Querier::<_, _, _, SvCustomMsg>::custom_generic_query_one(
&querier,
SvCustomMsg {},
SvCustomMsg {},
);
// let _: Result<SvCustomMsg, _> =
// querier.custom_generic_query_one(SvCustomMsg {}, SvCustomMsg {});
super::sv::Querier::custom_generic_query_one(&querier, SvCustomMsg {}, SvCustomMsg {});
let _: Result<SvCustomMsg, _> =
querier.custom_generic_query_one(SvCustomMsg {}, SvCustomMsg {});
let _: Result<SvCustomMsg, _> =
super::sv::Querier::custom_generic_query_two(&querier, SvCustomMsg {}, SvCustomMsg {});
let _: Result<SvCustomMsg, _> =
super::sv::Querier::<SvCustomMsg, _, _, _>::custom_generic_query_two(
&querier,
SvCustomMsg {},
SvCustomMsg {},
);
// let _: Result<SvCustomMsg, _> =
// querier.custom_generic_query_two(SvCustomMsg {}, SvCustomMsg {});
querier.custom_generic_query_two(SvCustomMsg {}, SvCustomMsg {});

// Construct messages with Interface extension
let _ = <super::sv::Api<
Expand Down
68 changes: 34 additions & 34 deletions examples/interfaces/generic/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,59 @@
use cosmwasm_std::{CosmosMsg, Response, StdError};

use serde::Deserialize;
use sylvia::types::{CustomMsg, ExecCtx, QueryCtx};
use sylvia::{interface, schemars};

#[interface]
pub trait Generic<Exec1T, Exec2T, Exec3T, Query1T, Query2T, Query3T, RetT>
where
for<'msg_de> Exec1T: CustomMsg + Deserialize<'msg_de>,
Exec2T: CustomMsg,
Exec3T: CustomMsg,
Query1T: CustomMsg,
Query2T: CustomMsg,
Query3T: CustomMsg,
RetT: CustomMsg,
{
pub trait Generic {
type Error: From<StdError>;
type Exec1T: CustomMsg;
type Exec2T: CustomMsg;
type Exec3T: CustomMsg;
type Query1T: CustomMsg;
type Query2T: CustomMsg;
type Query3T: CustomMsg;
type RetT: CustomMsg;

#[msg(exec)]
fn generic_exec_one(
&self,
ctx: ExecCtx,
msgs1: Vec<CosmosMsg<Exec1T>>,
msgs2: Vec<CosmosMsg<Exec2T>>,
msgs1: Vec<CosmosMsg<Self::Exec1T>>,
msgs2: Vec<CosmosMsg<Self::Exec2T>>,
) -> Result<Response, Self::Error>;

#[msg(exec)]
fn generic_exec_two(
&self,
ctx: ExecCtx,
msgs1: Vec<CosmosMsg<Exec2T>>,
msgs2: Vec<CosmosMsg<Exec3T>>,
msgs1: Vec<CosmosMsg<Self::Exec2T>>,
msgs2: Vec<CosmosMsg<Self::Exec3T>>,
) -> Result<Response, Self::Error>;

#[msg(query)]
fn generic_query_one(
&self,
ctx: QueryCtx,
param1: Query1T,
param2: Query2T,
) -> Result<RetT, Self::Error>;
param1: Self::Query1T,
param2: Self::Query2T,
) -> Result<Self::RetT, Self::Error>;

#[msg(query)]
fn generic_query_two(
&self,
ctx: QueryCtx,
param1: Query2T,
param2: Query3T,
) -> Result<RetT, Self::Error>;
param1: Self::Query2T,
param2: Self::Query3T,
) -> Result<Self::RetT, Self::Error>;
}

#[cfg(test)]
mod tests {
use cosmwasm_std::{testing::mock_dependencies, Addr, CosmosMsg, Empty, QuerierWrapper};
use sylvia::types::{InterfaceApi, SvCustomMsg};

use crate::sv::Querier;

#[test]
fn construct_messages() {
let contract = Addr::unchecked("contract");
Expand All @@ -81,21 +80,22 @@ mod tests {
let deps = mock_dependencies();
let querier_wrapper: QuerierWrapper = QuerierWrapper::new(&deps.querier);

let querier = super::sv::BoundQuerier::borrowed(&contract, &querier_wrapper);
let querier = super::sv::BoundQuerier::<
Empty,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
>::borrowed(&contract, &querier_wrapper);
let _: Result<SvCustomMsg, _> =
super::sv::Querier::<_, _, _, SvCustomMsg>::generic_query_one(
&querier,
SvCustomMsg {},
SvCustomMsg {},
);
super::sv::Querier::generic_query_one(&querier, SvCustomMsg {}, SvCustomMsg {});
let _: Result<SvCustomMsg, _> =
super::sv::Querier::<SvCustomMsg, _, _, _>::generic_query_two(
&querier,
SvCustomMsg {},
SvCustomMsg {},
);
// let _: Result<SvCustomMsg, _> = querier.generic_query_one(SvCustomMsg {}, SvCustomMsg {});
// let _: Result<SvCustomMsg, _> = querier.generic_query_two(SvCustomMsg {}, SvCustomMsg {});
super::sv::Querier::generic_query_two(&querier, SvCustomMsg {}, SvCustomMsg {});
let _: Result<SvCustomMsg, _> = querier.generic_query_one(SvCustomMsg {}, SvCustomMsg {});
let _: Result<SvCustomMsg, _> = querier.generic_query_two(SvCustomMsg {}, SvCustomMsg {});

// Construct messages with Interface extension
let _ = <super::sv::Api<
Expand Down
Loading

0 comments on commit 30a6a0e

Please sign in to comment.