refactor(tauri): extract join_remote_path helper

Three call sites repeated the same "empty base -> child, otherwise
trim_end + slash + child" pattern. Pull it into a helper to keep the
join convention consistent across list_remote_folder, inspect, and
create_remote_workspace.
This commit is contained in:
Claude 2026-04-19 07:12:37 +00:00
parent e911ac1d94
commit 62cf05480d
No known key found for this signature in database

View file

@ -77,6 +77,15 @@ fn credential_domain(url: &str) -> String {
.to_string() .to_string()
} }
/// Join a remote base directory with a child path, handling empty base and trailing slashes.
fn join_remote_path(base: &str, child: &str) -> String {
if base.is_empty() {
child.to_string()
} else {
format!("{}/{}", base.trim_end_matches('/'), child)
}
}
/// Validate that a workspace path is a reasonable directory and not a system path. /// Validate that a workspace path is a reasonable directory and not a system path.
fn validate_workspace_path(path: &str) -> Result<(), String> { fn validate_workspace_path(path: &str) -> Result<(), String> {
let p = PathBuf::from(path); let p = PathBuf::from(path);
@ -655,10 +664,9 @@ async fn list_remote_folder(
let dir_entries: Vec<_> = entries.into_iter().filter(|e| e.is_dir).collect(); let dir_entries: Vec<_> = entries.into_iter().filter(|e| e.is_dir).collect();
// Check all subfolders for .onyx-workspace.json in parallel // Check all subfolders for .onyx-workspace.json in parallel
let sub_paths: Vec<_> = dir_entries.iter().map(|entry| { let sub_paths: Vec<_> = dir_entries.iter()
if path.is_empty() { entry.path.clone() } .map(|entry| join_remote_path(&path, &entry.path))
else { format!("{}/{}", path.trim_end_matches('/'), entry.path) } .collect();
}).collect();
let checks: Vec<_> = sub_paths.iter().map(|sp| { let checks: Vec<_> = sub_paths.iter().map(|sp| {
client.list_files(sp) client.list_files(sp)
}).collect(); }).collect();
@ -690,11 +698,7 @@ async fn inspect_remote_workspace(
let mut lists = Vec::new(); let mut lists = Vec::new();
for entry in entries { for entry in entries {
if !entry.is_dir { continue; } if !entry.is_dir { continue; }
let list_path = if path.is_empty() { let list_path = join_remote_path(&path, &entry.path);
entry.path.clone()
} else {
format!("{}/{}", path.trim_end_matches('/'), entry.path)
};
let files = client.list_files(&list_path).await.unwrap_or_else(|e| { let files = client.list_files(&list_path).await.unwrap_or_else(|e| {
eprintln!("Warning: failed to list remote folder '{}': {}", list_path, e); eprintln!("Warning: failed to list remote folder '{}': {}", list_path, e);
Vec::new() Vec::new()
@ -730,11 +734,7 @@ async fn create_remote_workspace(
"list_order": [], "list_order": [],
"last_opened_list": null, "last_opened_list": null,
}); });
let file_path = if path.is_empty() { let file_path = join_remote_path(&path, ".onyx-workspace.json");
".onyx-workspace.json".to_string()
} else {
format!("{}/{}", path.trim_end_matches('/'), ".onyx-workspace.json")
};
client.put_file(&file_path, serde_json::to_string_pretty(&metadata).map_err(|e| e.to_string())?.into_bytes()) client.put_file(&file_path, serde_json::to_string_pretty(&metadata).map_err(|e| e.to_string())?.into_bytes())
.await .await
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;