Trait napi::bindgen_prelude::ValidateNapiValue
source · pub trait ValidateNapiValue: FromNapiValue + TypeName {
unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value> { ... }
}
Provided Methods§
sourceunsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
unsafe fn validate(env: napi_env, napi_val: napi_value) -> Result<napi_value>
Safety
this function called to validate whether napi value passed to rust is valid type
The reason why this function return napi_value
is that if a Promise<T>
passed in
we need to return Promise.reject(T)
, not the T
.
So we need to create Promise.reject(T)
in this function.
Examples found in repository?
More examples
src/bindgen_runtime/js_values/either.rs (line 44)
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
unsafe fn validate(
env: sys::napi_env,
napi_val: sys::napi_value,
) -> crate::Result<sys::napi_value> {
unsafe { A::validate(env, napi_val).or_else(|_| B::validate(env, napi_val)) }
}
}
impl<A: TypeName, B: TypeName> TypeName for Either<A, B> {
fn type_name() -> &'static str {
"Either"
}
fn value_type() -> ValueType {
ValueType::Unknown
}
}
// Backwards compatibility with v1
impl<T> From<Either<T, JsUndefined>> for Option<T> {
fn from(value: Either<T, JsUndefined>) -> Option<T> {
match value {
Either::A(v) => Some(v),
Either::B(_) => None,
}
}
}
impl<T> From<Option<T>> for Either<T, Undefined> {
fn from(value: Option<T>) -> Self {
match value {
Some(v) => Either::A(v),
None => Either::B(()),
}
}
}
impl<T> From<Either<T, Null>> for Option<T> {
fn from(value: Either<T, Null>) -> Option<T> {
match value {
Either::A(v) => Some(v),
Either::B(_) => None,
}
}
}
impl<
A: TypeName + FromNapiValue + ValidateNapiValue,
B: TypeName + FromNapiValue + ValidateNapiValue,
> FromNapiValue for Either<A, B>
{
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> crate::Result<Self> {
if unsafe { A::validate(env, napi_val) }.is_ok() {
unsafe { A::from_napi_value(env, napi_val) }.map(Either::A)
} else if unsafe { B::validate(env, napi_val) }.is_ok() {
unsafe { B::from_napi_value(env, napi_val).map(Either::B) }
} else {
Err(Error::new(
Status::InvalidArg,
format!(
"Value is not either {} or {}",
A::type_name(),
B::type_name()
),
))
}
}
src/bindgen_runtime/js_values.rs (line 174)
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
unsafe fn validate(env: sys::napi_env, napi_val: sys::napi_value) -> Result<sys::napi_value> {
let mut result = -1;
check_status!(
unsafe { sys::napi_typeof(env, napi_val, &mut result) },
"Failed to detect napi value type",
)?;
let received_type = ValueType::from(result);
if received_type == ValueType::Null || received_type == ValueType::Undefined {
Ok(ptr::null_mut())
} else if let Ok(validate_ret) = unsafe { T::validate(env, napi_val) } {
Ok(validate_ret)
} else {
Err(Error::new(
Status::InvalidArg,
format!(
"Expect value to be Option<{}>, but received {}",
T::value_type(),
received_type
),
))
}
}