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:
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