refactor(tauri): extract credential_domain helper

Three call sites reproduced the same scheme://host parsing inline. Pull
it into a named helper so the domain-extraction convention lives in one
place.
This commit is contained in:
Claude 2026-04-19 07:11:53 +00:00
parent 937b6c2c7d
commit e911ac1d94
No known key found for this signature in database

View file

@ -67,6 +67,16 @@ impl AppState {
} }
} }
/// Extract the hostname from a URL (scheme://host/...), used as the credential key.
/// Returns an empty string if the URL has no scheme or host.
fn credential_domain(url: &str) -> String {
url.split("://")
.nth(1)
.and_then(|rest| rest.split('/').next())
.unwrap_or("")
.to_string()
}
/// 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);
@ -266,10 +276,7 @@ async fn rename_workspace(
let base_url = webdav_url.as_deref().ok_or("No WebDAV URL configured")?; let base_url = webdav_url.as_deref().ok_or("No WebDAV URL configured")?;
let remote_path = webdav_path.as_deref().unwrap_or(""); let remote_path = webdav_path.as_deref().unwrap_or("");
let domain = base_url let domain = credential_domain(base_url);
.split("://").nth(1)
.and_then(|rest| rest.split('/').next())
.unwrap_or("").to_string();
let creds = app_handle.state::<Credentials<tauri::Wry>>(); let creds = app_handle.state::<Credentials<tauri::Wry>>();
let (username, password) = creds.load(&domain)?; let (username, password) = creds.load(&domain)?;
@ -761,12 +768,7 @@ fn add_webdav_workspace(
s.repo = None; s.repo = None;
// Store credentials keyed by hostname // Store credentials keyed by hostname
let domain = webdav_url let domain = credential_domain(&webdav_url);
.split("://")
.nth(1)
.and_then(|rest| rest.split('/').next())
.unwrap_or("")
.to_string();
s.save_config()?; s.save_config()?;
drop(s); drop(s);
let creds = app_handle.state::<Credentials<tauri::Wry>>(); let creds = app_handle.state::<Credentials<tauri::Wry>>();
@ -829,12 +831,7 @@ async fn sync_workspace(
}; };
// Step 2: load credentials // Step 2: load credentials
let domain = webdav_url let domain = credential_domain(&webdav_url);
.split("://")
.nth(1)
.and_then(|rest| rest.split('/').next())
.unwrap_or("")
.to_string();
let creds = app_handle.state::<Credentials<tauri::Wry>>(); let creds = app_handle.state::<Credentials<tauri::Wry>>();
let (username, password) = creds.load(&domain)?; let (username, password) = creds.load(&domain)?;