use std::{any::type_name, fmt::Debug, marker::PhantomData}; use anyhow::Error; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use crate::{ error::{GetValueError, OperationError}, model::{instance::Instance, MessageTarget}, }; pub mod instance; pub mod repository; pub mod user; pub trait GiteratedObject: Send + Serialize + DeserializeOwned { fn object_name(&self) -> &str; fn from_object_str(object_str: &str) -> Result; } pub trait GiteratedOperation: Send + Serialize + DeserializeOwned { type Success: Serialize + DeserializeOwned + Send; type Failure: Serialize + DeserializeOwned + Send; fn operation_name(&self) -> &'static str { type_name::() } } pub trait GiteratedObjectValue: Serialize + DeserializeOwned { type Object: GiteratedObject; fn value_name() -> &'static str; } #[derive(Debug, Clone)] pub struct Object<'b, O: GiteratedObject, B: ObjectBackend + 'b + Send + Sync + Clone> { inner: O, backend: B, _marker: PhantomData<&'b ()>, } #[async_trait::async_trait] pub trait ObjectBackend: Send + Sync + Sized + Clone { async fn object_operation + Debug>( &self, object: O, operation: D, ) -> Result>; async fn get_object( &self, object_str: &str, ) -> Result, OperationError>; } impl<'b, B: ObjectBackend + Send + Sync + Clone, O: GiteratedObject> Object<'b, O, B> { pub unsafe fn new_unchecked(object: O, backend: B) -> Object<'b, O, B> { Object { inner: object, backend, _marker: PhantomData, } } } // impl<'b, O: GiteratedObject, B: ObjectBackend> Object<'b, O, B> { // pub unsafe fn new_unchecked(value: O, backend: Arc) -> Object<'b, O, B> { // todo!() // } // } impl<'b, O: GiteratedObject + Clone + Debug, B: ObjectBackend + Debug + Send + Sync + Clone> Object<'b, O, B> { pub async fn get + Send>( &self, ) -> Result> { let operation: GetValue = GetValue { value_name: V::value_name().to_string(), _marker: PhantomData, }; let _message: GiteratedMessage = GiteratedMessage { object: self.inner.clone(), operation: operation.operation_name().to_string(), payload: operation, }; todo!() } pub fn request + Debug>( &mut self, request: R, ) -> Result { self.backend.object_operation(self.inner.clone(), request); todo!() } } #[derive(Serialize, Deserialize)] pub struct GetValue { value_name: String, _marker: PhantomData, } impl + Send> GiteratedOperation for GetValue { fn operation_name(&self) -> &'static str { "get_value" } type Success = V; type Failure = GetValueError; } #[derive(Serialize)] #[serde(bound(deserialize = "O: GiteratedObject, V: GiteratedOperation"))] pub struct GiteratedMessage> { pub object: O, pub operation: String, pub payload: V, } impl> MessageTarget for GiteratedMessage {} impl + Debug, O: GiteratedObject + Debug> Debug for GiteratedMessage { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("GiteratedMessage") .field("object", &self.object) .field("operation", &self.operation) .field("payload", &self.payload) .finish() } } #[derive(Debug, Serialize, Deserialize)] pub struct ObjectRequest(pub String); #[derive(Serialize, Deserialize)] pub struct ObjectResponse(pub Vec); impl GiteratedOperation for ObjectRequest { type Success = ObjectResponse; type Failure = ObjectRequestError; } #[derive(Debug, thiserror::Error, Serialize, Deserialize)] pub enum ObjectRequestError { #[error("error decoding the object")] Deserialization(String), } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct AnyObject(Vec); impl GiteratedObject for AnyObject { fn object_name(&self) -> &str { "any" } fn from_object_str(object_str: &str) -> Result { Ok(Self(Vec::from(object_str.as_bytes()))) } } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct AnyOperation(Vec); impl GiteratedOperation for AnyOperation { type Success = Vec; type Failure = Vec; }