Refactoring loading of config file

- let `HuskConfig::load` return a `Result` instead of an `Option`.
- added comments
This commit is contained in:
Malte Meiboom
2026-01-05 12:32:30 +01:00
parent a56e933e80
commit b7e01a446c
7 changed files with 36 additions and 15 deletions
+7 -6
View File
@@ -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())
}
}
}