Refactoring loading of config file
- let `HuskConfig::load` return a `Result` instead of an `Option`. - added comments
This commit is contained in:
+7
-6
@@ -2,10 +2,13 @@
|
||||
// Parsing the config file
|
||||
//
|
||||
|
||||
use anyhow;
|
||||
use std::fs;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use serde_derive::Deserialize;
|
||||
|
||||
use crate::types::errors::HuskError;
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct HuskConfig {
|
||||
pub connection: String,
|
||||
@@ -15,20 +18,18 @@ pub struct HuskConfig {
|
||||
|
||||
impl HuskConfig {
|
||||
|
||||
pub fn load(path: &String) -> Option<Self> {
|
||||
pub fn load(path: &String) -> anyhow::Result<Self> {
|
||||
match fs::read_to_string(path) {
|
||||
Ok(data) => {
|
||||
match toml::from_str(data.as_str()) {
|
||||
Ok(configuration) => Some(configuration),
|
||||
Ok(configuration) => Ok(configuration),
|
||||
Err(e) => {
|
||||
eprintln!("Error while parsing config file: {:?}", e);
|
||||
None
|
||||
Err(HuskError::ConfigFileParseError(e.to_string()).into())
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
eprintln!("Error while reading config file: {:?}", e);
|
||||
None
|
||||
Err(HuskError::ConfigFileOpenError(e.to_string()).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user