From 87828a51f5bbc85f375f6a0fa76c93bc1c0ccd1f Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 3 Jan 2024 19:54:42 +0100 Subject: [PATCH] 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 --- Cargo.lock | 25 +++++++++++++++ Cargo.toml | 2 ++ src/config.rs | 86 ++++++++++++++++++++++++--------------------------- 3 files changed, 68 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f999968..2cab858 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 1325ead..594f9aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/src/config.rs b/src/config.rs index 1ecd3ee..0ffd923 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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, + #[serde(default)] pub public: Option, - #[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, + + #[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, } -// 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 }