This commit is contained in:
naskya 2024-06-20 12:12:07 +09:00
parent 56f52be53e
commit 33575178d9
Signed by: naskya
GPG key ID: 712D413B3A9FED5C
2 changed files with 85 additions and 26 deletions

View file

@ -183,23 +183,41 @@ async fn read_old_config() -> Result<(HashMap<String, Yaml>, Meta), Error> {
let default_yml = read_default_yml()?; let default_yml = read_default_yml()?;
let db = default_yml let db = default_yml
.get("db") .get("db")
.map(|db| db.as_hash())
.flatten()
.ok_or(Error::InvalidConfig("`db` is missing"))?; .ok_or(Error::InvalidConfig("`db` is missing"))?;
let meta = read_meta_table( let meta = read_meta_table(
db["host"].as_str().unwrap(), db.get(&Yaml::String("host".to_string()))
db["port"].as_i64().unwrap() as u16, .map(|v| v.as_str())
db["user"].as_str().unwrap(), .flatten()
db["pass"].as_str().unwrap(), .ok_or(Error::InvalidConfig("db.host"))?,
db["db"].as_str().unwrap(), 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?; .await?;
Ok((default_yml, meta)) Ok((default_yml, meta))
} }
async fn create_new_config( fn create_new_server_config(
default_yml: HashMap<String, Yaml>, default_yml: HashMap<String, Yaml>,
meta: Meta, meta: &Meta,
) -> Result<(server::Config, client::Config), Error> { ) -> Result<server::Config, Error> {
let db = default_yml let db = default_yml
.get("db") .get("db")
.map(|db| db.as_hash()) .map(|db| db.as_hash())
@ -246,10 +264,10 @@ async fn create_new_config(
let mut server_config = server::Config { let mut server_config = server::Config {
info: Some(server::Info { info: Some(server::Info {
name: meta.name, name: meta.name.to_owned(),
description: meta.description, description: meta.description.to_owned(),
maintainer_name: meta.maintainer_name, maintainer_name: meta.maintainer_name.to_owned(),
contact_info: meta.maintainer_email, contact_info: meta.maintainer_email.to_owned(),
open_registrations: !meta.disable_registration, open_registrations: !meta.disable_registration,
repository_url: Some(repository_url), 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<client::Config, Error> {
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> { pub(super) async fn run() -> Result<(), Error> {
let (default_yml, meta) = read_old_config().await?; 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(()) Ok(())
} }

View file

@ -10,19 +10,30 @@ use validator::{Validate, ValidationError};
#[derive(Deserialize, Serialize, Validate, Debug)] #[derive(Deserialize, Serialize, Validate, Debug)]
pub struct Config { pub struct Config {
/// Server-wide default light theme pub theme: Option<Theme>,
light_theme: Option<String>, pub image: Option<Image>,
/// Server-wide default light theme pub pinned_links: Vec<WebSite>,
dark_theme: Option<String>, }
#[validate(url)]
icon: Option<String>, #[derive(Deserialize, Serialize, Validate, Debug)]
#[validate(url)] pub struct Theme {
banner_image: Option<String>, /// Theme color
#[validate(url)]
background_image: Option<String>,
#[validate(custom(function = "is_color_code"))] #[validate(custom(function = "is_color_code"))]
theme_color: Option<String>, pub color: Option<String>,
pinned_links: Vec<WebSite>, /// Server-wide default light theme
pub light: Option<String>,
/// Server-wide default dark theme
pub dark: Option<String>,
}
#[derive(Deserialize, Serialize, Validate, Debug)]
pub struct Image {
#[validate(url)]
pub icon: Option<String>,
#[validate(url)]
pub banner: Option<String>,
#[validate(url)]
pub background: Option<String>,
} }
#[derive(Deserialize, Serialize, Validate, Debug)] #[derive(Deserialize, Serialize, Validate, Debug)]