napi_h/js_values/
date.rs

1use std::ptr;
2
3use super::check_status;
4use crate::{
5  bindgen_runtime::{TypeName, ValidateNapiValue},
6  sys, Error, Result, Status, Value, ValueType,
7};
8
9pub struct JsDate(pub(crate) Value);
10
11impl TypeName for JsDate {
12  fn type_name() -> &'static str {
13    "Date"
14  }
15
16  fn value_type() -> crate::ValueType {
17    ValueType::Object
18  }
19}
20
21impl ValidateNapiValue for JsDate {
22  unsafe fn validate(env: sys::napi_env, napi_val: sys::napi_value) -> Result<sys::napi_value> {
23    let mut is_date = false;
24    check_status!(unsafe { sys::napi_is_date(env, napi_val, &mut is_date) })?;
25    if !is_date {
26      return Err(Error::new(
27        Status::InvalidArg,
28        "Expected a Date object".to_owned(),
29      ));
30    }
31
32    Ok(ptr::null_mut())
33  }
34}
35
36impl JsDate {
37  pub fn value_of(&self) -> Result<f64> {
38    let mut timestamp: f64 = 0.0;
39    check_status!(unsafe { sys::napi_get_date_value(self.0.env, self.0.value, &mut timestamp) })?;
40    Ok(timestamp)
41  }
42}