From 80b66fb7cae8652742602944d53af72a99f6dd8b Mon Sep 17 00:00:00 2001 From: naskya Date: Thu, 20 Jun 2024 12:19:04 +0900 Subject: [PATCH] WIP --- src/command/config/migrate/v20240701.rs | 60 +++++++++++-------------- src/config/client.rs | 4 ++ 2 files changed, 29 insertions(+), 35 deletions(-) diff --git a/src/command/config/migrate/v20240701.rs b/src/command/config/migrate/v20240701.rs index 3de29b4..33b6c7c 100644 --- a/src/command/config/migrate/v20240701.rs +++ b/src/command/config/migrate/v20240701.rs @@ -183,30 +183,24 @@ 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() + .and_then(|db| db.as_hash()) .ok_or(Error::InvalidConfig("`db` is missing"))?; let meta = read_meta_table( db.get(&Yaml::String("host".to_string())) - .map(|v| v.as_str()) - .flatten() + .and_then(|v| v.as_str()) .ok_or(Error::InvalidConfig("db.host"))?, db.get(&Yaml::String("port".to_string())) - .map(|v| v.as_i64()) - .flatten() + .and_then(|v| v.as_i64()) .ok_or(Error::InvalidConfig("db.port"))? as u16, db.get(&Yaml::String("user".to_string())) - .map(|v| v.as_str()) - .flatten() + .and_then(|v| v.as_str()) .ok_or(Error::InvalidConfig("db.user"))?, db.get(&Yaml::String("pass".to_string())) - .map(|v| v.as_str()) - .flatten() + .and_then(|v| v.as_str()) .ok_or(Error::InvalidConfig("db.pass"))?, db.get(&Yaml::String("db".to_string())) - .map(|v| v.as_str()) - .flatten() + .and_then(|v| v.as_str()) .ok_or(Error::InvalidConfig("db.db"))?, ) .await?; @@ -220,20 +214,17 @@ fn create_new_server_config( ) -> Result { let db = default_yml .get("db") - .map(|db| db.as_hash()) - .flatten() + .and_then(|db| db.as_hash()) .ok_or(Error::InvalidConfig("`db` is missing"))?; let redis = default_yml .get("redis") - .map(|redis| redis.as_hash()) - .flatten() + .and_then(|redis| redis.as_hash()) .ok_or(Error::InvalidConfig("`redis` is missing"))?; let server_url = default_yml .get("url") - .map(|url| url.as_str()) - .flatten() + .and_then(|url| url.as_str()) .ok_or(Error::InvalidConfig("`url` is missing"))?; let parsed_server_url = Url::parse(server_url)?; @@ -285,52 +276,44 @@ fn create_new_server_config( host, port: default_yml .get("port") - .map(|v| v.as_i64()) - .flatten() + .and_then(|v| v.as_i64()) .ok_or(Error::InvalidConfig("port"))? as u16, }, database: server::Database { host: db .get(&Yaml::String("host".to_string())) - .map(|v| v.as_str()) - .flatten() + .and_then(|v| v.as_str()) .ok_or(Error::InvalidConfig("db.host"))? .to_string(), port: db .get(&Yaml::String("port".to_string())) - .map(|v| v.as_i64()) - .flatten() + .and_then(|v| v.as_i64()) .ok_or(Error::InvalidConfig("db.port"))? as u16, user: db .get(&Yaml::String("user".to_string())) - .map(|v| v.as_str()) - .flatten() + .and_then(|v| v.as_str()) .ok_or(Error::InvalidConfig("db.user"))? .to_string(), password: db .get(&Yaml::String("pass".to_string())) - .map(|v| v.as_str()) - .flatten() + .and_then(|v| v.as_str()) .ok_or(Error::InvalidConfig("db.pass"))? .to_string(), name: db .get(&Yaml::String("db".to_string())) - .map(|v| v.as_str()) - .flatten() + .and_then(|v| v.as_str()) .ok_or(Error::InvalidConfig("db.db"))? .to_string(), }, cache_server: server::CacheServer { host: redis .get(&Yaml::String("host".to_string())) - .map(|v| v.as_str()) - .flatten() + .and_then(|v| v.as_str()) .ok_or(Error::InvalidConfig("redis.host"))? .to_string(), port: redis .get(&Yaml::String("port".to_string())) - .map(|v| v.as_i64()) - .flatten() + .and_then(|v| v.as_i64()) .ok_or(Error::InvalidConfig("redis.port"))? as u16, user: match redis.get(&Yaml::String("user".to_string())) { Some(user) => Some( @@ -406,11 +389,18 @@ fn create_new_client_config(meta: Meta) -> Result { }); } - if meta.icon_url.is_some() || meta.banner_url.is_some() || meta.background_image_url.is_some() { + if meta.logo_image_url.is_some() + || meta.icon_url.is_some() + || meta.banner_url.is_some() + || meta.background_image_url.is_some() + || meta.error_image_url.is_some() + { config.image = Some(client::Image { + logo: meta.logo_image_url, icon: meta.icon_url, banner: meta.banner_url, background: meta.background_image_url, + error: meta.error_image_url, }); } diff --git a/src/config/client.rs b/src/config/client.rs index 63a57d3..ea5ace5 100644 --- a/src/config/client.rs +++ b/src/config/client.rs @@ -28,12 +28,16 @@ pub struct Theme { #[derive(Deserialize, Serialize, Validate, Debug)] pub struct Image { + #[validate(url)] + pub logo: Option, #[validate(url)] pub icon: Option, #[validate(url)] pub banner: Option, #[validate(url)] pub background: Option, + #[validate(url)] + pub error: Option, } #[derive(Deserialize, Serialize, Validate, Debug)]