use bpx::sd::Object;
use crate::reflection::property1::{AsProperty, PropertyType};
pub trait PropertyConfig: CloneBox
{
fn serialize(&self, obj: &mut bpx::sd::Object);
}
pub trait CloneBox
{
fn clone_box(&self) -> Box<dyn PropertyConfig>;
}
impl<T: 'static + Clone + PropertyConfig> CloneBox for T
{
fn clone_box(&self) -> Box<dyn PropertyConfig>
{
return Box::new(self.clone());
}
}
#[derive(Clone)]
pub struct NumericProperty<T: 'static + Clone>
{
pub min: T,
pub max: T,
pub step: T
}
impl<T: Clone + Into<bpx::sd::Value>> PropertyConfig for NumericProperty<T>
{
fn serialize(&self, obj: &mut Object)
{
obj.set("min", self.min.clone().into());
obj.set("max", self.max.clone().into());
obj.set("step", self.step.clone().into());
}
}
#[derive(Copy, Clone)]
pub struct StringProperty
{
pub max_chars: u32
}
impl PropertyConfig for StringProperty
{
fn serialize(&self, obj: &mut Object)
{
obj.set("max_chars", self.max_chars.into());
}
}
impl AsProperty for String
{
type ConfigType = StringProperty;
fn prop_type() -> PropertyType
{
return PropertyType {
type_name: "String",
class: None
};
}
}
macro_rules! impl_numeric_prop_provider {
($($t: ty) *) => {
$(
impl AsProperty for $t
{
type ConfigType = NumericProperty<$t>;
fn prop_type() -> PropertyType
{
return PropertyType
{
type_name: stringify!($t),
class: None
}
}
}
)*
};
}
impl_numeric_prop_provider!(
i64 u64
i32 u32
i16 u16
i8 u8
f32 f64
);