refactor Config::load

This commit is contained in:
steven-omaha
2023-02-15 15:23:11 +01:00
parent 354e8d4741
commit 9e0913ef25
+17 -13
View File
@@ -17,23 +17,27 @@ pub struct Config {
}
impl Config {
/// Load the config from the associated file. If the config does not exist, create a default
/// config.
/// Load the config from the associated file. If the file does not exist, create a default config.
///
/// # Errors
///
/// This function will return an error if the config file exists but cannot be read, its contents are not UTF-8, or the file is malformed.
pub fn load(config_file: &Path) -> Result<Self> {
let from_file = read_to_string(config_file);
if let Err(e) = from_file {
if e.kind() == ErrorKind::NotFound {
println!(
"creating default config under {}",
config_file.to_string_lossy()
);
return Self::use_default_and_save_to(config_file);
let content = match from_file {
Ok(content) => content,
Err(e) => {
if e.kind() == ErrorKind::NotFound {
println!(
"creating default config under {}",
config_file.to_string_lossy()
);
return Self::use_default_and_save_to(config_file);
}
bail!("unexpected error occured: {e:?}");
}
bail!("unexpected error occured: {e:?}");
}
let content = from_file.expect("we already handled that error");
};
serde_yaml::from_str(&content).context("parsing yaml config")
}