add SPA config UI, wire media/rss adapters, event-driven layout push

- React SPA: dashboard, data sources CRUD, widgets CRUD, layout builder,
  presets. TanStack Router + Query, shadcn/ui, Vite proxy to :3000
- wire media + rss adapters into polling loop, remove xtb source type
- media adapter: read username/password from headers, proper subsonic auth
- event handler: subscribe to LayoutChanged, push screen update to clients
- fix clippy warnings across workspace (Default impls, collapsible ifs,
  redundant closures, is_none_or, unused imports)
This commit is contained in:
2026-06-19 00:12:42 +02:00
parent 21c08911df
commit 26ebfad3a2
175 changed files with 12338 additions and 801 deletions

10
Cargo.lock generated
View File

@@ -166,6 +166,8 @@ dependencies = [
"dotenvy",
"http-api",
"http-json",
"media-adapter",
"rss-adapter",
"tcp-server",
"tokio",
"tracing",
@@ -1102,12 +1104,20 @@ dependencies = [
"digest",
]
[[package]]
name = "md5"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771"
[[package]]
name = "media-adapter"
version = "0.1.0"
dependencies = [
"axum",
"domain",
"fastrand",
"md5",
"reqwest",
"serde_json",
"thiserror",

View File

@@ -36,6 +36,8 @@ display-terminal = { path = "crates/adapters/display-terminal" }
config-sqlite = { path = "crates/adapters/config-sqlite" }
http-json = { path = "crates/adapters/http-json" }
http-api = { path = "crates/adapters/http-api" }
media-adapter = { path = "crates/adapters/media" }
rss-adapter = { path = "crates/adapters/rss" }
axum = { version = "0.8", features = ["macros"] }
tower-http = { version = "0.6", features = ["cors"] }
api-types = { path = "crates/api-types" }

View File

@@ -1,10 +1,9 @@
use domain::{
ConfigRepository, DataSource, DataSourceId, Layout, LayoutPreset, LayoutPresetId, WidgetConfig,
WidgetId,
};
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 {
@@ -19,8 +18,8 @@ pub struct MemoryConfigStore {
presets: RwLock<HashMap<LayoutPresetId, LayoutPreset>>,
}
impl MemoryConfigStore {
pub fn new() -> Self {
impl Default for MemoryConfigStore {
fn default() -> Self {
Self {
widgets: RwLock::new(HashMap::new()),
data_sources: RwLock::new(HashMap::new()),
@@ -30,82 +29,130 @@ impl MemoryConfigStore {
}
}
impl MemoryConfigStore {
pub fn new() -> Self {
Self::default()
}
}
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)?;
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)?;
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)?;
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)?;
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)?;
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)?;
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)?;
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)?;
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)?;
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)?;
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)?;
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)?;
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)?;
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)?;
let mut guard = self
.presets
.write()
.map_err(|_| MemoryConfigError::LockPoisoned)?;
guard.remove(&id);
Ok(())
}

View File

@@ -1,6 +1,6 @@
pub mod error;
mod serialization;
mod repository;
mod serialization;
use sqlx::SqlitePool;
@@ -27,8 +27,10 @@ impl SqliteConfigStore {
data_source_id INTEGER NOT NULL,
mappings TEXT NOT NULL,
max_data_size INTEGER NOT NULL
)"
).execute(&self.pool).await?;
)",
)
.execute(&self.pool)
.await?;
sqlx::query(
"CREATE TABLE IF NOT EXISTS data_sources (
@@ -37,23 +39,29 @@ impl SqliteConfigStore {
source_type TEXT NOT NULL,
poll_interval_secs INTEGER NOT NULL,
config TEXT NOT NULL
)"
).execute(&self.pool).await?;
)",
)
.execute(&self.pool)
.await?;
sqlx::query(
"CREATE TABLE IF NOT EXISTS layout (
id INTEGER PRIMARY KEY CHECK (id = 1),
data TEXT NOT NULL
)"
).execute(&self.pool).await?;
)",
)
.execute(&self.pool)
.await?;
sqlx::query(
"CREATE TABLE IF NOT EXISTS presets (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
layout_data TEXT NOT NULL
)"
).execute(&self.pool).await?;
)",
)
.execute(&self.pool)
.await?;
Ok(())
}

View File

@@ -1,10 +1,13 @@
use domain::{DataSource, DataSourceId};
use crate::SqliteConfigStore;
use crate::error::SqliteConfigError;
use crate::serialization::data_source as ser;
use domain::{DataSource, DataSourceId};
impl SqliteConfigStore {
pub(crate) async fn get_data_source_impl(&self, id: DataSourceId) -> Result<Option<DataSource>, SqliteConfigError> {
pub(crate) async fn get_data_source_impl(
&self,
id: DataSourceId,
) -> Result<Option<DataSource>, SqliteConfigError> {
let row = sqlx::query("SELECT * FROM data_sources WHERE id = ?")
.bind(id as i64)
.fetch_optional(&self.pool)
@@ -17,16 +20,21 @@ impl SqliteConfigStore {
}
}
pub(crate) async fn list_data_sources_impl(&self) -> Result<Vec<DataSource>, SqliteConfigError> {
pub(crate) async fn list_data_sources_impl(
&self,
) -> Result<Vec<DataSource>, SqliteConfigError> {
let rows = sqlx::query("SELECT * FROM data_sources")
.fetch_all(&self.pool)
.await
.map_err(SqliteConfigError::Sql)?;
rows.iter().map(|r| ser::data_source_from_row(r)).collect()
rows.iter().map(ser::data_source_from_row).collect()
}
pub(crate) async fn save_data_source_impl(&self, source: &DataSource) -> Result<(), SqliteConfigError> {
pub(crate) async fn save_data_source_impl(
&self,
source: &DataSource,
) -> Result<(), SqliteConfigError> {
let config_json = ser::data_source_config_to_json(&source.config)?;
let type_str = ser::data_source_type_to_str(&source.source_type);
@@ -46,7 +54,10 @@ impl SqliteConfigStore {
Ok(())
}
pub(crate) async fn delete_data_source_impl(&self, id: DataSourceId) -> Result<(), SqliteConfigError> {
pub(crate) async fn delete_data_source_impl(
&self,
id: DataSourceId,
) -> Result<(), SqliteConfigError> {
sqlx::query("DELETE FROM data_sources WHERE id = ?")
.bind(id as i64)
.execute(&self.pool)

View File

@@ -1,8 +1,8 @@
use sqlx::Row;
use domain::Layout;
use crate::SqliteConfigStore;
use crate::error::SqliteConfigError;
use crate::serialization::layout as ser;
use domain::Layout;
use sqlx::Row;
impl SqliteConfigStore {
pub(crate) async fn get_layout_impl(&self) -> Result<Option<Layout>, SqliteConfigError> {
@@ -23,13 +23,11 @@ impl SqliteConfigStore {
pub(crate) async fn save_layout_impl(&self, layout: &Layout) -> Result<(), SqliteConfigError> {
let json = ser::layout_to_json(layout)?;
sqlx::query(
"INSERT OR REPLACE INTO layout (id, data) VALUES (1, ?)"
)
.bind(&json)
.execute(&self.pool)
.await
.map_err(SqliteConfigError::Sql)?;
sqlx::query("INSERT OR REPLACE INTO layout (id, data) VALUES (1, ?)")
.bind(&json)
.execute(&self.pool)
.await
.map_err(SqliteConfigError::Sql)?;
Ok(())
}

View File

@@ -1,16 +1,14 @@
mod widgets;
mod data_sources;
mod layout;
mod presets;
mod widgets;
use domain::{
ConfigRepository,
DataSource, DataSourceId,
Layout, LayoutPreset, LayoutPresetId,
WidgetConfig, WidgetId,
};
use crate::SqliteConfigStore;
use crate::error::SqliteConfigError;
use domain::{
ConfigRepository, DataSource, DataSourceId, Layout, LayoutPreset, LayoutPresetId, WidgetConfig,
WidgetId,
};
impl ConfigRepository for SqliteConfigStore {
type Error = SqliteConfigError;

View File

@@ -1,10 +1,13 @@
use domain::{LayoutPreset, LayoutPresetId};
use crate::SqliteConfigStore;
use crate::error::SqliteConfigError;
use crate::serialization::{layout as layout_ser, preset as ser};
use domain::{LayoutPreset, LayoutPresetId};
impl SqliteConfigStore {
pub(crate) async fn get_preset_impl(&self, id: LayoutPresetId) -> Result<Option<LayoutPreset>, SqliteConfigError> {
pub(crate) async fn get_preset_impl(
&self,
id: LayoutPresetId,
) -> Result<Option<LayoutPreset>, SqliteConfigError> {
let row = sqlx::query("SELECT * FROM presets WHERE id = ?")
.bind(id as i64)
.fetch_optional(&self.pool)
@@ -23,26 +26,30 @@ impl SqliteConfigStore {
.await
.map_err(SqliteConfigError::Sql)?;
rows.iter().map(|r| ser::preset_from_row(r)).collect()
rows.iter().map(ser::preset_from_row).collect()
}
pub(crate) async fn save_preset_impl(&self, preset: &LayoutPreset) -> Result<(), SqliteConfigError> {
pub(crate) async fn save_preset_impl(
&self,
preset: &LayoutPreset,
) -> Result<(), SqliteConfigError> {
let layout_json = layout_ser::layout_to_json(&preset.layout)?;
sqlx::query(
"INSERT OR REPLACE INTO presets (id, name, layout_data) VALUES (?, ?, ?)"
)
.bind(preset.id as i64)
.bind(&preset.name)
.bind(&layout_json)
.execute(&self.pool)
.await
.map_err(SqliteConfigError::Sql)?;
sqlx::query("INSERT OR REPLACE INTO presets (id, name, layout_data) VALUES (?, ?, ?)")
.bind(preset.id as i64)
.bind(&preset.name)
.bind(&layout_json)
.execute(&self.pool)
.await
.map_err(SqliteConfigError::Sql)?;
Ok(())
}
pub(crate) async fn delete_preset_impl(&self, id: LayoutPresetId) -> Result<(), SqliteConfigError> {
pub(crate) async fn delete_preset_impl(
&self,
id: LayoutPresetId,
) -> Result<(), SqliteConfigError> {
sqlx::query("DELETE FROM presets WHERE id = ?")
.bind(id as i64)
.execute(&self.pool)

View File

@@ -1,10 +1,13 @@
use domain::{WidgetConfig, WidgetId};
use crate::SqliteConfigStore;
use crate::error::SqliteConfigError;
use crate::serialization::widget as ser;
use domain::{WidgetConfig, WidgetId};
impl SqliteConfigStore {
pub(crate) async fn get_widget_impl(&self, id: WidgetId) -> Result<Option<WidgetConfig>, SqliteConfigError> {
pub(crate) async fn get_widget_impl(
&self,
id: WidgetId,
) -> Result<Option<WidgetConfig>, SqliteConfigError> {
let row = sqlx::query("SELECT * FROM widgets WHERE id = ?")
.bind(id as i64)
.fetch_optional(&self.pool)
@@ -23,10 +26,13 @@ impl SqliteConfigStore {
.await
.map_err(SqliteConfigError::Sql)?;
rows.iter().map(|r| ser::widget_from_row(r)).collect()
rows.iter().map(ser::widget_from_row).collect()
}
pub(crate) async fn save_widget_impl(&self, config: &WidgetConfig) -> Result<(), SqliteConfigError> {
pub(crate) async fn save_widget_impl(
&self,
config: &WidgetConfig,
) -> Result<(), SqliteConfigError> {
let mappings_json = ser::mappings_to_json(&config.mappings)?;
let hint_str = ser::display_hint_to_str(&config.display_hint);

View File

@@ -1,14 +1,13 @@
use std::time::Duration;
use crate::error::SqliteConfigError;
use domain::{DataSource, DataSourceConfig, DataSourceType};
use sqlx::Row;
use sqlx::sqlite::SqliteRow;
use domain::{DataSource, DataSourceConfig, DataSourceType};
use crate::error::SqliteConfigError;
use std::time::Duration;
pub fn data_source_type_to_str(t: &DataSourceType) -> &'static str {
match t {
DataSourceType::Weather => "weather",
DataSourceType::Media => "media",
DataSourceType::Xtb => "xtb",
DataSourceType::Rss => "rss",
DataSourceType::HttpJson => "http_json",
DataSourceType::Webhook => "webhook",
@@ -19,11 +18,12 @@ fn data_source_type_from_str(s: &str) -> Result<DataSourceType, SqliteConfigErro
match s {
"weather" => Ok(DataSourceType::Weather),
"media" => Ok(DataSourceType::Media),
"xtb" => Ok(DataSourceType::Xtb),
"rss" => Ok(DataSourceType::Rss),
"http_json" => Ok(DataSourceType::HttpJson),
"webhook" => Ok(DataSourceType::Webhook),
_ => Err(SqliteConfigError::Serialization(format!("unknown source type: {s}"))),
_ => Err(SqliteConfigError::Serialization(format!(
"unknown source type: {s}"
))),
}
}
@@ -37,20 +37,27 @@ pub fn data_source_config_to_json(config: &DataSourceConfig) -> Result<String, S
}
fn data_source_config_from_json(json: &str) -> Result<DataSourceConfig, SqliteConfigError> {
let v: serde_json::Value = serde_json::from_str(json)
.map_err(|e| SqliteConfigError::Serialization(e.to_string()))?;
let v: serde_json::Value =
serde_json::from_str(json).map_err(|e| SqliteConfigError::Serialization(e.to_string()))?;
let url = v["url"].as_str().map(String::from);
let api_key = v["api_key"].as_str().map(String::from);
let headers = match v["headers"].as_array() {
Some(arr) => arr.iter().filter_map(|h| {
let pair = h.as_array()?;
Some((pair[0].as_str()?.into(), pair[1].as_str()?.into()))
}).collect(),
Some(arr) => arr
.iter()
.filter_map(|h| {
let pair = h.as_array()?;
Some((pair[0].as_str()?.into(), pair[1].as_str()?.into()))
})
.collect(),
None => vec![],
};
Ok(DataSourceConfig { url, headers, api_key })
Ok(DataSourceConfig {
url,
headers,
api_key,
})
}
pub fn data_source_from_row(row: &SqliteRow) -> Result<DataSource, SqliteConfigError> {

View File

@@ -1,5 +1,5 @@
use domain::{ContainerNode, Direction, Layout, LayoutChild, LayoutNode, Sizing};
use crate::error::SqliteConfigError;
use domain::{ContainerNode, Direction, Layout, LayoutChild, LayoutNode, Sizing};
pub fn layout_to_json(layout: &Layout) -> Result<String, SqliteConfigError> {
let v = node_to_json(&layout.root);
@@ -7,8 +7,8 @@ pub fn layout_to_json(layout: &Layout) -> Result<String, SqliteConfigError> {
}
pub fn layout_from_json(json: &str) -> Result<Layout, SqliteConfigError> {
let v: serde_json::Value = serde_json::from_str(json)
.map_err(|e| SqliteConfigError::Serialization(e.to_string()))?;
let v: serde_json::Value =
serde_json::from_str(json).map_err(|e| SqliteConfigError::Serialization(e.to_string()))?;
let root = node_from_json(&v)?;
Ok(Layout { root })
}
@@ -17,16 +17,20 @@ fn node_to_json(node: &LayoutNode) -> serde_json::Value {
match node {
LayoutNode::Leaf(id) => serde_json::json!({ "type": "leaf", "widget_id": id }),
LayoutNode::Container(c) => {
let children: Vec<serde_json::Value> = c.children.iter().map(|ch| {
let sizing = match &ch.sizing {
Sizing::Fixed(px) => serde_json::json!({ "type": "fixed", "value": px }),
Sizing::Flex(w) => serde_json::json!({ "type": "flex", "value": w }),
};
serde_json::json!({
"sizing": sizing,
"node": node_to_json(&ch.node),
let children: Vec<serde_json::Value> = c
.children
.iter()
.map(|ch| {
let sizing = match &ch.sizing {
Sizing::Fixed(px) => serde_json::json!({ "type": "fixed", "value": px }),
Sizing::Flex(w) => serde_json::json!({ "type": "flex", "value": w }),
};
serde_json::json!({
"sizing": sizing,
"node": node_to_json(&ch.node),
})
})
}).collect();
.collect();
serde_json::json!({
"type": "container",
@@ -44,25 +48,44 @@ fn node_from_json(v: &serde_json::Value) -> Result<LayoutNode, SqliteConfigError
match v["type"].as_str().ok_or_else(|| err("missing node type"))? {
"leaf" => {
let id = v["widget_id"].as_u64().ok_or_else(|| err("missing widget_id"))? as u16;
let id = v["widget_id"]
.as_u64()
.ok_or_else(|| err("missing widget_id"))? as u16;
Ok(LayoutNode::Leaf(id))
}
"container" => {
let direction = match v["direction"].as_str().ok_or_else(|| err("missing direction"))? {
let direction = match v["direction"]
.as_str()
.ok_or_else(|| err("missing direction"))?
{
"row" => Direction::Row,
"column" => Direction::Column,
d => return Err(err(&format!("unknown direction: {d}"))),
};
let gap = v["gap"].as_u64().unwrap_or(0) as u8;
let padding = v["padding"].as_u64().unwrap_or(0) as u8;
let children = v["children"].as_array()
let children = v["children"]
.as_array()
.ok_or_else(|| err("missing children"))?
.iter()
.map(|ch| {
let sizing_v = &ch["sizing"];
let sizing = match sizing_v["type"].as_str().ok_or_else(|| err("missing sizing type"))? {
"fixed" => Sizing::Fixed(sizing_v["value"].as_u64().ok_or_else(|| err("missing fixed value"))? as u16),
"flex" => Sizing::Flex(sizing_v["value"].as_u64().ok_or_else(|| err("missing flex value"))? as u8),
let sizing = match sizing_v["type"]
.as_str()
.ok_or_else(|| err("missing sizing type"))?
{
"fixed" => Sizing::Fixed(
sizing_v["value"]
.as_u64()
.ok_or_else(|| err("missing fixed value"))?
as u16,
),
"flex" => Sizing::Flex(
sizing_v["value"]
.as_u64()
.ok_or_else(|| err("missing flex value"))?
as u8,
),
s => return Err(err(&format!("unknown sizing: {s}"))),
};
let node = node_from_json(&ch["node"])?;
@@ -70,7 +93,12 @@ fn node_from_json(v: &serde_json::Value) -> Result<LayoutNode, SqliteConfigError
})
.collect::<Result<Vec<_>, _>>()?;
Ok(LayoutNode::Container(ContainerNode { direction, gap, padding, children }))
Ok(LayoutNode::Container(ContainerNode {
direction,
gap,
padding,
children,
}))
}
t => Err(err(&format!("unknown node type: {t}"))),
}

View File

@@ -1,4 +1,4 @@
pub mod widget;
pub mod data_source;
pub mod layout;
pub mod preset;
pub mod widget;

View File

@@ -1,8 +1,8 @@
use super::layout::layout_from_json;
use crate::error::SqliteConfigError;
use domain::LayoutPreset;
use sqlx::Row;
use sqlx::sqlite::SqliteRow;
use domain::LayoutPreset;
use crate::error::SqliteConfigError;
use super::layout::layout_from_json;
pub fn preset_from_row(row: &SqliteRow) -> Result<LayoutPreset, SqliteConfigError> {
let id: i64 = row.get("id");

View File

@@ -1,7 +1,7 @@
use crate::error::SqliteConfigError;
use domain::{DisplayHint, KeyMapping, WidgetConfig};
use sqlx::Row;
use sqlx::sqlite::SqliteRow;
use domain::{DisplayHint, KeyMapping, WidgetConfig};
use crate::error::SqliteConfigError;
pub fn display_hint_to_str(hint: &DisplayHint) -> &'static str {
match hint {
@@ -16,32 +16,44 @@ fn display_hint_from_str(s: &str) -> Result<DisplayHint, SqliteConfigError> {
"icon_value" => Ok(DisplayHint::IconValue),
"text_block" => Ok(DisplayHint::TextBlock),
"key_value" => Ok(DisplayHint::KeyValue),
_ => Err(SqliteConfigError::Serialization(format!("unknown display hint: {s}"))),
_ => Err(SqliteConfigError::Serialization(format!(
"unknown display hint: {s}"
))),
}
}
pub fn mappings_to_json(mappings: &[KeyMapping]) -> Result<String, SqliteConfigError> {
let entries: Vec<serde_json::Value> = mappings.iter().map(|m| {
serde_json::json!({
"source_path": m.source_path,
"target_key": m.target_key,
let entries: Vec<serde_json::Value> = mappings
.iter()
.map(|m| {
serde_json::json!({
"source_path": m.source_path,
"target_key": m.target_key,
})
})
}).collect();
.collect();
serde_json::to_string(&entries).map_err(|e| SqliteConfigError::Serialization(e.to_string()))
}
fn mappings_from_json(json: &str) -> Result<Vec<KeyMapping>, SqliteConfigError> {
let entries: Vec<serde_json::Value> = serde_json::from_str(json)
.map_err(|e| SqliteConfigError::Serialization(e.to_string()))?;
let entries: Vec<serde_json::Value> =
serde_json::from_str(json).map_err(|e| SqliteConfigError::Serialization(e.to_string()))?;
entries.iter().map(|v| {
Ok(KeyMapping {
source_path: v["source_path"].as_str()
.ok_or_else(|| SqliteConfigError::Serialization("missing source_path".into()))?.into(),
target_key: v["target_key"].as_str()
.ok_or_else(|| SqliteConfigError::Serialization("missing target_key".into()))?.into(),
entries
.iter()
.map(|v| {
Ok(KeyMapping {
source_path: v["source_path"]
.as_str()
.ok_or_else(|| SqliteConfigError::Serialization("missing source_path".into()))?
.into(),
target_key: v["target_key"]
.as_str()
.ok_or_else(|| SqliteConfigError::Serialization("missing target_key".into()))?
.into(),
})
})
}).collect()
.collect()
}
pub fn widget_from_row(row: &SqliteRow) -> Result<WidgetConfig, SqliteConfigError> {

View File

@@ -1,11 +1,9 @@
use std::time::Duration;
use domain::{
ConfigRepository, DisplayHint, KeyMapping, WidgetConfig,
DataSource, DataSourceConfig, DataSourceType,
Layout, LayoutNode, ContainerNode, LayoutChild, Direction, Sizing,
LayoutPreset,
};
use config_sqlite::SqliteConfigStore;
use domain::{
ConfigRepository, ContainerNode, DataSource, DataSourceConfig, DataSourceType, Direction,
DisplayHint, KeyMapping, Layout, LayoutChild, LayoutNode, LayoutPreset, Sizing, WidgetConfig,
};
use std::time::Duration;
async fn test_store() -> SqliteConfigStore {
SqliteConfigStore::new("sqlite::memory:").await.unwrap()
@@ -18,8 +16,14 @@ fn weather_widget() -> WidgetConfig {
display_hint: DisplayHint::IconValue,
data_source_id: 10,
mappings: vec![
KeyMapping { source_path: "$.temp".into(), target_key: "temperature".into() },
KeyMapping { source_path: "$.icon".into(), target_key: "icon".into() },
KeyMapping {
source_path: "$.temp".into(),
target_key: "temperature".into(),
},
KeyMapping {
source_path: "$.icon".into(),
target_key: "icon".into(),
},
],
max_data_size: 2048,
}
@@ -46,8 +50,14 @@ fn test_layout() -> Layout {
gap: 4,
padding: 2,
children: vec![
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(1) },
LayoutChild { sizing: Sizing::Fixed(80), node: LayoutNode::Leaf(2) },
LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(1),
},
LayoutChild {
sizing: Sizing::Fixed(80),
node: LayoutNode::Leaf(2),
},
],
}),
}
@@ -78,14 +88,17 @@ async fn get_nonexistent_widget_returns_none() {
async fn list_widgets_returns_all() {
let store = test_store().await;
store.save_widget(&weather_widget()).await.unwrap();
store.save_widget(&WidgetConfig {
id: 2,
name: "portfolio".into(),
display_hint: DisplayHint::KeyValue,
data_source_id: 20,
mappings: vec![],
max_data_size: 1024,
}).await.unwrap();
store
.save_widget(&WidgetConfig {
id: 2,
name: "portfolio".into(),
display_hint: DisplayHint::KeyValue,
data_source_id: 20,
mappings: vec![],
max_data_size: 1024,
})
.await
.unwrap();
let widgets = store.list_widgets().await.unwrap();
assert_eq!(widgets.len(), 2);
@@ -172,12 +185,22 @@ async fn save_and_retrieve_preset() {
#[tokio::test]
async fn list_and_delete_presets() {
let store = test_store().await;
store.save_preset(&LayoutPreset {
id: 1, name: "a".into(), layout: test_layout(),
}).await.unwrap();
store.save_preset(&LayoutPreset {
id: 2, name: "b".into(), layout: test_layout(),
}).await.unwrap();
store
.save_preset(&LayoutPreset {
id: 1,
name: "a".into(),
layout: test_layout(),
})
.await
.unwrap();
store
.save_preset(&LayoutPreset {
id: 2,
name: "b".into(),
layout: test_layout(),
})
.await
.unwrap();
assert_eq!(store.list_presets().await.unwrap().len(), 2);

View File

@@ -1,5 +1,6 @@
use client_domain::{BoundingBox, DisplayPort};
#[derive(Default)]
pub struct TerminalDisplay;
impl TerminalDisplay {
@@ -12,12 +13,24 @@ impl DisplayPort for TerminalDisplay {
type Error = std::io::Error;
fn clear_region(&mut self, bounds: BoundingBox) -> Result<(), Self::Error> {
println!("[CLEAR] ({}, {}) {}x{}", bounds.x, bounds.y, bounds.width, bounds.height);
println!(
"[CLEAR] ({}, {}) {}x{}",
bounds.x, bounds.y, bounds.width, bounds.height
);
Ok(())
}
fn draw_text(&mut self, text: &str, x: u16, y: u16, bounds: BoundingBox) -> Result<(), Self::Error> {
println!("[TEXT] ({x}, {y}) in {}x{}: \"{text}\"", bounds.width, bounds.height);
fn draw_text(
&mut self,
text: &str,
x: u16,
y: u16,
bounds: BoundingBox,
) -> Result<(), Self::Error> {
println!(
"[TEXT] ({x}, {y}) in {}x{}: \"{text}\"",
bounds.width, bounds.height
);
Ok(())
}
@@ -27,7 +40,10 @@ impl DisplayPort for TerminalDisplay {
}
fn fill_background(&mut self, bounds: BoundingBox) -> Result<(), Self::Error> {
println!("[BG] ({}, {}) {}x{}", bounds.x, bounds.y, bounds.width, bounds.height);
println!(
"[BG] ({}, {}) {}x{}",
bounds.x, bounds.y, bounds.width, bounds.height
);
Ok(())
}

View File

@@ -1,9 +1,9 @@
mod routes;
use std::sync::Arc;
use axum::Router;
use tower_http::cors::CorsLayer;
use domain::{ConfigRepository, EventPublisher};
use std::sync::Arc;
use tower_http::cors::CorsLayer;
pub struct AppState<C, E> {
pub config: Arc<C>,

View File

@@ -1,54 +1,107 @@
use crate::AppState;
use api_types::DataSourceDto;
use application::ConfigService;
use axum::{
extract::{Path, State},
http::StatusCode,
response::Json,
};
use domain::{ConfigRepository, EventPublisher};
use application::ConfigService;
use crate::AppState;
use api_types::DataSourceDto;
type S<C, E> = State<AppState<C, E>>;
pub async fn list_data_sources<C, E>(State(state): S<C, E>) -> Result<Json<Vec<DataSourceDto>>, StatusCode>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
pub async fn list_data_sources<C, E>(
State(state): S<C, E>,
) -> Result<Json<Vec<DataSourceDto>>, StatusCode>
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let sources = state.config.list_data_sources().await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let sources = state
.config
.list_data_sources()
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(Json(sources.iter().map(DataSourceDto::from).collect()))
}
pub async fn get_data_source<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<Json<DataSourceDto>, StatusCode>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
pub async fn get_data_source<C, E>(
State(state): S<C, E>,
Path(id): Path<u16>,
) -> Result<Json<DataSourceDto>, StatusCode>
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let source = state.config.get_data_source(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let source = state
.config
.get_data_source(id)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
match source {
Some(s) => Ok(Json(DataSourceDto::from(&s))),
None => Err(StatusCode::NOT_FOUND),
}
}
pub async fn create_data_source<C, E>(State(state): S<C, E>, Json(body): Json<DataSourceDto>) -> Result<StatusCode, (StatusCode, String)>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
pub async fn create_data_source<C, E>(
State(state): S<C, E>,
Json(body): Json<DataSourceDto>,
) -> Result<StatusCode, (StatusCode, String)>
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let source = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
let source = body
.into_domain()
.map_err(|e| (StatusCode::BAD_REQUEST, e))?;
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
svc.create_data_source(source).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
svc.create_data_source(source)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
Ok(StatusCode::CREATED)
}
pub async fn update_data_source<C, E>(State(state): S<C, E>, Path(_id): Path<u16>, Json(body): Json<DataSourceDto>) -> Result<StatusCode, (StatusCode, String)>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
pub async fn update_data_source<C, E>(
State(state): S<C, E>,
Path(_id): Path<u16>,
Json(body): Json<DataSourceDto>,
) -> Result<StatusCode, (StatusCode, String)>
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let source = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
let source = body
.into_domain()
.map_err(|e| (StatusCode::BAD_REQUEST, e))?;
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
svc.update_data_source(source).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
svc.update_data_source(source)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
Ok(StatusCode::OK)
}
pub async fn delete_data_source<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<StatusCode, StatusCode>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
pub async fn delete_data_source<C, E>(
State(state): S<C, E>,
Path(id): Path<u16>,
) -> Result<StatusCode, StatusCode>
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
svc.delete_data_source(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
svc.delete_data_source(id)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(StatusCode::NO_CONTENT)
}

View File

@@ -1,27 +1,42 @@
use axum::{
extract::State,
http::StatusCode,
response::Json,
};
use domain::{ConfigRepository, EventPublisher};
use application::ConfigService;
use crate::AppState;
use api_types::LayoutDto;
use application::ConfigService;
use axum::{extract::State, http::StatusCode, response::Json};
use domain::{ConfigRepository, EventPublisher};
type S<C, E> = State<AppState<C, E>>;
pub async fn get_layout<C, E>(State(state): S<C, E>) -> Result<Json<Option<LayoutDto>>, StatusCode>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let layout = state.config.get_layout().await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let layout = state
.config
.get_layout()
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(Json(layout.as_ref().map(LayoutDto::from)))
}
pub async fn update_layout<C, E>(State(state): S<C, E>, Json(body): Json<LayoutDto>) -> Result<StatusCode, (StatusCode, String)>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
pub async fn update_layout<C, E>(
State(state): S<C, E>,
Json(body): Json<LayoutDto>,
) -> Result<StatusCode, (StatusCode, String)>
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let layout = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
let layout = body
.into_domain()
.map_err(|e| (StatusCode::BAD_REQUEST, e))?;
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
svc.update_layout(layout).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
svc.update_layout(layout)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
Ok(StatusCode::OK)
}

View File

@@ -1,12 +1,12 @@
mod widgets;
mod data_sources;
mod layout;
mod presets;
mod widgets;
use axum::Router;
use axum::routing::{get, post, put, delete};
use domain::{ConfigRepository, EventPublisher};
use crate::AppState;
use axum::Router;
use axum::routing::{get, post};
use domain::{ConfigRepository, EventPublisher};
pub fn api_routes<C, E>() -> Router<AppState<C, E>>
where
@@ -16,12 +16,38 @@ where
E::Error: std::fmt::Debug + Send,
{
Router::new()
.route("/widgets", get(widgets::list_widgets::<C, E>).post(widgets::create_widget::<C, E>))
.route("/widgets/{id}", get(widgets::get_widget::<C, E>).put(widgets::update_widget::<C, E>).delete(widgets::delete_widget::<C, E>))
.route("/data-sources", get(data_sources::list_data_sources::<C, E>).post(data_sources::create_data_source::<C, E>))
.route("/data-sources/{id}", get(data_sources::get_data_source::<C, E>).put(data_sources::update_data_source::<C, E>).delete(data_sources::delete_data_source::<C, E>))
.route("/layout", get(layout::get_layout::<C, E>).put(layout::update_layout::<C, E>))
.route("/presets", get(presets::list_presets::<C, E>).post(presets::create_preset::<C, E>))
.route("/presets/{id}", get(presets::get_preset::<C, E>).delete(presets::delete_preset::<C, E>))
.route(
"/widgets",
get(widgets::list_widgets::<C, E>).post(widgets::create_widget::<C, E>),
)
.route(
"/widgets/{id}",
get(widgets::get_widget::<C, E>)
.put(widgets::update_widget::<C, E>)
.delete(widgets::delete_widget::<C, E>),
)
.route(
"/data-sources",
get(data_sources::list_data_sources::<C, E>)
.post(data_sources::create_data_source::<C, E>),
)
.route(
"/data-sources/{id}",
get(data_sources::get_data_source::<C, E>)
.put(data_sources::update_data_source::<C, E>)
.delete(data_sources::delete_data_source::<C, E>),
)
.route(
"/layout",
get(layout::get_layout::<C, E>).put(layout::update_layout::<C, E>),
)
.route(
"/presets",
get(presets::list_presets::<C, E>).post(presets::create_preset::<C, E>),
)
.route(
"/presets/{id}",
get(presets::get_preset::<C, E>).delete(presets::delete_preset::<C, E>),
)
.route("/presets/{id}/load", post(presets::load_preset::<C, E>))
}

View File

@@ -1,53 +1,101 @@
use crate::AppState;
use api_types::{CreatePresetDto, PresetDto};
use application::ConfigService;
use axum::{
extract::{Path, State},
http::StatusCode,
response::Json,
};
use domain::{ConfigRepository, EventPublisher};
use application::ConfigService;
use crate::AppState;
use api_types::{PresetDto, CreatePresetDto};
type S<C, E> = State<AppState<C, E>>;
pub async fn list_presets<C, E>(State(state): S<C, E>) -> Result<Json<Vec<PresetDto>>, StatusCode>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let presets = state.config.list_presets().await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let presets = state
.config
.list_presets()
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(Json(presets.iter().map(PresetDto::from).collect()))
}
pub async fn get_preset<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<Json<PresetDto>, StatusCode>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
pub async fn get_preset<C, E>(
State(state): S<C, E>,
Path(id): Path<u16>,
) -> Result<Json<PresetDto>, StatusCode>
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let preset = state.config.get_preset(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let preset = state
.config
.get_preset(id)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
match preset {
Some(p) => Ok(Json(PresetDto::from(&p))),
None => Err(StatusCode::NOT_FOUND),
}
}
pub async fn create_preset<C, E>(State(state): S<C, E>, Json(body): Json<CreatePresetDto>) -> Result<StatusCode, (StatusCode, String)>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
pub async fn create_preset<C, E>(
State(state): S<C, E>,
Json(body): Json<CreatePresetDto>,
) -> Result<StatusCode, (StatusCode, String)>
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let preset = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
let preset = body
.into_domain()
.map_err(|e| (StatusCode::BAD_REQUEST, e))?;
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
svc.save_preset(preset).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
svc.save_preset(preset)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
Ok(StatusCode::CREATED)
}
pub async fn delete_preset<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<StatusCode, StatusCode>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
pub async fn delete_preset<C, E>(
State(state): S<C, E>,
Path(id): Path<u16>,
) -> Result<StatusCode, StatusCode>
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
svc.delete_preset(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
svc.delete_preset(id)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(StatusCode::NO_CONTENT)
}
pub async fn load_preset<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<StatusCode, (StatusCode, String)>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
pub async fn load_preset<C, E>(
State(state): S<C, E>,
Path(id): Path<u16>,
) -> Result<StatusCode, (StatusCode, String)>
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
svc.load_preset(id).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
svc.load_preset(id)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
Ok(StatusCode::OK)
}

View File

@@ -1,54 +1,105 @@
use crate::AppState;
use api_types::{CreateWidgetDto, WidgetDto};
use application::ConfigService;
use axum::{
extract::{Path, State},
http::StatusCode,
response::Json,
};
use domain::{ConfigRepository, EventPublisher};
use application::ConfigService;
use crate::AppState;
use api_types::{WidgetDto, CreateWidgetDto};
type S<C, E> = State<AppState<C, E>>;
pub async fn list_widgets<C, E>(State(state): S<C, E>) -> Result<Json<Vec<WidgetDto>>, StatusCode>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let widgets = state.config.list_widgets().await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let widgets = state
.config
.list_widgets()
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(Json(widgets.iter().map(WidgetDto::from).collect()))
}
pub async fn get_widget<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<Json<WidgetDto>, StatusCode>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
pub async fn get_widget<C, E>(
State(state): S<C, E>,
Path(id): Path<u16>,
) -> Result<Json<WidgetDto>, StatusCode>
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let widget = state.config.get_widget(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let widget = state
.config
.get_widget(id)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
match widget {
Some(w) => Ok(Json(WidgetDto::from(&w))),
None => Err(StatusCode::NOT_FOUND),
}
}
pub async fn create_widget<C, E>(State(state): S<C, E>, Json(body): Json<CreateWidgetDto>) -> Result<StatusCode, (StatusCode, String)>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
pub async fn create_widget<C, E>(
State(state): S<C, E>,
Json(body): Json<CreateWidgetDto>,
) -> Result<StatusCode, (StatusCode, String)>
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let widget = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
let widget = body
.into_domain()
.map_err(|e| (StatusCode::BAD_REQUEST, e))?;
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
svc.create_widget(widget).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
svc.create_widget(widget)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
Ok(StatusCode::CREATED)
}
pub async fn update_widget<C, E>(State(state): S<C, E>, Path(_id): Path<u16>, Json(body): Json<CreateWidgetDto>) -> Result<StatusCode, (StatusCode, String)>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
pub async fn update_widget<C, E>(
State(state): S<C, E>,
Path(_id): Path<u16>,
Json(body): Json<CreateWidgetDto>,
) -> Result<StatusCode, (StatusCode, String)>
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let widget = body.into_domain().map_err(|e| (StatusCode::BAD_REQUEST, e))?;
let widget = body
.into_domain()
.map_err(|e| (StatusCode::BAD_REQUEST, e))?;
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
svc.update_widget(widget).await.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
svc.update_widget(widget)
.await
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("{e}")))?;
Ok(StatusCode::OK)
}
pub async fn delete_widget<C, E>(State(state): S<C, E>, Path(id): Path<u16>) -> Result<StatusCode, StatusCode>
where C: ConfigRepository, C::Error: std::fmt::Debug, E: EventPublisher, E::Error: std::fmt::Debug,
pub async fn delete_widget<C, E>(
State(state): S<C, E>,
Path(id): Path<u16>,
) -> Result<StatusCode, StatusCode>
where
C: ConfigRepository,
C::Error: std::fmt::Debug,
E: EventPublisher,
E::Error: std::fmt::Debug,
{
let svc = ConfigService::new(state.config.as_ref(), state.events.as_ref());
svc.delete_widget(id).await.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
svc.delete_widget(id)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
Ok(StatusCode::NO_CONTENT)
}

View File

@@ -1,10 +1,10 @@
use std::sync::Arc;
use axum::body::Body;
use axum::http::{Request, StatusCode};
use tower::ServiceExt;
use config_memory::MemoryConfigStore;
use tcp_server::TcpEventBus;
use http_api::{AppState, router};
use std::sync::Arc;
use tcp_server::TcpEventBus;
use tower::ServiceExt;
fn test_app() -> axum::Router {
let config = Arc::new(MemoryConfigStore::new());
@@ -38,13 +38,22 @@ async fn create_and_get_widget() {
"mappings": [{"source_path": "$.temp", "target_key": "temperature"}]
}"#;
let resp = app.clone().oneshot(json_request("POST", "/api/widgets", Some(body))).await.unwrap();
let resp = app
.clone()
.oneshot(json_request("POST", "/api/widgets", Some(body)))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::CREATED);
let resp = app.oneshot(json_request("GET", "/api/widgets/1", None)).await.unwrap();
let resp = app
.oneshot(json_request("GET", "/api/widgets/1", None))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let body = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
.await
.unwrap();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["name"], "weather");
assert_eq!(json["display_hint"], "icon_value");
@@ -58,11 +67,22 @@ async fn list_widgets() {
let w1 = r#"{"id":1,"name":"a","display_hint":"icon_value","data_source_id":1,"mappings":[]}"#;
let w2 = r#"{"id":2,"name":"b","display_hint":"key_value","data_source_id":2,"mappings":[]}"#;
app.clone().oneshot(json_request("POST", "/api/widgets", Some(w1))).await.unwrap();
app.clone().oneshot(json_request("POST", "/api/widgets", Some(w2))).await.unwrap();
app.clone()
.oneshot(json_request("POST", "/api/widgets", Some(w1)))
.await
.unwrap();
app.clone()
.oneshot(json_request("POST", "/api/widgets", Some(w2)))
.await
.unwrap();
let resp = app.oneshot(json_request("GET", "/api/widgets", None)).await.unwrap();
let body = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
let resp = app
.oneshot(json_request("GET", "/api/widgets", None))
.await
.unwrap();
let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
.await
.unwrap();
let json: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
assert_eq!(json.len(), 2);
}
@@ -71,13 +91,24 @@ async fn list_widgets() {
async fn delete_widget() {
let app = test_app();
let body = r#"{"id":1,"name":"a","display_hint":"icon_value","data_source_id":1,"mappings":[]}"#;
app.clone().oneshot(json_request("POST", "/api/widgets", Some(body))).await.unwrap();
let body =
r#"{"id":1,"name":"a","display_hint":"icon_value","data_source_id":1,"mappings":[]}"#;
app.clone()
.oneshot(json_request("POST", "/api/widgets", Some(body)))
.await
.unwrap();
let resp = app.clone().oneshot(json_request("DELETE", "/api/widgets/1", None)).await.unwrap();
let resp = app
.clone()
.oneshot(json_request("DELETE", "/api/widgets/1", None))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::NO_CONTENT);
let resp = app.oneshot(json_request("GET", "/api/widgets/1", None)).await.unwrap();
let resp = app
.oneshot(json_request("GET", "/api/widgets/1", None))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}
@@ -95,13 +126,22 @@ async fn create_and_get_data_source() {
"headers": []
}"#;
let resp = app.clone().oneshot(json_request("POST", "/api/data-sources", Some(body))).await.unwrap();
let resp = app
.clone()
.oneshot(json_request("POST", "/api/data-sources", Some(body)))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::CREATED);
let resp = app.oneshot(json_request("GET", "/api/data-sources/10", None)).await.unwrap();
let resp = app
.oneshot(json_request("GET", "/api/data-sources/10", None))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let body = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
.await
.unwrap();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["name"], "weather_api");
assert_eq!(json["poll_interval_secs"], 300);
@@ -124,13 +164,22 @@ async fn update_and_get_layout() {
}
}"#;
let resp = app.clone().oneshot(json_request("PUT", "/api/layout", Some(body))).await.unwrap();
let resp = app
.clone()
.oneshot(json_request("PUT", "/api/layout", Some(body)))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let resp = app.oneshot(json_request("GET", "/api/layout", None)).await.unwrap();
let resp = app
.oneshot(json_request("GET", "/api/layout", None))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let body = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
.await
.unwrap();
let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(json["root"]["type"], "container");
assert_eq!(json["root"]["direction"], "row");
@@ -141,9 +190,16 @@ async fn update_and_get_layout() {
async fn get_nonexistent_returns_404() {
let app = test_app();
let resp = app.clone().oneshot(json_request("GET", "/api/widgets/99", None)).await.unwrap();
let resp = app
.clone()
.oneshot(json_request("GET", "/api/widgets/99", None))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
let resp = app.oneshot(json_request("GET", "/api/data-sources/99", None)).await.unwrap();
let resp = app
.oneshot(json_request("GET", "/api/data-sources/99", None))
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}

View File

@@ -14,26 +14,32 @@ pub enum HttpJsonError {
Parse(String),
}
impl HttpJsonAdapter {
pub fn new() -> Self {
impl Default for HttpJsonAdapter {
fn default() -> Self {
Self {
client: reqwest::Client::new(),
}
}
}
impl HttpJsonAdapter {
pub fn new() -> Self {
Self::default()
}
}
fn json_to_value(json: serde_json::Value) -> Value {
match json {
serde_json::Value::Null => Value::Null,
serde_json::Value::Bool(b) => Value::Bool(b),
serde_json::Value::Number(n) => Value::Number(n.as_f64().unwrap_or(0.0)),
serde_json::Value::String(s) => Value::String(s),
serde_json::Value::Array(arr) => {
Value::Array(arr.into_iter().map(json_to_value).collect())
}
serde_json::Value::Object(map) => {
Value::Object(map.into_iter().map(|(k, v)| (k, json_to_value(v))).collect())
}
serde_json::Value::Array(arr) => Value::Array(arr.into_iter().map(json_to_value).collect()),
serde_json::Value::Object(map) => Value::Object(
map.into_iter()
.map(|(k, v)| (k, json_to_value(v)))
.collect(),
),
}
}

View File

@@ -1,19 +1,23 @@
use std::time::Duration;
use axum::{Router, routing::get, response::Json};
use axum::{Router, response::Json, routing::get};
use domain::{DataSource, DataSourceConfig, DataSourcePort, DataSourceType, Value};
use http_json::HttpJsonAdapter;
use std::time::Duration;
async fn start_fake_api() -> String {
let app = Router::new()
.route("/weather", get(|| async {
Json(serde_json::json!({
"main": {"temp": 5.4, "humidity": 80},
"weather": [{"icon": "cloud_rain"}]
}))
}))
.route("/simple", get(|| async {
Json(serde_json::json!({"value": "hello"}))
}))
.route(
"/weather",
get(|| async {
Json(serde_json::json!({
"main": {"temp": 5.4, "humidity": 80},
"weather": [{"icon": "cloud_rain"}]
}))
}),
)
.route(
"/simple",
get(|| async { Json(serde_json::json!({"value": "hello"})) }),
)
.route("/not-json", get(|| async { "plain text" }));
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
@@ -46,10 +50,7 @@ async fn polls_url_and_returns_nested_json_as_value() {
let result = adapter.poll(&source).await.unwrap();
assert_eq!(
result.get_path("$.main.temp"),
Some(&Value::Number(5.4))
);
assert_eq!(result.get_path("$.main.temp"), Some(&Value::Number(5.4)));
assert_eq!(
result.get_path("$.main.humidity"),
Some(&Value::Number(80.0))

View File

@@ -8,6 +8,8 @@ domain.workspace = true
reqwest.workspace = true
serde_json.workspace = true
thiserror.workspace = true
md5 = "0.7"
fastrand = "2"
[dev-dependencies]
tokio.workspace = true

View File

@@ -4,6 +4,8 @@ pub enum MediaError {
Request(#[from] reqwest::Error),
#[error("no url configured")]
NoUrl,
#[error("missing field in headers: {0}")]
MissingField(&'static str),
#[error("parse: {0}")]
Parse(String),
}

View File

@@ -2,33 +2,61 @@ mod error;
pub use error::MediaError;
use std::collections::BTreeMap;
use domain::{DataSource, DataSourcePort, Value};
use std::collections::BTreeMap;
pub struct MediaAdapter {
client: reqwest::Client,
}
impl MediaAdapter {
pub fn new() -> Self {
impl Default for MediaAdapter {
fn default() -> Self {
Self {
client: reqwest::Client::new(),
}
}
}
impl MediaAdapter {
pub fn new() -> Self {
Self::default()
}
}
fn find_header<'a>(headers: &'a [(String, String)], key: &str) -> Option<&'a str> {
headers
.iter()
.find(|(k, _)| k.eq_ignore_ascii_case(key))
.map(|(_, v)| v.as_str())
}
fn subsonic_token(password: &str, salt: &str) -> String {
format!("{:x}", md5::compute(format!("{password}{salt}")))
}
impl DataSourcePort for MediaAdapter {
type Error = MediaError;
async fn poll(&self, source: &DataSource) -> Result<Value, Self::Error> {
let base_url = source.config.url.as_ref().ok_or(MediaError::NoUrl)?;
let api_key = source.config.api_key.as_deref().unwrap_or("");
let username = find_header(&source.config.headers, "username")
.ok_or(MediaError::MissingField("username"))?;
let password = find_header(&source.config.headers, "password")
.ok_or(MediaError::MissingField("password"))?;
let salt: String = (0..12).map(|_| fastrand::alphanumeric()).collect();
let token = subsonic_token(password, &salt);
let url = format!(
"{base_url}/rest/getNowPlaying.view?u=kframe&t={api_key}&s=salt&v=1.16.1&c=kframe&f=json"
"{base_url}/rest/getNowPlaying.view?u={username}&t={token}&s={salt}&v=1.16.1&c=kframe&f=json"
);
let resp = self.client.get(&url).send().await.map_err(MediaError::Request)?;
let resp = self
.client
.get(&url)
.send()
.await
.map_err(MediaError::Request)?;
let json: serde_json::Value = resp.json().await.map_err(MediaError::Request)?;
let entries = json["subsonic-response"]["nowPlaying"]["entry"]
@@ -45,15 +73,18 @@ impl DataSourcePort for MediaAdapter {
let entry = &entries[0];
let mut result = BTreeMap::new();
result.insert("playing".into(), Value::Bool(true));
result.insert("title".into(), Value::String(
entry["title"].as_str().unwrap_or("Unknown").into()
));
result.insert("artist".into(), Value::String(
entry["artist"].as_str().unwrap_or("Unknown").into()
));
result.insert("album".into(), Value::String(
entry["album"].as_str().unwrap_or("Unknown").into()
));
result.insert(
"title".into(),
Value::String(entry["title"].as_str().unwrap_or("Unknown").into()),
);
result.insert(
"artist".into(),
Value::String(entry["artist"].as_str().unwrap_or("Unknown").into()),
);
result.insert(
"album".into(),
Value::String(entry["album"].as_str().unwrap_or("Unknown").into()),
);
if let Some(duration) = entry["duration"].as_u64() {
result.insert("duration".into(), Value::Number(duration as f64));

View File

@@ -1,6 +1,6 @@
use std::time::Duration;
use domain::{DataSource, DataSourceConfig, DataSourcePort, DataSourceType, Value};
use media_adapter::MediaAdapter;
use std::time::Duration;
fn subsonic_response(playing: bool) -> serde_json::Value {
if playing {
@@ -28,10 +28,10 @@ fn subsonic_response(playing: bool) -> serde_json::Value {
}
async fn start_fake_subsonic(playing: bool) -> String {
let app = axum::Router::new()
.route("/rest/getNowPlaying.view", axum::routing::get(move || async move {
axum::response::Json(subsonic_response(playing))
}));
let app = axum::Router::new().route(
"/rest/getNowPlaying.view",
axum::routing::get(move || async move { axum::response::Json(subsonic_response(playing)) }),
);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
@@ -47,8 +47,11 @@ fn make_source(url: String) -> DataSource {
poll_interval: Duration::from_secs(5),
config: DataSourceConfig {
url: Some(url),
headers: vec![],
api_key: Some("testtoken".into()),
headers: vec![
("username".into(), "test".into()),
("password".into(), "testpass".into()),
],
api_key: None,
},
}
}
@@ -62,8 +65,14 @@ async fn returns_now_playing_info() {
let result = adapter.poll(&source).await.unwrap();
assert_eq!(result.get_path("$.playing"), Some(&Value::Bool(true)));
assert_eq!(result.get_path("$.title"), Some(&Value::String("Believer".into())));
assert_eq!(result.get_path("$.artist"), Some(&Value::String("Imagine Dragons".into())));
assert_eq!(
result.get_path("$.title"),
Some(&Value::String("Believer".into()))
);
assert_eq!(
result.get_path("$.artist"),
Some(&Value::String("Imagine Dragons".into()))
);
}
#[tokio::test]

View File

@@ -10,21 +10,32 @@ pub struct RssAdapter {
client: reqwest::Client,
}
impl RssAdapter {
pub fn new() -> Self {
impl Default for RssAdapter {
fn default() -> Self {
Self {
client: reqwest::Client::new(),
}
}
}
impl RssAdapter {
pub fn new() -> Self {
Self::default()
}
}
impl DataSourcePort for RssAdapter {
type Error = RssError;
async fn poll(&self, source: &DataSource) -> Result<Value, Self::Error> {
let url = source.config.url.as_ref().ok_or(RssError::NoUrl)?;
let resp = self.client.get(url).send().await.map_err(RssError::Request)?;
let resp = self
.client
.get(url)
.send()
.await
.map_err(RssError::Request)?;
let xml = resp.text().await.map_err(RssError::Request)?;
parser::parse_rss(&xml)

View File

@@ -29,10 +29,10 @@ pub fn parse_rss(xml: &str) -> Result<Value, RssError> {
}
Ok(Event::End(e)) => {
let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
if tag == "item" {
if let Some(item) = current_item.take() {
items.push(Value::Object(item));
}
if tag == "item"
&& let Some(item) = current_item.take()
{
items.push(Value::Object(item));
}
current_tag.clear();
}
@@ -52,10 +52,10 @@ pub fn parse_rss(xml: &str) -> Result<Value, RssError> {
}
Ok(Event::CData(e)) => {
let text = String::from_utf8_lossy(&e).to_string();
if !current_tag.is_empty() {
if let Some(item) = current_item.as_mut() {
item.insert(current_tag.clone(), Value::String(text));
}
if !current_tag.is_empty()
&& let Some(item) = current_item.as_mut()
{
item.insert(current_tag.clone(), Value::String(text));
}
}
Ok(Event::Eof) => break,

View File

@@ -1,5 +1,5 @@
use domain::Value;
use rss_adapter::{parse_rss, RssError};
use rss_adapter::{RssError, parse_rss};
const SAMPLE_RSS: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
@@ -23,8 +23,20 @@ const SAMPLE_RSS: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
fn parses_rss_into_value() {
let result = parse_rss(SAMPLE_RSS).unwrap();
assert_eq!(result.get_path("$.title"), Some(&Value::String("Test Feed".into())));
assert_eq!(result.get_path("$.items[0].title"), Some(&Value::String("First Article".into())));
assert_eq!(result.get_path("$.items[1].title"), Some(&Value::String("Second Article".into())));
assert_eq!(result.get_path("$.items[0].description"), Some(&Value::String("Description of first article".into())));
assert_eq!(
result.get_path("$.title"),
Some(&Value::String("Test Feed".into()))
);
assert_eq!(
result.get_path("$.items[0].title"),
Some(&Value::String("First Article".into()))
);
assert_eq!(
result.get_path("$.items[1].title"),
Some(&Value::String("Second Article".into()))
);
assert_eq!(
result.get_path("$.items[0].description"),
Some(&Value::String("Description of first article".into()))
);
}

View File

@@ -1,8 +1,8 @@
use client_domain::NetworkPort;
use protocol::MAX_FRAME_SIZE;
use std::io::{Read, Write};
use std::net::TcpStream;
use std::time::Duration;
use client_domain::NetworkPort;
use protocol::MAX_FRAME_SIZE;
#[derive(Debug, thiserror::Error)]
pub enum TcpClientError {
@@ -14,13 +14,14 @@ pub enum TcpClientError {
FrameTooLarge(usize),
}
#[derive(Default)]
pub struct StdTcpClient {
stream: Option<TcpStream>,
}
impl StdTcpClient {
pub fn new() -> Self {
Self { stream: None }
Self::default()
}
}
@@ -30,7 +31,9 @@ impl NetworkPort for StdTcpClient {
fn connect(&mut self, addr: &str) -> Result<(), Self::Error> {
let stream = TcpStream::connect(addr).map_err(TcpClientError::Io)?;
stream.set_nonblocking(true).map_err(TcpClientError::Io)?;
stream.set_read_timeout(Some(Duration::from_millis(10))).map_err(TcpClientError::Io)?;
stream
.set_read_timeout(Some(Duration::from_millis(10)))
.map_err(TcpClientError::Io)?;
self.stream = Some(stream);
Ok(())
}
@@ -63,7 +66,9 @@ impl NetworkPort for StdTcpClient {
let mut payload = vec![0u8; len];
stream.set_nonblocking(false).map_err(TcpClientError::Io)?;
stream.read_exact(&mut payload).map_err(TcpClientError::Io)?;
stream
.read_exact(&mut payload)
.map_err(TcpClientError::Io)?;
stream.set_nonblocking(true).map_err(TcpClientError::Io)?;
Ok(Some(payload))

View File

@@ -1,12 +1,7 @@
use tokio::sync::broadcast;
use domain::{
BroadcastPort, Layout, WidgetId, WidgetState,
};
use protocol::{
ServerMessage, WidgetDescriptor, WireDisplayHint, WireLayoutNode,
encode,
};
use crate::error::TcpServerError;
use domain::{BroadcastPort, Layout, WidgetId, WidgetState};
use protocol::{ServerMessage, WidgetDescriptor, WireDisplayHint, WireLayoutNode, encode};
use tokio::sync::broadcast;
pub struct TcpBroadcaster {
tx: broadcast::Sender<Vec<u8>>,
@@ -37,13 +32,14 @@ impl BroadcastPort for TcpBroadcaster {
widgets: &[(WidgetId, WidgetState)],
) -> Result<(), Self::Error> {
let wire_layout: WireLayoutNode = (&layout.root).into();
let wire_widgets: Vec<WidgetDescriptor> = widgets.iter().map(|(id, state)| {
WidgetDescriptor {
let wire_widgets: Vec<WidgetDescriptor> = widgets
.iter()
.map(|(id, state)| WidgetDescriptor {
id: *id,
display_hint: WireDisplayHint::IconValue,
state: state.into(),
}
}).collect();
})
.collect();
let msg = ServerMessage::ScreenUpdate {
layout: wire_layout,
@@ -58,13 +54,14 @@ impl BroadcastPort for TcpBroadcaster {
&self,
updates: &[(WidgetId, WidgetState)],
) -> Result<(), Self::Error> {
let wire_widgets: Vec<WidgetDescriptor> = updates.iter().map(|(id, state)| {
WidgetDescriptor {
let wire_widgets: Vec<WidgetDescriptor> = updates
.iter()
.map(|(id, state)| WidgetDescriptor {
id: *id,
display_hint: WireDisplayHint::IconValue,
state: state.into(),
}
}).collect();
})
.collect();
let msg = ServerMessage::DataUpdate {
widgets: wire_widgets,

View File

@@ -1,6 +1,6 @@
use tokio::sync::broadcast;
use domain::{EventPublisher, DomainEvent};
use crate::error::TcpServerError;
use domain::{DomainEvent, EventPublisher};
use tokio::sync::broadcast;
pub struct TcpEventBus {
tx: broadcast::Sender<DomainEvent>,

View File

@@ -1,9 +1,9 @@
mod error;
mod broadcaster;
mod error;
mod event_bus;
mod server;
pub use error::TcpServerError;
pub use broadcaster::TcpBroadcaster;
pub use error::TcpServerError;
pub use event_bus::TcpEventBus;
pub use server::run_tcp_server;

View File

@@ -1,10 +1,10 @@
use std::sync::Arc;
use tokio::net::TcpListener;
use tokio::sync::broadcast;
use tokio::io::AsyncWriteExt;
use tracing::{info, warn};
use crate::broadcaster::TcpBroadcaster;
use crate::error::TcpServerError;
use std::sync::Arc;
use tokio::io::AsyncWriteExt;
use tokio::net::TcpListener;
use tokio::sync::broadcast;
use tracing::{info, warn};
pub async fn run_tcp_server(
addr: &str,

View File

@@ -1,6 +1,6 @@
use serde::{Serialize, Deserialize};
use std::time::Duration;
use domain::*;
use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Serialize, Deserialize)]
pub struct DataSourceDto {
@@ -21,11 +21,12 @@ impl From<&DataSource> for DataSourceDto {
source_type: match ds.source_type {
DataSourceType::Weather => "weather",
DataSourceType::Media => "media",
DataSourceType::Xtb => "xtb",
DataSourceType::Rss => "rss",
DataSourceType::HttpJson => "http_json",
DataSourceType::Webhook => "webhook",
}.into(),
}
.into(),
poll_interval_secs: ds.poll_interval.as_secs(),
url: ds.config.url.clone(),
api_key: ds.config.api_key.clone(),
@@ -39,7 +40,7 @@ impl DataSourceDto {
let source_type = match self.source_type.as_str() {
"weather" => DataSourceType::Weather,
"media" => DataSourceType::Media,
"xtb" => DataSourceType::Xtb,
"rss" => DataSourceType::Rss,
"http_json" => DataSourceType::HttpJson,
"webhook" => DataSourceType::Webhook,

View File

@@ -1,5 +1,5 @@
use serde::{Serialize, Deserialize};
use domain::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct SizingDto {
@@ -41,30 +41,41 @@ impl From<&LayoutNode> for LayoutNodeDto {
LayoutNode::Leaf(id) => Self {
node_type: "leaf".into(),
widget_id: Some(*id),
direction: None, gap: None, padding: None, children: None,
direction: None,
gap: None,
padding: None,
children: None,
},
LayoutNode::Container(c) => Self {
node_type: "container".into(),
widget_id: None,
direction: Some(match c.direction {
Direction::Row => "row",
Direction::Column => "column",
}.into()),
direction: Some(
match c.direction {
Direction::Row => "row",
Direction::Column => "column",
}
.into(),
),
gap: Some(c.gap),
padding: Some(c.padding),
children: Some(c.children.iter().map(|ch| LayoutChildDto {
sizing: SizingDto {
sizing_type: match ch.sizing {
Sizing::Fixed(_) => "fixed".into(),
Sizing::Flex(_) => "flex".into(),
},
value: match ch.sizing {
Sizing::Fixed(v) => v,
Sizing::Flex(v) => v as u16,
},
},
node: (&ch.node).into(),
}).collect()),
children: Some(
c.children
.iter()
.map(|ch| LayoutChildDto {
sizing: SizingDto {
sizing_type: match ch.sizing {
Sizing::Fixed(_) => "fixed".into(),
Sizing::Flex(_) => "flex".into(),
},
value: match ch.sizing {
Sizing::Fixed(v) => v,
Sizing::Flex(v) => v as u16,
},
},
node: (&ch.node).into(),
})
.collect(),
),
},
}
}
@@ -83,7 +94,9 @@ impl LayoutNodeDto {
"column" => Direction::Column,
d => return Err(format!("unknown direction: {d}")),
};
let children = self.children.ok_or("missing children")?
let children = self
.children
.ok_or("missing children")?
.into_iter()
.map(|ch| {
let sizing = match ch.sizing.sizing_type.as_str() {
@@ -110,12 +123,16 @@ impl LayoutNodeDto {
impl From<&Layout> for LayoutDto {
fn from(l: &Layout) -> Self {
Self { root: (&l.root).into() }
Self {
root: (&l.root).into(),
}
}
}
impl LayoutDto {
pub fn into_domain(self) -> Result<Layout, String> {
Ok(Layout { root: self.root.into_domain()? })
Ok(Layout {
root: self.root.into_domain()?,
})
}
}

View File

@@ -1,9 +1,9 @@
pub mod widget;
pub mod data_source;
pub mod layout;
pub mod preset;
pub mod widget;
pub use widget::{KeyMappingDto, WidgetDto, CreateWidgetDto};
pub use data_source::DataSourceDto;
pub use layout::{LayoutDto, LayoutNodeDto, LayoutChildDto, SizingDto};
pub use preset::{PresetDto, CreatePresetDto};
pub use layout::{LayoutChildDto, LayoutDto, LayoutNodeDto, SizingDto};
pub use preset::{CreatePresetDto, PresetDto};
pub use widget::{CreateWidgetDto, KeyMappingDto, WidgetDto};

View File

@@ -1,6 +1,6 @@
use serde::{Serialize, Deserialize};
use domain::*;
use crate::layout::LayoutDto;
use domain::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct PresetDto {

View File

@@ -1,5 +1,5 @@
use serde::{Serialize, Deserialize};
use domain::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct KeyMappingDto {
@@ -28,7 +28,9 @@ pub struct CreateWidgetDto {
pub max_data_size: u16,
}
fn default_max_data_size() -> u16 { 2048 }
fn default_max_data_size() -> u16 {
2048
}
impl From<&WidgetConfig> for WidgetDto {
fn from(w: &WidgetConfig) -> Self {
@@ -39,12 +41,17 @@ impl From<&WidgetConfig> for WidgetDto {
DisplayHint::IconValue => "icon_value",
DisplayHint::TextBlock => "text_block",
DisplayHint::KeyValue => "key_value",
}.into(),
}
.into(),
data_source_id: w.data_source_id,
mappings: w.mappings.iter().map(|m| KeyMappingDto {
source_path: m.source_path.clone(),
target_key: m.target_key.clone(),
}).collect(),
mappings: w
.mappings
.iter()
.map(|m| KeyMappingDto {
source_path: m.source_path.clone(),
target_key: m.target_key.clone(),
})
.collect(),
max_data_size: w.max_data_size,
}
}
@@ -63,10 +70,14 @@ impl CreateWidgetDto {
name: self.name,
display_hint: hint,
data_source_id: self.data_source_id,
mappings: self.mappings.into_iter().map(|m| KeyMapping {
source_path: m.source_path,
target_key: m.target_key,
}).collect(),
mappings: self
.mappings
.into_iter()
.map(|m| KeyMapping {
source_path: m.source_path,
target_key: m.target_key,
})
.collect(),
max_data_size: self.max_data_size,
})
}

View File

@@ -1,10 +1,8 @@
use std::fmt;
use domain::{
ConfigRepository, EventPublisher, DomainEvent,
WidgetConfig, WidgetId,
DataSource, DataSourceId, DataSourceValidationError,
Layout, LayoutPreset, LayoutPresetId,
ConfigRepository, DataSource, DataSourceId, DataSourceValidationError, DomainEvent,
EventPublisher, Layout, LayoutPreset, LayoutPresetId, WidgetConfig, WidgetId,
};
use std::fmt;
pub struct ConfigService<'a, C, E> {
config: &'a C,
@@ -34,78 +32,173 @@ where
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)?;
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)?;
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)?;
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>> {
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)?;
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>> {
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)?;
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)?;
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)?;
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)?;
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
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.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)?;
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)?;
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

@@ -1,15 +1,18 @@
use std::collections::HashMap;
use domain::{DataSourceId, Value, WidgetConfig, WidgetId, WidgetState};
use std::collections::HashMap;
#[derive(Default)]
pub struct DataProjection {
current: HashMap<WidgetId, WidgetState>,
}
impl DataProjection {
pub fn new() -> Self {
Self {
current: HashMap::new(),
}
Self::default()
}
pub fn get_state(&self, widget_id: WidgetId) -> Option<&WidgetState> {
self.current.get(&widget_id)
}
pub fn apply_poll_result(
@@ -27,9 +30,10 @@ impl DataProjection {
let new_state = config.extract(raw);
let is_changed = self.current
let is_changed = self
.current
.get(&config.id)
.map_or(true, |prev| *prev != new_state);
.is_none_or(|prev| *prev != new_state);
if is_changed {
self.current.insert(config.id, new_state.clone());

View File

@@ -1,13 +1,12 @@
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 domain::{
ConfigRepository, ContainerNode, DataSource, DataSourceConfig, DataSourceType, Direction,
DisplayHint, DomainEvent, KeyMapping, Layout, LayoutChild, LayoutNode, LayoutPreset, Sizing,
WidgetConfig,
};
use std::time::Duration;
use support::{InMemoryConfigRepository, InMemoryEventPublisher};
#[tokio::test]
@@ -21,9 +20,10 @@ async fn create_widget_persists_and_emits_event() {
"weather".into(),
DisplayHint::IconValue,
1,
vec![
KeyMapping { source_path: "$.temp".into(), target_key: "temperature".into() },
],
vec![KeyMapping {
source_path: "$.temp".into(),
target_key: "temperature".into(),
}],
);
service.create_widget(config).await.unwrap();
@@ -99,8 +99,14 @@ async fn update_layout_persists_and_emits_event() {
gap: 4,
padding: 2,
children: vec![
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(1) },
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(2) },
LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(1),
},
LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(2),
},
],
}),
};
@@ -111,7 +117,10 @@ async fn update_layout_persists_and_emits_event() {
assert_eq!(stored, Some(layout));
assert_eq!(events.emitted().len(), 1);
assert!(matches!(events.emitted()[0], DomainEvent::LayoutChanged { .. }));
assert!(matches!(
events.emitted()[0],
DomainEvent::LayoutChanged { .. }
));
}
#[tokio::test]
@@ -125,9 +134,10 @@ async fn load_preset_replaces_active_layout() {
direction: Direction::Column,
gap: 0,
padding: 0,
children: vec![
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(5) },
],
children: vec![LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(5),
}],
}),
};
@@ -146,6 +156,9 @@ async fn load_preset_replaces_active_layout() {
let emitted = events.emitted();
assert_eq!(emitted.len(), 2);
assert!(matches!(emitted[0], DomainEvent::LayoutPresetLoaded { id: 1 }));
assert!(matches!(
emitted[0],
DomainEvent::LayoutPresetLoaded { id: 1 }
));
assert!(matches!(emitted[1], DomainEvent::LayoutChanged { .. }));
}

View File

@@ -1,8 +1,6 @@
use std::collections::BTreeMap;
use domain::{
DisplayHint, KeyMapping, Value, WidgetConfig, WidgetId, WidgetState,
};
use application::DataProjection;
use domain::{DisplayHint, KeyMapping, Value, WidgetConfig, WidgetId, WidgetState};
use std::collections::BTreeMap;
fn weather_widget() -> WidgetConfig {
WidgetConfig::new(
@@ -11,8 +9,14 @@ fn weather_widget() -> WidgetConfig {
DisplayHint::IconValue,
10,
vec![
KeyMapping { source_path: "$.temp".into(), target_key: "temperature".into() },
KeyMapping { source_path: "$.icon".into(), target_key: "icon".into() },
KeyMapping {
source_path: "$.temp".into(),
target_key: "temperature".into(),
},
KeyMapping {
source_path: "$.icon".into(),
target_key: "icon".into(),
},
],
)
}
@@ -33,7 +37,10 @@ fn apply_poll_result_detects_new_widget_state() {
assert_eq!(changed.len(), 1);
assert_eq!(changed[0].0, 1);
assert_eq!(changed[0].1.data.get("temperature"), Some(&Value::Number(5.4)));
assert_eq!(
changed[0].1.data.get("temperature"),
Some(&Value::Number(5.4))
);
}
#[test]
@@ -56,7 +63,10 @@ fn apply_poll_result_detects_changed_value() {
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)));
assert_eq!(
changed[0].1.data.get("temperature"),
Some(&Value::Number(6.1))
);
}
#[test]
@@ -69,9 +79,10 @@ fn apply_poll_result_only_updates_widgets_bound_to_source() {
"portfolio".into(),
DisplayHint::KeyValue,
20,
vec![
KeyMapping { source_path: "$.value".into(), target_key: "amount".into() },
],
vec![KeyMapping {
source_path: "$.value".into(),
target_key: "amount".into(),
}],
),
];

View File

@@ -1,10 +1,9 @@
use std::sync::Mutex;
use std::collections::HashMap;
use domain::{
ConfigRepository, EventPublisher,
DataSource, DataSourceId, Layout, LayoutPreset, LayoutPresetId,
WidgetConfig, WidgetId, DomainEvent,
ConfigRepository, DataSource, DataSourceId, DomainEvent, EventPublisher, Layout, LayoutPreset,
LayoutPresetId, WidgetConfig, WidgetId,
};
use std::collections::HashMap;
use std::sync::Mutex;
pub struct InMemoryConfigRepository {
widgets: Mutex<HashMap<WidgetId, WidgetConfig>>,
@@ -45,7 +44,10 @@ impl ConfigRepository for InMemoryConfigRepository {
}
async fn save_widget(&self, config: &WidgetConfig) -> Result<(), Self::Error> {
self.widgets.lock().unwrap().insert(config.id, config.clone());
self.widgets
.lock()
.unwrap()
.insert(config.id, config.clone());
Ok(())
}
@@ -59,11 +61,20 @@ impl ConfigRepository for InMemoryConfigRepository {
}
async fn list_data_sources(&self) -> Result<Vec<DataSource>, Self::Error> {
Ok(self.data_sources.lock().unwrap().values().cloned().collect())
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());
self.data_sources
.lock()
.unwrap()
.insert(source.id, source.clone());
Ok(())
}
@@ -90,7 +101,10 @@ impl ConfigRepository for InMemoryConfigRepository {
}
async fn save_preset(&self, preset: &LayoutPreset) -> Result<(), Self::Error> {
self.presets.lock().unwrap().insert(preset.id, preset.clone());
self.presets
.lock()
.unwrap()
.insert(preset.id, preset.clone());
Ok(())
}

View File

@@ -10,6 +10,8 @@ config-sqlite.workspace = true
tcp-server.workspace = true
http-api.workspace = true
http-json.workspace = true
media-adapter.workspace = true
rss-adapter.workspace = true
tokio.workspace = true
anyhow.workspace = true
tracing.workspace = true

View File

@@ -12,10 +12,8 @@ impl ServerConfig {
Self {
database_url: env::var("KFRAME_DATABASE_URL")
.unwrap_or_else(|_| "sqlite:kframe.db?mode=rwc".into()),
tcp_addr: env::var("KFRAME_TCP_ADDR")
.unwrap_or_else(|_| "0.0.0.0:2699".into()),
http_addr: env::var("KFRAME_HTTP_ADDR")
.unwrap_or_else(|_| "0.0.0.0:3000".into()),
tcp_addr: env::var("KFRAME_TCP_ADDR").unwrap_or_else(|_| "0.0.0.0:2699".into()),
http_addr: env::var("KFRAME_HTTP_ADDR").unwrap_or_else(|_| "0.0.0.0:3000".into()),
poll_interval_secs: env::var("KFRAME_POLL_INTERVAL_SECS")
.ok()
.and_then(|v| v.parse().ok())

View File

@@ -0,0 +1,53 @@
use application::DataProjection;
use config_sqlite::SqliteConfigStore;
use domain::{BroadcastPort, ConfigRepository, DomainEvent};
use std::sync::Arc;
use tcp_server::{TcpBroadcaster, TcpEventBus};
use tokio::sync::Mutex;
use tracing::{error, info, warn};
pub async fn run(
event_bus: Arc<TcpEventBus>,
config: Arc<SqliteConfigStore>,
broadcaster: Arc<TcpBroadcaster>,
projection: Arc<Mutex<DataProjection>>,
) {
let mut rx = event_bus.subscribe();
loop {
match rx.recv().await {
Ok(DomainEvent::LayoutChanged { layout }) => {
let widgets = match config.list_widgets().await {
Ok(w) => w,
Err(e) => {
error!(error = %e, "failed to fetch widgets for screen update");
continue;
}
};
let proj = projection.lock().await;
let widget_states: Vec<_> = widgets
.iter()
.filter_map(|w| proj.get_state(w.id).map(|s| (w.id, s.clone())))
.collect();
if let Err(e) = broadcaster
.push_screen_update(&layout, &widget_states)
.await
{
error!(error = %e, "failed to push screen update");
}
info!("layout changed, pushed screen update to clients");
}
Ok(_) => {}
Err(tokio::sync::broadcast::error::RecvError::Lagged(n)) => {
warn!(skipped = n, "event handler lagged, missed events");
}
Err(tokio::sync::broadcast::error::RecvError::Closed) => {
error!("event bus closed");
break;
}
}
}
}

View File

@@ -1,14 +1,15 @@
mod config;
mod event_handler;
mod polling;
use std::sync::Arc;
use anyhow::Result;
use application::DataProjection;
use config_sqlite::SqliteConfigStore;
use tcp_server::{TcpBroadcaster, TcpEventBus, run_tcp_server};
use http_api::AppState;
use std::sync::Arc;
use tcp_server::{TcpBroadcaster, TcpEventBus, run_tcp_server};
use tokio::sync::Mutex;
use tracing::{info, error};
use tracing::{error, info};
#[tokio::main]
async fn main() -> Result<()> {
@@ -53,5 +54,19 @@ async fn main() -> Result<()> {
info!("K-Frame server running");
polling::run(config_store, broadcaster, projection, cfg.poll_interval_secs).await
let ev_bus = event_bus.clone();
let ev_config = config_store.clone();
let ev_bc = broadcaster.clone();
let ev_proj = projection.clone();
tokio::spawn(async move {
event_handler::run(ev_bus, ev_config, ev_bc, ev_proj).await;
});
polling::run(
config_store,
broadcaster,
projection,
cfg.poll_interval_secs,
)
.await
}

View File

@@ -1,16 +1,17 @@
use anyhow::Result;
use application::DataProjection;
use config_sqlite::SqliteConfigStore;
use domain::{
BroadcastPort, ConfigRepository, DataSource, DataSourcePort, DataSourceType, Value, WidgetState,
};
use http_json::HttpJsonAdapter;
use media_adapter::MediaAdapter;
use rss_adapter::RssAdapter;
use std::sync::Arc;
use std::time::Duration;
use anyhow::Result;
use domain::{
ConfigRepository, BroadcastPort, DataSourcePort, DataSourceType,
DataSource, Value, WidgetState,
};
use application::DataProjection;
use http_json::HttpJsonAdapter;
use tcp_server::TcpBroadcaster;
use config_sqlite::SqliteConfigStore;
use tokio::sync::Mutex;
use tracing::{info, warn, debug};
use tracing::{debug, info, warn};
pub async fn run(
config: Arc<SqliteConfigStore>,
@@ -19,6 +20,8 @@ pub async fn run(
poll_interval_secs: u64,
) -> Result<()> {
let http_adapter = HttpJsonAdapter::new();
let media_adapter = MediaAdapter::new();
let rss_adapter = RssAdapter::new();
let interval = Duration::from_secs(poll_interval_secs);
info!(interval_secs = poll_interval_secs, "polling loop started");
@@ -26,11 +29,17 @@ pub async fn run(
loop {
tokio::time::sleep(interval).await;
let sources = config.list_data_sources().await
let sources = config
.list_data_sources()
.await
.map_err(|e| anyhow::anyhow!("{e}"))?;
let widgets = config.list_widgets().await
let widgets = config
.list_widgets()
.await
.map_err(|e| anyhow::anyhow!("{e}"))?;
let layout = config.get_layout().await
let layout = config
.get_layout()
.await
.map_err(|e| anyhow::anyhow!("{e}"))?;
if sources.is_empty() || widgets.is_empty() {
@@ -41,13 +50,14 @@ pub async fn run(
let mut all_changed: Vec<(u16, WidgetState)> = Vec::new();
for source in &sources {
let result = match poll_source(&http_adapter, source).await {
Ok(v) => v,
Err(e) => {
warn!(source = %source.name, error = %e, "poll failed");
continue;
}
};
let result =
match poll_source(&http_adapter, &media_adapter, &rss_adapter, source).await {
Ok(v) => v,
Err(e) => {
warn!(source = %source.name, error = %e, "poll failed");
continue;
}
};
let mut proj = projection.lock().await;
let changed = proj.apply_poll_result(source.id, &result, &widgets);
@@ -56,7 +66,9 @@ pub async fn run(
if !all_changed.is_empty() {
if let Some(l) = &layout {
broadcaster.push_screen_update(l, &all_changed).await
broadcaster
.push_screen_update(l, &all_changed)
.await
.map_err(|e| anyhow::anyhow!("{e}"))?;
}
info!(count = all_changed.len(), "pushed widget updates");
@@ -66,15 +78,25 @@ pub async fn run(
async fn poll_source(
http_adapter: &HttpJsonAdapter,
media_adapter: &MediaAdapter,
rss_adapter: &RssAdapter,
source: &DataSource,
) -> Result<Value> {
match source.source_type {
DataSourceType::HttpJson | DataSourceType::Weather => {
http_adapter.poll(source).await
.map_err(|e| anyhow::anyhow!("{e}"))
}
_ => {
Err(anyhow::anyhow!("unsupported source type: {:?}", source.source_type))
}
DataSourceType::HttpJson | DataSourceType::Weather => http_adapter
.poll(source)
.await
.map_err(|e| anyhow::anyhow!("{e}")),
DataSourceType::Media => media_adapter
.poll(source)
.await
.map_err(|e| anyhow::anyhow!("{e}")),
DataSourceType::Rss => rss_adapter
.poll(source)
.await
.map_err(|e| anyhow::anyhow!("{e}")),
DataSourceType::Webhook => Err(anyhow::anyhow!(
"webhook sources are push-based, not polled"
)),
}
}

View File

@@ -1,9 +1,7 @@
use std::collections::HashMap;
use domain::LayoutNode;
use client_domain::{BoundingBox, LayoutEngine, RenderTree};
use protocol::{
ServerMessage, WidgetDescriptor, WireDisplayHint, WireWidgetState, WireLayoutNode,
};
use domain::LayoutNode;
use protocol::{ServerMessage, WidgetDescriptor, WireDisplayHint, WireLayoutNode, WireWidgetState};
use std::collections::HashMap;
pub struct ClientApp {
screen: BoundingBox,
@@ -33,9 +31,7 @@ impl ClientApp {
ServerMessage::ScreenUpdate { layout, widgets } => {
self.handle_screen_update(layout, widgets)
}
ServerMessage::DataUpdate { widgets } => {
self.handle_data_update(widgets)
}
ServerMessage::DataUpdate { widgets } => self.handle_data_update(widgets),
ServerMessage::Heartbeat => Vec::new(),
}
}
@@ -50,7 +46,8 @@ impl ClientApp {
self.widget_states.clear();
for w in &widgets {
self.widget_states.insert(w.id, (w.display_hint.clone(), w.state.clone()));
self.widget_states
.insert(w.id, (w.display_hint.clone(), w.state.clone()));
}
let repaints = self.build_repaints_for_all(&new_tree);
@@ -58,10 +55,7 @@ impl ClientApp {
repaints
}
fn handle_data_update(
&mut self,
widgets: Vec<WidgetDescriptor>,
) -> Vec<RepaintCommand> {
fn handle_data_update(&mut self, widgets: Vec<WidgetDescriptor>) -> Vec<RepaintCommand> {
let tree = match &self.render_tree {
Some(t) => t,
None => return Vec::new(),
@@ -70,9 +64,10 @@ impl ClientApp {
let mut repaints = Vec::new();
for w in widgets {
let changed = self.widget_states
let changed = self
.widget_states
.get(&w.id)
.map_or(true, |(_, prev_state)| *prev_state != w.state);
.is_none_or(|(_, prev_state)| *prev_state != w.state);
if changed {
if let Some(bounds) = tree.get_widget_bounds(w.id) {

View File

@@ -1,9 +1,8 @@
use client_application::{ClientApp, RepaintCommand};
use client_domain::BoundingBox;
use protocol::{
ServerMessage, WidgetDescriptor,
WireDisplayHint, WireLayoutNode, WireContainerNode, WireLayoutChild,
WireDirection, WireSizing, WireWidgetState, WireKeyValue, WireValue,
ServerMessage, WidgetDescriptor, WireContainerNode, WireDirection, WireDisplayHint,
WireKeyValue, WireLayoutChild, WireLayoutNode, WireSizing, WireValue, WireWidgetState,
};
fn screen() -> BoundingBox {
@@ -15,9 +14,10 @@ fn weather_descriptor(id: u16, temp: &str) -> WidgetDescriptor {
id,
display_hint: WireDisplayHint::IconValue,
state: WireWidgetState {
data: vec![
WireKeyValue { key: "temperature".into(), value: WireValue::String(temp.into()) },
],
data: vec![WireKeyValue {
key: "temperature".into(),
value: WireValue::String(temp.into()),
}],
error: None,
},
}
@@ -29,8 +29,14 @@ fn two_widget_layout() -> WireLayoutNode {
gap: 0,
padding: 0,
children: vec![
WireLayoutChild { sizing: WireSizing::Flex(1), node: WireLayoutNode::Leaf(1) },
WireLayoutChild { sizing: WireSizing::Flex(1), node: WireLayoutNode::Leaf(2) },
WireLayoutChild {
sizing: WireSizing::Flex(1),
node: WireLayoutNode::Leaf(1),
},
WireLayoutChild {
sizing: WireSizing::Flex(1),
node: WireLayoutNode::Leaf(2),
},
],
})
}
@@ -116,8 +122,14 @@ fn second_screen_update_repaints_all_widgets_with_new_layout() {
gap: 0,
padding: 0,
children: vec![
WireLayoutChild { sizing: WireSizing::Flex(1), node: WireLayoutNode::Leaf(1) },
WireLayoutChild { sizing: WireSizing::Flex(1), node: WireLayoutNode::Leaf(2) },
WireLayoutChild {
sizing: WireSizing::Flex(1),
node: WireLayoutNode::Leaf(1),
},
WireLayoutChild {
sizing: WireSizing::Flex(1),
node: WireLayoutNode::Leaf(2),
},
],
});

View File

@@ -1,11 +1,11 @@
use std::thread;
use std::sync::mpsc;
use std::time::Duration;
use client_domain::{BoundingBox, DisplayPort, NetworkPort};
use client_application::ClientApp;
use tcp_client::StdTcpClient;
use client_domain::{BoundingBox, DisplayPort, NetworkPort};
use display_terminal::TerminalDisplay;
use protocol::decode_server_message;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
use tcp_client::StdTcpClient;
fn main() {
let screen = BoundingBox::screen(240, 320);
@@ -35,12 +35,12 @@ fn main() {
}
match net.receive() {
Ok(Some(payload)) => {
match decode_server_message(&payload) {
Ok(msg) => { let _ = tx.send(msg); }
Err(e) => println!("[NET] Decode error: {e}"),
Ok(Some(payload)) => match decode_server_message(&payload) {
Ok(msg) => {
let _ = tx.send(msg);
}
}
Err(e) => println!("[NET] Decode error: {e}"),
},
Ok(None) => {
thread::sleep(Duration::from_millis(50));
}
@@ -67,11 +67,14 @@ fn main() {
for kv in &cmd.state.data {
if let protocol::WireValue::String(s) = &kv.value {
display.draw_text(
&format!("{}: {s}", kv.key),
cmd.bounds.x, cmd.bounds.y,
cmd.bounds,
).unwrap();
display
.draw_text(
&format!("{}: {s}", kv.key),
cmd.bounds.x,
cmd.bounds.y,
cmd.bounds,
)
.unwrap();
}
}
}

View File

@@ -8,10 +8,20 @@ pub struct BoundingBox {
impl BoundingBox {
pub fn new(x: u16, y: u16, width: u16, height: u16) -> Self {
Self { x, y, width, height }
Self {
x,
y,
width,
height,
}
}
pub fn screen(width: u16, height: u16) -> Self {
Self { x: 0, y: 0, width, height }
Self {
x: 0,
y: 0,
width,
height,
}
}
}

View File

@@ -1,6 +1,6 @@
use std::collections::HashMap;
use domain::{LayoutNode, ContainerNode, Direction, Sizing};
use crate::{BoundingBox, RenderTree};
use domain::{ContainerNode, Direction, LayoutNode, Sizing};
use std::collections::HashMap;
pub struct LayoutEngine;
@@ -11,11 +11,7 @@ impl LayoutEngine {
RenderTree { widget_bounds }
}
fn compute_node(
node: &LayoutNode,
bounds: BoundingBox,
out: &mut HashMap<u16, BoundingBox>,
) {
fn compute_node(node: &LayoutNode, bounds: BoundingBox, out: &mut HashMap<u16, BoundingBox>) {
match node {
LayoutNode::Leaf(id) => {
out.insert(*id, bounds);
@@ -48,16 +44,22 @@ impl LayoutEngine {
let total_gap = container.gap as u16 * (children.len() as u16).saturating_sub(1);
let available = total_axis.saturating_sub(total_gap);
let fixed_total: u16 = children.iter().map(|c| match c.sizing {
Sizing::Fixed(px) => px,
Sizing::Flex(_) => 0,
}).sum();
let fixed_total: u16 = children
.iter()
.map(|c| match c.sizing {
Sizing::Fixed(px) => px,
Sizing::Flex(_) => 0,
})
.sum();
let flex_space = available.saturating_sub(fixed_total);
let flex_total: u16 = children.iter().map(|c| match c.sizing {
Sizing::Flex(w) => w as u16,
Sizing::Fixed(_) => 0,
}).sum();
let flex_total: u16 = children
.iter()
.map(|c| match c.sizing {
Sizing::Flex(w) => w as u16,
Sizing::Fixed(_) => 0,
})
.sum();
let mut offset = 0u16;
@@ -74,19 +76,9 @@ impl LayoutEngine {
};
let child_bounds = if is_row {
BoundingBox::new(
inner.x + offset,
inner.y,
child_size,
inner.height,
)
BoundingBox::new(inner.x + offset, inner.y, child_size, inner.height)
} else {
BoundingBox::new(
inner.x,
inner.y + offset,
inner.width,
child_size,
)
BoundingBox::new(inner.x, inner.y + offset, inner.width, child_size)
};
Self::compute_node(&child.node, child_bounds, out);

View File

@@ -1,9 +1,9 @@
mod bounding_box;
mod layout_engine;
mod render_tree;
pub mod ports;
mod render_tree;
pub use bounding_box::BoundingBox;
pub use layout_engine::LayoutEngine;
pub use ports::{ClientConfig, DisplayPort, NetworkPort, StoragePort};
pub use render_tree::RenderTree;
pub use ports::{DisplayPort, NetworkPort, StoragePort, ClientConfig};

View File

@@ -4,7 +4,13 @@ pub trait DisplayPort {
type Error;
fn clear_region(&mut self, bounds: BoundingBox) -> Result<(), Self::Error>;
fn draw_text(&mut self, text: &str, x: u16, y: u16, bounds: BoundingBox) -> Result<(), Self::Error>;
fn draw_text(
&mut self,
text: &str,
x: u16,
y: u16,
bounds: BoundingBox,
) -> Result<(), Self::Error>;
fn draw_icon(&mut self, icon: &str, x: u16, y: u16) -> Result<(), Self::Error>;
fn fill_background(&mut self, bounds: BoundingBox) -> Result<(), Self::Error>;
fn flush(&mut self) -> Result<(), Self::Error>;

View File

@@ -4,4 +4,4 @@ mod storage;
pub use display::DisplayPort;
pub use network::NetworkPort;
pub use storage::{StoragePort, ClientConfig};
pub use storage::{ClientConfig, StoragePort};

View File

@@ -1,6 +1,6 @@
use std::collections::HashMap;
use domain::WidgetId;
use crate::BoundingBox;
use domain::WidgetId;
use std::collections::HashMap;
pub struct RenderTree {
pub widget_bounds: HashMap<WidgetId, BoundingBox>,

View File

@@ -1,7 +1,5 @@
use domain::{
LayoutNode, ContainerNode, LayoutChild, Direction, Sizing,
};
use client_domain::{BoundingBox, LayoutEngine, RenderTree};
use domain::{ContainerNode, Direction, LayoutChild, LayoutNode, Sizing};
fn screen() -> BoundingBox {
BoundingBox::screen(240, 320)
@@ -73,9 +71,18 @@ fn row_splits_width_among_equal_flex_children() {
let layout = row(vec![leaf(1), leaf(2), leaf(3)]);
let tree = LayoutEngine::compute(&layout, screen());
assert_eq!(tree.get_widget_bounds(1), Some(&BoundingBox::new(0, 0, 80, 320)));
assert_eq!(tree.get_widget_bounds(2), Some(&BoundingBox::new(80, 0, 80, 320)));
assert_eq!(tree.get_widget_bounds(3), Some(&BoundingBox::new(160, 0, 80, 320)));
assert_eq!(
tree.get_widget_bounds(1),
Some(&BoundingBox::new(0, 0, 80, 320))
);
assert_eq!(
tree.get_widget_bounds(2),
Some(&BoundingBox::new(80, 0, 80, 320))
);
assert_eq!(
tree.get_widget_bounds(3),
Some(&BoundingBox::new(160, 0, 80, 320))
);
}
#[test]
@@ -83,8 +90,14 @@ fn column_splits_height_among_equal_flex_children() {
let layout = column(vec![leaf(1), leaf(2)]);
let tree = LayoutEngine::compute(&layout, screen());
assert_eq!(tree.get_widget_bounds(1), Some(&BoundingBox::new(0, 0, 240, 160)));
assert_eq!(tree.get_widget_bounds(2), Some(&BoundingBox::new(0, 160, 240, 160)));
assert_eq!(
tree.get_widget_bounds(1),
Some(&BoundingBox::new(0, 0, 240, 160))
);
assert_eq!(
tree.get_widget_bounds(2),
Some(&BoundingBox::new(0, 160, 240, 160))
);
}
#[test]
@@ -92,8 +105,14 @@ fn fixed_and_flex_children_coexist() {
let layout = row(vec![leaf_fixed(1, 40), leaf(2)]);
let tree = LayoutEngine::compute(&layout, screen());
assert_eq!(tree.get_widget_bounds(1), Some(&BoundingBox::new(0, 0, 40, 320)));
assert_eq!(tree.get_widget_bounds(2), Some(&BoundingBox::new(40, 0, 200, 320)));
assert_eq!(
tree.get_widget_bounds(1),
Some(&BoundingBox::new(0, 0, 40, 320))
);
assert_eq!(
tree.get_widget_bounds(2),
Some(&BoundingBox::new(40, 0, 200, 320))
);
}
#[test]
@@ -102,8 +121,14 @@ fn gap_is_subtracted_before_distributing_space() {
let layout = row_with_gap(10, vec![leaf(1), leaf(2)]);
let tree = LayoutEngine::compute(&layout, screen());
assert_eq!(tree.get_widget_bounds(1), Some(&BoundingBox::new(0, 0, 115, 320)));
assert_eq!(tree.get_widget_bounds(2), Some(&BoundingBox::new(125, 0, 115, 320)));
assert_eq!(
tree.get_widget_bounds(1),
Some(&BoundingBox::new(0, 0, 115, 320))
);
assert_eq!(
tree.get_widget_bounds(2),
Some(&BoundingBox::new(125, 0, 115, 320))
);
}
#[test]
@@ -112,7 +137,10 @@ fn padding_insets_available_area() {
let layout = row_with_padding(10, vec![leaf(1)]);
let tree = LayoutEngine::compute(&layout, screen());
assert_eq!(tree.get_widget_bounds(1), Some(&BoundingBox::new(10, 10, 220, 300)));
assert_eq!(
tree.get_widget_bounds(1),
Some(&BoundingBox::new(10, 10, 220, 300))
);
}
#[test]
@@ -125,9 +153,18 @@ fn nested_containers_compute_correctly() {
let layout = row(vec![leaf(1), inner_col]);
let tree = LayoutEngine::compute(&layout, screen());
assert_eq!(tree.get_widget_bounds(1), Some(&BoundingBox::new(0, 0, 120, 320)));
assert_eq!(tree.get_widget_bounds(2), Some(&BoundingBox::new(120, 0, 120, 160)));
assert_eq!(tree.get_widget_bounds(3), Some(&BoundingBox::new(120, 160, 120, 160)));
assert_eq!(
tree.get_widget_bounds(1),
Some(&BoundingBox::new(0, 0, 120, 320))
);
assert_eq!(
tree.get_widget_bounds(2),
Some(&BoundingBox::new(120, 0, 120, 160))
);
assert_eq!(
tree.get_widget_bounds(3),
Some(&BoundingBox::new(120, 160, 120, 160))
);
}
#[test]
@@ -138,14 +175,32 @@ fn weighted_flex_distributes_proportionally() {
gap: 0,
padding: 0,
children: vec![
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(1) },
LayoutChild { sizing: Sizing::Flex(2), node: LayoutNode::Leaf(2) },
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(3) },
LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(1),
},
LayoutChild {
sizing: Sizing::Flex(2),
node: LayoutNode::Leaf(2),
},
LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(3),
},
],
});
let tree = LayoutEngine::compute(&layout, screen());
assert_eq!(tree.get_widget_bounds(1), Some(&BoundingBox::new(0, 0, 60, 320)));
assert_eq!(tree.get_widget_bounds(2), Some(&BoundingBox::new(60, 0, 120, 320)));
assert_eq!(tree.get_widget_bounds(3), Some(&BoundingBox::new(180, 0, 60, 320)));
assert_eq!(
tree.get_widget_bounds(1),
Some(&BoundingBox::new(0, 0, 60, 320))
);
assert_eq!(
tree.get_widget_bounds(2),
Some(&BoundingBox::new(60, 0, 120, 320))
);
assert_eq!(
tree.get_widget_bounds(3),
Some(&BoundingBox::new(180, 0, 60, 320))
);
}

View File

@@ -1,7 +1,5 @@
use domain::{
LayoutNode, ContainerNode, LayoutChild, Direction, Sizing,
};
use client_domain::{BoundingBox, LayoutEngine};
use domain::{ContainerNode, Direction, LayoutChild, LayoutNode, Sizing};
fn screen() -> BoundingBox {
BoundingBox::screen(240, 320)
@@ -14,8 +12,14 @@ fn diff_detects_moved_widget_after_layout_change() {
gap: 0,
padding: 0,
children: vec![
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(1) },
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(2) },
LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(1),
},
LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(2),
},
],
});
@@ -24,8 +28,14 @@ fn diff_detects_moved_widget_after_layout_change() {
gap: 0,
padding: 0,
children: vec![
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(1) },
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(2) },
LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(1),
},
LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(2),
},
],
});
@@ -44,8 +54,14 @@ fn diff_returns_empty_for_identical_layouts() {
gap: 0,
padding: 0,
children: vec![
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(1) },
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(2) },
LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(1),
},
LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(2),
},
],
});
@@ -61,18 +77,20 @@ fn diff_detects_added_and_removed_widgets() {
direction: Direction::Row,
gap: 0,
padding: 0,
children: vec![
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(1) },
],
children: vec![LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(1),
}],
});
let layout_b = LayoutNode::Container(ContainerNode {
direction: Direction::Row,
gap: 0,
padding: 0,
children: vec![
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(2) },
],
children: vec![LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(2),
}],
});
let tree_a = LayoutEngine::compute(&layout_a, screen());

View File

@@ -6,7 +6,6 @@ pub type DataSourceId = u16;
pub enum DataSourceType {
Weather,
Media,
Xtb,
Rss,
HttpJson,
Webhook,

View File

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

View File

@@ -1,5 +1,5 @@
use std::collections::BTreeMap;
use crate::value_objects::{DisplayHint, KeyMapping, Value, WidgetState};
use std::collections::BTreeMap;
pub type WidgetId = u16;
pub type DataSourceId = u16;
@@ -58,7 +58,8 @@ impl WidgetConfig {
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()
let truncated: String = s
.char_indices()
.take_while(|(i, _)| *i < max_bytes)
.map(|(_, c)| c)
.collect();

View File

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

View File

@@ -1,6 +1,6 @@
use std::future::Future;
use crate::entities::WidgetId;
use crate::value_objects::{Layout, WidgetState};
use std::future::Future;
pub trait BroadcastPort {
type Error;

View File

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

View File

@@ -1,6 +1,6 @@
use std::future::Future;
use crate::entities::DataSource;
use crate::value_objects::Value;
use std::future::Future;
pub trait DataSourcePort {
type Error;

View File

@@ -1,5 +1,5 @@
use std::future::Future;
use crate::events::DomainEvent;
use std::future::Future;
pub trait EventPublisher {
type Error;

View File

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

View File

@@ -1,5 +1,5 @@
use std::collections::BTreeSet;
use crate::entities::WidgetId;
use std::collections::BTreeSet;
#[derive(Debug, Clone, PartialEq)]
pub enum Sizing {
@@ -81,7 +81,9 @@ impl Layout {
fn collect_ids(node: &LayoutNode, ids: &mut BTreeSet<WidgetId>) {
match node {
LayoutNode::Leaf(id) => { ids.insert(*id); }
LayoutNode::Leaf(id) => {
ids.insert(*id);
}
LayoutNode::Container(c) => {
for child in &c.children {
Self::collect_ids(&child.node, ids);

View File

@@ -1,11 +1,11 @@
mod value;
mod key_mapping;
mod widget_state;
mod layout;
mod value;
mod widget_state;
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,
ContainerNode, Direction, Layout, LayoutChild, LayoutNode, LayoutValidationError, Sizing,
};
pub use value::Value;
pub use widget_state::{DisplayHint, WidgetError, WidgetState};

View File

@@ -17,10 +17,7 @@ impl Value {
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(),
Value::Object(map) => map.iter().map(|(k, v)| k.len() + v.estimated_size()).sum(),
}
}

View File

@@ -1,5 +1,5 @@
use std::collections::BTreeMap;
use super::Value;
use std::collections::BTreeMap;
#[derive(Debug, Clone, PartialEq)]
pub struct WidgetState {

View File

@@ -1,5 +1,5 @@
use std::time::Duration;
use domain::{DataSource, DataSourceConfig, DataSourceType, DataSourceValidationError};
use std::time::Duration;
fn make_source(source_type: DataSourceType, url: Option<&str>, poll: Duration) -> DataSource {
DataSource {
@@ -38,14 +38,22 @@ fn webhook_with_zero_interval_is_valid() {
#[test]
fn poll_based_source_requires_nonzero_interval() {
let source = make_source(DataSourceType::Weather, Some("https://api.weather.com"), Duration::ZERO);
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 source = make_source(
DataSourceType::Rss,
Some("https://feed.example.com"),
Duration::from_secs(300),
);
let errors = source.validate();
assert!(errors.is_empty());
}

View File

@@ -1,5 +1,5 @@
use std::collections::BTreeMap;
use domain::{KeyMapping, Value};
use std::collections::BTreeMap;
#[test]
fn extracts_value_at_path_and_renames_key() {
@@ -8,11 +8,10 @@ fn extracts_value_at_path_and_renames_key() {
target_key: "temperature".into(),
};
let raw = Value::Object(BTreeMap::from([
("main".into(), Value::Object(BTreeMap::from([
("temp".into(), Value::Number(5.4)),
]))),
]));
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))));
@@ -25,9 +24,7 @@ fn returns_none_when_path_does_not_match() {
target_key: "value".into(),
};
let raw = Value::Object(BTreeMap::from([
("other".into(), Value::Number(1.0)),
]));
let raw = Value::Object(BTreeMap::from([("other".into(), Value::Number(1.0))]));
assert_eq!(mapping.extract(&raw), None);
}

View File

@@ -1,8 +1,8 @@
use std::collections::BTreeSet;
use domain::{
ContainerNode, Direction, Layout, LayoutChild, LayoutNode, LayoutValidationError, Sizing,
WidgetId,
};
use std::collections::BTreeSet;
fn leaf(id: WidgetId) -> LayoutChild {
LayoutChild {
@@ -22,14 +22,18 @@ fn row(children: Vec<LayoutChild>) -> LayoutNode {
#[test]
fn validate_returns_empty_when_all_widgets_exist() {
let layout = Layout { root: row(vec![leaf(1), leaf(2)]) };
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 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)]);
@@ -49,7 +53,9 @@ fn validate_checks_nested_containers() {
sizing: Sizing::Flex(1),
node: row(vec![leaf(1), leaf(42)]),
};
let layout = Layout { root: row(vec![inner, leaf(2)]) };
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)]);
@@ -61,6 +67,8 @@ fn widget_ids_collects_all_leaf_ids() {
sizing: Sizing::Flex(1),
node: row(vec![leaf(3)]),
};
let layout = Layout { root: row(vec![leaf(1), inner, leaf(2)]) };
let layout = Layout {
root: row(vec![leaf(1), inner, leaf(2)]),
};
assert_eq!(layout.widget_ids(), BTreeSet::from([1, 2, 3]));
}

View File

@@ -1,5 +1,5 @@
use std::collections::BTreeMap;
use domain::Value;
use std::collections::BTreeMap;
#[test]
fn estimated_size_of_string_is_its_byte_length() {
@@ -29,37 +29,31 @@ fn estimated_size_of_nested_structure_sums_recursively() {
#[test]
fn estimated_size_of_array_sums_elements() {
let v = Value::Array(vec![
Value::String("abc".into()),
Value::Number(1.0),
]);
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)),
]));
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)),
]));
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![
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()))
@@ -68,10 +62,9 @@ fn get_path_accesses_array_by_index() {
#[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)),
]))),
]));
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

@@ -1,5 +1,5 @@
use std::collections::BTreeMap;
use domain::{DisplayHint, KeyMapping, Value, WidgetConfig};
use std::collections::BTreeMap;
#[test]
fn extract_applies_all_mappings_to_produce_widget_state() {
@@ -9,27 +9,39 @@ fn extract_applies_all_mappings_to_produce_widget_state() {
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() },
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())),
])),
])),
(
"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.data.get("icon"),
Some(&Value::String("cloud_rain".into()))
);
assert_eq!(state.error, None);
}
@@ -41,15 +53,14 @@ fn extract_truncates_string_values_exceeding_max_data_size() {
name: "news".into(),
display_hint: DisplayHint::TextBlock,
data_source_id: 1,
mappings: vec![
KeyMapping { source_path: "$.text".into(), target_key: "body".into() },
],
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 raw = Value::Object(BTreeMap::from([("text".into(), Value::String(long_text))]));
let state = config.extract(&raw);
match state.data.get("body") {
@@ -66,9 +77,18 @@ fn extract_respects_max_data_size_across_total_state() {
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() },
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,
};
@@ -92,15 +112,19 @@ fn extract_skips_mappings_that_dont_match() {
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() },
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 raw = Value::Object(BTreeMap::from([("temp".into(), Value::Number(5.4))]));
let state = config.extract(&raw);

View File

@@ -1,5 +1,5 @@
use serde::{Serialize, Deserialize};
use super::wire::{WireLayoutNode, WireWidgetState, WireDisplayHint};
use super::wire::{WireDisplayHint, WireLayoutNode, WireWidgetState};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WidgetDescriptor {

View File

@@ -1,13 +1,11 @@
mod wire;
mod frame;
mod wire;
pub use wire::{
WireValue, WireWidgetState, WireWidgetError, WireDisplayHint,
WireLayoutNode, WireContainerNode, WireLayoutChild, WireDirection, WireSizing,
WireKeyValue,
};
pub use frame::{
ServerMessage, ClientMessage, WidgetDescriptor,
encode, decode_server_message, encode_client, decode_client_message,
MAX_FRAME_SIZE,
ClientMessage, MAX_FRAME_SIZE, ServerMessage, WidgetDescriptor, decode_client_message,
decode_server_message, encode, encode_client,
};
pub use wire::{
WireContainerNode, WireDirection, WireDisplayHint, WireKeyValue, WireLayoutChild,
WireLayoutNode, WireSizing, WireValue, WireWidgetError, WireWidgetState,
};

View File

@@ -1,9 +1,9 @@
use std::collections::BTreeMap;
use serde::{Serialize, Deserialize};
use domain::value_objects::{
ContainerNode, Direction, DisplayHint, LayoutChild, LayoutNode, Sizing, Value,
WidgetError, WidgetState,
ContainerNode, Direction, DisplayHint, LayoutChild, LayoutNode, Sizing, Value, WidgetError,
WidgetState,
};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum WireValue {
@@ -84,10 +84,14 @@ pub struct WireWidgetState {
impl From<&WidgetState> for WireWidgetState {
fn from(s: &WidgetState) -> Self {
WireWidgetState {
data: s.data.iter().map(|(k, v)| WireKeyValue {
key: k.clone(),
value: v.into(),
}).collect(),
data: s
.data
.iter()
.map(|(k, v)| WireKeyValue {
key: k.clone(),
value: v.into(),
})
.collect(),
error: s.error.as_ref().map(Into::into),
}
}
@@ -96,7 +100,11 @@ impl From<&WidgetState> for WireWidgetState {
impl From<WireWidgetState> for WidgetState {
fn from(w: WireWidgetState) -> Self {
WidgetState {
data: w.data.into_iter().map(|kv| (kv.key, kv.value.into())).collect(),
data: w
.data
.into_iter()
.map(|kv| (kv.key, kv.value.into()))
.collect(),
error: w.error.map(Into::into),
}
}
@@ -205,10 +213,14 @@ impl From<&LayoutNode> for WireLayoutNode {
direction: (&c.direction).into(),
gap: c.gap,
padding: c.padding,
children: c.children.iter().map(|ch| WireLayoutChild {
sizing: (&ch.sizing).into(),
node: (&ch.node).into(),
}).collect(),
children: c
.children
.iter()
.map(|ch| WireLayoutChild {
sizing: (&ch.sizing).into(),
node: (&ch.node).into(),
})
.collect(),
}),
}
}
@@ -222,10 +234,14 @@ impl From<WireLayoutNode> for LayoutNode {
direction: c.direction.into(),
gap: c.gap,
padding: c.padding,
children: c.children.into_iter().map(|ch| LayoutChild {
sizing: ch.sizing.into(),
node: ch.node.into(),
}).collect(),
children: c
.children
.into_iter()
.map(|ch| LayoutChild {
sizing: ch.sizing.into(),
node: ch.node.into(),
})
.collect(),
}),
}
}

View File

@@ -1,24 +1,24 @@
use std::collections::BTreeMap;
use domain::{
Value, WidgetState, WidgetError, DisplayHint,
LayoutNode, ContainerNode, LayoutChild, Direction, Sizing,
ContainerNode, Direction, DisplayHint, LayoutChild, LayoutNode, Sizing, Value, WidgetError,
WidgetState,
};
use protocol::{
WireValue, WireWidgetState, WireWidgetError, WireDisplayHint,
WireLayoutNode, WireContainerNode, WireLayoutChild, WireDirection, WireSizing,
WireKeyValue,
WireContainerNode, WireDirection, WireDisplayHint, WireKeyValue, WireLayoutChild,
WireLayoutNode, WireSizing, WireValue, WireWidgetError, WireWidgetState,
};
use std::collections::BTreeMap;
#[test]
fn value_converts_to_wire_and_back() {
let original = Value::Object(BTreeMap::from([
("items".into(), Value::Array(vec![
let original = Value::Object(BTreeMap::from([(
"items".into(),
Value::Array(vec![
Value::String("hello".into()),
Value::Number(42.0),
Value::Bool(true),
Value::Null,
])),
]));
]),
)]));
let wire: WireValue = (&original).into();
let roundtripped: Value = wire.into();
@@ -28,9 +28,7 @@ fn value_converts_to_wire_and_back() {
#[test]
fn widget_state_with_error_converts_to_wire_and_back() {
let original = WidgetState {
data: BTreeMap::from([
("temp".into(), Value::Number(5.4)),
]),
data: BTreeMap::from([("temp".into(), Value::Number(5.4))]),
error: Some(WidgetError::SourceUnavailable),
};
@@ -56,12 +54,10 @@ fn layout_tree_converts_to_wire_and_back() {
direction: Direction::Column,
gap: 2,
padding: 0,
children: vec![
LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(2),
},
],
children: vec![LayoutChild {
sizing: Sizing::Flex(1),
node: LayoutNode::Leaf(2),
}],
}),
},
],
@@ -74,7 +70,11 @@ fn layout_tree_converts_to_wire_and_back() {
#[test]
fn display_hint_converts_to_wire_and_back() {
for hint in [DisplayHint::IconValue, DisplayHint::TextBlock, DisplayHint::KeyValue] {
for hint in [
DisplayHint::IconValue,
DisplayHint::TextBlock,
DisplayHint::KeyValue,
] {
let wire: WireDisplayHint = (&hint).into();
let roundtripped: DisplayHint = wire.into();
assert_eq!(hint, roundtripped);

View File

@@ -1,8 +1,7 @@
use protocol::{
ServerMessage, ClientMessage, WidgetDescriptor,
WireDisplayHint, WireLayoutNode, WireContainerNode, WireLayoutChild,
WireDirection, WireSizing, WireWidgetState, WireKeyValue, WireValue,
encode, decode_server_message, encode_client, decode_client_message,
ClientMessage, ServerMessage, WidgetDescriptor, WireContainerNode, WireDirection,
WireDisplayHint, WireKeyValue, WireLayoutChild, WireLayoutNode, WireSizing, WireValue,
WireWidgetState, decode_client_message, decode_server_message, encode, encode_client,
};
#[test]
@@ -23,19 +22,23 @@ fn screen_update_round_trips() {
},
],
}),
widgets: vec![
WidgetDescriptor {
id: 1,
display_hint: WireDisplayHint::IconValue,
state: WireWidgetState {
data: vec![
WireKeyValue { key: "temperature".into(), value: WireValue::String("5.4°C".into()) },
WireKeyValue { key: "icon".into(), value: WireValue::String("cloud_rain".into()) },
],
error: None,
},
widgets: vec![WidgetDescriptor {
id: 1,
display_hint: WireDisplayHint::IconValue,
state: WireWidgetState {
data: vec![
WireKeyValue {
key: "temperature".into(),
value: WireValue::String("5.4°C".into()),
},
WireKeyValue {
key: "icon".into(),
value: WireValue::String("cloud_rain".into()),
},
],
error: None,
},
],
}],
};
let frame = encode(&msg).unwrap();
@@ -47,18 +50,17 @@ fn screen_update_round_trips() {
#[test]
fn data_update_round_trips() {
let msg = ServerMessage::DataUpdate {
widgets: vec![
WidgetDescriptor {
id: 3,
display_hint: WireDisplayHint::TextBlock,
state: WireWidgetState {
data: vec![
WireKeyValue { key: "body".into(), value: WireValue::String("Breaking news...".into()) },
],
error: None,
},
widgets: vec![WidgetDescriptor {
id: 3,
display_hint: WireDisplayHint::TextBlock,
state: WireWidgetState {
data: vec![WireKeyValue {
key: "body".into(),
value: WireValue::String("Breaking news...".into()),
}],
error: None,
},
],
}],
};
let frame = encode(&msg).unwrap();

147
docs/k-frame-spa-handoff.md Normal file
View File

@@ -0,0 +1,147 @@
# K-Frame SPA Handoff
## What is K-Frame
IoT dashboard system. Server aggregates data from configurable sources and pushes to ESP32 display clients via TCP. The server is fully functional — SQLite config, REST API, TCP broadcasting, data source polling. ESP32 firmware works end-to-end (display renders widgets). Now needs a config UI.
## What this session should build
A React SPA (config/admin UI) for the K-Frame server. The SPA is at `/mnt/drive/dev/k-frame/spa/` — fresh Vite + React 19 + shadcn/ui + TanStack Router + TanStack Query setup. Currently shows a placeholder page.
## Existing artifacts to read first
- **Design spec**: `docs/superpowers/specs/2026-06-18-k-frame-design.md`
- **Domain glossary**: `CONTEXT.md`
- **ADRs**: `docs/adr/0001-event-driven-cqrs.md`, `0002-static-dispatch-over-trait-objects.md`, `0003-postcard-over-flatbuffers.md`
- **API types (DTO definitions)**: `crates/api-types/src/` — widget.rs, data_source.rs, layout.rs, preset.rs. These define the exact JSON shapes the API accepts/returns.
## REST API (server runs on :3000)
All endpoints return/accept JSON.
| Method | Path | Description |
|--------|------|-------------|
| GET | /api/widgets | List all widgets |
| POST | /api/widgets | Create widget |
| GET | /api/widgets/{id} | Get widget |
| PUT | /api/widgets/{id} | Update widget |
| DELETE | /api/widgets/{id} | Delete widget |
| GET | /api/data-sources | List all data sources |
| POST | /api/data-sources | Create data source |
| GET | /api/data-sources/{id} | Get data source |
| PUT | /api/data-sources/{id} | Update data source |
| DELETE | /api/data-sources/{id} | Delete data source |
| GET | /api/layout | Get current layout (nullable) |
| PUT | /api/layout | Update layout |
| GET | /api/presets | List presets |
| POST | /api/presets | Create preset |
| GET | /api/presets/{id} | Get preset |
| DELETE | /api/presets/{id} | Delete preset |
| POST | /api/presets/{id}/load | Load preset as active layout |
### Key JSON shapes
**Widget** (create/update):
```json
{
"id": 1,
"name": "weather",
"display_hint": "icon_value",
"data_source_id": 10,
"mappings": [
{"source_path": "$.main.temp", "target_key": "temperature"},
{"source_path": "$.weather[0].icon", "target_key": "icon"}
],
"max_data_size": 2048
}
```
`display_hint` values: `"icon_value"`, `"text_block"`, `"key_value"`
**Data Source** (create/update):
```json
{
"id": 10,
"name": "weather",
"source_type": "weather",
"poll_interval_secs": 300,
"url": "https://api.openweathermap.org/...",
"api_key": "xxx",
"headers": []
}
```
`source_type` values: `"weather"`, `"media"`, `"rss"`, `"http_json"`, `"webhook"`
**Layout**:
```json
{
"root": {
"type": "container",
"direction": "row",
"gap": 4,
"padding": 2,
"children": [
{
"sizing": {"type": "flex", "value": 1},
"node": {"type": "leaf", "widget_id": 1}
},
{
"sizing": {"type": "fixed", "value": 80},
"node": {"type": "leaf", "widget_id": 2}
}
]
}
}
```
Nodes are recursive — containers can nest.
**Preset**:
```json
{"id": 1, "name": "dashboard", "layout": { "root": { ... } }}
```
## Pages to build
1. **Dashboard** — overview of connected clients, active data sources, current layout. Landing page.
2. **Data Sources** — CRUD list. Form: name, source_type (select), URL, API key, poll interval, headers.
3. **Widgets** — CRUD list. Form: name, display_hint (select), data_source_id (select from existing sources), key mappings (dynamic list of source_path + target_key pairs), max_data_size.
4. **Layout Builder** — visual tree editor. Add containers (row/column), nest them, place widgets as leaves, set sizing (fixed/flex), gap, padding. This is the most complex page.
5. **Presets** — save current layout as preset, load presets, delete presets.
## SPA tech stack (already set up)
- React 19 + TypeScript
- Vite 8
- shadcn/ui (components already installed in `src/components/ui/`)
- TanStack Router (not yet configured with routes)
- TanStack Query (not yet configured with provider)
- Tailwind CSS 4
- Bun (lockfile is bun.lock)
## Server integration
The SPA's built files need to be served by the Axum server. Two approaches:
1. **Dev**: SPA runs on Vite dev server (:5173), proxies API calls to :3000. Add proxy config to `vite.config.ts`.
2. **Prod**: `bun run build` outputs to `spa/dist/`, Axum serves these as static files. Need to add static file serving to the http-api adapter.
The Vite proxy setup is needed first so development works.
## User preferences
- Concise, no filler
- No mocking — test against real API
- Clean code, modules, no huge files
- shadcn components for all UI elements
- TanStack Router for routing, TanStack Query for data fetching
- No Co-Authored-By in commits
## What NOT to change
- No changes to Rust crates (domain, application, adapters, etc.)
- No changes to the ESP32 firmware
- API is stable — build against it as-is
## Suggested skills
- `superpowers:brainstorming` — for designing the layout builder UX (most complex page)
- `frontend-design` — for visual design direction, making it not look like a default template
- `shadcn` — for component usage, composition, and styling patterns

24
spa/.gitignore vendored Normal file
View File

@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

7
spa/.prettierignore Normal file
View File

@@ -0,0 +1,7 @@
node_modules/
coverage/
.pnpm-store/
pnpm-lock.yaml
package-lock.json
pnpm-lock.yaml
yarn.lock

11
spa/.prettierrc Normal file
View File

@@ -0,0 +1,11 @@
{
"endOfLine": "lf",
"semi": false,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "es5",
"printWidth": 80,
"plugins": ["prettier-plugin-tailwindcss"],
"tailwindStylesheet": "src/index.css",
"tailwindFunctions": ["cn", "cva"]
}

21
spa/README.md Normal file
View File

@@ -0,0 +1,21 @@
# React + TypeScript + Vite + shadcn/ui
This is a template for a new Vite project with React, TypeScript, and shadcn/ui.
## Adding components
To add components to your app, run the following command:
```bash
npx shadcn@latest add button
```
This will place the ui components in the `src/components` directory.
## Using components
To use the components in your app, import them as follows:
```tsx
import { Button } from "@/components/ui/button"
```

1292
spa/bun.lock Normal file

File diff suppressed because it is too large Load Diff

25
spa/components.json Normal file
View File

@@ -0,0 +1,25 @@
{
"$schema": "https://ui.shadcn.com/schema.json",
"style": "radix-nova",
"rsc": false,
"tsx": true,
"tailwind": {
"config": "",
"css": "src/index.css",
"baseColor": "neutral",
"cssVariables": true,
"prefix": ""
},
"iconLibrary": "lucide",
"rtl": false,
"aliases": {
"components": "@/components",
"utils": "@/lib/utils",
"ui": "@/components/ui",
"lib": "@/lib",
"hooks": "@/hooks"
},
"menuColor": "default",
"menuAccent": "subtle",
"registries": {}
}

22
spa/eslint.config.js Normal file
View File

@@ -0,0 +1,22 @@
import js from '@eslint/js'
import globals from 'globals'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import tseslint from 'typescript-eslint'
import { defineConfig, globalIgnores } from 'eslint/config'
export default defineConfig([
globalIgnores(['dist']),
{
files: ['**/*.{ts,tsx}'],
extends: [
js.configs.recommended,
tseslint.configs.recommended,
reactHooks.configs.flat.recommended,
reactRefresh.configs.vite,
],
languageOptions: {
globals: globals.browser,
},
},
])

13
spa/index.html Normal file
View File

@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>K-Frame</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>

57
spa/package.json Normal file
View File

@@ -0,0 +1,57 @@
{
"name": "spa",
"private": true,
"version": "0.0.1",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "eslint .",
"format": "prettier --write \"**/*.{ts,tsx}\"",
"typecheck": "tsc --noEmit",
"preview": "vite preview"
},
"dependencies": {
"@base-ui/react": "^1.6.0",
"@fontsource-variable/geist": "^5.2.9",
"@tailwindcss/vite": "^4",
"@tanstack/react-query": "^5.101.0",
"@tanstack/react-router": "^1.170.16",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"cmdk": "^1.1.1",
"date-fns": "^4.4.0",
"embla-carousel-react": "^8.6.0",
"input-otp": "^1.4.2",
"lucide-react": "^1.21.0",
"next-themes": "^0.4.6",
"radix-ui": "^1.6.0",
"react": "^19.2.6",
"react-day-picker": "^10.0.1",
"react-dom": "^19.2.6",
"react-resizable-panels": "^4.11.2",
"recharts": "3.8.0",
"shadcn": "^4.11.0",
"sonner": "^2.0.7",
"tailwind-merge": "^3.6.0",
"tailwindcss": "^4",
"tw-animate-css": "^1.4.0",
"vaul": "^1.1.2"
},
"devDependencies": {
"@eslint/js": "^10",
"@types/node": "^24",
"@types/react": "^19",
"@types/react-dom": "^19",
"@vitejs/plugin-react": "^6",
"eslint": "^10",
"eslint-plugin-react-hooks": "^7.1.1",
"eslint-plugin-react-refresh": "^0.5.2",
"globals": "^17",
"prettier": "^3.8.3",
"prettier-plugin-tailwindcss": "^0.8.0",
"typescript": "~6",
"typescript-eslint": "^8",
"vite": "^8"
}
}

1
spa/public/vite.svg Normal file
View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

28
spa/src/api/client.ts Normal file
View File

@@ -0,0 +1,28 @@
const BASE = "/api"
async function request<T>(path: string, init?: RequestInit): Promise<T> {
const res = await fetch(`${BASE}${path}`, {
...init,
headers: {
"Content-Type": "application/json",
...init?.headers,
},
})
if (!res.ok) {
const text = await res.text().catch(() => res.statusText)
throw new Error(`${res.status}: ${text}`)
}
if (res.status === 204) return undefined as T
const text = await res.text()
if (!text) return null as T
return JSON.parse(text)
}
export const api = {
get: <T>(path: string) => request<T>(path),
post: <T>(path: string, body: unknown) =>
request<T>(path, { method: "POST", body: JSON.stringify(body) }),
put: <T>(path: string, body: unknown) =>
request<T>(path, { method: "PUT", body: JSON.stringify(body) }),
delete: <T>(path: string) => request<T>(path, { method: "DELETE" }),
}

View File

@@ -0,0 +1,50 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
import { api } from "./client"
import type { DataSource } from "./types"
const KEYS = {
all: ["data-sources"] as const,
one: (id: number) => ["data-sources", id] as const,
}
export function useDataSources() {
return useQuery({
queryKey: KEYS.all,
queryFn: () => api.get<DataSource[]>("/data-sources"),
})
}
export function useDataSource(id: number) {
return useQuery({
queryKey: KEYS.one(id),
queryFn: () => api.get<DataSource>(`/data-sources/${id}`),
})
}
export function useCreateDataSource() {
const qc = useQueryClient()
return useMutation({
mutationFn: (ds: DataSource) => api.post<DataSource>("/data-sources", ds),
onSuccess: () => qc.invalidateQueries({ queryKey: KEYS.all }),
})
}
export function useUpdateDataSource() {
const qc = useQueryClient()
return useMutation({
mutationFn: (ds: DataSource) =>
api.put<DataSource>(`/data-sources/${ds.id}`, ds),
onSuccess: (_, ds) => {
qc.invalidateQueries({ queryKey: KEYS.all })
qc.invalidateQueries({ queryKey: KEYS.one(ds.id) })
},
})
}
export function useDeleteDataSource() {
const qc = useQueryClient()
return useMutation({
mutationFn: (id: number) => api.delete(`/data-sources/${id}`),
onSuccess: () => qc.invalidateQueries({ queryKey: KEYS.all }),
})
}

22
spa/src/api/layout.ts Normal file
View File

@@ -0,0 +1,22 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
import { api } from "./client"
import type { Layout } from "./types"
const KEYS = {
current: ["layout"] as const,
}
export function useLayout() {
return useQuery({
queryKey: KEYS.current,
queryFn: () => api.get<Layout | null>("/layout"),
})
}
export function useUpdateLayout() {
const qc = useQueryClient()
return useMutation({
mutationFn: (layout: Layout) => api.put<Layout>("/layout", layout),
onSuccess: () => qc.invalidateQueries({ queryKey: KEYS.current }),
})
}

Some files were not shown because too many files have changed in this diff Show More