From c4d7a7de48eeda416edfd18046335e516b78068f Mon Sep 17 00:00:00 2001 From: Benno van den Berg Date: Wed, 18 Sep 2024 10:59:08 +0200 Subject: [PATCH] Refactor fn --- fpx-app/src/commands/workspace.rs | 68 ++++++++++++++----------------- 1 file changed, 30 insertions(+), 38 deletions(-) diff --git a/fpx-app/src/commands/workspace.rs b/fpx-app/src/commands/workspace.rs index dafb0e4a8..8415801c7 100644 --- a/fpx-app/src/commands/workspace.rs +++ b/fpx-app/src/commands/workspace.rs @@ -43,50 +43,42 @@ pub fn open_workspace_by_path( app: AppHandle, stores: State<'_, StoreCollection>, ) -> Result { - match read_to_string(format!("{}/fpx.toml", path)) { - Ok(content) => match toml::from_str::(&content) { - Ok(config) => { - let workspace = Workspace::new(path.clone(), config); - state.set_workspace(workspace.clone()); + let content = read_to_string(format!("{}/fpx.toml", path)) + .map_err(|_| OpenWorkspaceByPathError::ConfigFileMissing { path: path.clone() })?; - with_store(app, stores, STORE_PATH, |store| { - let mut recents = match store.get(RECENT_WORKSPACES_STORE_KEY) { - Some(Value::Array(arr)) => { - let vec_of_strings: Vec = arr - .iter() - .filter_map(|item| { - if let Some(s) = item.as_str() { - if s != path { - return Some(s.to_string()); - } - } - None - }) - .collect(); + let config: Config = + toml::from_str(&content).map_err(|err| OpenWorkspaceByPathError::InvalidConfiguration { + message: format!("{}", err), + })?; - vec_of_strings - } - _ => vec![], - }; + let workspace = Workspace::new(path.clone(), config); + state.set_workspace(workspace.clone()); - recents.insert(0, path); + with_store(app, stores, STORE_PATH, |store| { + let mut recents: Vec = store + .get(RECENT_WORKSPACES_STORE_KEY) + .map_or_else(Default::default, |value| { + value.as_array().map(|arr| { + arr.iter() + .filter_map(|item| { + item.as_str().filter(|s| s == &path).map(|s| s.to_string()) + }) + .collect() + }) + }) + .unwrap_or_default(); - store - .insert(RECENT_WORKSPACES_STORE_KEY.into(), recents.into()) - .unwrap(); + recents.insert(0, path); - store.save() - }) - .unwrap(); + store + .insert(RECENT_WORKSPACES_STORE_KEY.into(), recents.into()) + .unwrap(); - Ok(workspace) - } - Err(error) => Err(OpenWorkspaceByPathError::InvalidConfiguration { - message: format!("{}", error), - }), - }, - Err(_) => Err(OpenWorkspaceByPathError::ConfigFileMissing { path }), - } + store.save() + }) + .unwrap(); + + Ok(workspace) } #[tauri::command]