use std::{ fmt::{Debug, Display}, marker::PhantomData, str::FromStr, }; use anyhow::Error; use crate::{ error::{GetValueError, OperationError}, object_backend::ObjectBackend, operation::GiteratedOperation, settings::{GetSetting, GetSettingError, SetSetting, SetSettingError, Setting}, value::{GetValue, GiteratedObjectValue}, }; mod operations; pub use operations::*; #[derive(Debug, Clone)] pub struct Object<'b, O: GiteratedObject, B: ObjectBackend + 'b + Send + Sync + Clone> { pub(crate) inner: O, pub(crate) backend: B, pub(crate) _marker: PhantomData<&'b ()>, } impl<'b, B: ObjectBackend + Send + Sync + Clone, O: GiteratedObject> Object<'b, O, B> { pub fn object(&self) -> &O { &self.inner } pub unsafe fn new_unchecked(object: O, backend: B) -> Object<'b, O, B> { Object { inner: object, backend, _marker: PhantomData, } } } impl Display for Object<'_, O, B> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.inner.fmt(f) } } pub trait GiteratedObject: Send + Display + FromStr { fn object_name() -> &'static str; fn from_object_str(object_str: &str) -> Result; } impl<'b, O: GiteratedObject + Clone + Debug, B: ObjectBackend> Object<'b, O, B> { pub async fn get + Send + Debug>( &mut self, ) -> Result> { let result = self .request(GetValue { value_name: V::value_name().to_string(), _marker: PhantomData, }) .await; result } pub async fn get_setting( &mut self, ) -> Result> { self.request(GetSetting { setting_name: S::name().to_string(), _marker: PhantomData, }) .await } pub async fn set_setting( &mut self, setting: S, ) -> Result<(), OperationError> { self.request(SetSetting { setting_name: S::name().to_string(), value: setting, }) .await } pub async fn request + Debug>( &mut self, request: R, ) -> Result> { self.backend .object_operation(self.inner.clone(), R::operation_name(), request) .await } }