1 | use std::convert::TryFrom; |
2 | |
3 | use super::Value; |
4 | use crate::bindgen_runtime::{TypeName, ValidateNapiValue}; |
5 | use crate::{check_status, ValueType}; |
6 | use crate::{sys, Error, Result}; |
7 | |
8 | #[derive (Clone, Copy)] |
9 | pub struct JsBoolean(pub(crate) Value); |
10 | |
11 | impl TypeName for JsBoolean { |
12 | fn type_name() -> &'static str { |
13 | "bool" |
14 | } |
15 | |
16 | fn value_type() -> crate::ValueType { |
17 | ValueType::Boolean |
18 | } |
19 | } |
20 | |
21 | impl ValidateNapiValue for JsBoolean {} |
22 | |
23 | impl JsBoolean { |
24 | pub fn get_value(&self) -> Result<bool> { |
25 | let mut result: bool = false; |
26 | check_status!(unsafe { sys::napi_get_value_bool(self.0.env, self.0.value, &mut result) })?; |
27 | Ok(result) |
28 | } |
29 | } |
30 | |
31 | impl TryFrom<JsBoolean> for bool { |
32 | type Error = Error; |
33 | |
34 | fn try_from(value: JsBoolean) -> Result<bool> { |
35 | value.get_value() |
36 | } |
37 | } |
38 | |