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

8
crates/domain/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "domain"
version = "0.1.0"
edition = "2024"
[dependencies]
[dev-dependencies]

View File

@@ -0,0 +1,69 @@
use std::time::Duration;
pub type DataSourceId = u16;
#[derive(Debug, Clone, PartialEq)]
pub enum DataSourceType {
Weather,
Media,
Xtb,
Rss,
HttpJson,
Webhook,
}
#[derive(Debug, Clone)]
pub struct DataSourceConfig {
pub url: Option<String>,
pub headers: Vec<(String, String)>,
pub api_key: Option<String>,
}
#[derive(Debug, Clone)]
pub struct DataSource {
pub id: DataSourceId,
pub name: String,
pub source_type: DataSourceType,
pub poll_interval: Duration,
pub config: DataSourceConfig,
}
#[derive(Debug, Clone, PartialEq)]
pub enum DataSourceValidationError {
UrlRequired,
PollIntervalNotAllowed,
PollIntervalRequired,
}
impl DataSource {
pub fn validate(&self) -> Vec<DataSourceValidationError> {
let mut errors = Vec::new();
let is_webhook = self.source_type == DataSourceType::Webhook;
if is_webhook {
if !self.poll_interval.is_zero() {
errors.push(DataSourceValidationError::PollIntervalNotAllowed);
}
} else {
if self.poll_interval.is_zero() {
errors.push(DataSourceValidationError::PollIntervalRequired);
}
if self.requires_url() && self.config.url.is_none() {
errors.push(DataSourceValidationError::UrlRequired);
}
}
errors
}
fn requires_url(&self) -> bool {
matches!(
self.source_type,
DataSourceType::Weather
| DataSourceType::Media
| DataSourceType::Rss
| DataSourceType::HttpJson
)
}
}

View File

@@ -0,0 +1,10 @@
use crate::value_objects::Layout;
pub type LayoutPresetId = u16;
#[derive(Debug, Clone)]
pub struct LayoutPreset {
pub id: LayoutPresetId,
pub name: String,
pub layout: Layout,
}

View File

@@ -0,0 +1,7 @@
mod widget_config;
mod data_source;
mod layout_preset;
pub use widget_config::{WidgetConfig, WidgetId};
pub use data_source::{DataSource, DataSourceId, DataSourceType, DataSourceConfig, DataSourceValidationError};
pub use layout_preset::{LayoutPreset, LayoutPresetId};

View File

@@ -0,0 +1,70 @@
use std::collections::BTreeMap;
use crate::value_objects::{DisplayHint, KeyMapping, Value, WidgetState};
pub type WidgetId = u16;
pub type DataSourceId = u16;
const DEFAULT_MAX_DATA_SIZE: u16 = 2048;
#[derive(Debug, Clone)]
pub struct WidgetConfig {
pub id: WidgetId,
pub name: String,
pub display_hint: DisplayHint,
pub data_source_id: DataSourceId,
pub mappings: Vec<KeyMapping>,
pub max_data_size: u16,
}
impl WidgetConfig {
pub fn new(
id: WidgetId,
name: String,
display_hint: DisplayHint,
data_source_id: DataSourceId,
mappings: Vec<KeyMapping>,
) -> Self {
Self {
id,
name,
display_hint,
data_source_id,
mappings,
max_data_size: DEFAULT_MAX_DATA_SIZE,
}
}
pub fn extract(&self, raw: &Value) -> WidgetState {
let budget = self.max_data_size as usize;
let mut used = 0usize;
let mut data = BTreeMap::new();
for mapping in &self.mappings {
if let Some((key, value)) = mapping.extract(raw) {
let key_cost = key.len();
let remaining = budget.saturating_sub(used + key_cost);
let value = Self::truncate_value(value, remaining);
used += key_cost + value.estimated_size();
data.insert(key, value);
if used >= budget {
break;
}
}
}
WidgetState { data, error: None }
}
fn truncate_value(value: Value, max_bytes: usize) -> Value {
match value {
Value::String(s) if s.len() > max_bytes => {
let truncated: String = s.char_indices()
.take_while(|(i, _)| *i < max_bytes)
.map(|(_, c)| c)
.collect();
Value::String(truncated)
}
other => other,
}
}
}

View File

@@ -0,0 +1,16 @@
use crate::entities::{DataSourceId, LayoutPresetId, WidgetId};
use crate::value_objects::Layout;
#[derive(Debug, Clone)]
pub enum DomainEvent {
WidgetCreated { id: WidgetId },
WidgetUpdated { id: WidgetId },
WidgetDeleted { id: WidgetId },
DataSourceAdded { id: DataSourceId },
DataSourceUpdated { id: DataSourceId },
DataSourceRemoved { id: DataSourceId },
LayoutChanged { layout: Layout },
LayoutPresetSaved { id: LayoutPresetId },
LayoutPresetLoaded { id: LayoutPresetId },
LayoutPresetDeleted { id: LayoutPresetId },
}

19
crates/domain/src/lib.rs Normal file
View File

@@ -0,0 +1,19 @@
#![allow(async_fn_in_trait)]
pub mod entities;
pub mod value_objects;
pub mod events;
pub mod ports;
pub use entities::{
WidgetConfig, WidgetId,
DataSource, DataSourceId, DataSourceType, DataSourceConfig, DataSourceValidationError,
LayoutPreset, LayoutPresetId,
};
pub use value_objects::{
Value, KeyMapping,
WidgetState, WidgetError, DisplayHint,
Layout, LayoutNode, LayoutChild, ContainerNode, Direction, Sizing, LayoutValidationError,
};
pub use events::DomainEvent;
pub use ports::{ConfigRepository, DataSourcePort, BroadcastPort, EventPublisher};

View File

@@ -0,0 +1,17 @@
use crate::entities::WidgetId;
use crate::value_objects::{Layout, WidgetState};
pub trait BroadcastPort {
type Error;
async fn push_screen_update(
&self,
layout: &Layout,
widgets: &[(WidgetId, WidgetState)],
) -> Result<(), Self::Error>;
async fn push_data_update(
&self,
updates: &[(WidgetId, WidgetState)],
) -> Result<(), Self::Error>;
}

View File

@@ -0,0 +1,26 @@
use crate::entities::{
DataSource, DataSourceId, LayoutPreset, LayoutPresetId, WidgetConfig, WidgetId,
};
use crate::value_objects::Layout;
pub trait ConfigRepository {
type Error;
async fn get_widget(&self, id: WidgetId) -> Result<Option<WidgetConfig>, Self::Error>;
async fn list_widgets(&self) -> Result<Vec<WidgetConfig>, Self::Error>;
async fn save_widget(&self, config: &WidgetConfig) -> Result<(), Self::Error>;
async fn delete_widget(&self, id: WidgetId) -> Result<(), Self::Error>;
async fn get_data_source(&self, id: DataSourceId) -> Result<Option<DataSource>, Self::Error>;
async fn list_data_sources(&self) -> Result<Vec<DataSource>, Self::Error>;
async fn save_data_source(&self, source: &DataSource) -> Result<(), Self::Error>;
async fn delete_data_source(&self, id: DataSourceId) -> Result<(), Self::Error>;
async fn get_layout(&self) -> Result<Option<Layout>, Self::Error>;
async fn save_layout(&self, layout: &Layout) -> Result<(), Self::Error>;
async fn get_preset(&self, id: LayoutPresetId) -> Result<Option<LayoutPreset>, Self::Error>;
async fn list_presets(&self) -> Result<Vec<LayoutPreset>, Self::Error>;
async fn save_preset(&self, preset: &LayoutPreset) -> Result<(), Self::Error>;
async fn delete_preset(&self, id: LayoutPresetId) -> Result<(), Self::Error>;
}

View File

@@ -0,0 +1,8 @@
use crate::entities::DataSource;
use crate::value_objects::Value;
pub trait DataSourcePort {
type Error;
async fn poll(&self, source: &DataSource) -> Result<Value, Self::Error>;
}

View File

@@ -0,0 +1,7 @@
use crate::events::DomainEvent;
pub trait EventPublisher {
type Error;
async fn publish(&self, event: DomainEvent) -> Result<(), Self::Error>;
}

View File

@@ -0,0 +1,9 @@
mod config_repository;
mod data_source_port;
mod broadcast;
mod event;
pub use config_repository::ConfigRepository;
pub use data_source_port::DataSourcePort;
pub use broadcast::BroadcastPort;
pub use event::EventPublisher;

View File

@@ -0,0 +1,14 @@
use super::Value;
#[derive(Debug, Clone, PartialEq)]
pub struct KeyMapping {
pub source_path: String,
pub target_key: String,
}
impl KeyMapping {
pub fn extract(&self, raw: &Value) -> Option<(String, Value)> {
let value = raw.get_path(&self.source_path)?;
Some((self.target_key.clone(), value.clone()))
}
}

View File

@@ -0,0 +1,92 @@
use std::collections::BTreeSet;
use crate::entities::WidgetId;
#[derive(Debug, Clone, PartialEq)]
pub enum Sizing {
Fixed(u16),
Flex(u8),
}
#[derive(Debug, Clone, PartialEq)]
pub enum Direction {
Row,
Column,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ContainerNode {
pub direction: Direction,
pub gap: u8,
pub padding: u8,
pub children: Vec<LayoutChild>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct LayoutChild {
pub sizing: Sizing,
pub node: LayoutNode,
}
#[derive(Debug, Clone, PartialEq)]
pub enum LayoutNode {
Container(ContainerNode),
Leaf(WidgetId),
}
#[derive(Debug, Clone, PartialEq)]
pub struct Layout {
pub root: LayoutNode,
}
#[derive(Debug, Clone, PartialEq)]
pub enum LayoutValidationError {
UnknownWidget(WidgetId),
EmptyContainer,
}
impl Layout {
pub fn validate(&self, known_widgets: &BTreeSet<WidgetId>) -> Vec<LayoutValidationError> {
let mut errors = Vec::new();
Self::validate_node(&self.root, known_widgets, &mut errors);
errors
}
fn validate_node(
node: &LayoutNode,
known: &BTreeSet<WidgetId>,
errors: &mut Vec<LayoutValidationError>,
) {
match node {
LayoutNode::Leaf(id) => {
if !known.contains(id) {
errors.push(LayoutValidationError::UnknownWidget(*id));
}
}
LayoutNode::Container(c) => {
if c.children.is_empty() {
errors.push(LayoutValidationError::EmptyContainer);
}
for child in &c.children {
Self::validate_node(&child.node, known, errors);
}
}
}
}
pub fn widget_ids(&self) -> BTreeSet<WidgetId> {
let mut ids = BTreeSet::new();
Self::collect_ids(&self.root, &mut ids);
ids
}
fn collect_ids(node: &LayoutNode, ids: &mut BTreeSet<WidgetId>) {
match node {
LayoutNode::Leaf(id) => { ids.insert(*id); }
LayoutNode::Container(c) => {
for child in &c.children {
Self::collect_ids(&child.node, ids);
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
mod value;
mod key_mapping;
mod widget_state;
mod layout;
pub use value::Value;
pub use key_mapping::KeyMapping;
pub use widget_state::{WidgetState, WidgetError, DisplayHint};
pub use layout::{
Layout, LayoutNode, LayoutChild, ContainerNode, Direction, Sizing, LayoutValidationError,
};

View File

@@ -0,0 +1,58 @@
use std::collections::BTreeMap;
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
Null,
Bool(bool),
Number(f64),
String(String),
Array(Vec<Value>),
Object(BTreeMap<String, Value>),
}
impl Value {
pub fn estimated_size(&self) -> usize {
match self {
Value::Null | Value::Bool(_) => 1,
Value::Number(_) => 8,
Value::String(s) => s.len(),
Value::Array(arr) => arr.iter().map(|v| v.estimated_size()).sum(),
Value::Object(map) => map
.iter()
.map(|(k, v)| k.len() + v.estimated_size())
.sum(),
}
}
pub fn get_path(&self, path: &str) -> Option<&Value> {
let path = path.strip_prefix("$").unwrap_or(path);
let mut current = self;
for raw_segment in path.split('.').filter(|s| !s.is_empty()) {
if let Some(bracket_pos) = raw_segment.find('[') {
let key = &raw_segment[..bracket_pos];
let index_str = raw_segment[bracket_pos + 1..].strip_suffix(']')?;
let index: usize = index_str.parse().ok()?;
if !key.is_empty() {
match current {
Value::Object(map) => current = map.get(key)?,
_ => return None,
}
}
match current {
Value::Array(arr) => current = arr.get(index)?,
_ => return None,
}
} else {
match current {
Value::Object(map) => current = map.get(raw_segment)?,
_ => return None,
}
}
}
Some(current)
}
}

View File

@@ -0,0 +1,21 @@
use std::collections::BTreeMap;
use super::Value;
#[derive(Debug, Clone, PartialEq)]
pub struct WidgetState {
pub data: BTreeMap<String, Value>,
pub error: Option<WidgetError>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum WidgetError {
SourceUnavailable,
ExtractionFailed,
}
#[derive(Debug, Clone, PartialEq)]
pub enum DisplayHint {
IconValue,
TextBlock,
KeyValue,
}

View File

@@ -0,0 +1,51 @@
use std::time::Duration;
use domain::{DataSource, DataSourceConfig, DataSourceType, DataSourceValidationError};
fn make_source(source_type: DataSourceType, url: Option<&str>, poll: Duration) -> DataSource {
DataSource {
id: 1,
name: "test".into(),
source_type,
poll_interval: poll,
config: DataSourceConfig {
url: url.map(Into::into),
headers: vec![],
api_key: None,
},
}
}
#[test]
fn http_json_requires_url() {
let source = make_source(DataSourceType::HttpJson, None, Duration::from_secs(60));
let errors = source.validate();
assert!(errors.contains(&DataSourceValidationError::UrlRequired));
}
#[test]
fn webhook_does_not_allow_poll_interval() {
let source = make_source(DataSourceType::Webhook, None, Duration::from_secs(60));
let errors = source.validate();
assert!(errors.contains(&DataSourceValidationError::PollIntervalNotAllowed));
}
#[test]
fn webhook_with_zero_interval_is_valid() {
let source = make_source(DataSourceType::Webhook, None, Duration::ZERO);
let errors = source.validate();
assert!(errors.is_empty());
}
#[test]
fn poll_based_source_requires_nonzero_interval() {
let source = make_source(DataSourceType::Weather, Some("https://api.weather.com"), Duration::ZERO);
let errors = source.validate();
assert!(errors.contains(&DataSourceValidationError::PollIntervalRequired));
}
#[test]
fn valid_poll_source_has_no_errors() {
let source = make_source(DataSourceType::Rss, Some("https://feed.example.com"), Duration::from_secs(300));
let errors = source.validate();
assert!(errors.is_empty());
}

View File

@@ -0,0 +1,33 @@
use std::collections::BTreeMap;
use domain::{KeyMapping, Value};
#[test]
fn extracts_value_at_path_and_renames_key() {
let mapping = KeyMapping {
source_path: "$.main.temp".into(),
target_key: "temperature".into(),
};
let raw = Value::Object(BTreeMap::from([
("main".into(), Value::Object(BTreeMap::from([
("temp".into(), Value::Number(5.4)),
]))),
]));
let result = mapping.extract(&raw);
assert_eq!(result, Some(("temperature".into(), Value::Number(5.4))));
}
#[test]
fn returns_none_when_path_does_not_match() {
let mapping = KeyMapping {
source_path: "$.missing.path".into(),
target_key: "value".into(),
};
let raw = Value::Object(BTreeMap::from([
("other".into(), Value::Number(1.0)),
]));
assert_eq!(mapping.extract(&raw), None);
}

View File

@@ -0,0 +1,66 @@
use std::collections::BTreeSet;
use domain::{
ContainerNode, Direction, Layout, LayoutChild, LayoutNode, LayoutValidationError, Sizing,
WidgetId,
};
fn leaf(id: WidgetId) -> LayoutChild {
LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(id),
}
}
fn row(children: Vec<LayoutChild>) -> LayoutNode {
LayoutNode::Container(ContainerNode {
direction: Direction::Row,
gap: 0,
padding: 0,
children,
})
}
#[test]
fn validate_returns_empty_when_all_widgets_exist() {
let layout = Layout { root: row(vec![leaf(1), leaf(2)]) };
let known = BTreeSet::from([1, 2]);
assert!(layout.validate(&known).is_empty());
}
#[test]
fn validate_reports_unknown_widget_ids() {
let layout = Layout { root: row(vec![leaf(1), leaf(99)]) };
let known = BTreeSet::from([1]);
let errors = layout.validate(&known);
assert_eq!(errors, vec![LayoutValidationError::UnknownWidget(99)]);
}
#[test]
fn validate_reports_empty_container() {
let layout = Layout { root: row(vec![]) };
let known = BTreeSet::new();
let errors = layout.validate(&known);
assert_eq!(errors, vec![LayoutValidationError::EmptyContainer]);
}
#[test]
fn validate_checks_nested_containers() {
let inner = LayoutChild {
sizing: Sizing::Flex(1),
node: row(vec![leaf(1), leaf(42)]),
};
let layout = Layout { root: row(vec![inner, leaf(2)]) };
let known = BTreeSet::from([1, 2]);
let errors = layout.validate(&known);
assert_eq!(errors, vec![LayoutValidationError::UnknownWidget(42)]);
}
#[test]
fn widget_ids_collects_all_leaf_ids() {
let inner = LayoutChild {
sizing: Sizing::Flex(1),
node: row(vec![leaf(3)]),
};
let layout = Layout { root: row(vec![leaf(1), inner, leaf(2)]) };
assert_eq!(layout.widget_ids(), BTreeSet::from([1, 2, 3]));
}

View File

@@ -0,0 +1,77 @@
use std::collections::BTreeMap;
use domain::Value;
#[test]
fn estimated_size_of_string_is_its_byte_length() {
let v = Value::String("hello".into());
assert_eq!(v.estimated_size(), 5);
}
#[test]
fn estimated_size_of_number_is_8_bytes() {
assert_eq!(Value::Number(3.14).estimated_size(), 8);
}
#[test]
fn estimated_size_of_null_and_bool_is_1() {
assert_eq!(Value::Null.estimated_size(), 1);
assert_eq!(Value::Bool(true).estimated_size(), 1);
}
#[test]
fn estimated_size_of_nested_structure_sums_recursively() {
let v = Value::Object(BTreeMap::from([
("key".into(), Value::String("value".into())),
("num".into(), Value::Number(1.0)),
]));
assert_eq!(v.estimated_size(), 19);
}
#[test]
fn estimated_size_of_array_sums_elements() {
let v = Value::Array(vec![
Value::String("abc".into()),
Value::Number(1.0),
]);
assert_eq!(v.estimated_size(), 11);
}
#[test]
fn get_path_returns_none_for_missing_key() {
let data = Value::Object(BTreeMap::from([
("main".into(), Value::Number(1.0)),
]));
assert_eq!(data.get_path("$.missing"), None);
}
#[test]
fn get_path_returns_none_when_traversing_non_object() {
let data = Value::Object(BTreeMap::from([
("temp".into(), Value::Number(5.4)),
]));
assert_eq!(data.get_path("$.temp.nested"), None);
}
#[test]
fn get_path_accesses_array_by_index() {
let data = Value::Object(BTreeMap::from([
("items".into(), Value::Array(vec![
Value::String("first".into()),
Value::String("second".into()),
])),
]));
assert_eq!(
data.get_path("$.items[1]"),
Some(&Value::String("second".into()))
);
}
#[test]
fn get_path_traverses_nested_object() {
let data = Value::Object(BTreeMap::from([
("main".into(), Value::Object(BTreeMap::from([
("temp".into(), Value::Number(5.4)),
]))),
]));
assert_eq!(data.get_path("$.main.temp"), Some(&Value::Number(5.4)));
}

View File

@@ -0,0 +1,110 @@
use std::collections::BTreeMap;
use domain::{DisplayHint, KeyMapping, Value, WidgetConfig};
#[test]
fn extract_applies_all_mappings_to_produce_widget_state() {
let config = WidgetConfig {
id: 1,
name: "weather".into(),
display_hint: DisplayHint::IconValue,
data_source_id: 1,
mappings: vec![
KeyMapping { source_path: "$.main.temp".into(), target_key: "temperature".into() },
KeyMapping { source_path: "$.weather[0].icon".into(), target_key: "icon".into() },
],
max_data_size: 2048,
};
let raw = Value::Object(BTreeMap::from([
("main".into(), Value::Object(BTreeMap::from([
("temp".into(), Value::Number(5.4)),
]))),
("weather".into(), Value::Array(vec![
Value::Object(BTreeMap::from([
("icon".into(), Value::String("cloud_rain".into())),
])),
])),
]));
let state = config.extract(&raw);
assert_eq!(state.data.get("temperature"), Some(&Value::Number(5.4)));
assert_eq!(state.data.get("icon"), Some(&Value::String("cloud_rain".into())));
assert_eq!(state.error, None);
}
#[test]
fn extract_truncates_string_values_exceeding_max_data_size() {
let long_text = "a".repeat(3000);
let config = WidgetConfig {
id: 1,
name: "news".into(),
display_hint: DisplayHint::TextBlock,
data_source_id: 1,
mappings: vec![
KeyMapping { source_path: "$.text".into(), target_key: "body".into() },
],
max_data_size: 100,
};
let raw = Value::Object(BTreeMap::from([
("text".into(), Value::String(long_text)),
]));
let state = config.extract(&raw);
match state.data.get("body") {
Some(Value::String(s)) => assert!(s.len() <= 100),
other => panic!("expected truncated string, got {:?}", other),
}
}
#[test]
fn extract_respects_max_data_size_across_total_state() {
let config = WidgetConfig {
id: 1,
name: "big".into(),
display_hint: DisplayHint::TextBlock,
data_source_id: 1,
mappings: vec![
KeyMapping { source_path: "$.a".into(), target_key: "a".into() },
KeyMapping { source_path: "$.b".into(), target_key: "b".into() },
KeyMapping { source_path: "$.c".into(), target_key: "c".into() },
],
max_data_size: 50,
};
let raw = Value::Object(BTreeMap::from([
("a".into(), Value::String("x".repeat(20))),
("b".into(), Value::String("y".repeat(20))),
("c".into(), Value::String("z".repeat(20))),
]));
let state = config.extract(&raw);
let total: usize = state.data.values().map(|v| v.estimated_size()).sum();
assert!(total <= 50, "total size {total} exceeds max 50");
}
#[test]
fn extract_skips_mappings_that_dont_match() {
let config = WidgetConfig {
id: 1,
name: "weather".into(),
display_hint: DisplayHint::IconValue,
data_source_id: 1,
mappings: vec![
KeyMapping { source_path: "$.temp".into(), target_key: "temperature".into() },
KeyMapping { source_path: "$.missing".into(), target_key: "gone".into() },
],
max_data_size: 2048,
};
let raw = Value::Object(BTreeMap::from([
("temp".into(), Value::Number(5.4)),
]));
let state = config.extract(&raw);
assert_eq!(state.data.len(), 1);
assert_eq!(state.data.get("temperature"), Some(&Value::Number(5.4)));
assert_eq!(state.data.get("gone"), None);
}