napi_h/bindgen_runtime/js_values/
nil.rs

1use std::ptr;
2
3use crate::{bindgen_prelude::*, check_status, sys, type_of, Error, Result, Status, ValueType};
4
5#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
6pub struct Null;
7pub type Undefined = ();
8
9impl TypeName for Null {
10  fn type_name() -> &'static str {
11    "null"
12  }
13
14  fn value_type() -> ValueType {
15    ValueType::Null
16  }
17}
18
19impl ValidateNapiValue for Null {}
20
21impl FromNapiValue for Null {
22  unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
23    match type_of!(env, napi_val) {
24      Ok(ValueType::Null) => Ok(Null),
25      _ => Err(Error::new(
26        Status::InvalidArg,
27        "Value is not null".to_owned(),
28      )),
29    }
30  }
31}
32
33impl ToNapiValue for Null {
34  unsafe fn to_napi_value(env: sys::napi_env, _val: Self) -> Result<sys::napi_value> {
35    let mut ret = ptr::null_mut();
36
37    check_status!(
38      unsafe { sys::napi_get_null(env, &mut ret) },
39      "Failed to create napi null value"
40    )?;
41
42    Ok(ret)
43  }
44}
45
46impl TypeName for Undefined {
47  fn type_name() -> &'static str {
48    "undefined"
49  }
50
51  fn value_type() -> ValueType {
52    ValueType::Undefined
53  }
54}
55
56impl ValidateNapiValue for Undefined {}
57
58impl FromNapiValue for Undefined {
59  unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
60    match type_of!(env, napi_val) {
61      Ok(ValueType::Undefined) => Ok(()),
62      _ => Err(Error::new(
63        Status::InvalidArg,
64        "Value is not undefined".to_owned(),
65      )),
66    }
67  }
68}
69
70impl ToNapiValue for Undefined {
71  unsafe fn to_napi_value(env: sys::napi_env, _val: Self) -> Result<sys::napi_value> {
72    let mut ret = ptr::null_mut();
73
74    check_status!(
75      unsafe { sys::napi_get_undefined(env, &mut ret) },
76      "Failed to create napi undefined value"
77    )?;
78
79    Ok(ret)
80  }
81}