diff --git a/src/config/config.rs b/src/config/config.rs index 806f78e..4d86f38 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -26,6 +26,8 @@ use elefren::Data; use serde_derive::Deserialize; use serde_derive::Serialize; +use crate::config::errors::ConfigError; + /// The last seen favourite #[derive(Serialize, Deserialize, Debug)] struct Favourite { @@ -78,35 +80,12 @@ impl Config { } /// Save the current configuration file. - // TODO Result for save result? - pub fn save(&self) { - let content = toml::to_string(&self.0).unwrap(); - let filename = Config::filename().unwrap(); + pub fn save(&self) -> Result<(), ConfigError> { + let content = toml::to_string(&self.0)?; + let filename = Config::filename()?; log::debug!("Saving configuration to file \"{:?}\"", filename); - let mut fp = File::create(filename).unwrap(); - fp.write_all(content.as_bytes()).unwrap(); - } -} - -/// Errors from the configuration -#[derive(Debug)] -pub enum ConfigError { - /// The system can't figure out the path for the configuration file - CantFigureConfigPath, - /// The configuration file has an error and can't be parsed - ConfigFileIsBroken, -} - -impl From for ConfigError { - fn from(e: toml::de::Error) -> Self { - log::debug!("Toml error: {:?}", e); - ConfigError::ConfigFileIsBroken - } -} - -impl From for ConfigError { - fn from(e: std::io::Error) -> Self { - log::debug!("I/O error: {:?}", e); - ConfigError::ConfigFileIsBroken + let mut fp = File::create(filename)?; + fp.write_all(content.as_bytes())?; + Ok(()) } } diff --git a/src/config/errors.rs b/src/config/errors.rs new file mode 100644 index 0000000..1d1636d --- /dev/null +++ b/src/config/errors.rs @@ -0,0 +1,49 @@ +/* + DOWNFAV - Download Favourites + Copyright (C) 2020-2021 Julio Biason + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +/// Errors from the configuration +#[derive(Debug)] +pub enum ConfigError { + /// The system can't figure out the path for the configuration file + CantFigureConfigPath, + /// The configuration file has an error and can't be parsed + ConfigFileIsBroken, + /// There was something broken with the data and we couldn't save it properly + InvalidConfiguration, +} + +impl From for ConfigError { + fn from(e: toml::de::Error) -> Self { + log::debug!("Toml error: {:?}", e); + ConfigError::ConfigFileIsBroken + } +} + +impl From for ConfigError { + fn from(e: std::io::Error) -> Self { + log::debug!("I/O error: {:?}", e); + ConfigError::ConfigFileIsBroken + } +} + +impl From for ConfigError { + fn from(e: toml::ser::Error) -> Self { + log::debug!("TOML error: {:?}", e); + ConfigError::InvalidConfiguration + } +} diff --git a/src/config/mod.rs b/src/config/mod.rs index 456b3ba..b67f40c 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -25,6 +25,7 @@ use serde_derive::Deserialize; use serde_derive::Serialize; pub mod config; +pub mod errors; #[derive(Serialize, Deserialize, Debug)] pub struct JoplinConfig {