2020-09-02 18:05:53 +09:00
|
|
|
use std::convert::From;
|
|
|
|
use std::ffi::CString;
|
2020-06-21 20:10:06 +09:00
|
|
|
use std::ptr;
|
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
use crate::{sys, Callback, Result};
|
2020-06-21 20:10:06 +09:00
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
#[derive(Clone, Default)]
|
|
|
|
pub struct Property {
|
|
|
|
pub name: CString,
|
|
|
|
getter: sys::napi_callback,
|
|
|
|
setter: sys::napi_callback,
|
|
|
|
method: sys::napi_callback,
|
|
|
|
attrs: PropertyAttributes,
|
|
|
|
pub(crate) is_ctor: bool,
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
|
|
|
|
2021-11-29 13:52:42 +09:00
|
|
|
#[repr(i32)]
|
2020-09-02 18:05:53 +09:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub enum PropertyAttributes {
|
2021-11-29 13:52:42 +09:00
|
|
|
Default = sys::PropertyAttributes::default,
|
|
|
|
Writable = sys::PropertyAttributes::writable,
|
|
|
|
Enumerable = sys::PropertyAttributes::enumerable,
|
|
|
|
Configurable = sys::PropertyAttributes::configurable,
|
|
|
|
Static = sys::PropertyAttributes::static_,
|
2020-09-02 18:05:53 +09:00
|
|
|
}
|
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
impl Default for PropertyAttributes {
|
|
|
|
fn default() -> Self {
|
|
|
|
PropertyAttributes::Default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 18:05:53 +09:00
|
|
|
impl From<PropertyAttributes> for sys::napi_property_attributes {
|
|
|
|
fn from(value: PropertyAttributes) -> Self {
|
|
|
|
match value {
|
2021-11-29 13:52:42 +09:00
|
|
|
PropertyAttributes::Default => sys::PropertyAttributes::default,
|
|
|
|
PropertyAttributes::Writable => sys::PropertyAttributes::writable,
|
|
|
|
PropertyAttributes::Enumerable => sys::PropertyAttributes::enumerable,
|
|
|
|
PropertyAttributes::Configurable => sys::PropertyAttributes::configurable,
|
|
|
|
PropertyAttributes::Static => sys::PropertyAttributes::static_,
|
2020-09-02 18:05:53 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
impl Property {
|
|
|
|
pub fn new(name: &str) -> Result<Self> {
|
2020-09-03 01:35:47 +09:00
|
|
|
Ok(Property {
|
2021-09-23 02:29:09 +09:00
|
|
|
name: CString::new(name)?,
|
|
|
|
..Default::default()
|
2020-09-03 01:35:47 +09:00
|
|
|
})
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
pub fn with_name(mut self, name: &str) -> Self {
|
|
|
|
self.name = CString::new(name).unwrap();
|
2020-06-21 20:10:06 +09:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_method(mut self, callback: Callback) -> Self {
|
2021-09-23 02:29:09 +09:00
|
|
|
self.method = Some(callback);
|
2020-06-21 20:10:06 +09:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_getter(mut self, callback: Callback) -> Self {
|
2021-09-23 02:29:09 +09:00
|
|
|
self.getter = Some(callback);
|
2020-06-21 20:10:06 +09:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-09-02 18:05:53 +09:00
|
|
|
pub fn with_setter(mut self, callback: Callback) -> Self {
|
2021-09-23 02:29:09 +09:00
|
|
|
self.setter = Some(callback);
|
2020-09-02 18:05:53 +09:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-10-31 23:22:23 +09:00
|
|
|
pub fn with_property_attributes(mut self, attributes: PropertyAttributes) -> Self {
|
2021-09-23 02:29:09 +09:00
|
|
|
self.attrs = attributes;
|
2020-10-31 23:22:23 +09:00
|
|
|
self
|
2020-09-02 18:05:53 +09:00
|
|
|
}
|
|
|
|
|
2020-09-03 01:35:47 +09:00
|
|
|
pub(crate) fn raw(&self) -> sys::napi_property_descriptor {
|
2021-09-23 02:29:09 +09:00
|
|
|
sys::napi_property_descriptor {
|
|
|
|
utf8name: self.name.as_ptr(),
|
|
|
|
name: ptr::null_mut(),
|
|
|
|
method: self.method,
|
|
|
|
getter: self.getter,
|
|
|
|
setter: self.setter,
|
|
|
|
value: ptr::null_mut(),
|
|
|
|
attributes: self.attrs.into(),
|
|
|
|
data: ptr::null_mut(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_ctor(mut self, callback: Callback) -> Self {
|
|
|
|
self.method = Some(callback);
|
|
|
|
self.is_ctor = true;
|
|
|
|
self
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
|
|
|
}
|