use std::{any::Any, str::FromStr}; use giterated_models::{ object::GiteratedObject, operation::GiteratedOperation, settings::Setting, value::{GetValueTyped, GiteratedObjectValue}, }; use serde_json::Value; pub struct ValueMeta { pub name: String, pub deserialize: Box Result, serde_json::Error> + Send + Sync>, pub serialize: Box) -> Result, serde_json::Error> + Send + Sync>, pub typed_get: Box Box + Send + Sync>, pub is_get_value_typed: Box) -> bool + Send + Sync>, } pub trait IntoValueMeta { fn name() -> String; fn deserialize(buffer: &[u8]) -> Result, serde_json::Error>; fn serialize(value: Box) -> Result, serde_json::Error>; fn typed_get() -> Box; fn is_get_value_typed(typed_get_value: &Box) -> bool; } impl + 'static> IntoValueMeta for V { fn name() -> String { V::value_name().to_string() } fn deserialize(buffer: &[u8]) -> Result, serde_json::Error> { Ok(Box::new(serde_json::from_slice(&buffer)?)) } fn serialize(value: Box) -> Result, serde_json::Error> { let value = value.downcast::().unwrap(); Ok(serde_json::to_vec(&*value)?) } fn typed_get() -> Box { Box::new(GetValueTyped:: { value_name: V::value_name().to_string(), ty: Default::default(), }) } fn is_get_value_typed(typed_get_value: &Box) -> bool { typed_get_value.is::>() } } impl ValueMeta { pub fn new() -> Self { Self { name: I::name(), deserialize: Box::new(I::deserialize) as _, serialize: Box::new(I::serialize) as _, typed_get: Box::new(I::typed_get) as _, is_get_value_typed: Box::new(I::is_get_value_typed) as _, } } } pub struct OperationMeta { pub name: String, pub object_kind: String, pub deserialize: Box Result, serde_json::Error> + Send + Sync>, pub any_is_same: Box bool + Send + Sync>, pub serialize_success: Box) -> Result, serde_json::Error> + Send + Sync>, pub serialize_error: Box) -> Result, serde_json::Error> + Send + Sync>, } pub trait IntoOperationMeta { fn name() -> String; fn deserialize(buffer: &[u8]) -> Result, serde_json::Error>; fn serialize_success(success: Box) -> Result, serde_json::Error>; fn serialize_failure(failure: Box) -> Result, serde_json::Error>; fn any_is_same(other: &dyn Any) -> bool; } impl IntoOperationMeta for D where D::Failure: 'static, D::Success: 'static, O: GiteratedObject, D: GiteratedOperation + 'static, { fn name() -> String { D::operation_name().to_string() } fn deserialize(buffer: &[u8]) -> Result, serde_json::Error> { Ok(Box::new(serde_json::from_slice::(buffer)?) as Box) } fn serialize_success(success: Box) -> Result, serde_json::Error> { let to_serialize = success.downcast::().unwrap(); serde_json::to_vec(&to_serialize) } fn serialize_failure(failure: Box) -> Result, serde_json::Error> { let to_serialize = failure.downcast::().unwrap(); serde_json::to_vec(&to_serialize) } fn any_is_same(other: &dyn Any) -> bool { other.is::() } } impl OperationMeta { pub fn new + 'static>() -> Self { Self { name: I::name(), deserialize: Box::new(I::deserialize) as _, serialize_success: Box::new(I::serialize_success) as _, serialize_error: Box::new(I::serialize_failure) as _, object_kind: O::object_name().to_string(), any_is_same: Box::new(I::any_is_same) as _, } } } pub struct ObjectMeta { pub name: String, pub from_str: Box Result, ()> + Send + Sync>, pub any_is_same: Box bool + Send + Sync>, } pub trait IntoObjectMeta: FromStr { fn name() -> String; fn any_is_same(other: &dyn Any) -> bool; } impl IntoObjectMeta for O { fn name() -> String { O::object_name().to_string() } fn any_is_same(other: &dyn Any) -> bool { other.is::() } } impl ObjectMeta { pub fn new() -> Self { Self { name: I::name(), from_str: Box::new(|source| { let object = I::from_str(source).map_err(|_| ())?; Ok(Box::new(object) as Box) }), any_is_same: Box::new(I::any_is_same) as _, } } } pub struct SettingMeta { pub name: String, pub deserialize: Box Result, serde_json::Error> + Send + Sync>, pub serialize: Box) -> Result + Send + Sync>, } pub trait IntoSettingMeta { fn name() -> String; fn deserialize(buffer: &[u8]) -> Result, serde_json::Error>; fn serialize(setting: Box) -> Result; } impl IntoSettingMeta for S { fn name() -> String { S::name().to_string() } fn deserialize(buffer: &[u8]) -> Result, serde_json::Error> { Ok(Box::new(serde_json::from_slice(buffer)?)) } fn serialize(setting: Box) -> Result { Ok(*setting.downcast::().unwrap()) } } impl SettingMeta { pub fn new() -> Self { Self { name: I::name(), deserialize: Box::new(I::deserialize) as _, serialize: Box::new(I::serialize) as _, } } }