1 | use std::ptr; |
2 | |
3 | use crate::{bindgen_prelude::*, check_status, sys, type_of, Error, Result, Status, ValueType}; |
4 | |
5 | #[derive (Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)] |
6 | pub struct Null; |
7 | pub type Undefined = (); |
8 | |
9 | impl TypeName for Null { |
10 | fn type_name() -> &'static str { |
11 | "null" |
12 | } |
13 | |
14 | fn value_type() -> ValueType { |
15 | ValueType::Null |
16 | } |
17 | } |
18 | |
19 | impl ValidateNapiValue for Null {} |
20 | |
21 | impl 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 | reason:"Value is not null" .to_owned(), |
28 | )), |
29 | } |
30 | } |
31 | } |
32 | |
33 | impl ToNapiValue for Null { |
34 | unsafe fn to_napi_value(env: sys::napi_env, _val: Self) -> Result<sys::napi_value> { |
35 | let mut ret: *mut napi_value__ = 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 | |
46 | impl TypeName for Undefined { |
47 | fn type_name() -> &'static str { |
48 | "undefined" |
49 | } |
50 | |
51 | fn value_type() -> ValueType { |
52 | ValueType::Undefined |
53 | } |
54 | } |
55 | |
56 | impl ValidateNapiValue for Undefined {} |
57 | |
58 | impl 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 | reason:"Value is not undefined" .to_owned(), |
65 | )), |
66 | } |
67 | } |
68 | } |
69 | |
70 | impl ToNapiValue for Undefined { |
71 | unsafe fn to_napi_value(env: sys::napi_env, _val: Self) -> Result<sys::napi_value> { |
72 | let mut ret: *mut napi_value__ = 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 | } |
82 | |