From f3e7a4204317b565d70aec38041e72355e342d5f Mon Sep 17 00:00:00 2001 From: mxve <68632137+mxve@users.noreply.github.com> Date: Mon, 2 Oct 2023 15:48:22 +0200 Subject: [PATCH] Create config path if it doesn't exist; don't panic --- src/config.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index e848576..f1d92b5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,7 +13,18 @@ pub fn load(config_path: PathBuf) -> Config { } pub fn save(config_path: PathBuf, config: Config) { - fs::write(config_path, serde_json::to_string_pretty(&config).unwrap()).unwrap(); + match fs::write(config_path.clone(), serde_json::to_string_pretty(&config).unwrap()) { + Ok(_) => (), + Err(e) => { + match e.kind() { + std::io::ErrorKind::NotFound => { + fs::create_dir_all(config_path.parent().unwrap()).unwrap(); + return save(config_path, config); + } + _ => println!("Could not save config file, got:\n{}\n", e), + } + } + } } pub fn save_value(config_path: PathBuf, key: &str, value: bool) {