diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 155c36c660..013dd97c7c 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -52,14 +52,22 @@ async fn set_state(app: tauri::AppHandle, state: Option) -> Result Ok(()) } -fn get_app_settings_file_path(app: &tauri::AppHandle) -> Result { +async fn get_app_settings_file_path(app: &tauri::AppHandle) -> Result { let app_config_dir = app.path().app_config_dir()?; + + // Ensure this directory exists. + if !app_config_dir.exists() { + tokio::fs::create_dir_all(&app_config_dir) + .await + .map_err(|e| InvokeError::from_anyhow(e.into()))?; + } + Ok(app_config_dir.join(SETTINGS_FILE_NAME)) } #[tauri::command] async fn read_app_settings_file(app: tauri::AppHandle) -> Result { - let mut settings_path = get_app_settings_file_path(&app)?; + let mut settings_path = get_app_settings_file_path(&app).await?; let mut needs_migration = false; // Check if this file exists. @@ -104,7 +112,7 @@ async fn read_app_settings_file(app: tauri::AppHandle) -> Result Result<(), InvokeError> { - let settings_path = get_app_settings_file_path(&app)?; + let settings_path = get_app_settings_file_path(&app).await?; let contents = toml::to_string_pretty(&configuration).map_err(|e| InvokeError::from_anyhow(e.into()))?; tokio::fs::write(settings_path, contents.as_bytes()) .await @@ -113,13 +121,19 @@ async fn write_app_settings_file(app: tauri::AppHandle, configuration: Configura Ok(()) } -fn get_project_settings_file_path(app_settings: Configuration, project_name: &str) -> Result { - Ok(app_settings - .settings - .project - .directory - .join(project_name) - .join(PROJECT_SETTINGS_FILE_NAME)) +async fn get_project_settings_file_path( + app_settings: Configuration, + project_name: &str, +) -> Result { + let project_dir = app_settings.settings.project.directory.join(project_name); + + if !project_dir.exists() { + tokio::fs::create_dir_all(&project_dir) + .await + .map_err(|e| InvokeError::from_anyhow(e.into()))?; + } + + Ok(project_dir.join(PROJECT_SETTINGS_FILE_NAME)) } #[tauri::command] @@ -127,7 +141,7 @@ async fn read_project_settings_file( app_settings: Configuration, project_name: &str, ) -> Result { - let settings_path = get_project_settings_file_path(app_settings, project_name)?; + let settings_path = get_project_settings_file_path(app_settings, project_name).await?; // Check if this file exists. if !settings_path.exists() { @@ -149,7 +163,7 @@ async fn write_project_settings_file( project_name: &str, configuration: ProjectConfiguration, ) -> Result<(), InvokeError> { - let settings_path = get_project_settings_file_path(app_settings, project_name)?; + let settings_path = get_project_settings_file_path(app_settings, project_name).await?; let contents = toml::to_string_pretty(&configuration).map_err(|e| InvokeError::from_anyhow(e.into()))?; tokio::fs::write(settings_path, contents.as_bytes()) .await