diff --git a/crates/backend/src/codegen/enum.rs b/crates/backend/src/codegen/enum.rs index 14ed2a4e..6ba794e1 100644 --- a/crates/backend/src/codegen/enum.rs +++ b/crates/backend/src/codegen/enum.rs @@ -39,6 +39,81 @@ impl NapiEnum { quote! { napi::bindgen_prelude::ValueType::Number } }; + let from_napi_value = if self.variants.is_empty() { + quote! { + impl napi::bindgen_prelude::FromNapiValue for #name { + unsafe fn from_napi_value( + env: napi::bindgen_prelude::sys::napi_env, + napi_val: napi::bindgen_prelude::sys::napi_value + ) -> napi::bindgen_prelude::Result { + Err(napi::bindgen_prelude::error!( + napi::bindgen_prelude::Status::InvalidArg, + "enum `{}` has no variants", + #name_str + )) + } + } + } + } else { + quote! { + impl napi::bindgen_prelude::FromNapiValue for #name { + unsafe fn from_napi_value( + env: napi::bindgen_prelude::sys::napi_env, + napi_val: napi::bindgen_prelude::sys::napi_value + ) -> napi::bindgen_prelude::Result { + let val = napi::bindgen_prelude::FromNapiValue::from_napi_value(env, napi_val).map_err(|e| { + napi::bindgen_prelude::error!( + e.status, + "Failed to convert napi value into enum `{}`. {}", + #name_str, + e, + ) + })?; + + match val { + #(#from_napi_branches,)* + _ => { + Err(napi::bindgen_prelude::error!( + napi::bindgen_prelude::Status::InvalidArg, + "value `{:?}` does not match any variant of enum `{}`", + val, + #name_str + )) + } + } + } + } + } + }; + + let to_napi_value = if self.variants.is_empty() { + quote! { + impl napi::bindgen_prelude::ToNapiValue for #name { + unsafe fn to_napi_value( + env: napi::bindgen_prelude::sys::napi_env, + val: Self + ) -> napi::bindgen_prelude::Result { + napi::bindgen_prelude::ToNapiValue::to_napi_value(env, ()) + } + } + } + } else { + quote! { + impl napi::bindgen_prelude::ToNapiValue for #name { + unsafe fn to_napi_value( + env: napi::bindgen_prelude::sys::napi_env, + val: Self + ) -> napi::bindgen_prelude::Result { + let val = match val { + #(#to_napi_branches,)* + }; + + napi::bindgen_prelude::ToNapiValue::to_napi_value(env, val) + } + } + } + }; + quote! { impl napi::bindgen_prelude::TypeName for #name { fn type_name() -> &'static str { @@ -60,46 +135,9 @@ impl NapiEnum { } } - impl napi::bindgen_prelude::FromNapiValue for #name { - unsafe fn from_napi_value( - env: napi::bindgen_prelude::sys::napi_env, - napi_val: napi::bindgen_prelude::sys::napi_value - ) -> napi::bindgen_prelude::Result { - let val = napi::bindgen_prelude::FromNapiValue::from_napi_value(env, napi_val).map_err(|e| { - napi::bindgen_prelude::error!( - e.status, - "Failed to convert napi value into enum `{}`. {}", - #name_str, - e, - ) - })?; + #from_napi_value - match val { - #(#from_napi_branches,)* - _ => { - Err(napi::bindgen_prelude::error!( - napi::bindgen_prelude::Status::InvalidArg, - "value `{:?}` does not match any variant of enum `{}`", - val, - #name_str - )) - } - } - } - } - - impl napi::bindgen_prelude::ToNapiValue for #name { - unsafe fn to_napi_value( - env: napi::bindgen_prelude::sys::napi_env, - val: Self - ) -> napi::bindgen_prelude::Result { - let val = match val { - #(#to_napi_branches,)* - }; - - napi::bindgen_prelude::ToNapiValue::to_napi_value(env, val) - } - } + #to_napi_value } } diff --git a/crates/macro/src/lib.rs b/crates/macro/src/lib.rs index 30612ffd..ee127617 100644 --- a/crates/macro/src/lib.rs +++ b/crates/macro/src/lib.rs @@ -18,7 +18,7 @@ use syn::{fold::Fold, parse_macro_input, ItemFn}; /// ```ignore /// #[napi] -/// fn test(ctx: CallContext, name: String) { +/// fn test(name: String) { /// "hello" + name /// } /// ```