use serde::Deserialize;
use std::collections::HashMap;
#[derive(Debug, Clone, Deserialize, Default)]
pub struct GateStyle {
#[serde(default)]
pub border_color: Option<String>,
#[serde(default)]
pub background_color: Option<String>,
#[serde(default)]
pub font_size: Option<f64>,
#[serde(default)]
pub text_color: Option<String>,
#[serde(default)]
pub line_color: Option<String>,
#[serde(default)]
pub line_width: Option<f64>,
}
#[derive(Debug, Clone)]
pub struct StyleBook {
styles: HashMap<String, GateStyle>,
default_style: GateStyle,
}
impl StyleBook {
pub fn new(style_name: &str, overrides: &HashMap<String, GateStyle>) -> Self {
let mut styles = load_style(style_name);
for (k, v) in overrides {
styles.insert(k.clone(), v.clone());
}
if !styles.contains_key("default") {
styles.insert("default".to_string(), GateStyle::default());
}
let default_style = styles.get("default").cloned().unwrap_or_default();
Self {
styles,
default_style,
}
}
pub fn get(&self, gate_name: &str) -> &GateStyle {
self.styles.get(gate_name).unwrap_or(&self.default_style)
}
}
fn load_style(style_name: &str) -> HashMap<String, GateStyle> {
let style_key = style_name.trim().to_ascii_lowercase();
let json = match style_key.as_str() {
"gray" => include_str!("styles/gray.json"),
_ => include_str!("styles/default.json"),
};
serde_json::from_str(json).unwrap_or_else(|_| {
let mut fallback = HashMap::new();
fallback.insert("default".to_string(), GateStyle::default());
fallback
})
}