2020-06-21 20:10:06 +09:00
|
|
|
use std::ptr;
|
|
|
|
|
|
|
|
use crate::{sys, Callback, Env, NapiValue, Result};
|
|
|
|
|
2020-08-10 13:37:58 +09:00
|
|
|
#[derive(Debug)]
|
2020-06-21 20:10:06 +09:00
|
|
|
pub struct Property {
|
|
|
|
name: String,
|
|
|
|
raw_descriptor: sys::napi_property_descriptor,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Property {
|
|
|
|
pub fn new(name: &str) -> Self {
|
|
|
|
Property {
|
|
|
|
name: String::from(name),
|
|
|
|
raw_descriptor: sys::napi_property_descriptor {
|
|
|
|
utf8name: ptr::null_mut(),
|
|
|
|
name: ptr::null_mut(),
|
|
|
|
method: None,
|
|
|
|
getter: None,
|
|
|
|
setter: None,
|
|
|
|
value: ptr::null_mut(),
|
|
|
|
attributes: sys::napi_property_attributes::napi_default,
|
|
|
|
data: ptr::null_mut(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_value<T: NapiValue>(mut self, value: T) -> Self {
|
|
|
|
self.raw_descriptor.value = T::raw_value(&value);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_method(mut self, callback: Callback) -> Self {
|
|
|
|
self.raw_descriptor.method = Some(callback);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_getter(mut self, callback: Callback) -> Self {
|
|
|
|
self.raw_descriptor.getter = Some(callback);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn into_raw(mut self, env: &Env) -> Result<sys::napi_property_descriptor> {
|
2020-07-18 03:00:48 +09:00
|
|
|
self.raw_descriptor.name = env.create_string(&self.name)?.raw_value();
|
2020-06-21 20:10:06 +09:00
|
|
|
Ok(self.raw_descriptor)
|
|
|
|
}
|
|
|
|
}
|