fix: use serde_inline_default and serde_default

now config behaves as expected, with fallback fields merged with the
main struct, also less impl Default and values right next to the field
This commit is contained in:
əlemi 2024-01-03 19:54:42 +01:00
parent 9e5b2ffb13
commit 87828a51f5
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 68 additions and 45 deletions

25
Cargo.lock generated
View file

@ -908,6 +908,8 @@ dependencies = [
"md-5",
"sailfish",
"serde",
"serde-inline-default",
"serde_default",
"serde_json",
"sqlx",
"teloxide",
@ -2310,6 +2312,29 @@ dependencies = [
"serde_derive",
]
[[package]]
name = "serde-inline-default"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa824cde50b5f01ff28a955114d8152a07cd62d81f53459dad0f2610136be844"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "serde_default"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fd4c77b86d9fb10363e52607ca6dc3043d8dfde6c790b702ed4ffafb34e7b99"
dependencies = [
"darling",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "serde_derive"
version = "1.0.194"

View file

@ -22,6 +22,7 @@ uuid = { version = "1.6", features = ["v4", "fast-rng"] }
chrono = { version = "0.4", features = ["serde"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde-inline-default = "0.1" # TODO this hopefully won't be needed anymore soon
async-trait = "0.1"
html-escape = "0.2"
clap = { version = "4.4", features = ["derive", "cargo"] }
@ -36,6 +37,7 @@ tokio-rustls = { version = "0.25", optional = true }
# frontend
sailfish = { version = "0.8", optional = true }
axum-extra = { version = "0.9", optional = true }
serde_default = "0.1.0"
[features]
default = ["mysql", "sqlite", "postgres", "telegram", "email", "web"] # all features by default

View file

@ -1,87 +1,85 @@
use serde_inline_default::serde_inline_default;
// note that serde would want us to create a function for each immediate value we would want to set
// as default. that gets VERY annoying VERY fast, and we have a lot of fields here.
// i really dislike what serde-inline-default does, just adding more complexity making all these
// immediate functions for me, but the code would be EXTREMELY bad to maintain otherwise
//
// track progress for official #[serde(default_value = ...)] here : https://github.com/serde-rs/serde/issues/368
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, serde_default::DefaultFromSerde)]
pub struct Config {
#[serde(default)]
pub overrides: ConfigOverrides,
#[serde(default)]
pub notifiers: ConfigNotifiers,
#[serde(default)]
pub template: ConfigTemplate,
}
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[serde_inline_default]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, serde_default::DefaultFromSerde)]
pub struct ConfigOverrides {
#[serde(default)]
pub author: Option<String>,
#[serde(default)]
pub public: Option<bool>,
#[serde(default = "_true")]
#[serde_inline_default(true)]
pub date: bool,
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde_inline_default]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, serde_default::DefaultFromSerde)]
pub struct ConfigTemplate {
#[serde_inline_default("guestbook".into())]
pub title: String,
#[serde_inline_default("https://cdn.alemi.dev/social/someriver.jpg".into())]
pub logo: String,
#[serde_inline_default("you found my guestbook! please take a moment to sign it (:".into())]
pub description: String,
#[serde_inline_default("Kilroy was here Ω".into())]
pub placeholder_body: String,
#[serde(default)]
pub canonical: Option<String>,
#[serde(default)]
pub palette: ConfigPalette,
}
impl Default for ConfigTemplate {
fn default() -> Self {
ConfigTemplate {
title: "guestbook.rs".into(),
logo: "https://cdn.alemi.dev/social/someriver.jpg".into(),
description: "you found my guestbook! please take a moment to sign it (:".into(),
placeholder_body: "Kilroy was here Ω".into(),
canonical: None,
palette: ConfigPalette::default(),
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde_inline_default]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, serde_default::DefaultFromSerde)]
pub struct ConfigPalette {
#[serde_inline_default("#201F29".into())]
pub bg_main: String,
#[serde_inline_default("#292835".into())]
pub bg_off: String,
#[serde_inline_default("#E8E1D3".into())]
pub fg_main: String,
#[serde_inline_default("#ADA9A1".into())]
pub fg_off: String,
#[serde_inline_default("#BF616A".into())]
pub accent: String,
#[serde_inline_default("#824E53".into())]
pub accent_dark: String,
#[serde_inline_default("#D1888E".into())]
pub accent_light: String,
}
impl Default for ConfigPalette {
fn default() -> Self {
ConfigPalette {
bg_main: "#201F29".into(),
bg_off: "#292835".into(),
fg_main: "#E8E1D3".into(),
fg_off: "#ADA9A1".into(),
accent: "#BF616A".into(),
accent_dark: "#824E53".into(),
accent_light: "#D1888E".into(),
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde_inline_default]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, serde_default::DefaultFromSerde)]
pub struct ConfigNotifiers {
#[serde_inline_default(vec![NotifierProvider::Console])]
pub providers: Vec<NotifierProvider>,
}
// by default enable console notifier
impl Default for ConfigNotifiers {
fn default() -> Self {
ConfigNotifiers {
providers: vec![NotifierProvider::Console],
}
}
}
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum NotifierProvider {
Console,
@ -103,5 +101,3 @@ pub enum NotifierProvider {
subject: String,
},
}
fn _true() -> bool { true }