api-types: standalone crate with DTOs (widget, data source, layout, preset) extracted from http-api. Shared between http-api and future SPA. thiserror: replaced all manual Display impls with derive macros across 8 crates (config-sqlite, config-memory, tcp-server, tcp-client, http-json, rss, media, application).
113 lines
4.0 KiB
Rust
113 lines
4.0 KiB
Rust
use std::collections::HashMap;
|
|
use std::sync::RwLock;
|
|
use domain::{
|
|
ConfigRepository,
|
|
DataSource, DataSourceId, Layout, LayoutPreset, LayoutPresetId,
|
|
WidgetConfig, WidgetId,
|
|
};
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum MemoryConfigError {
|
|
#[error("lock poisoned")]
|
|
LockPoisoned,
|
|
}
|
|
|
|
pub struct MemoryConfigStore {
|
|
widgets: RwLock<HashMap<WidgetId, WidgetConfig>>,
|
|
data_sources: RwLock<HashMap<DataSourceId, DataSource>>,
|
|
layout: RwLock<Option<Layout>>,
|
|
presets: RwLock<HashMap<LayoutPresetId, LayoutPreset>>,
|
|
}
|
|
|
|
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<Option<WidgetConfig>, Self::Error> {
|
|
let guard = self.widgets.read().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
|
Ok(guard.get(&id).cloned())
|
|
}
|
|
|
|
async fn list_widgets(&self) -> Result<Vec<WidgetConfig>, 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<Option<DataSource>, Self::Error> {
|
|
let guard = self.data_sources.read().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
|
Ok(guard.get(&id).cloned())
|
|
}
|
|
|
|
async fn list_data_sources(&self) -> Result<Vec<DataSource>, 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<Option<Layout>, 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<Option<LayoutPreset>, Self::Error> {
|
|
let guard = self.presets.read().map_err(|_| MemoryConfigError::LockPoisoned)?;
|
|
Ok(guard.get(&id).cloned())
|
|
}
|
|
|
|
async fn list_presets(&self) -> Result<Vec<LayoutPreset>, 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(())
|
|
}
|
|
}
|