refactor Config::load
This commit is contained in:
@@ -17,12 +17,17 @@ pub struct Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
/// Load the config from the associated file. If the config does not exist, create a default
|
/// Load the config from the associated file. If the file does not exist, create a default config.
|
||||||
/// 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> {
|
pub fn load(config_file: &Path) -> Result<Self> {
|
||||||
let from_file = read_to_string(config_file);
|
let from_file = read_to_string(config_file);
|
||||||
|
|
||||||
if let Err(e) = from_file {
|
let content = match from_file {
|
||||||
|
Ok(content) => content,
|
||||||
|
Err(e) => {
|
||||||
if e.kind() == ErrorKind::NotFound {
|
if e.kind() == ErrorKind::NotFound {
|
||||||
println!(
|
println!(
|
||||||
"creating default config under {}",
|
"creating default config under {}",
|
||||||
@@ -32,8 +37,7 @@ impl Config {
|
|||||||
}
|
}
|
||||||
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")
|
serde_yaml::from_str(&content).context("parsing yaml config")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user