use std::{any::Any, ops::Deref, sync::Arc}; use giterated_models::{ object::GiteratedObject, operation::GiteratedOperation, settings::Setting, value::GiteratedObjectValue, }; use crate::{ ObjectMeta, ObjectOperationPair, ObjectSettingPair, ObjectValuePair, OperationMeta, ValueMeta, }; #[derive(Clone)] pub struct AnyObject { inner: Arc, meta: Arc, kind: &'static str, } impl AnyObject { pub fn new(object: O) -> Self { Self { inner: Arc::new(object) as _, meta: Arc::new(ObjectMeta::new::()), kind: O::object_name(), } } pub fn new_raw(_object: Arc, _kind: &'static str) -> Self { todo!() } pub fn kind(&self) -> &'static str { self.kind } pub fn meta(&self) -> &Arc { &self.meta } } impl Deref for AnyObject { type Target = dyn Any + Send + Sync; fn deref(&self) -> &Self::Target { self.inner.as_ref() } } #[derive(Clone)] pub struct AnyOperation { inner: Arc, meta: Arc, kind: ObjectOperationPair<'static>, } impl AnyOperation { pub fn new + 'static>( operation: D, ) -> Self { Self { inner: Arc::new(operation) as _, meta: Arc::new(OperationMeta::new::()), kind: ObjectOperationPair::from_types::(), } } pub fn new_raw( _operation: Arc, _kind: ObjectOperationPair<'static>, ) -> Self { todo!() } pub fn kind(&self) -> ObjectOperationPair<'static> { self.kind } pub fn meta(&self) -> &Arc { &self.meta } } impl Deref for AnyOperation { type Target = dyn Any + Send + Sync; fn deref(&self) -> &Self::Target { self.inner.as_ref() } } #[derive(Clone)] pub struct AnyValue { inner: Arc, kind: ObjectValuePair<'static>, } impl AnyValue { pub fn new + 'static>( value: V, ) -> Self { Self { inner: Arc::new(value) as _, kind: ObjectValuePair::from_types::(), } } pub fn new_raw(_value: Arc, _kind: ObjectValuePair<'static>) -> Self { todo!() } pub fn kind(&self) -> ObjectValuePair<'static> { self.kind } } impl Deref for AnyValue { type Target = dyn Any + Send + Sync; fn deref(&self) -> &Self::Target { self.inner.as_ref() } } #[derive(Clone)] pub struct AnySetting { inner: Arc, kind: ObjectSettingPair<'static>, } impl AnySetting { pub fn new(setting: S) -> Self { Self { inner: Arc::new(setting) as _, kind: ObjectSettingPair::from_types::(), } } pub fn new_raw( _setting: Arc, _kind: ObjectSettingPair<'static>, ) -> Self { todo!() } pub fn kind(&self) -> ObjectSettingPair<'static> { self.kind } } impl Deref for AnySetting { type Target = dyn Any + Send + Sync; fn deref(&self) -> &Self::Target { self.inner.as_ref() } } #[derive(Clone)] pub struct AnySuccess(pub Arc); #[derive(Clone)] pub struct AnyFailure(pub Arc); /// Should be renamed. /// /// Allows accepting object types that are either GiteratedObject types or /// AnyObject. pub trait MaybeDynamicObject: Clone { fn from_any(object: &AnyObject) -> Self; fn object_name() -> &'static str; } impl MaybeDynamicObject for O { fn from_any(_object: &AnyObject) -> Self { todo!() } fn object_name() -> &'static str { ::object_name() } } impl MaybeDynamicObject for AnyObject { fn from_any(object: &AnyObject) -> Self { object.clone() } fn object_name() -> &'static str { "any" } } pub trait MaybeDynamicValue { fn from_any(value: &AnyValue) -> Self; fn into_any(self) -> AnyValue; fn meta() -> Option; fn value_name() -> &'static str; } impl MaybeDynamicValue for V { fn from_any(_object: &AnyValue) -> Self { todo!() } fn value_name() -> &'static str { todo!() } fn into_any(self) -> AnyValue { todo!() } fn meta() -> Option { todo!() } } impl MaybeDynamicValue for AnyValue { fn value_name() -> &'static str { "any" } fn from_any(value: &AnyValue) -> Self { value.clone() } fn into_any(self) -> AnyValue { self } fn meta() -> Option { todo!() } }