mirror of
https://github.com/napi-rs/napi-rs.git
synced 2025-12-08 19:56:07 +00:00
fix(napi-derive): warning in empty enum codegen (#2214)
This commit is contained in:
parent
d84fc42832
commit
2e5bf773e3
@ -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<Self> {
|
||||
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<Self> {
|
||||
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::sys::napi_value> {
|
||||
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<napi::bindgen_prelude::sys::napi_value> {
|
||||
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<Self> {
|
||||
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<napi::bindgen_prelude::sys::napi_value> {
|
||||
let val = match val {
|
||||
#(#to_napi_branches,)*
|
||||
};
|
||||
|
||||
napi::bindgen_prelude::ToNapiValue::to_napi_value(env, val)
|
||||
}
|
||||
}
|
||||
#to_napi_value
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user