Skip to content

Commit

Permalink
Refactor fn
Browse files Browse the repository at this point in the history
  • Loading branch information
hatchan committed Sep 18, 2024
1 parent 7deec92 commit c4d7a7d
Showing 1 changed file with 30 additions and 38 deletions.
68 changes: 30 additions & 38 deletions fpx-app/src/commands/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,50 +43,42 @@ pub fn open_workspace_by_path<R: Runtime>(
app: AppHandle<R>,
stores: State<'_, StoreCollection<R>>,
) -> Result<Workspace, OpenWorkspaceByPathError> {
match read_to_string(format!("{}/fpx.toml", path)) {
Ok(content) => match toml::from_str::<Config>(&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<String> = 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<String> = 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]
Expand Down

0 comments on commit c4d7a7d

Please sign in to comment.