Skip to content

Commit

Permalink
chore: Update after review. Simplify Executor Builder type
Browse files Browse the repository at this point in the history
  • Loading branch information
kulikthebird committed Jun 24, 2024
1 parent f086c11 commit a26a68e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 112 deletions.
57 changes: 28 additions & 29 deletions sylvia-derive/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ use crate::message::{MsgField, MsgVariant, MsgVariants};
use crate::parser::attributes::msg::MsgType;
use crate::utils::{emit_bracketed_generics, SvCasing};

pub struct ExecutorMethods<'a, Generic> {
executor_variants: &'a MsgVariants<'a, Generic>,
pub struct InterfaceExecutor<'a, Generic> {
variants: &'a MsgVariants<'a, Generic>,
associated_types: &'a AssociatedTypes<'a>,
interface_name: &'a Ident,
}

impl<'a, Generic> ExecutorMethods<'a, Generic>
impl<'a, Generic> InterfaceExecutor<'a, Generic>
where
Generic: GetPath + PartialEq + ToTokens,
{
pub fn new(
executor_variants: &'a MsgVariants<'a, Generic>,
variants: &'a MsgVariants<'a, Generic>,
associated_types: &'a AssociatedTypes,
interface_name: &'a Ident,
) -> Self {
Self {
executor_variants,
variants,
associated_types,
interface_name,
}
Expand All @@ -35,7 +35,7 @@ where
pub fn emit_executor_trait(&self) -> TokenStream {
let sylvia = crate_module();
let Self {
executor_variants,
variants,
associated_types,
interface_name,
} = self;
Expand All @@ -55,32 +55,34 @@ where
let executor_api_path =
quote! { < Api #bracketed_generics as #sylvia ::types::InterfaceApi>:: #accessor };

let executor_methods_trait_impl = executor_variants
let methods_trait_impl = variants
.variants()
.map(|variant| variant.emit_executor_impl(&executor_api_path))
.collect::<Vec<_>>();

let executor_methods_declaration = executor_variants
let executor_methods_declaration = variants
.variants()
.map(|variant| variant.emit_executor_method_declaration());

let types_declaration = associated_types.filtered().collect::<Vec<_>>();
let where_clause = associated_types.as_where_clause();

quote! {
pub trait Executor<'sv_executor_lifetime> {
pub trait Executor {
#(#types_declaration)*
#(#executor_methods_declaration)*
}

impl <'sv_executor_lifetime, #(#all_generics,)*> Executor<'sv_executor_lifetime> for #sylvia ::types::ExecutorEmptyBuilder<'sv_executor_lifetime, dyn #interface_name <#( #all_generics = #all_generics,)* > > #where_clause {
impl <#(#all_generics,)*> Executor
for #sylvia ::types::ExecutorBuilder<(#sylvia ::types::EmptyExecutorBuilderState, dyn #interface_name <#( #all_generics = #all_generics,)* > ) > #where_clause {
#(type #generics = #generics;)*
#(#executor_methods_trait_impl)*
#(#methods_trait_impl)*
}

impl <'sv_executor_lifetime, Contract: #interface_name> Executor<'sv_executor_lifetime> for #sylvia ::types::ExecutorEmptyBuilder<'sv_executor_lifetime, Contract> {
impl <Contract: #interface_name> Executor
for #sylvia ::types::ExecutorBuilder<( #sylvia ::types::EmptyExecutorBuilderState, Contract )> {
#(type #generics = <Contract as #interface_name > :: #generics;)*
#(#executor_methods_trait_impl)*
#(#methods_trait_impl)*
}
}
}
Expand All @@ -89,19 +91,15 @@ where
pub struct ContractExecutor<'a> {
generics: Generics,
self_ty: Type,
executor_variants: MsgVariants<'a, GenericParam>,
variants: MsgVariants<'a, GenericParam>,
}

impl<'a> ContractExecutor<'a> {
pub fn new(
generics: Generics,
self_ty: Type,
executor_variants: MsgVariants<'a, GenericParam>,
) -> Self {
pub fn new(generics: Generics, self_ty: Type, variants: MsgVariants<'a, GenericParam>) -> Self {
Self {
generics,
self_ty,
executor_variants,
variants,
}
}

Expand All @@ -110,7 +108,7 @@ impl<'a> ContractExecutor<'a> {
let Self {
generics,
self_ty,
executor_variants,
variants,
..
} = self;

Expand All @@ -121,11 +119,11 @@ impl<'a> ContractExecutor<'a> {
let accessor = MsgType::Exec.as_accessor_name();
let executor_api_path = quote! { < #contract as #sylvia ::types::ContractApi>:: #accessor };

let executor_methods_impl = executor_variants
let executor_methods_impl = variants
.variants()
.map(|variant| variant.emit_executor_impl(&executor_api_path));

let executor_methods_declaration = executor_variants
let executor_methods_declaration = variants
.variants()
.map(|variant| variant.emit_executor_method_declaration());

Expand All @@ -140,12 +138,13 @@ impl<'a> ContractExecutor<'a> {
.unwrap_or(vec![]);

quote! {
pub trait Executor<'sv_executor_lifetime, #(#generics,)*> #where_clause {
pub trait Executor<#(#generics,)*> #where_clause {
#(#types_declaration)*
#(#executor_methods_declaration)*
}

impl <'sv_executor_lifetime, #(#generics,)*> Executor<'sv_executor_lifetime, #(#generics,)*> for #sylvia ::types::ExecutorEmptyBuilder<'sv_executor_lifetime, #contract > #where_clause {
impl <#(#generics,)*> Executor<#(#generics,)*>
for #sylvia ::types::ExecutorBuilder<( #sylvia ::types::EmptyExecutorBuilderState, #contract )> #where_clause {
#(#types_implementation)*
#(#executor_methods_impl)*
}
Expand All @@ -169,9 +168,9 @@ impl EmitExecutorMethod for MsgVariant<'_> {
let variant_name = name.to_case(Case::Snake);

quote! {
fn #variant_name(self, #(#parameters),*) -> Result<#sylvia ::types::ExecutorBuilder<'sv_executor_lifetime>, #sylvia ::cw_std::StdError> {
Ok(#sylvia ::types::ExecutorBuilder::<'sv_executor_lifetime>::new(
self.contract(),
fn #variant_name(self, #(#parameters),*) -> Result<#sylvia ::types::ExecutorBuilder< #sylvia ::types::ReadyExecutorBuilderState >, #sylvia ::cw_std::StdError> {
Ok(#sylvia ::types::ExecutorBuilder::<#sylvia ::types::ReadyExecutorBuilderState>::new(
self.contract().to_owned(),
self.funds().to_owned(),
#sylvia ::cw_std::to_json_binary( & #api_path :: #variant_name (#(#fields_names),*) )?,
))
Expand All @@ -190,7 +189,7 @@ impl EmitExecutorMethod for MsgVariant<'_> {
let variant_name = name.to_case(Case::Snake);

quote! {
fn #variant_name(self, #(#parameters),*) -> Result< #sylvia ::types::ExecutorBuilder<'sv_executor_lifetime>, #sylvia ::cw_std::StdError>;
fn #variant_name(self, #(#parameters),*) -> Result< #sylvia ::types::ExecutorBuilder<#sylvia ::types::ReadyExecutorBuilderState>, #sylvia ::cw_std::StdError>;
}
}
}
8 changes: 4 additions & 4 deletions sylvia-derive/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use quote::quote;
use syn::{GenericParam, Ident, ItemImpl, ItemTrait, TraitItem};

use crate::associated_types::{AssociatedTypes, ItemType, EXEC_TYPE, QUERY_TYPE};
use crate::executor::{ContractExecutor, ExecutorMethods};
use crate::executor::{ContractExecutor, InterfaceExecutor};
use crate::interfaces::Interfaces;
use crate::message::{
ContractApi, ContractEnumMessage, EnumMessage, GlueMessage, InterfaceApi, MsgVariants,
Expand All @@ -16,7 +16,7 @@ use crate::parser::{
assert_new_method_defined, ContractErrorAttr, Custom, OverrideEntryPoint,
ParsedSylviaAttributes,
};
use crate::querier::{ContractQuerier, QuerierMethods};
use crate::querier::{ContractQuerier, InterfaceQuerier};
use crate::variant_descs::AsVariantDescs;

/// Preprocessed `interface` macro input
Expand Down Expand Up @@ -96,9 +96,9 @@ impl<'a> TraitInput<'a> {
MsgVariants::new(item.as_variants(), MsgType::Exec, &associated_names, &None);
let query_variants =
MsgVariants::new(item.as_variants(), MsgType::Query, &associated_names, &None);
let executor = ExecutorMethods::new(&executor_variants, associated_types, &item.ident)
let executor = InterfaceExecutor::new(&executor_variants, associated_types, &item.ident)
.emit_executor_trait();
let querier = QuerierMethods::new(&query_variants, associated_types, &item.ident)
let querier = InterfaceQuerier::new(&query_variants, associated_types, &item.ident)
.emit_querier_trait();

let interface_messages = InterfaceApi::new(item, associated_types, custom).emit();
Expand Down
44 changes: 20 additions & 24 deletions sylvia-derive/src/querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ use crate::message::{MsgField, MsgVariant, MsgVariants};
use crate::parser::attributes::msg::MsgType;
use crate::utils::{emit_bracketed_generics, SvCasing};

pub struct QuerierMethods<'a, Generic> {
querier_variants: &'a MsgVariants<'a, Generic>,
pub struct InterfaceQuerier<'a, Generic> {
variants: &'a MsgVariants<'a, Generic>,
associated_types: &'a AssociatedTypes<'a>,
interface_name: &'a Ident,
}

impl<'a, Generic> QuerierMethods<'a, Generic>
impl<'a, Generic> InterfaceQuerier<'a, Generic>
where
Generic: GetPath + PartialEq + ToTokens,
{
pub fn new(
querier_variants: &'a MsgVariants<'a, Generic>,
variants: &'a MsgVariants<'a, Generic>,
associated_types: &'a AssociatedTypes,
interface_name: &'a Ident,
) -> Self {
Self {
querier_variants,
variants,
associated_types,
interface_name,
}
Expand All @@ -35,7 +35,7 @@ where
pub fn emit_querier_trait(&self) -> TokenStream {
let sylvia = crate_module();
let Self {
querier_variants,
variants,
associated_types,
interface_name,
} = self;
Expand All @@ -51,15 +51,15 @@ where
.collect();
let bracketed_generics = emit_bracketed_generics(&assoc_types);
let accessor = MsgType::Query.as_accessor_name();
let querier_api_path =
let api_path =
quote! { < Api #bracketed_generics as #sylvia ::types::InterfaceApi>:: #accessor };

let querier_methods_trait_impl = querier_variants
let methods_trait_impl = variants
.variants()
.map(|variant| variant.emit_querier_impl(&querier_api_path))
.map(|variant| variant.emit_querier_impl(&api_path))
.collect::<Vec<_>>();

let querier_methods_declaration = querier_variants
let querier_methods_declaration = variants
.variants()
.map(|variant| variant.emit_querier_method_declaration());

Expand All @@ -74,12 +74,12 @@ where

impl <'a, C: #sylvia ::cw_std::CustomQuery, #(#all_generics,)*> Querier for #sylvia ::types::BoundQuerier<'a, C, dyn #interface_name <#( #all_generics = #all_generics,)*> > #where_clause {
#(type #generics = #generics;)*
#(#querier_methods_trait_impl)*
#(#methods_trait_impl)*
}

impl <'a, C: #sylvia ::cw_std::CustomQuery, Contract: #interface_name> Querier for #sylvia ::types::BoundQuerier<'a, C, Contract> {
#(type #generics = <Contract as #interface_name > :: #generics;)*
#(#querier_methods_trait_impl)*
#(#methods_trait_impl)*
}
}
}
Expand All @@ -88,19 +88,15 @@ where
pub struct ContractQuerier<'a> {
generics: Generics,
self_ty: Type,
querier_variants: MsgVariants<'a, GenericParam>,
variants: MsgVariants<'a, GenericParam>,
}

impl<'a> ContractQuerier<'a> {
pub fn new(
generics: Generics,
self_ty: Type,
querier_variants: MsgVariants<'a, GenericParam>,
) -> Self {
pub fn new(generics: Generics, self_ty: Type, variants: MsgVariants<'a, GenericParam>) -> Self {
Self {
generics,
self_ty,
querier_variants,
variants,
}
}

Expand All @@ -109,7 +105,7 @@ impl<'a> ContractQuerier<'a> {
let Self {
generics,
self_ty,
querier_variants,
variants,
..
} = self;

Expand All @@ -118,13 +114,13 @@ impl<'a> ContractQuerier<'a> {
let contract = &self_ty;

let accessor = MsgType::Query.as_accessor_name();
let querier_api_path = quote! { < #contract as #sylvia ::types::ContractApi>:: #accessor };
let api_path = quote! { < #contract as #sylvia ::types::ContractApi>:: #accessor };

let querier_methods_impl = querier_variants
let querier_methods_impl = variants
.variants()
.map(|variant| variant.emit_querier_impl(&querier_api_path));
.map(|variant| variant.emit_querier_impl(&api_path));

let querier_methods_declaration = querier_variants
let querier_methods_declaration = variants
.variants()
.map(|variant| variant.emit_querier_method_declaration());

Expand Down
Loading

0 comments on commit a26a68e

Please sign in to comment.