summaryrefslogtreecommitdiffstats
path: root/src/config/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config/mod.rs')
-rw-r--r--src/config/mod.rs32
1 files changed, 17 insertions, 15 deletions
diff --git a/src/config/mod.rs b/src/config/mod.rs
index c1c71b7..db46f4d 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -13,8 +13,11 @@ pub use self::theme::*;
use serde::de::DeserializeOwned;
use std::fs;
+use std::io;
use std::path::{Path, PathBuf};
+use crate::error::JoshutoError;
+use crate::error::JoshutoResult;
use crate::CONFIG_HIERARCHY;
pub trait TomlConfigFile {
@@ -36,25 +39,24 @@ where
}
// parses a config file into its appropriate format
-fn parse_to_config_file<T, S>(filename: &str) -> Option<S>
+fn parse_to_config_file<T, S>(filename: &str) -> JoshutoResult<S>
where
T: DeserializeOwned,
S: From<T>,
{
- let file_path = search_directories(filename, &CONFIG_HIERARCHY)?;
- let file_contents = match fs::read_to_string(&file_path) {
- Ok(content) => content,
- Err(e) => {
- eprintln!("Error reading {} file: {}", filename, e);
- return None;
+ match search_directories(filename, &CONFIG_HIERARCHY) {
+ Some(file_path) => {
+ let file_contents = fs::read_to_string(&file_path)?;
+ let config = toml::from_str::<T>(&file_contents)?;
+ Ok(S::from(config))
}
- };
- let config = match toml::from_str::<T>(&file_contents) {
- Ok(config) => config,
- Err(e) => {
- eprintln!("Error parsing {} file: {}", filename, e);
- return None;
+ None => {
+ let error_kind = io::ErrorKind::NotFound;
+ let error = JoshutoError::new(
+ crate::error::JoshutoErrorKind::Io(error_kind),
+ "No config directory found".to_string(),
+ );
+ Err(error)
}
- };
- Some(S::from(config))
+ }
}