use std::collections::HashMap; use std::sync::RwLock; use domain::{ ConfigRepository, DataSource, DataSourceId, Layout, LayoutPreset, LayoutPresetId, WidgetConfig, WidgetId, }; #[derive(Debug)] pub enum MemoryConfigError { LockPoisoned, } impl std::fmt::Display for MemoryConfigError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { MemoryConfigError::LockPoisoned => write!(f, "lock poisoned"), } } } pub struct MemoryConfigStore { widgets: RwLock>, data_sources: RwLock>, layout: RwLock>, presets: RwLock>, } impl MemoryConfigStore { pub fn new() -> Self { Self { widgets: RwLock::new(HashMap::new()), data_sources: RwLock::new(HashMap::new()), layout: RwLock::new(None), presets: RwLock::new(HashMap::new()), } } } impl ConfigRepository for MemoryConfigStore { type Error = MemoryConfigError; async fn get_widget(&self, id: WidgetId) -> Result, Self::Error> { let guard = self.widgets.read().map_err(|_| MemoryConfigError::LockPoisoned)?; Ok(guard.get(&id).cloned()) } async fn list_widgets(&self) -> Result, Self::Error> { let guard = self.widgets.read().map_err(|_| MemoryConfigError::LockPoisoned)?; Ok(guard.values().cloned().collect()) } async fn save_widget(&self, config: &WidgetConfig) -> Result<(), Self::Error> { let mut guard = self.widgets.write().map_err(|_| MemoryConfigError::LockPoisoned)?; guard.insert(config.id, config.clone()); Ok(()) } async fn delete_widget(&self, id: WidgetId) -> Result<(), Self::Error> { let mut guard = self.widgets.write().map_err(|_| MemoryConfigError::LockPoisoned)?; guard.remove(&id); Ok(()) } async fn get_data_source(&self, id: DataSourceId) -> Result, Self::Error> { let guard = self.data_sources.read().map_err(|_| MemoryConfigError::LockPoisoned)?; Ok(guard.get(&id).cloned()) } async fn list_data_sources(&self) -> Result, Self::Error> { let guard = self.data_sources.read().map_err(|_| MemoryConfigError::LockPoisoned)?; Ok(guard.values().cloned().collect()) } async fn save_data_source(&self, source: &DataSource) -> Result<(), Self::Error> { let mut guard = self.data_sources.write().map_err(|_| MemoryConfigError::LockPoisoned)?; guard.insert(source.id, source.clone()); Ok(()) } async fn delete_data_source(&self, id: DataSourceId) -> Result<(), Self::Error> { let mut guard = self.data_sources.write().map_err(|_| MemoryConfigError::LockPoisoned)?; guard.remove(&id); Ok(()) } async fn get_layout(&self) -> Result, Self::Error> { let guard = self.layout.read().map_err(|_| MemoryConfigError::LockPoisoned)?; Ok(guard.clone()) } async fn save_layout(&self, layout: &Layout) -> Result<(), Self::Error> { let mut guard = self.layout.write().map_err(|_| MemoryConfigError::LockPoisoned)?; *guard = Some(layout.clone()); Ok(()) } async fn get_preset(&self, id: LayoutPresetId) -> Result, Self::Error> { let guard = self.presets.read().map_err(|_| MemoryConfigError::LockPoisoned)?; Ok(guard.get(&id).cloned()) } async fn list_presets(&self) -> Result, Self::Error> { let guard = self.presets.read().map_err(|_| MemoryConfigError::LockPoisoned)?; Ok(guard.values().cloned().collect()) } async fn save_preset(&self, preset: &LayoutPreset) -> Result<(), Self::Error> { let mut guard = self.presets.write().map_err(|_| MemoryConfigError::LockPoisoned)?; guard.insert(preset.id, preset.clone()); Ok(()) } async fn delete_preset(&self, id: LayoutPresetId) -> Result<(), Self::Error> { let mut guard = self.presets.write().map_err(|_| MemoryConfigError::LockPoisoned)?; guard.remove(&id); Ok(()) } }