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