From abdcbdf6dc2e4e37e970365b1dece47a40128162 Mon Sep 17 00:00:00 2001 From: TheAwiteb Date: Wed, 3 Jan 2024 15:46:07 +0300 Subject: [PATCH] Create the local dir in the `local_project_file` function Co-authored-by: Amjad Alsharafi <26300843+Amjad50@users.noreply.github.com> --- src/utils.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/utils.rs b/src/utils.rs index 945459e..d053f34 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -20,19 +20,20 @@ use crate::{LprsError, LprsResult}; /// Returns the local project dir joined with the given file name pub fn local_project_file(filename: &str) -> LprsResult { - directories::ProjectDirs::from("", "", "lprs") - .map(|d| d.data_local_dir().to_path_buf().join(filename)) + let local_dir = directories::ProjectDirs::from("", "", "lprs") + .map(|d| d.data_local_dir().to_path_buf()) .ok_or_else(|| { LprsError::ProjectDir("Can't extract the project_dir from this OS".to_owned()) - }) + })?; + if !local_dir.exists() { + fs::create_dir_all(&local_dir)?; + } + Ok(local_dir.join(filename)) } /// Returns the default passwords json file pub fn passwords_file() -> LprsResult { let password_file = local_project_file(crate::DEFAULT_PASSWORD_FILE)?; - if let Some(parent) = password_file.parent() { - std::fs::create_dir_all(parent)?; - } if !password_file.exists() { fs::write(&password_file, "[]")?; }