From e21e9bf889c81e4005958322bcb1353305c1fb01 Mon Sep 17 00:00:00 2001 From: VAmuzing Date: Thu, 7 Dec 2023 07:10:48 +0300 Subject: [PATCH 1/4] bump cli_derive to syn2; remove duplications Signed-off-by: VAmuzing --- Cargo.lock | 3 ++- cli/derive/Cargo.toml | 3 ++- cli/derive/src/lib.rs | 38 ++++++++++++++++---------------------- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9c70ec9200b..c5792659e4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2675,9 +2675,10 @@ dependencies = [ name = "iroha_cli_derive" version = "2.0.0-pre-rc.20" dependencies = [ + "manyhow", "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.38", "warp", ] diff --git a/cli/derive/Cargo.toml b/cli/derive/Cargo.toml index d258df86e55..281ae329a8e 100644 --- a/cli/derive/Cargo.toml +++ b/cli/derive/Cargo.toml @@ -14,9 +14,10 @@ workspace = true proc-macro = true [dependencies] -syn = { workspace = true, features = ["default", "parsing", "printing", "extra-traits"] } +syn2 = { workspace = true } quote = { workspace = true } proc-macro2 = { workspace = true } +manyhow = { workspace = true } [dev-dependencies] warp = { workspace = true } diff --git a/cli/derive/src/lib.rs b/cli/derive/src/lib.rs index 00438a1df47..b97920c7324 100644 --- a/cli/derive/src/lib.rs +++ b/cli/derive/src/lib.rs @@ -3,11 +3,11 @@ use proc_macro::TokenStream; use proc_macro2::Span; use quote::quote; -use syn::{ +use syn2::{ parse::{Parse, ParseStream}, parse_macro_input, punctuated::Punctuated, - Ident, LitInt, Result as SynResult, Token, + Ident, LitInt, Token, }; /// Generate warp filters for endpoints, accepting functions @@ -53,13 +53,13 @@ use syn::{ #[proc_macro] pub fn generate_endpoints(input: TokenStream) -> TokenStream { let EndpointList(list) = parse_macro_input!(input as EndpointList); - let arg_names = (1_u8..).map(|count| { + let lazy_arg_names = (1_u8..).map(|count| { Ident::new( format!("__endpoint_arg_{count}").as_str(), Span::call_site(), ) }); - let arg_types = (1_u8..).map(|count| { + let lazy_arg_types = (1_u8..).map(|count| { Ident::new( format!("__Endpoint_Arg_{count}").as_str(), Span::call_site(), @@ -68,29 +68,23 @@ pub fn generate_endpoints(input: TokenStream) -> TokenStream { let mut endpoints = Vec::new(); for item in list { - let (fun_name, arg_names, arg_types) = match item { + let (fun_name, arg_count) = match item { EndpointItem::ArgCount(arg_count) => { let fun_name = Ident::new(&format!("endpoint{arg_count}"), Span::call_site()); - let count = arg_count - .base10_parse::() - .expect("Already checked at parse stage"); - let arg_names = arg_names.clone().take(count).collect::>(); - let arg_types = arg_types.clone().take(count).collect::>(); - (fun_name, arg_names, arg_types) + (fun_name, arg_count) } EndpointItem::NameAndArgCount { name: fun_name, arg_count, - } => { - let count = arg_count - .base10_parse::() - .expect("Already checked at parse stage"); - let arg_names = arg_names.clone().take(count).collect::>(); - let arg_types = arg_types.clone().take(count).collect::>(); - (*fun_name, arg_names, arg_types) - } + } => (*fun_name, arg_count), }; + let count = arg_count + .base10_parse::() + .expect("Already checked at parse stage"); + let arg_names = lazy_arg_names.clone().take(count).collect::>(); + let arg_types = lazy_arg_types.clone().take(count).collect::>(); + let expanded = quote! { #[inline] #[allow(clippy::redundant_pub_crate)] @@ -129,7 +123,7 @@ enum EndpointItem { } impl Parse for EndpointList { - fn parse(input: ParseStream) -> SynResult { + fn parse(input: ParseStream) -> syn2::Result { let items = Punctuated::::parse_terminated(input)?; let mut seen_arg_counts = Vec::new(); for item in &items { @@ -138,7 +132,7 @@ impl Parse for EndpointList { | EndpointItem::ArgCount(arg_count) => { let curr_count = arg_count.base10_parse::()?; if seen_arg_counts.contains(&curr_count) { - return Err(syn::Error::new_spanned( + return Err(syn2::Error::new_spanned( arg_count.token(), "argument counts for all endpoints should be distinct", )); @@ -153,7 +147,7 @@ impl Parse for EndpointList { } impl Parse for EndpointItem { - fn parse(input: ParseStream) -> SynResult { + fn parse(input: ParseStream) -> syn2::Result { let lookahead = input.lookahead1(); if lookahead.peek(LitInt) { input.parse().map(EndpointItem::ArgCount) From 02380f9c0e69d732a0e48b8cac122f9ddd3a4d87 Mon Sep 17 00:00:00 2001 From: VAmuzing Date: Thu, 7 Dec 2023 07:38:08 +0300 Subject: [PATCH 2/4] remove manyhow from dependencies Signed-off-by: VAmuzing --- Cargo.lock | 1 - cli/derive/Cargo.toml | 1 - 2 files changed, 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c5792659e4c..a72771f1327 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2675,7 +2675,6 @@ dependencies = [ name = "iroha_cli_derive" version = "2.0.0-pre-rc.20" dependencies = [ - "manyhow", "proc-macro2", "quote", "syn 2.0.38", diff --git a/cli/derive/Cargo.toml b/cli/derive/Cargo.toml index 281ae329a8e..aa7a6e534d7 100644 --- a/cli/derive/Cargo.toml +++ b/cli/derive/Cargo.toml @@ -17,7 +17,6 @@ proc-macro = true syn2 = { workspace = true } quote = { workspace = true } proc-macro2 = { workspace = true } -manyhow = { workspace = true } [dev-dependencies] warp = { workspace = true } From b4e19c877bba3e3243f2c36eb13dca20b9d4187d Mon Sep 17 00:00:00 2001 From: VAmuzing Date: Tue, 12 Dec 2023 02:52:03 +0300 Subject: [PATCH 3/4] [fix] add manyhow for TokenStream conversion & error handling Signed-off-by: VAmuzing --- Cargo.lock | 1 + cli/derive/Cargo.toml | 1 + cli/derive/src/lib.rs | 15 +++++++-------- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a72771f1327..c5792659e4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2675,6 +2675,7 @@ dependencies = [ name = "iroha_cli_derive" version = "2.0.0-pre-rc.20" dependencies = [ + "manyhow", "proc-macro2", "quote", "syn 2.0.38", diff --git a/cli/derive/Cargo.toml b/cli/derive/Cargo.toml index aa7a6e534d7..281ae329a8e 100644 --- a/cli/derive/Cargo.toml +++ b/cli/derive/Cargo.toml @@ -17,6 +17,7 @@ proc-macro = true syn2 = { workspace = true } quote = { workspace = true } proc-macro2 = { workspace = true } +manyhow = { workspace = true } [dev-dependencies] warp = { workspace = true } diff --git a/cli/derive/src/lib.rs b/cli/derive/src/lib.rs index b97920c7324..b4549df5d76 100644 --- a/cli/derive/src/lib.rs +++ b/cli/derive/src/lib.rs @@ -1,11 +1,10 @@ //! Crate with a proc macro for torii endpoint generation -use proc_macro::TokenStream; -use proc_macro2::Span; +use manyhow::{manyhow, Result}; +use proc_macro2::{Span, TokenStream}; use quote::quote; use syn2::{ parse::{Parse, ParseStream}, - parse_macro_input, punctuated::Punctuated, Ident, LitInt, Token, }; @@ -50,9 +49,10 @@ use syn2::{ /// // defaults, such as `endpoint3`. /// generate_endpoints!(3, my_endpoint: 2, 4, anotherOne: 5, ); /// ``` +#[manyhow] #[proc_macro] -pub fn generate_endpoints(input: TokenStream) -> TokenStream { - let EndpointList(list) = parse_macro_input!(input as EndpointList); +pub fn generate_endpoints(input: TokenStream) -> Result { + let EndpointList(list) = syn2::parse2(input)?; let lazy_arg_names = (1_u8..).map(|count| { Ident::new( format!("__endpoint_arg_{count}").as_str(), @@ -107,10 +107,9 @@ pub fn generate_endpoints(input: TokenStream) -> TokenStream { endpoints.push(expanded); } - quote! { + Ok(quote! { #( #endpoints )* - } - .into() + }) } #[derive(Debug)] From b6abbc3c01310b12ed0a654176084b54e47cb582 Mon Sep 17 00:00:00 2001 From: VAmuzing Date: Fri, 22 Dec 2023 05:05:42 +0300 Subject: [PATCH 4/4] [fix] remove merge conflicts Signed-off-by: VAmuzing --- Cargo.lock | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 1ff201d4026..463503bae8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3247,9 +3247,10 @@ dependencies = [ name = "iroha_torii_macro" version = "2.0.0-pre-rc.20" dependencies = [ + "manyhow", "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.41", "warp", ]