expose h_align/v_align through full stack
display_hint becomes {kind, h_align, v_align} object in API, SQLite
gets alignment columns, SPA widget form gets alignment selects, layout
preview reflects actual alignment instead of hardcoded center
This commit is contained in:
@@ -96,6 +96,14 @@ impl SqliteConfigStore {
|
||||
.execute(&self.pool)
|
||||
.await?;
|
||||
|
||||
// Add alignment columns to widgets (idempotent)
|
||||
let _ = sqlx::query("ALTER TABLE widgets ADD COLUMN h_align TEXT NOT NULL DEFAULT 'left'")
|
||||
.execute(&self.pool)
|
||||
.await;
|
||||
let _ = sqlx::query("ALTER TABLE widgets ADD COLUMN v_align TEXT NOT NULL DEFAULT 'top'")
|
||||
.execute(&self.pool)
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,15 +34,19 @@ impl SqliteConfigStore {
|
||||
config: &WidgetConfig,
|
||||
) -> Result<(), SqliteConfigError> {
|
||||
let mappings_json = ser::mappings_to_json(&config.mappings)?;
|
||||
let hint_str = ser::display_hint_to_str(&config.display_hint);
|
||||
let hint_str = ser::display_hint_kind_to_str(&config.display_hint);
|
||||
let h_align_str = ser::h_align_to_str(config.display_hint.h_align);
|
||||
let v_align_str = ser::v_align_to_str(config.display_hint.v_align);
|
||||
|
||||
sqlx::query(
|
||||
"INSERT OR REPLACE INTO widgets (id, name, display_hint, data_source_id, mappings, max_data_size)
|
||||
VALUES (?, ?, ?, ?, ?, ?)"
|
||||
"INSERT OR REPLACE INTO widgets (id, name, display_hint, h_align, v_align, data_source_id, mappings, max_data_size)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
|
||||
)
|
||||
.bind(config.id as i64)
|
||||
.bind(&config.name)
|
||||
.bind(hint_str)
|
||||
.bind(h_align_str)
|
||||
.bind(v_align_str)
|
||||
.bind(config.data_source_id as i64)
|
||||
.bind(&mappings_json)
|
||||
.bind(config.max_data_size as i64)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
use crate::error::SqliteConfigError;
|
||||
use domain::{DisplayHint, DisplayHintKind, KeyMapping, WidgetConfig};
|
||||
use domain::{DisplayHint, DisplayHintKind, HAlign, KeyMapping, VAlign, WidgetConfig};
|
||||
use sqlx::Row;
|
||||
use sqlx::sqlite::SqliteRow;
|
||||
|
||||
pub fn display_hint_to_str(hint: &DisplayHint) -> &'static str {
|
||||
pub fn display_hint_kind_to_str(hint: &DisplayHint) -> &'static str {
|
||||
match hint.kind {
|
||||
DisplayHintKind::IconValue => "icon_value",
|
||||
DisplayHintKind::TextBlock => "text_block",
|
||||
@@ -11,17 +11,55 @@ pub fn display_hint_to_str(hint: &DisplayHint) -> &'static str {
|
||||
}
|
||||
}
|
||||
|
||||
fn display_hint_from_str(s: &str) -> Result<DisplayHint, SqliteConfigError> {
|
||||
pub fn h_align_to_str(a: HAlign) -> &'static str {
|
||||
match a {
|
||||
HAlign::Left => "left",
|
||||
HAlign::Center => "center",
|
||||
HAlign::Right => "right",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn v_align_to_str(a: VAlign) -> &'static str {
|
||||
match a {
|
||||
VAlign::Top => "top",
|
||||
VAlign::Middle => "middle",
|
||||
VAlign::Bottom => "bottom",
|
||||
}
|
||||
}
|
||||
|
||||
fn hint_kind_from_str(s: &str) -> Result<DisplayHintKind, SqliteConfigError> {
|
||||
match s {
|
||||
"icon_value" => Ok(DisplayHint::new(DisplayHintKind::IconValue)),
|
||||
"text_block" => Ok(DisplayHint::new(DisplayHintKind::TextBlock)),
|
||||
"key_value" => Ok(DisplayHint::new(DisplayHintKind::KeyValue)),
|
||||
"icon_value" => Ok(DisplayHintKind::IconValue),
|
||||
"text_block" => Ok(DisplayHintKind::TextBlock),
|
||||
"key_value" => Ok(DisplayHintKind::KeyValue),
|
||||
_ => Err(SqliteConfigError::Serialization(format!(
|
||||
"unknown display hint: {s}"
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
fn h_align_from_str(s: &str) -> Result<HAlign, SqliteConfigError> {
|
||||
match s {
|
||||
"left" => Ok(HAlign::Left),
|
||||
"center" => Ok(HAlign::Center),
|
||||
"right" => Ok(HAlign::Right),
|
||||
_ => Err(SqliteConfigError::Serialization(format!(
|
||||
"unknown h_align: {s}"
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
fn v_align_from_str(s: &str) -> Result<VAlign, SqliteConfigError> {
|
||||
match s {
|
||||
"top" => Ok(VAlign::Top),
|
||||
"middle" => Ok(VAlign::Middle),
|
||||
"bottom" => Ok(VAlign::Bottom),
|
||||
_ => Err(SqliteConfigError::Serialization(format!(
|
||||
"unknown v_align: {s}"
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn mappings_to_json(mappings: &[KeyMapping]) -> Result<String, SqliteConfigError> {
|
||||
let entries: Vec<serde_json::Value> = mappings
|
||||
.iter()
|
||||
@@ -60,6 +98,8 @@ pub fn widget_from_row(row: &SqliteRow) -> Result<WidgetConfig, SqliteConfigErro
|
||||
let id: i64 = row.get("id");
|
||||
let name: String = row.get("name");
|
||||
let hint_str: String = row.get("display_hint");
|
||||
let h_align_str: String = row.get("h_align");
|
||||
let v_align_str: String = row.get("v_align");
|
||||
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");
|
||||
@@ -67,7 +107,11 @@ pub fn widget_from_row(row: &SqliteRow) -> Result<WidgetConfig, SqliteConfigErro
|
||||
Ok(WidgetConfig {
|
||||
id: id as u16,
|
||||
name,
|
||||
display_hint: display_hint_from_str(&hint_str)?,
|
||||
display_hint: DisplayHint {
|
||||
kind: hint_kind_from_str(&hint_str)?,
|
||||
h_align: h_align_from_str(&h_align_str)?,
|
||||
v_align: v_align_from_str(&v_align_str)?,
|
||||
},
|
||||
data_source_id: ds_id as u16,
|
||||
mappings: mappings_from_json(&mappings_json)?,
|
||||
max_data_size: max_size as u16,
|
||||
|
||||
@@ -86,7 +86,7 @@ async fn create_and_get_widget() {
|
||||
let body = r#"{
|
||||
"id": 1,
|
||||
"name": "weather",
|
||||
"display_hint": "icon_value",
|
||||
"display_hint": {"kind": "icon_value", "h_align": "left", "v_align": "top"},
|
||||
"data_source_id": 10,
|
||||
"mappings": [{"source_path": "$.temp", "target_key": "temperature"}]
|
||||
}"#;
|
||||
@@ -115,8 +115,8 @@ async fn create_and_get_widget() {
|
||||
async fn list_widgets() {
|
||||
let app = test_app();
|
||||
|
||||
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":[]}"#;
|
||||
let w1 = r#"{"id":1,"name":"a","display_hint":{"kind":"icon_value"},"data_source_id":1,"mappings":[]}"#;
|
||||
let w2 = r#"{"id":2,"name":"b","display_hint":{"kind":"key_value"},"data_source_id":2,"mappings":[]}"#;
|
||||
|
||||
app.clone()
|
||||
.oneshot(authed_json_request("POST", "/api/widgets", Some(w1)))
|
||||
@@ -142,8 +142,7 @@ 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":[]}"#;
|
||||
let body = r#"{"id":1,"name":"a","display_hint":{"kind":"icon_value"},"data_source_id":1,"mappings":[]}"#;
|
||||
app.clone()
|
||||
.oneshot(authed_json_request("POST", "/api/widgets", Some(body)))
|
||||
.await
|
||||
|
||||
@@ -7,11 +7,28 @@ pub struct KeyMappingDto {
|
||||
pub target_key: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct DisplayHintDto {
|
||||
pub kind: String,
|
||||
#[serde(default = "default_h_align")]
|
||||
pub h_align: String,
|
||||
#[serde(default = "default_v_align")]
|
||||
pub v_align: String,
|
||||
}
|
||||
|
||||
fn default_h_align() -> String {
|
||||
"left".into()
|
||||
}
|
||||
|
||||
fn default_v_align() -> String {
|
||||
"top".into()
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct WidgetDto {
|
||||
pub id: u16,
|
||||
pub name: String,
|
||||
pub display_hint: String,
|
||||
pub display_hint: DisplayHintDto,
|
||||
pub data_source_id: u16,
|
||||
pub mappings: Vec<KeyMappingDto>,
|
||||
pub max_data_size: u16,
|
||||
@@ -21,7 +38,7 @@ pub struct WidgetDto {
|
||||
pub struct CreateWidgetDto {
|
||||
pub id: u16,
|
||||
pub name: String,
|
||||
pub display_hint: String,
|
||||
pub display_hint: DisplayHintDto,
|
||||
pub data_source_id: u16,
|
||||
pub mappings: Vec<KeyMappingDto>,
|
||||
#[serde(default = "default_max_data_size")]
|
||||
@@ -32,17 +49,40 @@ fn default_max_data_size() -> u16 {
|
||||
2048
|
||||
}
|
||||
|
||||
fn kind_to_str(kind: &DisplayHintKind) -> &'static str {
|
||||
match kind {
|
||||
DisplayHintKind::IconValue => "icon_value",
|
||||
DisplayHintKind::TextBlock => "text_block",
|
||||
DisplayHintKind::KeyValue => "key_value",
|
||||
}
|
||||
}
|
||||
|
||||
fn h_align_to_str(a: HAlign) -> &'static str {
|
||||
match a {
|
||||
HAlign::Left => "left",
|
||||
HAlign::Center => "center",
|
||||
HAlign::Right => "right",
|
||||
}
|
||||
}
|
||||
|
||||
fn v_align_to_str(a: VAlign) -> &'static str {
|
||||
match a {
|
||||
VAlign::Top => "top",
|
||||
VAlign::Middle => "middle",
|
||||
VAlign::Bottom => "bottom",
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&WidgetConfig> for WidgetDto {
|
||||
fn from(w: &WidgetConfig) -> Self {
|
||||
Self {
|
||||
id: w.id,
|
||||
name: w.name.clone(),
|
||||
display_hint: match w.display_hint.kind {
|
||||
DisplayHintKind::IconValue => "icon_value",
|
||||
DisplayHintKind::TextBlock => "text_block",
|
||||
DisplayHintKind::KeyValue => "key_value",
|
||||
}
|
||||
.into(),
|
||||
display_hint: DisplayHintDto {
|
||||
kind: kind_to_str(&w.display_hint.kind).into(),
|
||||
h_align: h_align_to_str(w.display_hint.h_align).into(),
|
||||
v_align: v_align_to_str(w.display_hint.v_align).into(),
|
||||
},
|
||||
data_source_id: w.data_source_id,
|
||||
mappings: w
|
||||
.mappings
|
||||
@@ -59,16 +99,32 @@ impl From<&WidgetConfig> for WidgetDto {
|
||||
|
||||
impl CreateWidgetDto {
|
||||
pub fn into_domain(self) -> Result<WidgetConfig, String> {
|
||||
let hint = match self.display_hint.as_str() {
|
||||
"icon_value" => DisplayHint::new(DisplayHintKind::IconValue),
|
||||
"text_block" => DisplayHint::new(DisplayHintKind::TextBlock),
|
||||
"key_value" => DisplayHint::new(DisplayHintKind::KeyValue),
|
||||
h => return Err(format!("unknown display_hint: {h}")),
|
||||
let kind = match self.display_hint.kind.as_str() {
|
||||
"icon_value" => DisplayHintKind::IconValue,
|
||||
"text_block" => DisplayHintKind::TextBlock,
|
||||
"key_value" => DisplayHintKind::KeyValue,
|
||||
h => return Err(format!("unknown display_hint kind: {h}")),
|
||||
};
|
||||
let h_align = match self.display_hint.h_align.as_str() {
|
||||
"left" => HAlign::Left,
|
||||
"center" => HAlign::Center,
|
||||
"right" => HAlign::Right,
|
||||
h => return Err(format!("unknown h_align: {h}")),
|
||||
};
|
||||
let v_align = match self.display_hint.v_align.as_str() {
|
||||
"top" => VAlign::Top,
|
||||
"middle" => VAlign::Middle,
|
||||
"bottom" => VAlign::Bottom,
|
||||
v => return Err(format!("unknown v_align: {v}")),
|
||||
};
|
||||
Ok(WidgetConfig {
|
||||
id: self.id,
|
||||
name: self.name,
|
||||
display_hint: hint,
|
||||
display_hint: DisplayHint {
|
||||
kind,
|
||||
h_align,
|
||||
v_align,
|
||||
},
|
||||
data_source_id: self.data_source_id,
|
||||
mappings: self
|
||||
.mappings
|
||||
|
||||
Reference in New Issue
Block a user