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:
2026-06-18 21:43:59 +02:00
parent 6ad76b98a2
commit 557cceb498
83 changed files with 5844 additions and 1 deletions

View File

@@ -0,0 +1,10 @@
[package]
name = "application"
version = "0.1.0"
edition = "2024"
[dependencies]
domain.workspace = true
[dev-dependencies]
tokio = { workspace = true }

View 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(())
}
}

View 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
}
}

View File

@@ -0,0 +1,5 @@
mod config_service;
mod data_projection;
pub use config_service::ConfigService;
pub use data_projection::DataProjection;

View File

@@ -0,0 +1,151 @@
mod support;
use std::time::Duration;
use domain::{
ConfigRepository, DisplayHint, DomainEvent, KeyMapping, WidgetConfig,
DataSource, DataSourceConfig, DataSourceType,
Layout, LayoutNode, ContainerNode, LayoutChild, Direction, Sizing,
LayoutPreset,
};
use application::ConfigService;
use support::{InMemoryConfigRepository, InMemoryEventPublisher};
#[tokio::test]
async fn create_widget_persists_and_emits_event() {
let repo = InMemoryConfigRepository::new();
let events = InMemoryEventPublisher::new();
let service = ConfigService::new(&repo, &events);
let config = WidgetConfig::new(
1,
"weather".into(),
DisplayHint::IconValue,
1,
vec![
KeyMapping { source_path: "$.temp".into(), target_key: "temperature".into() },
],
);
service.create_widget(config).await.unwrap();
let stored = repo.get_widget(1).await.unwrap();
assert!(stored.is_some());
let emitted = events.emitted();
assert_eq!(emitted.len(), 1);
assert!(matches!(emitted[0], DomainEvent::WidgetCreated { id: 1 }));
}
#[tokio::test]
async fn create_data_source_rejects_invalid() {
let repo = InMemoryConfigRepository::new();
let events = InMemoryEventPublisher::new();
let service = ConfigService::new(&repo, &events);
let source = DataSource {
id: 1,
name: "bad".into(),
source_type: DataSourceType::HttpJson,
poll_interval: Duration::from_secs(60),
config: DataSourceConfig {
url: None,
headers: vec![],
api_key: None,
},
};
let result = service.create_data_source(source).await;
assert!(result.is_err());
assert!(events.emitted().is_empty());
}
#[tokio::test]
async fn create_data_source_persists_valid_and_emits_event() {
let repo = InMemoryConfigRepository::new();
let events = InMemoryEventPublisher::new();
let service = ConfigService::new(&repo, &events);
let source = DataSource {
id: 1,
name: "weather".into(),
source_type: DataSourceType::Weather,
poll_interval: Duration::from_secs(300),
config: DataSourceConfig {
url: Some("https://api.weather.com".into()),
headers: vec![],
api_key: None,
},
};
service.create_data_source(source).await.unwrap();
let stored = repo.get_data_source(1).await.unwrap();
assert!(stored.is_some());
let emitted = events.emitted();
assert_eq!(emitted.len(), 1);
assert!(matches!(emitted[0], DomainEvent::DataSourceAdded { id: 1 }));
}
#[tokio::test]
async fn update_layout_persists_and_emits_event() {
let repo = InMemoryConfigRepository::new();
let events = InMemoryEventPublisher::new();
let service = ConfigService::new(&repo, &events);
let layout = Layout {
root: LayoutNode::Container(ContainerNode {
direction: Direction::Row,
gap: 4,
padding: 2,
children: vec![
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(1) },
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(2) },
],
}),
};
service.update_layout(layout.clone()).await.unwrap();
let stored = repo.get_layout().await.unwrap();
assert_eq!(stored, Some(layout));
assert_eq!(events.emitted().len(), 1);
assert!(matches!(events.emitted()[0], DomainEvent::LayoutChanged { .. }));
}
#[tokio::test]
async fn load_preset_replaces_active_layout() {
let repo = InMemoryConfigRepository::new();
let events = InMemoryEventPublisher::new();
let service = ConfigService::new(&repo, &events);
let preset_layout = Layout {
root: LayoutNode::Container(ContainerNode {
direction: Direction::Column,
gap: 0,
padding: 0,
children: vec![
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(5) },
],
}),
};
let preset = LayoutPreset {
id: 1,
name: "vertical".into(),
layout: preset_layout.clone(),
};
repo.save_preset(&preset).await.unwrap();
service.load_preset(1).await.unwrap();
let stored = repo.get_layout().await.unwrap();
assert_eq!(stored, Some(preset_layout));
let emitted = events.emitted();
assert_eq!(emitted.len(), 2);
assert!(matches!(emitted[0], DomainEvent::LayoutPresetLoaded { id: 1 }));
assert!(matches!(emitted[1], DomainEvent::LayoutChanged { .. }));
}

View File

@@ -0,0 +1,82 @@
use std::collections::BTreeMap;
use domain::{
DisplayHint, KeyMapping, Value, WidgetConfig, WidgetId, WidgetState,
};
use application::DataProjection;
fn weather_widget() -> WidgetConfig {
WidgetConfig::new(
1,
"weather".into(),
DisplayHint::IconValue,
10,
vec![
KeyMapping { source_path: "$.temp".into(), target_key: "temperature".into() },
KeyMapping { source_path: "$.icon".into(), target_key: "icon".into() },
],
)
}
fn weather_response(temp: f64) -> Value {
Value::Object(BTreeMap::from([
("temp".into(), Value::Number(temp)),
("icon".into(), Value::String("sunny".into())),
]))
}
#[test]
fn apply_poll_result_detects_new_widget_state() {
let mut projection = DataProjection::new();
let widgets = vec![weather_widget()];
let changed = projection.apply_poll_result(10, &weather_response(5.4), &widgets);
assert_eq!(changed.len(), 1);
assert_eq!(changed[0].0, 1);
assert_eq!(changed[0].1.data.get("temperature"), Some(&Value::Number(5.4)));
}
#[test]
fn apply_poll_result_returns_empty_when_nothing_changed() {
let mut projection = DataProjection::new();
let widgets = vec![weather_widget()];
projection.apply_poll_result(10, &weather_response(5.4), &widgets);
let changed = projection.apply_poll_result(10, &weather_response(5.4), &widgets);
assert!(changed.is_empty());
}
#[test]
fn apply_poll_result_detects_changed_value() {
let mut projection = DataProjection::new();
let widgets = vec![weather_widget()];
projection.apply_poll_result(10, &weather_response(5.4), &widgets);
let changed = projection.apply_poll_result(10, &weather_response(6.1), &widgets);
assert_eq!(changed.len(), 1);
assert_eq!(changed[0].1.data.get("temperature"), Some(&Value::Number(6.1)));
}
#[test]
fn apply_poll_result_only_updates_widgets_bound_to_source() {
let mut projection = DataProjection::new();
let widgets = vec![
weather_widget(),
WidgetConfig::new(
2,
"portfolio".into(),
DisplayHint::KeyValue,
20,
vec![
KeyMapping { source_path: "$.value".into(), target_key: "amount".into() },
],
),
];
let changed = projection.apply_poll_result(10, &weather_response(5.4), &widgets);
assert_eq!(changed.len(), 1);
assert_eq!(changed[0].0, 1);
}

View File

@@ -0,0 +1,126 @@
use std::cell::RefCell;
use std::collections::HashMap;
use domain::{
ConfigRepository, EventPublisher,
DataSource, DataSourceId, Layout, LayoutPreset, LayoutPresetId,
WidgetConfig, WidgetId, DomainEvent,
};
pub struct InMemoryConfigRepository {
pub widgets: RefCell<HashMap<WidgetId, WidgetConfig>>,
pub data_sources: RefCell<HashMap<DataSourceId, DataSource>>,
pub layout: RefCell<Option<Layout>>,
pub presets: RefCell<HashMap<LayoutPresetId, LayoutPreset>>,
}
impl InMemoryConfigRepository {
pub fn new() -> Self {
Self {
widgets: RefCell::new(HashMap::new()),
data_sources: RefCell::new(HashMap::new()),
layout: RefCell::new(None),
presets: RefCell::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.borrow().get(&id).cloned())
}
async fn list_widgets(&self) -> Result<Vec<WidgetConfig>, Self::Error> {
Ok(self.widgets.borrow().values().cloned().collect())
}
async fn save_widget(&self, config: &WidgetConfig) -> Result<(), Self::Error> {
self.widgets.borrow_mut().insert(config.id, config.clone());
Ok(())
}
async fn delete_widget(&self, id: WidgetId) -> Result<(), Self::Error> {
self.widgets.borrow_mut().remove(&id);
Ok(())
}
async fn get_data_source(&self, id: DataSourceId) -> Result<Option<DataSource>, Self::Error> {
Ok(self.data_sources.borrow().get(&id).cloned())
}
async fn list_data_sources(&self) -> Result<Vec<DataSource>, Self::Error> {
Ok(self.data_sources.borrow().values().cloned().collect())
}
async fn save_data_source(&self, source: &DataSource) -> Result<(), Self::Error> {
self.data_sources.borrow_mut().insert(source.id, source.clone());
Ok(())
}
async fn delete_data_source(&self, id: DataSourceId) -> Result<(), Self::Error> {
self.data_sources.borrow_mut().remove(&id);
Ok(())
}
async fn get_layout(&self) -> Result<Option<Layout>, Self::Error> {
Ok(self.layout.borrow().clone())
}
async fn save_layout(&self, layout: &Layout) -> Result<(), Self::Error> {
*self.layout.borrow_mut() = Some(layout.clone());
Ok(())
}
async fn get_preset(&self, id: LayoutPresetId) -> Result<Option<LayoutPreset>, Self::Error> {
Ok(self.presets.borrow().get(&id).cloned())
}
async fn list_presets(&self) -> Result<Vec<LayoutPreset>, Self::Error> {
Ok(self.presets.borrow().values().cloned().collect())
}
async fn save_preset(&self, preset: &LayoutPreset) -> Result<(), Self::Error> {
self.presets.borrow_mut().insert(preset.id, preset.clone());
Ok(())
}
async fn delete_preset(&self, id: LayoutPresetId) -> Result<(), Self::Error> {
self.presets.borrow_mut().remove(&id);
Ok(())
}
}
pub struct InMemoryEventPublisher {
pub events: RefCell<Vec<DomainEvent>>,
}
impl InMemoryEventPublisher {
pub fn new() -> Self {
Self {
events: RefCell::new(Vec::new()),
}
}
pub fn emitted(&self) -> Vec<DomainEvent> {
self.events.borrow().clone()
}
}
impl EventPublisher for InMemoryEventPublisher {
type Error = Never;
async fn publish(&self, event: DomainEvent) -> Result<(), Self::Error> {
self.events.borrow_mut().push(event);
Ok(())
}
}