From 911b35f356225eee9f02e998651b9394c8cbb687 Mon Sep 17 00:00:00 2001 From: naskya Date: Fri, 26 Jul 2024 02:32:59 +0900 Subject: [PATCH] refactor (macro-rs): use syn::Result for internal outputs --- Cargo.lock | 1 + packages/macro-rs/macros-impl/src/error.rs | 9 +-------- packages/macro-rs/macros-impl/src/napi.rs | 10 +++++----- packages/macro-rs/macros-impl/src/util/tester.rs | 6 +++--- packages/macro-rs/macros/Cargo.toml | 1 + packages/macro-rs/macros/src/helper.rs | 5 ++++- 6 files changed, 15 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f3c4f1754c..d3d7afb7e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1768,6 +1768,7 @@ dependencies = [ "quote", "serde", "serde_json", + "syn 2.0.72", ] [[package]] diff --git a/packages/macro-rs/macros-impl/src/error.rs b/packages/macro-rs/macros-impl/src/error.rs index e14ea70c94..dd81f395b4 100644 --- a/packages/macro-rs/macros-impl/src/error.rs +++ b/packages/macro-rs/macros-impl/src/error.rs @@ -1,14 +1,7 @@ use proc_macro2::TokenStream; use quote::quote; -pub fn error_variants(_attr: TokenStream, item: TokenStream) -> TokenStream { - match error_variants_impl(item) { - Ok(tokens) => tokens, - Err(error) => error.to_compile_error(), - } -} - -fn error_variants_impl(item: TokenStream) -> syn::Result { +pub fn error_variants(_attr: TokenStream, item: TokenStream) -> syn::Result { let mut item: syn::ItemEnum = syn::parse2(item)?; item.variants = item diff --git a/packages/macro-rs/macros-impl/src/napi.rs b/packages/macro-rs/macros-impl/src/napi.rs index 13e2242ab8..57de95c273 100644 --- a/packages/macro-rs/macros-impl/src/napi.rs +++ b/packages/macro-rs/macros-impl/src/napi.rs @@ -184,7 +184,7 @@ use quote::{quote, ToTokens}; /// # }); /// ``` /// -pub fn napi(macro_attr: TokenStream, item: TokenStream) -> TokenStream { +pub fn napi(macro_attr: TokenStream, item: TokenStream) -> syn::Result { let macro_attr_tokens: Vec = macro_attr.clone().into_iter().collect(); // generated extra macro attr TokenStream (prepended before original input `macro_attr`) let mut extra_macro_attr = TokenStream::new(); @@ -202,10 +202,10 @@ pub fn napi(macro_attr: TokenStream, item: TokenStream) -> TokenStream { }) { quote! { use_nullable = true, }.to_tokens(&mut extra_macro_attr); } - return quote! { + return Ok(quote! { #[napi_derive::napi(#extra_macro_attr #macro_attr)] #item - }; + }); }; // handle functions @@ -333,7 +333,7 @@ pub fn napi(macro_attr: TokenStream, item: TokenStream) -> TokenStream { quote! { js_name = #js_name, }.to_tokens(&mut extra_macro_attr); } - quote! { + Ok(quote! { #item_fn #[napi_derive::napi(#extra_macro_attr #macro_attr)] @@ -342,7 +342,7 @@ pub fn napi(macro_attr: TokenStream, item: TokenStream) -> TokenStream { #ident(#(#called_args),*) #(#function_call_modifiers)* } - } + }) } crate::macro_unit_tests! { diff --git a/packages/macro-rs/macros-impl/src/util/tester.rs b/packages/macro-rs/macros-impl/src/util/tester.rs index 717ed5fb19..dfe9d3bea0 100644 --- a/packages/macro-rs/macros-impl/src/util/tester.rs +++ b/packages/macro-rs/macros-impl/src/util/tester.rs @@ -33,7 +33,7 @@ macro_rules! macro_doctest { &$macro_name( ::quote::quote!($( $($attr)* )?), ::quote::quote!($($item)*), - ) + ).unwrap() ), ::std::string::ToString::to_string( &::quote::quote!($($expanded)*) @@ -81,14 +81,14 @@ macro_rules! macro_doctest { macro_rules! macro_unit_tests { (@test $macro_name:ident($attr:ident, $item:ident) becomes $expanded:ident) => { assert_eq!( - ::std::format!("{}", $macro_name($attr, $item)), + ::std::format!("{}", $macro_name($attr, $item).unwrap()), ::std::format!("{}", $expanded), ); }; (@test $macro_name:ident($attr:ident, $item:ident) generates $expanded:ident) => { let item_str = format!("{}", $item); assert_eq!( - ::std::format!("{}", $macro_name($attr, $item)), + ::std::format!("{}", $macro_name($attr, $item).unwrap()), ::std::format!("{} {}", item_str, $expanded), ); }; diff --git a/packages/macro-rs/macros/Cargo.toml b/packages/macro-rs/macros/Cargo.toml index 323c87ac6b..9300948bb2 100644 --- a/packages/macro-rs/macros/Cargo.toml +++ b/packages/macro-rs/macros/Cargo.toml @@ -16,3 +16,4 @@ proc-macro2 = { workspace = true } quote = { workspace = true } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true, features = ["std"] } +syn = { workspace = true } diff --git a/packages/macro-rs/macros/src/helper.rs b/packages/macro-rs/macros/src/helper.rs index a901ba27e9..cf77ae1f3c 100644 --- a/packages/macro-rs/macros/src/helper.rs +++ b/packages/macro-rs/macros/src/helper.rs @@ -80,7 +80,10 @@ macro_rules! reexport_proc_macro_attributes { attr: ::proc_macro::TokenStream, item: ::proc_macro::TokenStream, ) -> ::proc_macro::TokenStream { - $impl_path(attr.into(), item.into()).into() + match $impl_path(attr.into(), item.into()) { + ::std::result::Result::Ok(token_stream) => token_stream.into(), + ::std::result::Result::Err(error) => error.to_compile_error().into(), + } } )* }