From 33575178d9ffc489ad859d6f5387fade3c4b9ae9 Mon Sep 17 00:00:00 2001 From: naskya Date: Thu, 20 Jun 2024 12:12:07 +0900 Subject: [PATCH] WIP --- src/command/config/migrate/v20240701.rs | 76 ++++++++++++++++++++----- src/config/client.rs | 35 ++++++++---- 2 files changed, 85 insertions(+), 26 deletions(-) diff --git a/src/command/config/migrate/v20240701.rs b/src/command/config/migrate/v20240701.rs index 476ba48..3de29b4 100644 --- a/src/command/config/migrate/v20240701.rs +++ b/src/command/config/migrate/v20240701.rs @@ -183,23 +183,41 @@ async fn read_old_config() -> Result<(HashMap, Meta), Error> { let default_yml = read_default_yml()?; let db = default_yml .get("db") + .map(|db| db.as_hash()) + .flatten() .ok_or(Error::InvalidConfig("`db` is missing"))?; + let meta = read_meta_table( - db["host"].as_str().unwrap(), - db["port"].as_i64().unwrap() as u16, - db["user"].as_str().unwrap(), - db["pass"].as_str().unwrap(), - db["db"].as_str().unwrap(), + db.get(&Yaml::String("host".to_string())) + .map(|v| v.as_str()) + .flatten() + .ok_or(Error::InvalidConfig("db.host"))?, + db.get(&Yaml::String("port".to_string())) + .map(|v| v.as_i64()) + .flatten() + .ok_or(Error::InvalidConfig("db.port"))? as u16, + db.get(&Yaml::String("user".to_string())) + .map(|v| v.as_str()) + .flatten() + .ok_or(Error::InvalidConfig("db.user"))?, + db.get(&Yaml::String("pass".to_string())) + .map(|v| v.as_str()) + .flatten() + .ok_or(Error::InvalidConfig("db.pass"))?, + db.get(&Yaml::String("db".to_string())) + .map(|v| v.as_str()) + .flatten() + .ok_or(Error::InvalidConfig("db.db"))?, ) .await?; Ok((default_yml, meta)) } -async fn create_new_config( +fn create_new_server_config( default_yml: HashMap, - meta: Meta, -) -> Result<(server::Config, client::Config), Error> { + meta: &Meta, +) -> Result { let db = default_yml .get("db") .map(|db| db.as_hash()) @@ -246,10 +264,10 @@ async fn create_new_config( let mut server_config = server::Config { info: Some(server::Info { - name: meta.name, - description: meta.description, - maintainer_name: meta.maintainer_name, - contact_info: meta.maintainer_email, + name: meta.name.to_owned(), + description: meta.description.to_owned(), + maintainer_name: meta.maintainer_name.to_owned(), + contact_info: meta.maintainer_email.to_owned(), open_registrations: !meta.disable_registration, repository_url: Some(repository_url), }), @@ -367,12 +385,42 @@ async fn create_new_config( }); } - Ok((server_config, todo!())) + Ok(server_config) +} + +fn create_new_client_config(meta: Meta) -> Result { + let mut config = client::Config { + theme: None, + image: None, + pinned_links: vec![], + }; + + if meta.theme_color.is_some() + || meta.default_light_theme.is_some() + || meta.default_dark_theme.is_some() + { + config.theme = Some(client::Theme { + light: meta.default_light_theme, + dark: meta.default_dark_theme, + color: meta.theme_color, + }); + } + + if meta.icon_url.is_some() || meta.banner_url.is_some() || meta.background_image_url.is_some() { + config.image = Some(client::Image { + icon: meta.icon_url, + banner: meta.banner_url, + background: meta.background_image_url, + }); + } + + Ok(config) } pub(super) async fn run() -> Result<(), Error> { let (default_yml, meta) = read_old_config().await?; - let (server_config, client_config) = create_new_config(default_yml, meta).await?; + let server_config = create_new_server_config(default_yml, &meta)?; + let client_config = create_new_client_config(meta)?; Ok(()) } diff --git a/src/config/client.rs b/src/config/client.rs index f572630..63a57d3 100644 --- a/src/config/client.rs +++ b/src/config/client.rs @@ -10,19 +10,30 @@ use validator::{Validate, ValidationError}; #[derive(Deserialize, Serialize, Validate, Debug)] pub struct Config { - /// Server-wide default light theme - light_theme: Option, - /// Server-wide default light theme - dark_theme: Option, - #[validate(url)] - icon: Option, - #[validate(url)] - banner_image: Option, - #[validate(url)] - background_image: Option, + pub theme: Option, + pub image: Option, + pub pinned_links: Vec, +} + +#[derive(Deserialize, Serialize, Validate, Debug)] +pub struct Theme { + /// Theme color #[validate(custom(function = "is_color_code"))] - theme_color: Option, - pinned_links: Vec, + pub color: Option, + /// Server-wide default light theme + pub light: Option, + /// Server-wide default dark theme + pub dark: Option, +} + +#[derive(Deserialize, Serialize, Validate, Debug)] +pub struct Image { + #[validate(url)] + pub icon: Option, + #[validate(url)] + pub banner: Option, + #[validate(url)] + pub background: Option, } #[derive(Deserialize, Serialize, Validate, Debug)]