Skip to content

Commit

Permalink
Merge branch 'main' into custom_queries
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia authored Aug 23, 2023
2 parents 9e42a78 + 4357dea commit 5e17605
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 194 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.7.1](https://github.com/CosmWasm/sylvia/compare/sylvia-derive-v0.7.0...sylvia-derive-v0.7.1) - 2023-08-14

### Fixed
- Prefix interface proxy with module as Path

## [0.7.0](https://github.com/CosmWasm/sylvia/compare/sylvia-derive-v0.6.1...sylvia-derive-v0.7.0) - 2023-08-01

### Added
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exclude = ["examples/*"]
resolver = "2"

[workspace.package]
version = "0.7.0"
version = "0.7.1"

[workspace.dependencies]
sylvia-derive = { version = "0.7.0", path = "sylvia-derive" }
sylvia-derive = { version = "0.7.1", path = "sylvia-derive" }
4 changes: 2 additions & 2 deletions examples/Cargo.lock

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

18 changes: 15 additions & 3 deletions sylvia-derive/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct ImplInput<'a> {
generics: Vec<&'a GenericParam>,
custom: Custom<'a>,
override_entry_points: OverrideEntryPoints,
interfaces: Interfaces,
}

impl<'a> TraitInput<'a> {
Expand Down Expand Up @@ -60,7 +61,7 @@ impl<'a> TraitInput<'a> {
pub fn process(&self) -> TokenStream {
let messages = self.emit_messages();
let multitest_helpers = self.emit_helpers();
let remote = Remote::new(&[]).emit();
let remote = Remote::new(&Interfaces::default()).emit();
let querier = MsgVariants::new(self.item.as_variants(), &self.generics).emit_querier();

#[cfg(not(tarpaulin_include))]
Expand Down Expand Up @@ -128,6 +129,7 @@ impl<'a> ImplInput<'a> {

let custom = Custom::new(&item.attrs);
let override_entry_points = OverrideEntryPoints::new(&item.attrs);
let interfaces = Interfaces::new(item);

Self {
attributes,
Expand All @@ -136,6 +138,7 @@ impl<'a> ImplInput<'a> {
error,
custom,
override_entry_points,
interfaces,
}
}

Expand All @@ -149,6 +152,7 @@ impl<'a> ImplInput<'a> {
&self.generics,
&self.custom,
&self.override_entry_points,
&self.interfaces,
)
.emit()
} else {
Expand Down Expand Up @@ -187,7 +191,7 @@ impl<'a> ImplInput<'a> {
multitest_helpers: TokenStream,
) -> TokenStream {
let messages = self.emit_messages();
let remote = Remote::new(&self.item.attrs).emit();
let remote = Remote::new(interfaces).emit();
let querier = variants.emit_querier();
let querier_from_impl = interfaces.emit_querier_from_impl();

Expand Down Expand Up @@ -253,7 +257,15 @@ impl<'a> ImplInput<'a> {
}

fn emit_glue_msg(&self, name: &Ident, msg_ty: MsgType) -> TokenStream {
GlueMessage::new(name, self.item, msg_ty, &self.error, &self.custom).emit()
GlueMessage::new(
name,
self.item,
msg_ty,
&self.error,
&self.custom,
&self.interfaces,
)
.emit()
}

fn emit_querier_for_bound_impl(
Expand Down
117 changes: 110 additions & 7 deletions sylvia-derive/src/interfaces.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
use proc_macro2::TokenStream;
use convert_case::{Case, Casing};
use proc_macro2::{Ident, TokenStream};
use proc_macro_error::emit_error;
use quote::quote;
use syn::parse::{Parse, Parser};
use syn::spanned::Spanned;
use syn::ItemImpl;
use syn::{ItemImpl, Path, Type};

use crate::crate_module;
use crate::parser::ContractMessageAttr;
use crate::parser::{ContractMessageAttr, MsgType};

#[derive(Debug, Default)]
pub struct Interfaces {
interfaces: Vec<ContractMessageAttr>,
}

impl Interfaces {
fn merge_module_with_name(message_attr: &ContractMessageAttr, name: &syn::Ident) -> syn::Ident {
// ContractMessageAttr will fail to parse empty `#[messsages()]` attribute so we can safely unwrap here
let syn::PathSegment { ident, .. } = &message_attr.module.segments.last().unwrap();
let module_name = ident.to_string().to_case(Case::UpperCamel);
syn::Ident::new(&format!("{}{}", module_name, name), name.span())
}

pub fn new(source: &ItemImpl) -> Self {
let interfaces: Vec<_> = source
.attrs
Expand Down Expand Up @@ -41,10 +50,8 @@ impl Interfaces {
pub fn emit_querier_from_impl(&self) -> Vec<TokenStream> {
let sylvia = crate_module();

self.interfaces
.iter()
.map(|interface| {
let ContractMessageAttr { module, .. } = interface;
self.as_modules()
.map(|module| {
quote! {
impl<'a, C: #sylvia ::cw_std::CustomQuery> From<&'a BoundQuerier<'a, C>> for #module ::BoundQuerier<'a, C> {
fn from(querier: &'a BoundQuerier<'a, C>) -> Self {
Expand All @@ -55,4 +62,100 @@ impl Interfaces {
})
.collect()
}

pub fn emit_proxy_accessors(&self, mt_app: &Type) -> Vec<TokenStream> {
self.as_modules()
.map(|module| {
// ContractMessageAttr will fail to parse empty `#[messsages()]` attribute so we can safely unwrap here
let module_name = &module.segments.last().unwrap().ident;
let method_name = Ident::new(&format!("{}_proxy", module_name), module_name.span());
let proxy_name = Ident::new(
&format!("{}Proxy", module_name.to_string().to_case(Case::UpperCamel)),
module_name.span(),
);

quote! {
pub fn #method_name (&self) -> #module ::trait_utils:: #proxy_name <'app, #mt_app> {
#module ::trait_utils:: #proxy_name ::new(self.contract_addr.clone(), self.app)
}
}
})
.collect()
}

pub fn emit_glue_message_variants(
&self,
msg_ty: &MsgType,
msg_name: &Ident,
) -> Vec<TokenStream> {
self.interfaces
.iter()
.map(|interface| {
let ContractMessageAttr {
module,
exec_generic_params,
query_generic_params,
variant,
..
} = interface;

let generics = match msg_ty {
MsgType::Exec => exec_generic_params.as_slice(),
MsgType::Query => query_generic_params.as_slice(),
_ => &[],
};

let enum_name = Self::merge_module_with_name(interface, msg_name);
quote! { #variant(#module :: #enum_name<#(#generics,)*>) }
})
.collect()
}

pub fn emit_messages_call(&self, msg_name: &Ident) -> Vec<TokenStream> {
self.interfaces
.iter()
.map(|interface| {
let enum_name = Self::merge_module_with_name(interface, msg_name);
let module = &interface.module;
quote! { &#module :: #enum_name :: messages()}
})
.collect()
}

pub fn emit_deserialization_attempts(&self, msg_name: &Ident) -> Vec<TokenStream> {
self.interfaces
.iter()
.map(|interface| {
let ContractMessageAttr {
module, variant, ..
} = interface;
let enum_name = Self::merge_module_with_name(interface, msg_name);

quote! {
let msgs = &#module :: #enum_name ::messages();
if msgs.into_iter().any(|msg| msg == &recv_msg_name) {
match val.deserialize_into() {
Ok(msg) => return Ok(Self:: #variant (msg)),
Err(err) => return Err(D::Error::custom(err)).map(Self:: #variant),
};
}
}
})
.collect()
}

pub fn emit_response_schemas_calls(&self, msg_name: &Ident) -> Vec<TokenStream> {
self.interfaces
.iter()
.map(|interface| {
let enum_name = Self::merge_module_with_name(interface, msg_name);
let module = &interface.module;
quote! { #module :: #enum_name :: response_schemas_impl()}
})
.collect()
}

pub fn as_modules(&self) -> impl Iterator<Item = &Path> {
self.interfaces.iter().map(|interface| &interface.module)
}
}
Loading

0 comments on commit 5e17605

Please sign in to comment.