Files
Gabriel Kaszewski 7001b5e911 arch: split ConfigRepository, extract polling, consolidate conversions, decouple protocol
- Value↔JSON: From impls on domain Value behind `json` feature, delete 4 duplicate converters
- ConfigRepository split into ConfigRepository (12), UserRepository (3), WidgetStateCache (2)
- polling orchestration moved from bootstrap to application::polling_service
- WidgetRenderer in client-domain owns scroll/cache, both clients use it
- network loop consolidated into client-application::run_connection_loop
- protocol crate drops domain dep, Wire↔Domain conversions move to adapters
2026-06-19 18:12:50 +02:00

184 lines
5.0 KiB
Rust

use domain::{
ConfigRepository, DataSource, DataSourceId, DomainEvent, EventPublisher, Layout, LayoutPreset,
LayoutPresetId, ThemeConfig, User, UserRepository, WidgetConfig, WidgetId, WidgetState,
WidgetStateCache,
};
use std::collections::HashMap;
use std::sync::Mutex;
pub struct InMemoryConfigRepository {
widgets: Mutex<HashMap<WidgetId, WidgetConfig>>,
data_sources: Mutex<HashMap<DataSourceId, DataSource>>,
layout: Mutex<Option<Layout>>,
theme: Mutex<Option<ThemeConfig>>,
presets: Mutex<HashMap<LayoutPresetId, LayoutPreset>>,
}
impl InMemoryConfigRepository {
pub fn new() -> Self {
Self {
widgets: Mutex::new(HashMap::new()),
data_sources: Mutex::new(HashMap::new()),
layout: Mutex::new(None),
theme: Mutex::new(None),
presets: Mutex::new(HashMap::new()),
}
}
}
#[derive(Debug)]
pub struct Never;
impl std::fmt::Display for Never {
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
unreachable!()
}
}
impl ConfigRepository for InMemoryConfigRepository {
type Error = Never;
async fn get_widget(&self, id: WidgetId) -> Result<Option<WidgetConfig>, Self::Error> {
Ok(self.widgets.lock().unwrap().get(&id).cloned())
}
async fn list_widgets(&self) -> Result<Vec<WidgetConfig>, Self::Error> {
Ok(self.widgets.lock().unwrap().values().cloned().collect())
}
async fn save_widget(&self, config: &WidgetConfig) -> Result<(), Self::Error> {
self.widgets
.lock()
.unwrap()
.insert(config.id, config.clone());
Ok(())
}
async fn delete_widget(&self, id: WidgetId) -> Result<(), Self::Error> {
self.widgets.lock().unwrap().remove(&id);
Ok(())
}
async fn get_data_source(&self, id: DataSourceId) -> Result<Option<DataSource>, Self::Error> {
Ok(self.data_sources.lock().unwrap().get(&id).cloned())
}
async fn list_data_sources(&self) -> Result<Vec<DataSource>, Self::Error> {
Ok(self
.data_sources
.lock()
.unwrap()
.values()
.cloned()
.collect())
}
async fn save_data_source(&self, source: &DataSource) -> Result<(), Self::Error> {
self.data_sources
.lock()
.unwrap()
.insert(source.id, source.clone());
Ok(())
}
async fn delete_data_source(&self, id: DataSourceId) -> Result<(), Self::Error> {
self.data_sources.lock().unwrap().remove(&id);
Ok(())
}
async fn get_layout(&self) -> Result<Option<Layout>, Self::Error> {
Ok(self.layout.lock().unwrap().clone())
}
async fn save_layout(&self, layout: &Layout) -> Result<(), Self::Error> {
*self.layout.lock().unwrap() = Some(layout.clone());
Ok(())
}
async fn get_theme(&self) -> Result<Option<ThemeConfig>, Self::Error> {
Ok(self.theme.lock().unwrap().clone())
}
async fn save_theme(&self, theme: &ThemeConfig) -> Result<(), Self::Error> {
*self.theme.lock().unwrap() = Some(theme.clone());
Ok(())
}
async fn get_preset(&self, id: LayoutPresetId) -> Result<Option<LayoutPreset>, Self::Error> {
Ok(self.presets.lock().unwrap().get(&id).cloned())
}
async fn list_presets(&self) -> Result<Vec<LayoutPreset>, Self::Error> {
Ok(self.presets.lock().unwrap().values().cloned().collect())
}
async fn save_preset(&self, preset: &LayoutPreset) -> Result<(), Self::Error> {
self.presets
.lock()
.unwrap()
.insert(preset.id, preset.clone());
Ok(())
}
async fn delete_preset(&self, id: LayoutPresetId) -> Result<(), Self::Error> {
self.presets.lock().unwrap().remove(&id);
Ok(())
}
}
impl UserRepository for InMemoryConfigRepository {
type Error = Never;
async fn get_user_by_username(&self, _username: &str) -> Result<Option<User>, Self::Error> {
Ok(None)
}
async fn save_user(&self, _user: &User) -> Result<(), Self::Error> {
Ok(())
}
async fn count_users(&self) -> Result<u32, Self::Error> {
Ok(0)
}
}
impl WidgetStateCache for InMemoryConfigRepository {
type Error = Never;
async fn save_widget_states(
&self,
_states: &[(WidgetId, WidgetState)],
) -> Result<(), Self::Error> {
Ok(())
}
async fn load_widget_states(&self) -> Result<Vec<(WidgetId, WidgetState)>, Self::Error> {
Ok(vec![])
}
}
pub struct InMemoryEventPublisher {
events: Mutex<Vec<DomainEvent>>,
}
impl InMemoryEventPublisher {
pub fn new() -> Self {
Self {
events: Mutex::new(Vec::new()),
}
}
pub fn emitted(&self) -> Vec<DomainEvent> {
self.events.lock().unwrap().clone()
}
}
impl EventPublisher for InMemoryEventPublisher {
type Error = Never;
async fn publish(&self, event: DomainEvent) -> Result<(), Self::Error> {
self.events.lock().unwrap().push(event);
Ok(())
}
}