refactor (macro-rs): use syn::Result for internal outputs

This commit is contained in:
naskya 2024-07-26 02:32:59 +09:00
parent 9f0e471965
commit 911b35f356
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
6 changed files with 15 additions and 17 deletions

1
Cargo.lock generated
View file

@ -1768,6 +1768,7 @@ dependencies = [
"quote", "quote",
"serde", "serde",
"serde_json", "serde_json",
"syn 2.0.72",
] ]
[[package]] [[package]]

View file

@ -1,14 +1,7 @@
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use quote::quote; use quote::quote;
pub fn error_variants(_attr: TokenStream, item: TokenStream) -> TokenStream { pub fn error_variants(_attr: TokenStream, item: TokenStream) -> syn::Result<TokenStream> {
match error_variants_impl(item) {
Ok(tokens) => tokens,
Err(error) => error.to_compile_error(),
}
}
fn error_variants_impl(item: TokenStream) -> syn::Result<TokenStream> {
let mut item: syn::ItemEnum = syn::parse2(item)?; let mut item: syn::ItemEnum = syn::parse2(item)?;
item.variants = item item.variants = item

View file

@ -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<TokenStream> {
let macro_attr_tokens: Vec<TokenTree> = macro_attr.clone().into_iter().collect(); let macro_attr_tokens: Vec<TokenTree> = macro_attr.clone().into_iter().collect();
// generated extra macro attr TokenStream (prepended before original input `macro_attr`) // generated extra macro attr TokenStream (prepended before original input `macro_attr`)
let mut extra_macro_attr = TokenStream::new(); 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); quote! { use_nullable = true, }.to_tokens(&mut extra_macro_attr);
} }
return quote! { return Ok(quote! {
#[napi_derive::napi(#extra_macro_attr #macro_attr)] #[napi_derive::napi(#extra_macro_attr #macro_attr)]
#item #item
}; });
}; };
// handle functions // 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! { js_name = #js_name, }.to_tokens(&mut extra_macro_attr);
} }
quote! { Ok(quote! {
#item_fn #item_fn
#[napi_derive::napi(#extra_macro_attr #macro_attr)] #[napi_derive::napi(#extra_macro_attr #macro_attr)]
@ -342,7 +342,7 @@ pub fn napi(macro_attr: TokenStream, item: TokenStream) -> TokenStream {
#ident(#(#called_args),*) #ident(#(#called_args),*)
#(#function_call_modifiers)* #(#function_call_modifiers)*
} }
} })
} }
crate::macro_unit_tests! { crate::macro_unit_tests! {

View file

@ -33,7 +33,7 @@ macro_rules! macro_doctest {
&$macro_name( &$macro_name(
::quote::quote!($( $($attr)* )?), ::quote::quote!($( $($attr)* )?),
::quote::quote!($($item)*), ::quote::quote!($($item)*),
) ).unwrap()
), ),
::std::string::ToString::to_string( ::std::string::ToString::to_string(
&::quote::quote!($($expanded)*) &::quote::quote!($($expanded)*)
@ -81,14 +81,14 @@ macro_rules! macro_doctest {
macro_rules! macro_unit_tests { macro_rules! macro_unit_tests {
(@test $macro_name:ident($attr:ident, $item:ident) becomes $expanded:ident) => { (@test $macro_name:ident($attr:ident, $item:ident) becomes $expanded:ident) => {
assert_eq!( assert_eq!(
::std::format!("{}", $macro_name($attr, $item)), ::std::format!("{}", $macro_name($attr, $item).unwrap()),
::std::format!("{}", $expanded), ::std::format!("{}", $expanded),
); );
}; };
(@test $macro_name:ident($attr:ident, $item:ident) generates $expanded:ident) => { (@test $macro_name:ident($attr:ident, $item:ident) generates $expanded:ident) => {
let item_str = format!("{}", $item); let item_str = format!("{}", $item);
assert_eq!( assert_eq!(
::std::format!("{}", $macro_name($attr, $item)), ::std::format!("{}", $macro_name($attr, $item).unwrap()),
::std::format!("{} {}", item_str, $expanded), ::std::format!("{} {}", item_str, $expanded),
); );
}; };

View file

@ -16,3 +16,4 @@ proc-macro2 = { workspace = true }
quote = { workspace = true } quote = { workspace = true }
serde = { workspace = true, features = ["derive"] } serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["std"] } serde_json = { workspace = true, features = ["std"] }
syn = { workspace = true }

View file

@ -80,7 +80,10 @@ macro_rules! reexport_proc_macro_attributes {
attr: ::proc_macro::TokenStream, attr: ::proc_macro::TokenStream,
item: ::proc_macro::TokenStream, item: ::proc_macro::TokenStream,
) -> ::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(),
}
} }
)* )*
} }