refactor (macro-rs): use syn::Result for internal outputs
This commit is contained in:
parent
9f0e471965
commit
911b35f356
6 changed files with 15 additions and 17 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1768,6 +1768,7 @@ dependencies = [
|
|||
"quote",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"syn 2.0.72",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -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<TokenStream> {
|
||||
pub fn error_variants(_attr: TokenStream, item: TokenStream) -> syn::Result<TokenStream> {
|
||||
let mut item: syn::ItemEnum = syn::parse2(item)?;
|
||||
|
||||
item.variants = item
|
||||
|
|
|
@ -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();
|
||||
// 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! {
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
};
|
||||
|
|
|
@ -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 }
|
||||
|
|
|
@ -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(),
|
||||
}
|
||||
}
|
||||
)*
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue