| 1 | use std::ptr; |
| 2 | |
| 3 | use super::check_status; |
| 4 | use crate::{ |
| 5 | bindgen_runtime::{TypeName, ValidateNapiValue}, |
| 6 | sys, Error, Result, Status, Value, ValueType, |
| 7 | }; |
| 8 | |
| 9 | pub struct JsDate(pub(crate) Value); |
| 10 | |
| 11 | impl 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 | |
| 21 | impl 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: bool = 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 | reason:"Expected a Date object" .to_owned(), |
| 29 | )); |
| 30 | } |
| 31 | |
| 32 | Ok(ptr::null_mut()) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | impl 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 | } |
| 43 | |