config: Rework error handling into explicit error types
This commit is contained in:
@ -18,8 +18,16 @@
|
||||
*/
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::{fs, io};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)]
|
||||
pub enum Error {
|
||||
FileNotFoundError(String),
|
||||
PermissionDeniedError(),
|
||||
InvalidFormatError(serde_yaml::Error),
|
||||
IoError(io::Error),
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone, Debug)]
|
||||
pub struct FeedConfig {
|
||||
@ -38,9 +46,19 @@ pub struct Config {
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load(config_file: &str) -> Result<Self, Box<dyn Error>> {
|
||||
let serialized_config = fs::read_to_string(config_file)?;
|
||||
let config: Config = serde_yaml::from_str(&serialized_config)?;
|
||||
pub fn load(config_file: &str) -> Result<Self, Error> {
|
||||
|
||||
let serialized_config = fs::read_to_string(config_file).map_err(|e| {
|
||||
match e.kind() {
|
||||
io::ErrorKind::NotFound => Error::FileNotFoundError(config_file.into()),
|
||||
io::ErrorKind::PermissionDenied => Error::PermissionDeniedError(),
|
||||
_ => Error::IoError(e),
|
||||
}
|
||||
})?;
|
||||
|
||||
let config: Config = serde_yaml::from_str(&serialized_config)
|
||||
.map_err(|e| Error::InvalidFormatError(e))?;
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user