refactor adapters into modular file structure
config-sqlite: split into repository/ (per entity) + serialization/ (per type) + error.rs http-api: split into dto/ (per resource) + routes/ (per resource) tcp-server: split into broadcaster, event_bus, server, error rss: split parser from adapter, external tests media: split error, external tests
This commit is contained in:
14
crates/adapters/config-sqlite/src/error.rs
Normal file
14
crates/adapters/config-sqlite/src/error.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
#[derive(Debug)]
|
||||
pub enum SqliteConfigError {
|
||||
Sql(sqlx::Error),
|
||||
Serialization(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for SqliteConfigError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
SqliteConfigError::Sql(e) => write!(f, "sql: {e}"),
|
||||
SqliteConfigError::Serialization(e) => write!(f, "serialization: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,29 +1,10 @@
|
||||
pub mod error;
|
||||
mod serialization;
|
||||
mod repository;
|
||||
|
||||
use std::time::Duration;
|
||||
use sqlx::{SqlitePool, Row};
|
||||
use domain::{
|
||||
ConfigRepository,
|
||||
DataSource, DataSourceId, DataSourceConfig, DataSourceType,
|
||||
Layout, LayoutPreset, LayoutPresetId,
|
||||
WidgetConfig, WidgetId,
|
||||
};
|
||||
use serialization as ser;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum SqliteConfigError {
|
||||
Sql(sqlx::Error),
|
||||
Serialization(String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for SqliteConfigError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
SqliteConfigError::Sql(e) => write!(f, "sql: {e}"),
|
||||
SqliteConfigError::Serialization(e) => write!(f, "serialization: {e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub use error::SqliteConfigError;
|
||||
|
||||
pub struct SqliteConfigStore {
|
||||
pool: SqlitePool,
|
||||
@@ -77,186 +58,3 @@ impl SqliteConfigStore {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl ConfigRepository for SqliteConfigStore {
|
||||
type Error = SqliteConfigError;
|
||||
|
||||
async fn get_widget(&self, id: WidgetId) -> Result<Option<WidgetConfig>, Self::Error> {
|
||||
let row = sqlx::query("SELECT * FROM widgets WHERE id = ?")
|
||||
.bind(id as i64)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
match row {
|
||||
None => Ok(None),
|
||||
Some(row) => Ok(Some(ser::widget_from_row(&row)?)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn list_widgets(&self) -> Result<Vec<WidgetConfig>, Self::Error> {
|
||||
let rows = sqlx::query("SELECT * FROM widgets")
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
rows.iter().map(|r| ser::widget_from_row(r)).collect()
|
||||
}
|
||||
|
||||
async fn save_widget(&self, config: &WidgetConfig) -> Result<(), Self::Error> {
|
||||
let mappings_json = ser::mappings_to_json(&config.mappings)?;
|
||||
let hint_str = ser::display_hint_to_str(&config.display_hint);
|
||||
|
||||
sqlx::query(
|
||||
"INSERT OR REPLACE INTO widgets (id, name, display_hint, data_source_id, mappings, max_data_size)
|
||||
VALUES (?, ?, ?, ?, ?, ?)"
|
||||
)
|
||||
.bind(config.id as i64)
|
||||
.bind(&config.name)
|
||||
.bind(hint_str)
|
||||
.bind(config.data_source_id as i64)
|
||||
.bind(&mappings_json)
|
||||
.bind(config.max_data_size as i64)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_widget(&self, id: WidgetId) -> Result<(), Self::Error> {
|
||||
sqlx::query("DELETE FROM widgets WHERE id = ?")
|
||||
.bind(id as i64)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_data_source(&self, id: DataSourceId) -> Result<Option<DataSource>, Self::Error> {
|
||||
let row = sqlx::query("SELECT * FROM data_sources WHERE id = ?")
|
||||
.bind(id as i64)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
match row {
|
||||
None => Ok(None),
|
||||
Some(row) => Ok(Some(ser::data_source_from_row(&row)?)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn list_data_sources(&self) -> Result<Vec<DataSource>, Self::Error> {
|
||||
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()
|
||||
}
|
||||
|
||||
async fn save_data_source(&self, source: &DataSource) -> Result<(), Self::Error> {
|
||||
let config_json = ser::data_source_config_to_json(&source.config)?;
|
||||
let type_str = ser::data_source_type_to_str(&source.source_type);
|
||||
|
||||
sqlx::query(
|
||||
"INSERT OR REPLACE INTO data_sources (id, name, source_type, poll_interval_secs, config)
|
||||
VALUES (?, ?, ?, ?, ?)"
|
||||
)
|
||||
.bind(source.id as i64)
|
||||
.bind(&source.name)
|
||||
.bind(type_str)
|
||||
.bind(source.poll_interval.as_secs() as i64)
|
||||
.bind(&config_json)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_data_source(&self, id: DataSourceId) -> Result<(), Self::Error> {
|
||||
sqlx::query("DELETE FROM data_sources WHERE id = ?")
|
||||
.bind(id as i64)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_layout(&self) -> Result<Option<Layout>, Self::Error> {
|
||||
let row = sqlx::query("SELECT data FROM layout WHERE id = 1")
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
match row {
|
||||
None => Ok(None),
|
||||
Some(row) => {
|
||||
let json: String = row.get("data");
|
||||
Ok(Some(ser::layout_from_json(&json)?))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn save_layout(&self, layout: &Layout) -> Result<(), Self::Error> {
|
||||
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)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_preset(&self, id: LayoutPresetId) -> Result<Option<LayoutPreset>, Self::Error> {
|
||||
let row = sqlx::query("SELECT * FROM presets WHERE id = ?")
|
||||
.bind(id as i64)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
match row {
|
||||
None => Ok(None),
|
||||
Some(row) => Ok(Some(ser::preset_from_row(&row)?)),
|
||||
}
|
||||
}
|
||||
|
||||
async fn list_presets(&self) -> Result<Vec<LayoutPreset>, Self::Error> {
|
||||
let rows = sqlx::query("SELECT * FROM presets")
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
rows.iter().map(|r| ser::preset_from_row(r)).collect()
|
||||
}
|
||||
|
||||
async fn save_preset(&self, preset: &LayoutPreset) -> Result<(), Self::Error> {
|
||||
let layout_json = 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)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_preset(&self, id: LayoutPresetId) -> Result<(), Self::Error> {
|
||||
sqlx::query("DELETE FROM presets WHERE id = ?")
|
||||
.bind(id as i64)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
57
crates/adapters/config-sqlite/src/repository/data_sources.rs
Normal file
57
crates/adapters/config-sqlite/src/repository/data_sources.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
use domain::{DataSource, DataSourceId};
|
||||
use crate::SqliteConfigStore;
|
||||
use crate::error::SqliteConfigError;
|
||||
use crate::serialization::data_source as ser;
|
||||
|
||||
impl SqliteConfigStore {
|
||||
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)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
match row {
|
||||
None => Ok(None),
|
||||
Some(row) => Ok(Some(ser::data_source_from_row(&row)?)),
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
sqlx::query(
|
||||
"INSERT OR REPLACE INTO data_sources (id, name, source_type, poll_interval_secs, config)
|
||||
VALUES (?, ?, ?, ?, ?)"
|
||||
)
|
||||
.bind(source.id as i64)
|
||||
.bind(&source.name)
|
||||
.bind(type_str)
|
||||
.bind(source.poll_interval.as_secs() as i64)
|
||||
.bind(&config_json)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
36
crates/adapters/config-sqlite/src/repository/layout.rs
Normal file
36
crates/adapters/config-sqlite/src/repository/layout.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use sqlx::Row;
|
||||
use domain::Layout;
|
||||
use crate::SqliteConfigStore;
|
||||
use crate::error::SqliteConfigError;
|
||||
use crate::serialization::layout as ser;
|
||||
|
||||
impl SqliteConfigStore {
|
||||
pub(crate) async fn get_layout_impl(&self) -> Result<Option<Layout>, SqliteConfigError> {
|
||||
let row = sqlx::query("SELECT data FROM layout WHERE id = 1")
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
match row {
|
||||
None => Ok(None),
|
||||
Some(row) => {
|
||||
let json: String = row.get("data");
|
||||
Ok(Some(ser::layout_from_json(&json)?))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
73
crates/adapters/config-sqlite/src/repository/mod.rs
Normal file
73
crates/adapters/config-sqlite/src/repository/mod.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
mod widgets;
|
||||
mod data_sources;
|
||||
mod layout;
|
||||
mod presets;
|
||||
|
||||
use domain::{
|
||||
ConfigRepository,
|
||||
DataSource, DataSourceId,
|
||||
Layout, LayoutPreset, LayoutPresetId,
|
||||
WidgetConfig, WidgetId,
|
||||
};
|
||||
use crate::SqliteConfigStore;
|
||||
use crate::error::SqliteConfigError;
|
||||
|
||||
impl ConfigRepository for SqliteConfigStore {
|
||||
type Error = SqliteConfigError;
|
||||
|
||||
async fn get_widget(&self, id: WidgetId) -> Result<Option<WidgetConfig>, Self::Error> {
|
||||
self.get_widget_impl(id).await
|
||||
}
|
||||
|
||||
async fn list_widgets(&self) -> Result<Vec<WidgetConfig>, Self::Error> {
|
||||
self.list_widgets_impl().await
|
||||
}
|
||||
|
||||
async fn save_widget(&self, config: &WidgetConfig) -> Result<(), Self::Error> {
|
||||
self.save_widget_impl(config).await
|
||||
}
|
||||
|
||||
async fn delete_widget(&self, id: WidgetId) -> Result<(), Self::Error> {
|
||||
self.delete_widget_impl(id).await
|
||||
}
|
||||
|
||||
async fn get_data_source(&self, id: DataSourceId) -> Result<Option<DataSource>, Self::Error> {
|
||||
self.get_data_source_impl(id).await
|
||||
}
|
||||
|
||||
async fn list_data_sources(&self) -> Result<Vec<DataSource>, Self::Error> {
|
||||
self.list_data_sources_impl().await
|
||||
}
|
||||
|
||||
async fn save_data_source(&self, source: &DataSource) -> Result<(), Self::Error> {
|
||||
self.save_data_source_impl(source).await
|
||||
}
|
||||
|
||||
async fn delete_data_source(&self, id: DataSourceId) -> Result<(), Self::Error> {
|
||||
self.delete_data_source_impl(id).await
|
||||
}
|
||||
|
||||
async fn get_layout(&self) -> Result<Option<Layout>, Self::Error> {
|
||||
self.get_layout_impl().await
|
||||
}
|
||||
|
||||
async fn save_layout(&self, layout: &Layout) -> Result<(), Self::Error> {
|
||||
self.save_layout_impl(layout).await
|
||||
}
|
||||
|
||||
async fn get_preset(&self, id: LayoutPresetId) -> Result<Option<LayoutPreset>, Self::Error> {
|
||||
self.get_preset_impl(id).await
|
||||
}
|
||||
|
||||
async fn list_presets(&self) -> Result<Vec<LayoutPreset>, Self::Error> {
|
||||
self.list_presets_impl().await
|
||||
}
|
||||
|
||||
async fn save_preset(&self, preset: &LayoutPreset) -> Result<(), Self::Error> {
|
||||
self.save_preset_impl(preset).await
|
||||
}
|
||||
|
||||
async fn delete_preset(&self, id: LayoutPresetId) -> Result<(), Self::Error> {
|
||||
self.delete_preset_impl(id).await
|
||||
}
|
||||
}
|
||||
53
crates/adapters/config-sqlite/src/repository/presets.rs
Normal file
53
crates/adapters/config-sqlite/src/repository/presets.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use domain::{LayoutPreset, LayoutPresetId};
|
||||
use crate::SqliteConfigStore;
|
||||
use crate::error::SqliteConfigError;
|
||||
use crate::serialization::{layout as layout_ser, preset as ser};
|
||||
|
||||
impl SqliteConfigStore {
|
||||
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)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
match row {
|
||||
None => Ok(None),
|
||||
Some(row) => Ok(Some(ser::preset_from_row(&row)?)),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn list_presets_impl(&self) -> Result<Vec<LayoutPreset>, SqliteConfigError> {
|
||||
let rows = sqlx::query("SELECT * FROM presets")
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
rows.iter().map(|r| ser::preset_from_row(r)).collect()
|
||||
}
|
||||
|
||||
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)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
58
crates/adapters/config-sqlite/src/repository/widgets.rs
Normal file
58
crates/adapters/config-sqlite/src/repository/widgets.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
use domain::{WidgetConfig, WidgetId};
|
||||
use crate::SqliteConfigStore;
|
||||
use crate::error::SqliteConfigError;
|
||||
use crate::serialization::widget as ser;
|
||||
|
||||
impl SqliteConfigStore {
|
||||
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)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
match row {
|
||||
None => Ok(None),
|
||||
Some(row) => Ok(Some(ser::widget_from_row(&row)?)),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn list_widgets_impl(&self) -> Result<Vec<WidgetConfig>, SqliteConfigError> {
|
||||
let rows = sqlx::query("SELECT * FROM widgets")
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
rows.iter().map(|r| ser::widget_from_row(r)).collect()
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
sqlx::query(
|
||||
"INSERT OR REPLACE INTO widgets (id, name, display_hint, data_source_id, mappings, max_data_size)
|
||||
VALUES (?, ?, ?, ?, ?, ?)"
|
||||
)
|
||||
.bind(config.id as i64)
|
||||
.bind(&config.name)
|
||||
.bind(hint_str)
|
||||
.bind(config.data_source_id as i64)
|
||||
.bind(&mappings_json)
|
||||
.bind(config.max_data_size as i64)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn delete_widget_impl(&self, id: WidgetId) -> Result<(), SqliteConfigError> {
|
||||
sqlx::query("DELETE FROM widgets WHERE id = ?")
|
||||
.bind(id as i64)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(SqliteConfigError::Sql)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,220 +0,0 @@
|
||||
use std::time::Duration;
|
||||
use sqlx::Row;
|
||||
use sqlx::sqlite::SqliteRow;
|
||||
use domain::{
|
||||
ContainerNode, DataSource, DataSourceConfig, DataSourceType, Direction,
|
||||
DisplayHint, KeyMapping, Layout, LayoutChild, LayoutNode, LayoutPreset,
|
||||
Sizing, WidgetConfig,
|
||||
};
|
||||
use crate::SqliteConfigError;
|
||||
|
||||
pub fn display_hint_to_str(hint: &DisplayHint) -> &'static str {
|
||||
match hint {
|
||||
DisplayHint::IconValue => "icon_value",
|
||||
DisplayHint::TextBlock => "text_block",
|
||||
DisplayHint::KeyValue => "key_value",
|
||||
}
|
||||
}
|
||||
|
||||
fn display_hint_from_str(s: &str) -> Result<DisplayHint, SqliteConfigError> {
|
||||
match s {
|
||||
"icon_value" => Ok(DisplayHint::IconValue),
|
||||
"text_block" => Ok(DisplayHint::TextBlock),
|
||||
"key_value" => Ok(DisplayHint::KeyValue),
|
||||
_ => Err(SqliteConfigError::Serialization(format!("unknown display hint: {s}"))),
|
||||
}
|
||||
}
|
||||
|
||||
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",
|
||||
}
|
||||
}
|
||||
|
||||
fn data_source_type_from_str(s: &str) -> Result<DataSourceType, SqliteConfigError> {
|
||||
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}"))),
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
}).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()))?;
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
pub fn data_source_config_to_json(config: &DataSourceConfig) -> Result<String, SqliteConfigError> {
|
||||
let v = serde_json::json!({
|
||||
"url": config.url,
|
||||
"headers": config.headers,
|
||||
"api_key": config.api_key,
|
||||
});
|
||||
serde_json::to_string(&v).map_err(|e| SqliteConfigError::Serialization(e.to_string()))
|
||||
}
|
||||
|
||||
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 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(),
|
||||
None => vec![],
|
||||
};
|
||||
|
||||
Ok(DataSourceConfig { url, headers, api_key })
|
||||
}
|
||||
|
||||
pub fn layout_to_json(layout: &Layout) -> Result<String, SqliteConfigError> {
|
||||
let v = node_to_json(&layout.root);
|
||||
serde_json::to_string(&v).map_err(|e| SqliteConfigError::Serialization(e.to_string()))
|
||||
}
|
||||
|
||||
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 root = node_from_json(&v)?;
|
||||
Ok(Layout { root })
|
||||
}
|
||||
|
||||
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),
|
||||
})
|
||||
}).collect();
|
||||
|
||||
serde_json::json!({
|
||||
"type": "container",
|
||||
"direction": match c.direction { Direction::Row => "row", Direction::Column => "column" },
|
||||
"gap": c.gap,
|
||||
"padding": c.padding,
|
||||
"children": children,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn node_from_json(v: &serde_json::Value) -> Result<LayoutNode, SqliteConfigError> {
|
||||
let err = |msg: &str| SqliteConfigError::Serialization(msg.into());
|
||||
|
||||
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;
|
||||
Ok(LayoutNode::Leaf(id))
|
||||
}
|
||||
"container" => {
|
||||
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()
|
||||
.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),
|
||||
s => return Err(err(&format!("unknown sizing: {s}"))),
|
||||
};
|
||||
let node = node_from_json(&ch["node"])?;
|
||||
Ok(LayoutChild { sizing, node })
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
Ok(LayoutNode::Container(ContainerNode { direction, gap, padding, children }))
|
||||
}
|
||||
t => Err(err(&format!("unknown node type: {t}"))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn widget_from_row(row: &SqliteRow) -> Result<WidgetConfig, SqliteConfigError> {
|
||||
let id: i64 = row.get("id");
|
||||
let name: String = row.get("name");
|
||||
let hint_str: String = row.get("display_hint");
|
||||
let ds_id: i64 = row.get("data_source_id");
|
||||
let mappings_json: String = row.get("mappings");
|
||||
let max_size: i64 = row.get("max_data_size");
|
||||
|
||||
Ok(WidgetConfig {
|
||||
id: id as u16,
|
||||
name,
|
||||
display_hint: display_hint_from_str(&hint_str)?,
|
||||
data_source_id: ds_id as u16,
|
||||
mappings: mappings_from_json(&mappings_json)?,
|
||||
max_data_size: max_size as u16,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn data_source_from_row(row: &SqliteRow) -> Result<DataSource, SqliteConfigError> {
|
||||
let id: i64 = row.get("id");
|
||||
let name: String = row.get("name");
|
||||
let type_str: String = row.get("source_type");
|
||||
let interval_secs: i64 = row.get("poll_interval_secs");
|
||||
let config_json: String = row.get("config");
|
||||
|
||||
Ok(DataSource {
|
||||
id: id as u16,
|
||||
name,
|
||||
source_type: data_source_type_from_str(&type_str)?,
|
||||
poll_interval: Duration::from_secs(interval_secs as u64),
|
||||
config: data_source_config_from_json(&config_json)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn preset_from_row(row: &SqliteRow) -> Result<LayoutPreset, SqliteConfigError> {
|
||||
let id: i64 = row.get("id");
|
||||
let name: String = row.get("name");
|
||||
let layout_json: String = row.get("layout_data");
|
||||
|
||||
Ok(LayoutPreset {
|
||||
id: id as u16,
|
||||
name,
|
||||
layout: layout_from_json(&layout_json)?,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
use std::time::Duration;
|
||||
use sqlx::Row;
|
||||
use sqlx::sqlite::SqliteRow;
|
||||
use domain::{DataSource, DataSourceConfig, DataSourceType};
|
||||
use crate::error::SqliteConfigError;
|
||||
|
||||
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",
|
||||
}
|
||||
}
|
||||
|
||||
fn data_source_type_from_str(s: &str) -> Result<DataSourceType, SqliteConfigError> {
|
||||
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}"))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn data_source_config_to_json(config: &DataSourceConfig) -> Result<String, SqliteConfigError> {
|
||||
let v = serde_json::json!({
|
||||
"url": config.url,
|
||||
"headers": config.headers,
|
||||
"api_key": config.api_key,
|
||||
});
|
||||
serde_json::to_string(&v).map_err(|e| SqliteConfigError::Serialization(e.to_string()))
|
||||
}
|
||||
|
||||
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 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(),
|
||||
None => vec![],
|
||||
};
|
||||
|
||||
Ok(DataSourceConfig { url, headers, api_key })
|
||||
}
|
||||
|
||||
pub fn data_source_from_row(row: &SqliteRow) -> Result<DataSource, SqliteConfigError> {
|
||||
let id: i64 = row.get("id");
|
||||
let name: String = row.get("name");
|
||||
let type_str: String = row.get("source_type");
|
||||
let interval_secs: i64 = row.get("poll_interval_secs");
|
||||
let config_json: String = row.get("config");
|
||||
|
||||
Ok(DataSource {
|
||||
id: id as u16,
|
||||
name,
|
||||
source_type: data_source_type_from_str(&type_str)?,
|
||||
poll_interval: Duration::from_secs(interval_secs as u64),
|
||||
config: data_source_config_from_json(&config_json)?,
|
||||
})
|
||||
}
|
||||
77
crates/adapters/config-sqlite/src/serialization/layout.rs
Normal file
77
crates/adapters/config-sqlite/src/serialization/layout.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
use domain::{ContainerNode, Direction, Layout, LayoutChild, LayoutNode, Sizing};
|
||||
use crate::error::SqliteConfigError;
|
||||
|
||||
pub fn layout_to_json(layout: &Layout) -> Result<String, SqliteConfigError> {
|
||||
let v = node_to_json(&layout.root);
|
||||
serde_json::to_string(&v).map_err(|e| SqliteConfigError::Serialization(e.to_string()))
|
||||
}
|
||||
|
||||
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 root = node_from_json(&v)?;
|
||||
Ok(Layout { root })
|
||||
}
|
||||
|
||||
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),
|
||||
})
|
||||
}).collect();
|
||||
|
||||
serde_json::json!({
|
||||
"type": "container",
|
||||
"direction": match c.direction { Direction::Row => "row", Direction::Column => "column" },
|
||||
"gap": c.gap,
|
||||
"padding": c.padding,
|
||||
"children": children,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn node_from_json(v: &serde_json::Value) -> Result<LayoutNode, SqliteConfigError> {
|
||||
let err = |msg: &str| SqliteConfigError::Serialization(msg.into());
|
||||
|
||||
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;
|
||||
Ok(LayoutNode::Leaf(id))
|
||||
}
|
||||
"container" => {
|
||||
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()
|
||||
.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),
|
||||
s => return Err(err(&format!("unknown sizing: {s}"))),
|
||||
};
|
||||
let node = node_from_json(&ch["node"])?;
|
||||
Ok(LayoutChild { sizing, node })
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
Ok(LayoutNode::Container(ContainerNode { direction, gap, padding, children }))
|
||||
}
|
||||
t => Err(err(&format!("unknown node type: {t}"))),
|
||||
}
|
||||
}
|
||||
4
crates/adapters/config-sqlite/src/serialization/mod.rs
Normal file
4
crates/adapters/config-sqlite/src/serialization/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
pub mod widget;
|
||||
pub mod data_source;
|
||||
pub mod layout;
|
||||
pub mod preset;
|
||||
17
crates/adapters/config-sqlite/src/serialization/preset.rs
Normal file
17
crates/adapters/config-sqlite/src/serialization/preset.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
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");
|
||||
let name: String = row.get("name");
|
||||
let layout_json: String = row.get("layout_data");
|
||||
|
||||
Ok(LayoutPreset {
|
||||
id: id as u16,
|
||||
name,
|
||||
layout: layout_from_json(&layout_json)?,
|
||||
})
|
||||
}
|
||||
63
crates/adapters/config-sqlite/src/serialization/widget.rs
Normal file
63
crates/adapters/config-sqlite/src/serialization/widget.rs
Normal file
@@ -0,0 +1,63 @@
|
||||
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 {
|
||||
DisplayHint::IconValue => "icon_value",
|
||||
DisplayHint::TextBlock => "text_block",
|
||||
DisplayHint::KeyValue => "key_value",
|
||||
}
|
||||
}
|
||||
|
||||
fn display_hint_from_str(s: &str) -> Result<DisplayHint, SqliteConfigError> {
|
||||
match s {
|
||||
"icon_value" => Ok(DisplayHint::IconValue),
|
||||
"text_block" => Ok(DisplayHint::TextBlock),
|
||||
"key_value" => Ok(DisplayHint::KeyValue),
|
||||
_ => 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,
|
||||
})
|
||||
}).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()))?;
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
pub fn widget_from_row(row: &SqliteRow) -> Result<WidgetConfig, SqliteConfigError> {
|
||||
let id: i64 = row.get("id");
|
||||
let name: String = row.get("name");
|
||||
let hint_str: String = row.get("display_hint");
|
||||
let ds_id: i64 = row.get("data_source_id");
|
||||
let mappings_json: String = row.get("mappings");
|
||||
let max_size: i64 = row.get("max_data_size");
|
||||
|
||||
Ok(WidgetConfig {
|
||||
id: id as u16,
|
||||
name,
|
||||
display_hint: display_hint_from_str(&hint_str)?,
|
||||
data_source_id: ds_id as u16,
|
||||
mappings: mappings_from_json(&mappings_json)?,
|
||||
max_data_size: max_size as u16,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user