From af5175a282836356f98fd04c88625e4d29ae5835 Mon Sep 17 00:00:00 2001 From: Nutomic Date: Fri, 12 May 2023 02:12:12 +0200 Subject: [PATCH] Set attribute `deny_unknown_fields` for Lemmy config (#2852) With this attribute, Lemmy will throw an error and exit if any invalid entry is found in the config file. I think can be useful to notice typos or keys that were removed or renamed in an upgrade. Currently you wouldnt notice these at all unless you manually compare the config file with settings that are listed in documentation. This should be considered a breaking change. Co-authored-by: Dessalines --- crates/utils/src/error.rs | 2 +- crates/utils/src/settings/mod.rs | 9 +++++---- crates/utils/src/settings/structs.rs | 6 ++++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/crates/utils/src/error.rs b/crates/utils/src/error.rs index 6fb5e5155..1aa3e1c62 100644 --- a/crates/utils/src/error.rs +++ b/crates/utils/src/error.rs @@ -80,7 +80,7 @@ impl Debug for LemmyError { f.debug_struct("LemmyError") .field("message", &self.message) .field("inner", &self.inner) - .field("context", &"SpanTrace") + .field("context", &self.context) .finish() } } diff --git a/crates/utils/src/settings/mod.rs b/crates/utils/src/settings/mod.rs index fe8c8ae4f..cd181ffb5 100644 --- a/crates/utils/src/settings/mod.rs +++ b/crates/utils/src/settings/mod.rs @@ -14,8 +14,9 @@ pub mod structs; static DEFAULT_CONFIG_FILE: &str = "config/config.hjson"; -pub static SETTINGS: Lazy = - Lazy::new(|| Settings::init().expect("Failed to load settings file")); +pub static SETTINGS: Lazy = Lazy::new(|| { + Settings::init().expect("Failed to load settings file, see documentation (https://join-lemmy.org/docs/en/administration/configuration.html)") +}); static WEBFINGER_REGEX: Lazy = Lazy::new(|| { Regex::new(&format!( "^acct:([a-zA-Z0-9_]{{3,}})@{}$", @@ -53,11 +54,11 @@ impl Settings { ) } - pub fn get_config_location() -> String { + fn get_config_location() -> String { env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| DEFAULT_CONFIG_FILE.to_string()) } - pub fn read_config_file() -> Result { + fn read_config_file() -> Result { fs::read_to_string(Self::get_config_location()) } diff --git a/crates/utils/src/settings/structs.rs b/crates/utils/src/settings/structs.rs index 9d46dbe8a..c135c3a8c 100644 --- a/crates/utils/src/settings/structs.rs +++ b/crates/utils/src/settings/structs.rs @@ -42,7 +42,7 @@ pub struct Settings { } #[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)] -#[serde(default)] +#[serde(default, deny_unknown_fields)] pub struct PictrsConfig { /// Address where pictrs is available (for image hosting) #[default(Url::parse("http://localhost:8080").expect("parse pictrs url"))] @@ -55,7 +55,7 @@ pub struct PictrsConfig { } #[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)] -#[serde(default)] +#[serde(default, deny_unknown_fields)] pub struct DatabaseConfig { /// Username to connect to postgres #[default("lemmy")] @@ -78,6 +78,7 @@ pub struct DatabaseConfig { } #[derive(Debug, Deserialize, Serialize, Clone, Document, SmartDefault)] +#[serde(deny_unknown_fields)] pub struct EmailConfig { /// Hostname and port of the smtp server #[doku(example = "localhost:25")] @@ -96,6 +97,7 @@ pub struct EmailConfig { } #[derive(Debug, Deserialize, Serialize, Clone, SmartDefault, Document)] +#[serde(deny_unknown_fields)] pub struct SetupConfig { /// Username for the admin user #[doku(example = "admin")]