Skip to content

Commit

Permalink
docs(sylvia-derive): Document inner types
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Aug 19, 2024
1 parent abfa2a5 commit dd7e0f9
Show file tree
Hide file tree
Showing 26 changed files with 117 additions and 32 deletions.
20 changes: 18 additions & 2 deletions sylvia-derive/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,26 @@ use crate::types::msg_variant::MsgVariants;
mod communication;
mod mt;

/// Preprocessed `contract` macro input for non-trait impl block
/// Preprocessed `contract` macro input for struct impl block.
///
/// Generates:
/// - [Messages](https://cosmwasm-docs.vercel.app/sylvia/macros/generated-types/message-types#contract-messages)
/// - InstantiateMsg
/// - ExecMsg
/// - QueryMsg
/// - SudoMsg
/// - MigrateMsg
/// - ContractExecMsg
/// - ContractQueryMsg
/// - ContractSudoMsg
/// - [MultiTest](https://cosmwasm-docs.vercel.app/sylvia/macros/generated-types/multitest) helpers
/// - [Querier](https://cosmwasm-docs.vercel.app/cw-multi-test) trait implementation
/// - [Executor](https://cosmwasm-docs.vercel.app/cw-multi-test) trait implementation
/// - Api trait implementation
pub struct ContractInput<'a> {
error: ContractErrorAttr,
item: &'a ItemImpl,
generics: Vec<&'a GenericParam>,
error: ContractErrorAttr,
custom: Custom,
override_entry_points: Vec<OverrideEntryPoint>,
interfaces: Interfaces,
Expand All @@ -52,6 +67,7 @@ impl<'a> ContractInput<'a> {
}
}

/// Process the input and generate the contract code.
pub fn process(&self) -> TokenStream {
let Self {
item,
Expand Down
1 change: 1 addition & 0 deletions sylvia-derive/src/contract/communication/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::types::msg_field::MsgField;
use crate::types::msg_variant::{MsgVariant, MsgVariants};
use crate::utils::SvCasing;

/// Emits execute helper
pub struct Executor<'a> {
generics: Generics,
self_ty: Type,
Expand Down
1 change: 1 addition & 0 deletions sylvia-derive/src/contract/communication/querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::types::msg_field::MsgField;
use crate::types::msg_variant::{MsgVariant, MsgVariants};
use crate::utils::SvCasing;

/// Emits query helper
pub struct Querier<'a> {
generics: Generics,
self_ty: Type,
Expand Down
1 change: 0 additions & 1 deletion sylvia-derive/src/contract/communication/struct_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub struct StructMessage<'a> {
}

impl<'a> StructMessage<'a> {
/// Creates new struct message of given type from impl block
pub fn new(
source: &'a ItemImpl,
msg_ty: MsgType,
Expand Down
3 changes: 2 additions & 1 deletion sylvia-derive/src/contract/communication/wrapper_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use syn::fold::Fold;
use syn::spanned::Spanned;
use syn::{Ident, ItemImpl, Type};

/// Glue message is the message composing Exec/Query messages from several traits
/// Glue message is the message composing Exec/Query/Sudo messages from several traits and a
/// contract
#[derive(Debug)]
pub struct GlueMessage<'a> {
source: &'a ItemImpl,
Expand Down
1 change: 1 addition & 0 deletions sylvia-derive/src/contract/mt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn get_ident_from_type(contract_name: &Type) -> &Ident {
&segment.ident
}

/// Emits helpers for testing contract messages using MultiTest.
pub struct MtHelpers<'a> {
error_type: Type,
contract_name: &'a Type,
Expand Down
9 changes: 8 additions & 1 deletion sylvia-derive/src/entry_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ use crate::parser::{
};
use crate::types::msg_variant::MsgVariants;

/// Preprocessed [`entry_points`](crate::entry_points) macro input.
///
/// Generates `entry_points` module containing:
/// - instantiate, execute, query and sudo entry points by default
/// - migrate and reply entry points if respective messages are defined
pub struct EntryPointInput<'a> {
item: &'a ItemImpl,
args: EntryPointArgs,
Expand Down Expand Up @@ -42,13 +47,15 @@ impl<'a> EntryPointInput<'a> {
Self { item, args }
}

/// Process the input and generate the interface code.
pub fn process(&self) -> TokenStream {
let Self { item, args } = self;

EntryPoints::new(item, args).emit()
}
}

/// Defines logic for generating entry points.
pub struct EntryPoints<'a> {
source: &'a ItemImpl,
name: Type,
Expand Down Expand Up @@ -156,7 +163,7 @@ impl<'a> EntryPoints<'a> {
}
}

pub fn emit_default_entry_point(&self, msg_ty: MsgType) -> TokenStream {
fn emit_default_entry_point(&self, msg_ty: MsgType) -> TokenStream {
let Self {
name,
error,
Expand Down
17 changes: 14 additions & 3 deletions sylvia-derive/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ use crate::types::msg_variant::MsgVariants;
mod communication;
mod mt;

/// Preprocessed `interface` macro input
/// Preprocessed [`interface`](crate::interface) macro input.
///
/// Generates `sv` module containing:
/// - [Messages](https://cosmwasm-docs.vercel.app/sylvia/macros/generated-types/message-types#interface-messages)
/// - ExecMsg
/// - QueryMsg
/// - SudoMsg
/// - [MultiTest](https://cosmwasm-docs.vercel.app/sylvia/macros/generated-types/multitest#proxy-trait) helpers
/// - [Querier](https://cosmwasm-docs.vercel.app/cw-multi-test) trait implementation
/// - [Executor](https://cosmwasm-docs.vercel.app/cw-multi-test) trait implementation
/// - Api trait implementation
pub struct InterfaceInput<'a> {
item: &'a ItemTrait,
custom: Custom,
Expand Down Expand Up @@ -52,7 +62,7 @@ impl<'a> InterfaceInput<'a> {

if custom.msg.is_none()
&& !associated_types
.all_names()
.as_names()
.any(|assoc_type| assoc_type == EXEC_TYPE)
{
emit_warning!(
Expand All @@ -64,7 +74,7 @@ impl<'a> InterfaceInput<'a> {

if custom.query.is_none()
&& !associated_types
.all_names()
.as_names()
.any(|assoc_type| assoc_type == QUERY_TYPE)
{
emit_warning!(
Expand All @@ -81,6 +91,7 @@ impl<'a> InterfaceInput<'a> {
}
}

/// Process the input and generate the interface code.
pub fn process(&self) -> TokenStream {
let Self {
associated_types,
Expand Down
1 change: 1 addition & 0 deletions sylvia-derive/src/interface/communication/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::ItemTrait;

/// Emits `InterfaceMessagesApi` trait.
pub struct Api<'a> {
source: &'a ItemTrait,
associated_types: &'a AssociatedTypes<'a>,
Expand Down
9 changes: 6 additions & 3 deletions sylvia-derive/src/interface/communication/enum_msg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::parser::attributes::MsgAttrForwarding;
use crate::parser::{parse_associated_custom_type, Custom, MsgType, ParsedSylviaAttributes};
use crate::parser::{Custom, MsgType, ParsedSylviaAttributes};
use crate::types::associated_types::{AssociatedTypes, ItemType, EXEC_TYPE, QUERY_TYPE};
use crate::types::msg_variant::MsgVariants;
use crate::utils::emit_bracketed_generics;
Expand All @@ -26,8 +26,11 @@ impl<'a> EnumMessage<'a> {
variants: MsgVariants<'a, Ident>,
associated_types: &'a AssociatedTypes<'a>,
) -> Self {
let associated_exec = parse_associated_custom_type(source, EXEC_TYPE);
let associated_query = parse_associated_custom_type(source, QUERY_TYPE);
let trait_name = &source.ident;
let associated_exec =
associated_types.emit_contract_custom_type_accessor(trait_name, EXEC_TYPE);
let associated_query =
associated_types.emit_contract_custom_type_accessor(trait_name, QUERY_TYPE);

let resp_type = custom
.msg
Expand Down
2 changes: 1 addition & 1 deletion sylvia-derive/src/interface/communication/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ where
.without_error()
.map(ItemType::as_name)
.collect();
let all_generics: Vec<_> = associated_types.all_names().collect();
let all_generics: Vec<_> = associated_types.as_names().collect();

let accessor = MsgType::Exec.as_accessor_name();
let executor_api_path = quote! {
Expand Down
3 changes: 2 additions & 1 deletion sylvia-derive/src/interface/communication/querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::types::msg_field::MsgField;
use crate::types::msg_variant::{MsgVariant, MsgVariants};
use crate::utils::SvCasing;

/// Emits query helper
pub struct Querier<'a, Generic> {
variants: &'a MsgVariants<'a, Generic>,
associated_types: &'a AssociatedTypes<'a>,
Expand Down Expand Up @@ -45,7 +46,7 @@ where
.without_error()
.map(ItemType::as_name)
.collect();
let all_generics: Vec<_> = associated_types.all_names().collect();
let all_generics: Vec<_> = associated_types.as_names().collect();
let accessor = MsgType::Query.as_accessor_name();
let api_path = quote! {
< dyn #interface_name < Error = (), #(#generics = Self:: #generics,)* > as InterfaceMessagesApi > :: #accessor
Expand Down
1 change: 1 addition & 0 deletions sylvia-derive/src/interface/mt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::types::associated_types::AssociatedTypes;
use crate::types::msg_variant::{MsgVariant, MsgVariants};
use crate::utils::SvCasing;

/// Emits helpers for testing interface messages using MultiTest.
pub struct MtHelpers<'a> {
source: &'a ItemTrait,
error_type: Type,
Expand Down
2 changes: 2 additions & 0 deletions sylvia-derive/src/parser/attributes/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use syn::{Ident, MetaList, Result, Token};

use super::MsgType;

/// Type wrapping data parsed from `sv::msg_attr` attribute.
#[derive(Clone, Debug)]
pub struct VariantAttrForwarding {
pub attrs: TokenStream,
Expand All @@ -21,6 +22,7 @@ impl VariantAttrForwarding {
}
}

/// Type wrapping data parsed from `sv::attr` attribute.
#[derive(Clone, Debug)]
pub struct MsgAttrForwarding {
pub msg_type: MsgType,
Expand Down
1 change: 1 addition & 0 deletions sylvia-derive/src/parser/attributes/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use syn::{parse_quote, Error, Ident, MetaList, Result, Token, Type};

use crate::crate_module;

/// Type wrapping data parsed from `sv::custom` attribute.
#[derive(Debug, Default)]
pub struct Custom {
pub msg: Option<Type>,
Expand Down
1 change: 1 addition & 0 deletions sylvia-derive/src/parser/attributes/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use syn::{parse_quote, MetaList, Result, Type};

use crate::crate_module;

/// Type wrapping data parsed from `sv::error` attribute.
#[derive(Debug)]
pub struct ContractErrorAttr {
pub error: Type,
Expand Down
6 changes: 6 additions & 0 deletions sylvia-derive/src/parser/attributes/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use proc_macro_error::emit_error;

use crate::fold::StripGenerics;

/// Type wrapping data parsed from `sv::message` attribute.
#[derive(Debug)]
pub struct ContractMessageAttr {
pub module: Path,
Expand All @@ -27,6 +28,11 @@ impl ContractMessageAttr {
}
}

/// The `Customs` struct is used to represent the presence of `: custom(msg, query)` parameter
/// within a `sv::message` attribute.
///
/// It is used to determine if an interface implemented on the contract uses `Empty` in place
/// of custom types.
#[derive(Debug)]
pub struct Customs {
pub has_msg: bool,
Expand Down
5 changes: 3 additions & 2 deletions sylvia-derive/src/parser/attributes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! Module defining parsing of Sylvia attributes.
//! Every Sylvia attribute should be prefixed with `sv::`
use proc_macro_error::emit_error;
use syn::spanned::Spanned;
use syn::{Attribute, MetaList, PathSegment};
Expand All @@ -18,7 +21,6 @@ pub use override_entry_point::{FilteredOverrideEntryPoints, OverrideEntryPoint};

/// This struct represents all possible attributes that
/// are parsed and utilized by sylvia.
///
pub enum SylviaAttribute {
Custom,
Error,
Expand Down Expand Up @@ -55,7 +57,6 @@ impl SylviaAttribute {

/// The structure parses all attributes provided in `new` method
/// and stores the one relevant for sylvia.
///
#[derive(Default)]
pub struct ParsedSylviaAttributes {
pub custom_attr: Option<Custom>,
Expand Down
1 change: 1 addition & 0 deletions sylvia-derive/src/parser/attributes/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum MsgType {
Sudo,
}

/// ArgumentParser holds `resp` parameter parsed from `sv::msg` attribute.
#[derive(Default)]
struct ArgumentParser {
pub resp_type: Option<Ident>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use syn::{parenthesized, Ident, MetaList, Path, Result, Token, Type};
use crate::crate_module;
use crate::parser::MsgType;

/// Type wrapping data parsed from `sv::override_entry_points` attribute.
#[derive(Debug, Clone)]
pub struct OverrideEntryPoint {
entry_point: Path,
Expand Down
22 changes: 7 additions & 15 deletions sylvia-derive/src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! Module defining parsing utilities for Sylvia attributes.
//! All parsing done for the [crate::types] should be done here.
pub mod attributes;
pub mod check_generics;
pub mod entry_point;
Expand All @@ -13,10 +16,7 @@ pub use entry_point::EntryPointArgs;
use proc_macro_error::emit_error;
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::{
parse_quote, FnArg, GenericArgument, Ident, ImplItem, ItemImpl, ItemTrait, Path, PathArguments,
Signature, Token, TraitItem, Type,
};
use syn::{FnArg, GenericArgument, ImplItem, ItemImpl, Path, PathArguments, Signature, Token};

use crate::types::msg_field::MsgField;

Expand All @@ -34,17 +34,8 @@ fn extract_generics_from_path(module: &Path) -> Punctuated<GenericArgument, Toke
generics
}

pub fn parse_associated_custom_type(source: &ItemTrait, type_name: &str) -> Option<Type> {
let trait_name = &source.ident;
source.items.iter().find_map(|item| match item {
TraitItem::Type(ty) if ty.ident == type_name => {
let type_name = Ident::new(type_name, ty.span());
Some(parse_quote! { <ContractT as #trait_name>:: #type_name})
}
_ => None,
})
}

/// Use to make sure that [contract](crate::contract) is used over implementation containing
/// mandatory `new` method.
pub fn assert_new_method_defined(item: &ItemImpl) {
const ERROR_NOTE: &str = "`sylvia::contract` requires parameterless `new` method to be defined for dispatch to work correctly.";

Expand All @@ -68,6 +59,7 @@ pub fn assert_new_method_defined(item: &ItemImpl) {
}
}

/// Parses method signature and returns a vector of [`MsgField`].
pub fn process_fields<'s, Generic>(
sig: &'s Signature,
generics_checker: &mut CheckGenerics<Generic>,
Expand Down
1 change: 1 addition & 0 deletions sylvia-derive/src/parser/variant_descs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::parser::attributes::VariantAttrForwarding;
use crate::parser::{MsgAttr, ParsedSylviaAttributes};
use syn::{Attribute, ImplItem, ItemImpl, ItemTrait, Signature, TraitItem};

/// Type wrapping common data between [ItemImpl] and [ItemTrait].
pub struct VariantDesc<'a> {
msg_attr: Option<MsgAttr>,
attrs_to_forward: Vec<VariantAttrForwarding>,
Expand Down
Loading

0 comments on commit dd7e0f9

Please sign in to comment.