From 96ac028918baa1094374e823a2464016f7f20479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Sat, 26 Dec 2020 22:33:10 +0100 Subject: add todos --- mumlib/src/command.rs | 1 + mumlib/src/config.rs | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'mumlib') diff --git a/mumlib/src/command.rs b/mumlib/src/command.rs index d2e8477..fc08ddf 100644 --- a/mumlib/src/command.rs +++ b/mumlib/src/command.rs @@ -31,6 +31,7 @@ pub enum Command { UserVolumeSet(String, f32), } +//TODO none-response #[derive(Debug, Deserialize, Serialize)] pub enum CommandResponse { ChannelList { diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs index 0a43253..b0ce3f7 100644 --- a/mumlib/src/config.rs +++ b/mumlib/src/config.rs @@ -41,7 +41,7 @@ impl Config { fs::write( path, - toml::to_string(&TOMLConfig::from(self.clone())).unwrap(), + toml::to_string(&TOMLConfig::from(self.clone())).unwrap(), //TODO handle panic ) } } @@ -162,7 +162,7 @@ impl From for TOMLConfig { config .servers .into_iter() - .map(|s| Value::try_from::(s).unwrap()) + .map(|s| Value::try_from::(s).unwrap()) //TODO handle panic .collect(), ), } @@ -175,7 +175,7 @@ pub fn read_default_cfg() -> Config { Ok(f) => f, Err(_) => return Config::default(), }) - .expect("invalid TOML in config file"), //TODO + .expect("invalid TOML in config file"), //TODO handle panic ) - .expect("invalid config in TOML") //TODO + .expect("invalid config in TOML") //TODO handle panic } -- cgit v1.2.1 From 25687fb7c98f7d7d8b1f0a04d32092f394ec4c44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 30 Mar 2021 12:09:49 +0200 Subject: mumlib::config: dirs --- mumlib/Cargo.toml | 1 + mumlib/src/config.rs | 72 ++++++++++++++++++++-------------------------------- 2 files changed, 29 insertions(+), 44 deletions(-) (limited to 'mumlib') diff --git a/mumlib/Cargo.toml b/mumlib/Cargo.toml index 240e017..43bd8c5 100644 --- a/mumlib/Cargo.toml +++ b/mumlib/Cargo.toml @@ -12,6 +12,7 @@ license = "MIT" [dependencies] colored = "2.0" +dirs = "3" fern = "0.6" log = "0.4" serde = { version = "1.0", features = ["derive"] } diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs index b0ce3f7..74b4b9c 100644 --- a/mumlib/src/config.rs +++ b/mumlib/src/config.rs @@ -1,9 +1,10 @@ use crate::DEFAULT_PORT; +use log::*; use serde::{Deserialize, Serialize}; use std::convert::TryFrom; use std::fs; use std::net::{SocketAddr, ToSocketAddrs}; -use std::path::Path; +use std::path::{Path, PathBuf}; use toml::value::Array; use toml::Value; @@ -21,12 +22,8 @@ pub struct Config { impl Config { pub fn write_default_cfg(&self, create: bool) -> Result<(), std::io::Error> { - let path = if create { - get_creatable_cfg_path() - } else { - get_cfg_path() - }; - let path = std::path::Path::new(&path); + let path = default_cfg_path(); + // Possible race here. It's fine since it shows when: // 1) the file doesn't exist when checked and is then created // 2) the file exists when checked but is then removed @@ -35,12 +32,13 @@ impl Config { // should work. Unless the file is removed AND the permissions // change, but then we don't have permissions so we can't // do anything anyways. + if !create && !path.exists() { return Ok(()); } fs::write( - path, + &path, toml::to_string(&TOMLConfig::from(self.clone())).unwrap(), //TODO handle panic ) } @@ -80,36 +78,15 @@ impl ServerConfig { } } -pub fn get_cfg_path() -> String { - if let Ok(var) = std::env::var("XDG_CONFIG_HOME") { - let path = format!("{}/mumdrc", var); - if Path::new(&path).exists() { - return path; - } - } else if let Ok(var) = std::env::var("HOME") { - let path = format!("{}/.config/mumdrc", var); - if Path::new(&path).exists() { - return path; - } - } - - "/etc/mumdrc".to_string() -} - -pub fn get_creatable_cfg_path() -> String { - if let Ok(var) = std::env::var("XDG_CONFIG_HOME") { - let path = format!("{}/mumdrc", var); - if !Path::new(&path).exists() { - return path; - } - } else if let Ok(var) = std::env::var("HOME") { - let path = format!("{}/.config/mumdrc", var); - if !Path::new(&path).exists() { - return path; +pub fn default_cfg_path() -> PathBuf { + match dirs::config_dir() { + Some(mut p) => { + p.push("mumdrc"); + p } + //TODO This isn't cross platform. + None => PathBuf::from("/etc/mumdrc") } - - "/etc/mumdrc".to_string() } pub fn cfg_exists() -> bool { @@ -170,12 +147,19 @@ impl From for TOMLConfig { } pub fn read_default_cfg() -> Config { - Config::try_from( - toml::from_str::(&match fs::read_to_string(get_cfg_path()) { - Ok(f) => f, - Err(_) => return Config::default(), - }) - .expect("invalid TOML in config file"), //TODO handle panic - ) - .expect("invalid config in TOML") //TODO handle panic + let path = default_cfg_path(); + match fs::read_to_string(&path) { + Ok(s) => { + let toml_config: TOMLConfig = toml::from_str(&s).expect("Invalid TOML in config file"); //TODO handle panic + return Config::try_from(toml_config).expect("Invalid config in TOML"); //TODO handle panic + }, + Err(e) => { + if matches!(e.kind(), std::io::ErrorKind::NotFound) && !path.exists() { + warn!("Config file not found"); + } else { + error!("Error reading config file: {}", e); + } + return Config::default(); + } + } } -- cgit v1.2.1 From 950158eaadd8db9ef0eb48187e825524499422d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Tue, 30 Mar 2021 12:36:53 +0200 Subject: config error --- mumlib/src/config.rs | 23 +++++++++++++---------- mumlib/src/error.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 10 deletions(-) (limited to 'mumlib') diff --git a/mumlib/src/config.rs b/mumlib/src/config.rs index 74b4b9c..9394b85 100644 --- a/mumlib/src/config.rs +++ b/mumlib/src/config.rs @@ -1,4 +1,6 @@ +use crate::error::ConfigError; use crate::DEFAULT_PORT; + use log::*; use serde::{Deserialize, Serialize}; use std::convert::TryFrom; @@ -21,7 +23,7 @@ pub struct Config { } impl Config { - pub fn write_default_cfg(&self, create: bool) -> Result<(), std::io::Error> { + pub fn write_default_cfg(&self, create: bool) -> Result<(), ConfigError> { let path = default_cfg_path(); // Possible race here. It's fine since it shows when: @@ -34,13 +36,13 @@ impl Config { // do anything anyways. if !create && !path.exists() { - return Ok(()); + return Err(ConfigError::WontCreateFile); } - fs::write( + Ok(fs::write( &path, - toml::to_string(&TOMLConfig::from(self.clone())).unwrap(), //TODO handle panic - ) + toml::to_string(&TOMLConfig::from(self.clone()))?, + )?) } } @@ -139,19 +141,20 @@ impl From for TOMLConfig { config .servers .into_iter() - .map(|s| Value::try_from::(s).unwrap()) //TODO handle panic + // Safe since all ServerConfigs are valid TOML + .map(|s| Value::try_from::(s).unwrap()) .collect(), ), } } } -pub fn read_default_cfg() -> Config { +pub fn read_default_cfg() -> Result { let path = default_cfg_path(); match fs::read_to_string(&path) { Ok(s) => { - let toml_config: TOMLConfig = toml::from_str(&s).expect("Invalid TOML in config file"); //TODO handle panic - return Config::try_from(toml_config).expect("Invalid config in TOML"); //TODO handle panic + let toml_config: TOMLConfig = toml::from_str(&s)?; + Ok(Config::try_from(toml_config)?) }, Err(e) => { if matches!(e.kind(), std::io::ErrorKind::NotFound) && !path.exists() { @@ -159,7 +162,7 @@ pub fn read_default_cfg() -> Config { } else { error!("Error reading config file: {}", e); } - return Config::default(); + return Ok(Config::default()); } } } diff --git a/mumlib/src/error.rs b/mumlib/src/error.rs index f6a02a7..6b7dccd 100644 --- a/mumlib/src/error.rs +++ b/mumlib/src/error.rs @@ -42,3 +42,42 @@ impl fmt::Display for ChannelIdentifierError { } } } + +pub enum ConfigError { + InvalidConfig, + TOMLErrorSer(toml::ser::Error), + TOMLErrorDe(toml::de::Error), + + WontCreateFile, + IOError(std::io::Error), +} + +impl fmt::Display for ConfigError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ConfigError::InvalidConfig => write!(f, "Invalid configuration"), + ConfigError::TOMLErrorSer(e) => write!(f, "Invalid TOML when serializing: {}", e), + ConfigError::TOMLErrorDe(e) => write!(f, "Invalid TOML when deserializing: {}", e), + ConfigError::WontCreateFile => write!(f, "File does not exist but caller didn't allow creation"), + ConfigError::IOError(e) => write!(f, "IO error: {}", e), + } + } +} + +impl From for ConfigError { + fn from(e: std::io::Error) -> Self { + ConfigError::IOError(e) + } +} + +impl From for ConfigError { + fn from(e: toml::ser::Error) -> Self { + ConfigError::TOMLErrorSer(e) + } +} + +impl From for ConfigError { + fn from(e: toml::de::Error) -> Self { + ConfigError::TOMLErrorDe(e) + } +} -- cgit v1.2.1 From 69f189fd45b410be2db3c77e2a4bfa6d9ad8946d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Wed, 31 Mar 2021 09:05:12 +0200 Subject: review --- mumlib/src/command.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'mumlib') diff --git a/mumlib/src/command.rs b/mumlib/src/command.rs index fc08ddf..d2e8477 100644 --- a/mumlib/src/command.rs +++ b/mumlib/src/command.rs @@ -31,7 +31,6 @@ pub enum Command { UserVolumeSet(String, f32), } -//TODO none-response #[derive(Debug, Deserialize, Serialize)] pub enum CommandResponse { ChannelList { -- cgit v1.2.1