From 0d5ac9f0ef306faa4e0ad5315fd6368400449d1a Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Fri, 4 Oct 2024 17:14:23 +0200 Subject: add default configuration through `Default` --- src/config.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/config.rs b/src/config.rs index 18c3bab..4b333fe 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,6 +4,7 @@ use serde::Deserialize; use crate::auth; #[derive(Debug, Clone, Deserialize)] +#[serde(default)] pub struct Config { pub serveraddr: String, pub pingtimeout: i64, @@ -11,23 +12,37 @@ pub struct Config { pub auth: Auth, } +impl Default for Config { + fn default() -> Self { + Self { + serveraddr: "0.0.0.0:7229".to_string(), + pingtimeout: 10, + pingthreshold: 1, + auth: Default::default(), + } + } +} + #[derive(Debug, Clone, Deserialize)] pub struct Auth { pub method: auth::Methods, pub secret: String, } +impl Default for Auth { + fn default() -> Self { + Self { + method: auth::Methods::None, + secret: String::new(), + } + } +} + impl Config { pub fn load() -> Result { let config = config::Config::builder() - .set_default("serveraddr", "0.0.0.0:7229")? - .set_default("pingtimeout", 10)? - .set_default("pingthreshold", 1)? - .set_default("timeoffset", 0)? - .set_default("auth.method", "none")? - .set_default("auth.secret", "")? + .add_source(File::with_name("/etc/webol").required(false)) .add_source(File::with_name("config.toml").required(false)) - .add_source(File::with_name("config.dev.toml").required(false)) .add_source(config::Environment::with_prefix("WEBOL").separator("_")) .build()?; -- cgit v1.2.3