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 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<String, Yaml>,
meta: Meta,
) -> Result<(server::Config, client::Config), Error> {
meta: &Meta,
) -> Result<server::Config, Error> {
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<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> {
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(())
}

View file

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