From b7a9def20defb0685f322b3bc1023c9ce643af62 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Mon, 6 Dec 2021 17:17:24 +0100 Subject: [PATCH] updated config management --- src/config/mod.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 8817870..3fbaa7b 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -85,6 +85,7 @@ pub struct Config { pub server: String, pub username: String, pub password: String, + #[serde(skip_serializing)] pub authtoken: Option, pub sloweffect: Option, pub refresh: Option, @@ -107,6 +108,7 @@ impl<'a> Config { pub fn load(&'a mut self, configfile: &str) { let fileopen: Result; let filemeta = std::fs::metadata(configfile); + let mut errorreading = false; let fileexists = match filemeta { Ok(_) => true, Err(_) => false, @@ -126,25 +128,29 @@ impl<'a> Config { let mut contents = String::new(); file.read_to_string(&mut contents).unwrap(); let parse: Result = serde_json::from_str(contents.as_str()); - *self = match parse { - Ok(cfg) => { + let newcfg = match parse { + Ok(ncfg) => { let tmpcfg = Config::new(); Config { - server: cfg["server"].as_str().unwrap_or(tmpcfg.server.as_str()).to_string(), - username: cfg["username"].as_str().unwrap_or(tmpcfg.username.as_str()).to_string(), - password: cfg["password"].as_str().unwrap_or(tmpcfg.password.as_str()).to_string(), - authtoken: Some(cfg["authtoken"].as_str().unwrap_or(tmpcfg.authtoken.unwrap().as_str()).to_string()), - sloweffect: Some(cfg["sloweffect"].as_bool().unwrap_or(tmpcfg.sloweffect.unwrap())), - refresh: Some(cfg["refresh"].as_u64().unwrap_or(tmpcfg.refresh.unwrap())), - limit: Some(cfg["limit"].as_u64().unwrap_or(tmpcfg.limit.unwrap())), + server: ncfg["server"].as_str().unwrap_or(tmpcfg.server.as_str()).to_string(), + username: ncfg["username"].as_str().unwrap_or(tmpcfg.username.as_str()).to_string(), + password: ncfg["password"].as_str().unwrap_or(tmpcfg.password.as_str()).to_string(), + authtoken: Some(ncfg["authtoken"].as_str().unwrap_or(self.authtoken.clone().unwrap().as_str()).to_string()), + sloweffect: Some(ncfg["sloweffect"].as_bool().unwrap_or(tmpcfg.sloweffect.unwrap())), + refresh: Some(ncfg["refresh"].as_u64().unwrap_or(tmpcfg.refresh.unwrap())), + limit: Some(ncfg["limit"].as_u64().unwrap_or(tmpcfg.limit.unwrap())), } }, Err(err) => { + errorreading = true; println!("error occured: '{}'", err); Config::new() } }; - self.save(&configfile); + *self = newcfg; + if !fileexists || errorreading { + self.save(&configfile); + } } pub fn save(&self, configfile: &str) {