Browse Source

Moved configuration errors to errors.rs

master
Julio Biason 3 years ago
parent
commit
294d790e5c
  1. 37
      src/config/config.rs
  2. 49
      src/config/errors.rs
  3. 1
      src/config/mod.rs

37
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<toml::de::Error> for ConfigError {
fn from(e: toml::de::Error) -> Self {
log::debug!("Toml error: {:?}", e);
ConfigError::ConfigFileIsBroken
}
}
impl From<std::io::Error> 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(())
}
}

49
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 <https://www.gnu.org/licenses/>.
*/
/// 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<toml::de::Error> for ConfigError {
fn from(e: toml::de::Error) -> Self {
log::debug!("Toml error: {:?}", e);
ConfigError::ConfigFileIsBroken
}
}
impl From<std::io::Error> for ConfigError {
fn from(e: std::io::Error) -> Self {
log::debug!("I/O error: {:?}", e);
ConfigError::ConfigFileIsBroken
}
}
impl From<toml::ser::Error> for ConfigError {
fn from(e: toml::ser::Error) -> Self {
log::debug!("TOML error: {:?}", e);
ConfigError::InvalidConfiguration
}
}

1
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 {

Loading…
Cancel
Save