add all crates: domain, protocol, application, client, adapters, ESP32 firmware
Server: domain (entities, value objects, ports), protocol (postcard wire types), application (config service, data projection), adapters (config-memory, tcp-server), bootstrap (composition root with fake data). Client: client-domain (layout engine, render tree, HAL ports), client-application (message handling, repaint commands), adapters (tcp-client, display-terminal), client-desktop (end-to-end working). ESP32: client-esp32 firmware with ILI9341 display over SPI, WiFi networking. Display test verified on hardware — landscape orientation, text rendering works. 60 workspace tests, all passing.
This commit is contained in:
118
crates/application/src/config_service.rs
Normal file
118
crates/application/src/config_service.rs
Normal file
@@ -0,0 +1,118 @@
|
||||
use std::fmt;
|
||||
use domain::{
|
||||
ConfigRepository, EventPublisher, DomainEvent,
|
||||
WidgetConfig, WidgetId,
|
||||
DataSource, DataSourceId, DataSourceValidationError,
|
||||
Layout, LayoutPreset, LayoutPresetId,
|
||||
};
|
||||
|
||||
pub struct ConfigService<'a, C, E> {
|
||||
config: &'a C,
|
||||
events: &'a E,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ConfigError<C: fmt::Debug, E: fmt::Debug> {
|
||||
Repository(C),
|
||||
Event(E),
|
||||
Validation(Vec<DataSourceValidationError>),
|
||||
NotFound,
|
||||
}
|
||||
|
||||
impl<C: fmt::Debug, E: fmt::Debug> fmt::Display for ConfigError<C, E> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
ConfigError::Repository(e) => write!(f, "repository error: {:?}", e),
|
||||
ConfigError::Event(e) => write!(f, "event error: {:?}", e),
|
||||
ConfigError::Validation(errors) => write!(f, "validation errors: {:?}", errors),
|
||||
ConfigError::NotFound => write!(f, "not found"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, C, E> ConfigService<'a, C, E>
|
||||
where
|
||||
C: ConfigRepository,
|
||||
C::Error: fmt::Debug,
|
||||
E: EventPublisher,
|
||||
E::Error: fmt::Debug,
|
||||
{
|
||||
pub fn new(config: &'a C, events: &'a E) -> Self {
|
||||
Self { config, events }
|
||||
}
|
||||
|
||||
pub async fn create_widget(&self, widget: WidgetConfig) -> Result<(), ConfigError<C::Error, E::Error>> {
|
||||
self.config.save_widget(&widget).await.map_err(ConfigError::Repository)?;
|
||||
self.events.publish(DomainEvent::WidgetCreated { id: widget.id }).await.map_err(ConfigError::Event)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn update_widget(&self, widget: WidgetConfig) -> Result<(), ConfigError<C::Error, E::Error>> {
|
||||
self.config.save_widget(&widget).await.map_err(ConfigError::Repository)?;
|
||||
self.events.publish(DomainEvent::WidgetUpdated { id: widget.id }).await.map_err(ConfigError::Event)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete_widget(&self, id: WidgetId) -> Result<(), ConfigError<C::Error, E::Error>> {
|
||||
self.config.delete_widget(id).await.map_err(ConfigError::Repository)?;
|
||||
self.events.publish(DomainEvent::WidgetDeleted { id }).await.map_err(ConfigError::Event)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn create_data_source(&self, source: DataSource) -> Result<(), ConfigError<C::Error, E::Error>> {
|
||||
let errors = source.validate();
|
||||
if !errors.is_empty() {
|
||||
return Err(ConfigError::Validation(errors));
|
||||
}
|
||||
self.config.save_data_source(&source).await.map_err(ConfigError::Repository)?;
|
||||
self.events.publish(DomainEvent::DataSourceAdded { id: source.id }).await.map_err(ConfigError::Event)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn update_data_source(&self, source: DataSource) -> Result<(), ConfigError<C::Error, E::Error>> {
|
||||
let errors = source.validate();
|
||||
if !errors.is_empty() {
|
||||
return Err(ConfigError::Validation(errors));
|
||||
}
|
||||
self.config.save_data_source(&source).await.map_err(ConfigError::Repository)?;
|
||||
self.events.publish(DomainEvent::DataSourceUpdated { id: source.id }).await.map_err(ConfigError::Event)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete_data_source(&self, id: DataSourceId) -> Result<(), ConfigError<C::Error, E::Error>> {
|
||||
self.config.delete_data_source(id).await.map_err(ConfigError::Repository)?;
|
||||
self.events.publish(DomainEvent::DataSourceRemoved { id }).await.map_err(ConfigError::Event)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn update_layout(&self, layout: Layout) -> Result<(), ConfigError<C::Error, E::Error>> {
|
||||
self.config.save_layout(&layout).await.map_err(ConfigError::Repository)?;
|
||||
self.events.publish(DomainEvent::LayoutChanged { layout }).await.map_err(ConfigError::Event)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn save_preset(&self, preset: LayoutPreset) -> Result<(), ConfigError<C::Error, E::Error>> {
|
||||
self.config.save_preset(&preset).await.map_err(ConfigError::Repository)?;
|
||||
self.events.publish(DomainEvent::LayoutPresetSaved { id: preset.id }).await.map_err(ConfigError::Event)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn load_preset(&self, id: LayoutPresetId) -> Result<(), ConfigError<C::Error, E::Error>> {
|
||||
let preset = self.config.get_preset(id).await
|
||||
.map_err(ConfigError::Repository)?
|
||||
.ok_or(ConfigError::NotFound)?;
|
||||
|
||||
self.events.publish(DomainEvent::LayoutPresetLoaded { id }).await.map_err(ConfigError::Event)?;
|
||||
|
||||
self.config.save_layout(&preset.layout).await.map_err(ConfigError::Repository)?;
|
||||
self.events.publish(DomainEvent::LayoutChanged { layout: preset.layout }).await.map_err(ConfigError::Event)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete_preset(&self, id: LayoutPresetId) -> Result<(), ConfigError<C::Error, E::Error>> {
|
||||
self.config.delete_preset(id).await.map_err(ConfigError::Repository)?;
|
||||
self.events.publish(DomainEvent::LayoutPresetDeleted { id }).await.map_err(ConfigError::Event)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
42
crates/application/src/data_projection.rs
Normal file
42
crates/application/src/data_projection.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use std::collections::HashMap;
|
||||
use domain::{DataSourceId, Value, WidgetConfig, WidgetId, WidgetState};
|
||||
|
||||
pub struct DataProjection {
|
||||
current: HashMap<WidgetId, WidgetState>,
|
||||
}
|
||||
|
||||
impl DataProjection {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
current: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn apply_poll_result(
|
||||
&mut self,
|
||||
data_source_id: DataSourceId,
|
||||
raw: &Value,
|
||||
widget_configs: &[WidgetConfig],
|
||||
) -> Vec<(WidgetId, WidgetState)> {
|
||||
let mut changed = Vec::new();
|
||||
|
||||
for config in widget_configs {
|
||||
if config.data_source_id != data_source_id {
|
||||
continue;
|
||||
}
|
||||
|
||||
let new_state = config.extract(raw);
|
||||
|
||||
let is_changed = self.current
|
||||
.get(&config.id)
|
||||
.map_or(true, |prev| *prev != new_state);
|
||||
|
||||
if is_changed {
|
||||
self.current.insert(config.id, new_state.clone());
|
||||
changed.push((config.id, new_state));
|
||||
}
|
||||
}
|
||||
|
||||
changed
|
||||
}
|
||||
}
|
||||
5
crates/application/src/lib.rs
Normal file
5
crates/application/src/lib.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
mod config_service;
|
||||
mod data_projection;
|
||||
|
||||
pub use config_service::ConfigService;
|
||||
pub use data_projection::DataProjection;
|
||||
Reference in New Issue
Block a user