arch: split ConfigRepository, extract polling, consolidate conversions, decouple protocol
- Value↔JSON: From impls on domain Value behind `json` feature, delete 4 duplicate converters - ConfigRepository split into ConfigRepository (12), UserRepository (3), WidgetStateCache (2) - polling orchestration moved from bootstrap to application::polling_service - WidgetRenderer in client-domain owns scroll/cache, both clients use it - network loop consolidated into client-application::run_connection_loop - protocol crate drops domain dep, Wire↔Domain conversions move to adapters
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use crate::conversions::{display_hint_to_wire, layout_to_wire, widget_state_to_wire};
|
||||
use crate::error::TcpServerError;
|
||||
use domain::{BroadcastPort, DisplayHint, Layout, ThemeConfig, WidgetId, WidgetState};
|
||||
use protocol::{ServerMessage, WidgetDescriptor, WireColor, WireLayoutNode, WireTheme, encode};
|
||||
use protocol::{ServerMessage, WidgetDescriptor, WireColor, WireTheme, encode};
|
||||
use tokio::sync::broadcast;
|
||||
|
||||
pub struct TcpBroadcaster {
|
||||
@@ -31,13 +32,13 @@ impl BroadcastPort for TcpBroadcaster {
|
||||
layout: &Layout,
|
||||
widgets: &[(WidgetId, DisplayHint, WidgetState)],
|
||||
) -> Result<(), Self::Error> {
|
||||
let wire_layout: WireLayoutNode = (&layout.root).into();
|
||||
let wire_layout = layout_to_wire(&layout.root);
|
||||
let wire_widgets: Vec<WidgetDescriptor> = widgets
|
||||
.iter()
|
||||
.map(|(id, hint, state)| WidgetDescriptor {
|
||||
id: *id,
|
||||
display_hint: hint.into(),
|
||||
state: state.into(),
|
||||
display_hint: display_hint_to_wire(hint),
|
||||
state: widget_state_to_wire(state),
|
||||
})
|
||||
.collect();
|
||||
|
||||
@@ -58,8 +59,8 @@ impl BroadcastPort for TcpBroadcaster {
|
||||
.iter()
|
||||
.map(|(id, hint, state)| WidgetDescriptor {
|
||||
id: *id,
|
||||
display_hint: hint.into(),
|
||||
state: state.into(),
|
||||
display_hint: display_hint_to_wire(hint),
|
||||
state: widget_state_to_wire(state),
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
||||
103
crates/adapters/tcp-server/src/conversions.rs
Normal file
103
crates/adapters/tcp-server/src/conversions.rs
Normal file
@@ -0,0 +1,103 @@
|
||||
use domain::value_objects::{
|
||||
AlignItems, Direction, DisplayHint, DisplayHintKind, HAlign, JustifyContent, LayoutNode,
|
||||
Sizing, VAlign, Value, WidgetError, WidgetState,
|
||||
};
|
||||
use protocol::{
|
||||
WireAlignItems, WireContainerNode, WireDirection, WireDisplayHint, WireDisplayHintKind,
|
||||
WireHAlign, WireJustifyContent, WireKeyValue, WireLayoutChild, WireLayoutNode, WireSizing,
|
||||
WireVAlign, WireValue, WireWidgetError, WireWidgetState,
|
||||
};
|
||||
|
||||
pub fn value_to_wire(v: &Value) -> WireValue {
|
||||
match v {
|
||||
Value::Null => WireValue::Null,
|
||||
Value::Bool(b) => WireValue::Bool(*b),
|
||||
Value::Number(n) => WireValue::Number(*n),
|
||||
Value::String(s) => WireValue::String(s.clone()),
|
||||
Value::Array(arr) => WireValue::Array(arr.iter().map(value_to_wire).collect()),
|
||||
Value::Object(map) => WireValue::Object(
|
||||
map.iter()
|
||||
.map(|(k, v)| (k.clone(), value_to_wire(v)))
|
||||
.collect(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn widget_error_to_wire(e: &WidgetError) -> WireWidgetError {
|
||||
match e {
|
||||
WidgetError::SourceUnavailable => WireWidgetError::SourceUnavailable,
|
||||
WidgetError::ExtractionFailed => WireWidgetError::ExtractionFailed,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn widget_state_to_wire(s: &WidgetState) -> WireWidgetState {
|
||||
WireWidgetState {
|
||||
data: s
|
||||
.data
|
||||
.iter()
|
||||
.map(|(k, v)| WireKeyValue {
|
||||
key: k.clone(),
|
||||
value: value_to_wire(v),
|
||||
})
|
||||
.collect(),
|
||||
error: s.error.as_ref().map(widget_error_to_wire),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn display_hint_to_wire(h: &DisplayHint) -> WireDisplayHint {
|
||||
WireDisplayHint {
|
||||
kind: match h.kind {
|
||||
DisplayHintKind::IconValue => WireDisplayHintKind::IconValue,
|
||||
DisplayHintKind::TextBlock => WireDisplayHintKind::TextBlock,
|
||||
DisplayHintKind::KeyValue => WireDisplayHintKind::KeyValue,
|
||||
},
|
||||
h_align: match h.h_align {
|
||||
HAlign::Left => WireHAlign::Left,
|
||||
HAlign::Center => WireHAlign::Center,
|
||||
HAlign::Right => WireHAlign::Right,
|
||||
},
|
||||
v_align: match h.v_align {
|
||||
VAlign::Top => WireVAlign::Top,
|
||||
VAlign::Middle => WireVAlign::Middle,
|
||||
VAlign::Bottom => WireVAlign::Bottom,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn layout_to_wire(n: &LayoutNode) -> WireLayoutNode {
|
||||
match n {
|
||||
LayoutNode::Leaf(id) => WireLayoutNode::Leaf(*id),
|
||||
LayoutNode::Container(c) => WireLayoutNode::Container(WireContainerNode {
|
||||
direction: match c.direction {
|
||||
Direction::Row => WireDirection::Row,
|
||||
Direction::Column => WireDirection::Column,
|
||||
},
|
||||
gap: c.gap,
|
||||
padding: c.padding,
|
||||
justify_content: match c.justify_content {
|
||||
JustifyContent::Start => WireJustifyContent::Start,
|
||||
JustifyContent::Center => WireJustifyContent::Center,
|
||||
JustifyContent::End => WireJustifyContent::End,
|
||||
JustifyContent::SpaceBetween => WireJustifyContent::SpaceBetween,
|
||||
JustifyContent::SpaceEvenly => WireJustifyContent::SpaceEvenly,
|
||||
},
|
||||
align_items: match c.align_items {
|
||||
AlignItems::Start => WireAlignItems::Start,
|
||||
AlignItems::Center => WireAlignItems::Center,
|
||||
AlignItems::End => WireAlignItems::End,
|
||||
AlignItems::Stretch => WireAlignItems::Stretch,
|
||||
},
|
||||
children: c
|
||||
.children
|
||||
.iter()
|
||||
.map(|ch| WireLayoutChild {
|
||||
sizing: match ch.sizing {
|
||||
Sizing::Fixed(px) => WireSizing::Fixed(px),
|
||||
Sizing::Flex(w) => WireSizing::Flex(w),
|
||||
},
|
||||
node: layout_to_wire(&ch.node),
|
||||
})
|
||||
.collect(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
mod broadcaster;
|
||||
mod client_tracker;
|
||||
mod conversions;
|
||||
mod error;
|
||||
mod event_bus;
|
||||
mod server;
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
use crate::broadcaster::domain_theme_to_wire;
|
||||
use crate::client_tracker::ClientTracker;
|
||||
use crate::conversions::{display_hint_to_wire, layout_to_wire, widget_state_to_wire};
|
||||
use crate::error::TcpServerError;
|
||||
use domain::{ConfigRepository, WidgetStateReader};
|
||||
use protocol::{ServerMessage, WidgetDescriptor, WireLayoutNode, encode};
|
||||
use protocol::{ServerMessage, WidgetDescriptor, encode};
|
||||
use std::sync::Arc;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::net::TcpListener;
|
||||
@@ -87,14 +88,14 @@ where
|
||||
}
|
||||
};
|
||||
|
||||
let wire_layout: WireLayoutNode = (&layout.root).into();
|
||||
let wire_layout = layout_to_wire(&layout.root);
|
||||
let mut wire_widgets = Vec::new();
|
||||
for w in &widgets {
|
||||
if let Some(s) = widget_states.get_widget_state(w.id).await {
|
||||
wire_widgets.push(WidgetDescriptor {
|
||||
id: w.id,
|
||||
display_hint: (&w.display_hint).into(),
|
||||
state: (&s).into(),
|
||||
display_hint: display_hint_to_wire(&w.display_hint),
|
||||
state: widget_state_to_wire(&s),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user