Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to syn 2.0 #488

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

## Unpublished

- Add methods to set the private key and mnemonic of an existing sender
- Deprecate `authz_granter` and `fee_granter` on `Daemon` struct
- Add a method on `TxHandler` to select instantiation permissions on Wasm upload
- Adds an `upload_wasm` function to CosmosSender to upload wasm code associated to no Contract structure
- Add methods to set the private key and mnemonic of an existing sender
- Deprecate `authz_granter` and `fee_granter` on `Daemon` struct
- Add a method on `TxHandler` to select instantiation permissions on Wasm upload
- Adds an `upload_wasm` function to CosmosSender to upload wasm code associated to no Contract structure
- Update syn to 2.0

### Breaking

Expand Down
2 changes: 1 addition & 1 deletion packages/macros/cw-orch-fns-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ proc-macro = true
[dependencies]
quote = "1"
proc-macro2 = "1"
syn = { version = "1", features = ["full", "extra-traits", "visit-mut"] }
syn = { version = "2", features = ["full", "extra-traits", "visit-mut"] }
convert_case = "0.6.0"
5 changes: 0 additions & 5 deletions packages/macros/cw-orch-fns-derive/src/execute_fns.rs

This file was deleted.

31 changes: 19 additions & 12 deletions packages/macros/cw-orch-fns-derive/src/fns_derive.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extern crate proc_macro;
use crate::{
execute_fns::payable,
helpers::{
has_into, process_fn_name, process_sorting, LexiographicMatching, MsgType, SyncType,
parse_enum_attributes, parse_field_attributes, parse_variant_attributes,
LexiographicMatching, MsgType, SyncType,
},
query_fns::parse_query_type,
};
Expand Down Expand Up @@ -64,12 +64,16 @@ pub fn fns_derive(msg_type: MsgType, sync_type: SyncType, mut input: ItemEnum) -
SyncType::Async => ("Async", Some(quote!(async)), Some(quote!(.await)), "_async"),
};

let enum_attributes = parse_enum_attributes(&input);

let variant_fns = input.variants.into_iter().map( |mut variant|{
let variant_name = variant.ident.clone();

let variant_attributes = parse_variant_attributes(&variant);

// We rename the variant if it has a fn_name attribute associated with it
let mut variant_func_name =
format_ident!("{}{async_fn_name_suffix}", process_fn_name(&variant).to_case(Case::Snake));
format_ident!("{}{async_fn_name_suffix}", variant_attributes.fn_name.to_case(Case::Snake));
variant_func_name.set_span(variant_name.span());


Expand All @@ -80,11 +84,10 @@ pub fn fns_derive(msg_type: MsgType, sync_type: SyncType, mut input: ItemEnum) -
)
};

// TODO
// Execute Specific

let (maybe_coins_attr,passed_coins) = match msg_type{
MsgType::Execute => {
let is_payable = payable(&variant);
let is_payable = variant_attributes.payable;
if is_payable {
(quote!(coins: &[::cosmwasm_std::Coin]),quote!(coins))
} else {
Expand All @@ -103,6 +106,7 @@ pub fn fns_derive(msg_type: MsgType, sync_type: SyncType, mut input: ItemEnum) -
};

match &mut variant.fields {

Fields::Unnamed(variant_fields) => {
let mut variant_idents = variant_fields.unnamed.clone();

Expand All @@ -123,8 +127,9 @@ pub fn fns_derive(msg_type: MsgType, sync_type: SyncType, mut input: ItemEnum) -
.iter()
.map(|field| {
let ident = &field.ident;
let field_attributes = parse_field_attributes(field);

if has_into(field){
if field_attributes.into{
quote!(#ident.into())
}else{
quote!(#ident)
Expand All @@ -136,7 +141,8 @@ pub fn fns_derive(msg_type: MsgType, sync_type: SyncType, mut input: ItemEnum) -
let variant_params = variant_fields.iter().map(|field| {
let field_name = &field.ident;
let field_type = &field.ty;
if has_into(field){
let field_attributes = parse_field_attributes(field);
if field_attributes.into{
quote! (#field_name: impl Into<#field_type> )
}else{
quote! (#field_name: #field_type )
Expand Down Expand Up @@ -166,9 +172,8 @@ pub fn fns_derive(msg_type: MsgType, sync_type: SyncType, mut input: ItemEnum) -
)
}
Fields::Named(variant_fields) => {
let is_attributes_sorted = process_sorting(&input.attrs);

if is_attributes_sorted{
if !enum_attributes.disable_fields_sorting{
// sort fields on field name
LexiographicMatching::default().visit_fields_named_mut(variant_fields);
}
Expand All @@ -179,7 +184,8 @@ pub fn fns_derive(msg_type: MsgType, sync_type: SyncType, mut input: ItemEnum) -
// Generate the struct members (This can be kept, it doesn't disturb)
let variant_idents = variant_fields.iter().map(|field|{
let ident = field.ident.clone().unwrap();
if has_into(field){
let field_attributes = parse_field_attributes(field);
if field_attributes.into{
quote!(#ident: #ident.into())
}else{
quote!(#ident)
Expand All @@ -190,7 +196,8 @@ pub fn fns_derive(msg_type: MsgType, sync_type: SyncType, mut input: ItemEnum) -
let variant_attr = variant_fields.iter().map(|field| {
let field_name = &field.ident;
let field_type = &field.ty;
if has_into(field){
let field_attributes = parse_field_attributes(field);
if field_attributes.into{
quote! (#field_name: impl Into<#field_type> )
}else{
quote! (#field_name: #field_type )
Expand Down
110 changes: 69 additions & 41 deletions packages/macros/cw-orch-fns-derive/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use proc_macro2::TokenTree;
use quote::ToTokens;
use std::cmp::Ordering;
use syn::{
punctuated::Punctuated, token::Comma, Attribute, Field, FieldsNamed, Lit, Meta, MetaList,
NestedMeta, Type,
parenthesized, punctuated::Punctuated, token::Comma, Field, FieldsNamed, ItemEnum, LitStr, Type,
};

pub enum MsgType {
Expand All @@ -16,29 +14,6 @@ pub enum SyncType {
Async,
}

pub(crate) fn process_fn_name(v: &syn::Variant) -> String {
for attr in &v.attrs {
if let Ok(Meta::List(list)) = attr.parse_meta() {
if let Some(ident) = list.path.get_ident() {
if ident == "cw_orch" {
for meta in list.nested {
if let NestedMeta::Meta(Meta::List(MetaList { nested, .. })) = &meta {
if let Some(NestedMeta::Lit(Lit::Str(lit_str))) = nested.last() {
return lit_str.value();
}
}
}
}
}
}
}
v.ident.to_string()
}

pub(crate) fn process_sorting(attrs: &[Attribute]) -> bool {
!has_cw_orch_attribute(attrs, "disable_fields_sorting")
}

#[derive(Default)]
pub(crate) struct LexiographicMatching {}

Expand Down Expand Up @@ -116,26 +91,79 @@ pub(crate) fn is_type_using_into(field_type: &Type) -> bool {
_ => false,
}
}
#[derive(Default)]
pub struct EnumAttributes {
pub disable_fields_sorting: bool,
}

pub(crate) fn has_cw_orch_attribute(attrs: &[Attribute], attribute_name: &str) -> bool {
for attr in attrs {
if attr.path.segments.len() == 1 && attr.path.segments[0].ident == "cw_orch" {
// We check the payable attribute is in there
for token_tree in attr.tokens.clone() {
if let TokenTree::Group(e) = token_tree {
for ident in e.stream() {
if ident.to_string() == attribute_name {
return true;
}
}
pub(crate) fn parse_enum_attributes(item_enum: &ItemEnum) -> EnumAttributes {
let mut enum_attributes = EnumAttributes::default();
for attr in &item_enum.attrs {
if attr.path().is_ident("cw_orch") {
attr.parse_nested_meta(|meta| {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in syn2 the namespacing of macros is refered to as the "ident"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes ! This is directly inspired from the official syn2 docs

if meta.path.is_ident("disable_fields_sorting") {
enum_attributes.disable_fields_sorting = true;
}
}
Ok(())
})
.unwrap();
}
}
enum_attributes
}

false
#[derive(Default)]
pub struct VariantAttributes {
pub fn_name: String,
pub payable: bool,
}

pub(crate) fn parse_variant_attributes(variant: &syn::Variant) -> VariantAttributes {
let mut cw_orch_attributes = VariantAttributes {
fn_name: variant.ident.to_string(),
..Default::default()
};
for attr in &variant.attrs {
if attr.path().is_ident("cw_orch") {
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("payable") {
cw_orch_attributes.payable = true;
}
if meta.path.is_ident("fn_name") {
let content;
parenthesized!(content in meta.input);
let lit: LitStr = content.parse()?;
cw_orch_attributes.fn_name = lit.value();
}
Ok(())
})
.unwrap();
}
}

cw_orch_attributes
}

#[derive(Default)]
pub struct FieldAttributes {
pub into: bool,
}

pub(crate) fn has_into(field: &syn::Field) -> bool {
is_type_using_into(&field.ty) || has_cw_orch_attribute(&field.attrs, "into")
pub(crate) fn parse_field_attributes(field: &syn::Field) -> FieldAttributes {
let mut cw_orch_attributes = FieldAttributes::default();
for attr in &field.attrs {
if attr.path().is_ident("cw_orch") {
attr.parse_nested_meta(|meta| {
if meta.path.is_ident("into") {
cw_orch_attributes.into = true;
}
Ok(())
})
.unwrap();
}
}

cw_orch_attributes.into = cw_orch_attributes.into || is_type_using_into(&field.ty);

cw_orch_attributes
}
1 change: 0 additions & 1 deletion packages/macros/cw-orch-fns-derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![recursion_limit = "128"]

mod execute_fns;
mod fns_derive;
mod helpers;
mod query_fns;
Expand Down
2 changes: 1 addition & 1 deletion packages/macros/cw-orch-fns-derive/src/query_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn parse_query_type(v: &syn::Variant) -> proc_macro2::TokenStream {
let response_ty: syn::Type = v
.attrs
.iter()
.find(|a| a.path.get_ident().unwrap() == RETURNS)
.find(|a| a.path().is_ident(RETURNS))
.unwrap_or_else(|| panic!("missing return type for query: {}", v.ident))
.parse_args()
.unwrap_or_else(|_| panic!("return for {} must be a type", v.ident));
Expand Down
Loading