mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-25 13:45:03 +08:00
chore(backend): satisfy cargo fmt and clippy --all-targets
- Apply rustfmt diffs in claude_desktop_config.rs - Allow needless_return on current_platform_paths (cfg-mirrored arms) - Allow too_many_arguments on RequestForwarder::forward - Replace `let mut + reassign` with struct literals in tests (settings, backup, provider, response_processor) - Use Path::new instead of PathBuf::from to fix cmp_owned in misc tests - Replace 3.14 with 3.5 in config test to avoid approx_constant lint
This commit is contained in:
@@ -586,15 +586,12 @@ pub fn map_proxy_request_model(mut body: Value, provider: &Provider) -> Result<V
|
||||
})?;
|
||||
|
||||
let routes = proxy_model_routes(provider)?;
|
||||
let route = routes
|
||||
.iter()
|
||||
.find(|r| r.route_id == requested)
|
||||
.or_else(|| {
|
||||
let base = strip_one_m_context_suffix(&requested);
|
||||
routes
|
||||
.iter()
|
||||
.find(|r| strip_one_m_context_suffix(&r.route_id) == base)
|
||||
});
|
||||
let route = routes.iter().find(|r| r.route_id == requested).or_else(|| {
|
||||
let base = strip_one_m_context_suffix(&requested);
|
||||
routes
|
||||
.iter()
|
||||
.find(|r| strip_one_m_context_suffix(&r.route_id) == base)
|
||||
});
|
||||
let Some(route) = route else {
|
||||
return Err(AppError::localized(
|
||||
"claude_desktop.provider.route_unknown",
|
||||
@@ -628,7 +625,9 @@ fn apply_provider_to_paths(
|
||||
}
|
||||
|
||||
validate_provider(provider)?;
|
||||
with_rollback(paths, |paths| apply_provider_to_paths_inner(db, provider, paths))
|
||||
with_rollback(paths, |paths| {
|
||||
apply_provider_to_paths_inner(db, provider, paths)
|
||||
})
|
||||
}
|
||||
|
||||
fn restore_official_at_paths(paths: &ClaudeDesktopPaths) -> Result<(), AppError> {
|
||||
@@ -645,9 +644,7 @@ where
|
||||
Err(err) => match restore_snapshots(&snapshots) {
|
||||
Ok(()) => Err(err),
|
||||
Err(rollback_err) => {
|
||||
log::error!(
|
||||
"Failed to rollback Claude Desktop config after error: {rollback_err}"
|
||||
);
|
||||
log::error!("Failed to rollback Claude Desktop config after error: {rollback_err}");
|
||||
Err(AppError::Message(format!(
|
||||
"{err}; rollback failed: {rollback_err}"
|
||||
)))
|
||||
@@ -894,6 +891,7 @@ fn is_supported_platform() -> bool {
|
||||
cfg!(any(target_os = "macos", windows))
|
||||
}
|
||||
|
||||
#[allow(clippy::needless_return)]
|
||||
fn current_platform_paths() -> Result<ClaudeDesktopPaths, AppError> {
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
|
||||
@@ -1591,7 +1591,7 @@ pub async fn set_window_theme(window: tauri::Window, theme: String) -> Result<()
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[test]
|
||||
fn test_extract_version() {
|
||||
@@ -1679,7 +1679,7 @@ mod tests {
|
||||
|
||||
let count = paths
|
||||
.iter()
|
||||
.filter(|path| **path == PathBuf::from("/same/path"))
|
||||
.filter(|path| path.as_path() == Path::new("/same/path"))
|
||||
.count();
|
||||
assert_eq!(count, 1);
|
||||
}
|
||||
@@ -1691,7 +1691,7 @@ mod tests {
|
||||
|
||||
let count = paths
|
||||
.iter()
|
||||
.filter(|path| **path == PathBuf::from("/home/tester/.bun/bin"))
|
||||
.filter(|path| path.as_path() == Path::new("/home/tester/.bun/bin"))
|
||||
.count();
|
||||
assert_eq!(count, 1);
|
||||
}
|
||||
|
||||
@@ -87,13 +87,15 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn save_settings_should_preserve_existing_webdav_when_payload_omits_it() {
|
||||
let mut existing = AppSettings::default();
|
||||
existing.webdav_sync = Some(WebDavSyncSettings {
|
||||
base_url: "https://dav.example.com".to_string(),
|
||||
username: "alice".to_string(),
|
||||
password: "secret".to_string(),
|
||||
..WebDavSyncSettings::default()
|
||||
});
|
||||
let existing = AppSettings {
|
||||
webdav_sync: Some(WebDavSyncSettings {
|
||||
base_url: "https://dav.example.com".to_string(),
|
||||
username: "alice".to_string(),
|
||||
password: "secret".to_string(),
|
||||
..WebDavSyncSettings::default()
|
||||
}),
|
||||
..AppSettings::default()
|
||||
};
|
||||
|
||||
let incoming = AppSettings::default();
|
||||
let merged = merge_settings_for_save(incoming, &existing);
|
||||
@@ -107,21 +109,25 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn save_settings_should_keep_incoming_webdav_when_present() {
|
||||
let mut existing = AppSettings::default();
|
||||
existing.webdav_sync = Some(WebDavSyncSettings {
|
||||
base_url: "https://dav.old.example.com".to_string(),
|
||||
username: "old".to_string(),
|
||||
password: "old-pass".to_string(),
|
||||
..WebDavSyncSettings::default()
|
||||
});
|
||||
let existing = AppSettings {
|
||||
webdav_sync: Some(WebDavSyncSettings {
|
||||
base_url: "https://dav.old.example.com".to_string(),
|
||||
username: "old".to_string(),
|
||||
password: "old-pass".to_string(),
|
||||
..WebDavSyncSettings::default()
|
||||
}),
|
||||
..AppSettings::default()
|
||||
};
|
||||
|
||||
let mut incoming = AppSettings::default();
|
||||
incoming.webdav_sync = Some(WebDavSyncSettings {
|
||||
base_url: "https://dav.new.example.com".to_string(),
|
||||
username: "new".to_string(),
|
||||
password: "new-pass".to_string(),
|
||||
..WebDavSyncSettings::default()
|
||||
});
|
||||
let incoming = AppSettings {
|
||||
webdav_sync: Some(WebDavSyncSettings {
|
||||
base_url: "https://dav.new.example.com".to_string(),
|
||||
username: "new".to_string(),
|
||||
password: "new-pass".to_string(),
|
||||
..WebDavSyncSettings::default()
|
||||
}),
|
||||
..AppSettings::default()
|
||||
};
|
||||
|
||||
let merged = merge_settings_for_save(incoming, &existing);
|
||||
|
||||
@@ -137,22 +143,26 @@ mod tests {
|
||||
/// must NOT overwrite the existing one.
|
||||
#[test]
|
||||
fn save_settings_should_preserve_password_when_incoming_has_empty_password() {
|
||||
let mut existing = AppSettings::default();
|
||||
existing.webdav_sync = Some(WebDavSyncSettings {
|
||||
base_url: "https://dav.example.com".to_string(),
|
||||
username: "alice".to_string(),
|
||||
password: "secret".to_string(),
|
||||
..WebDavSyncSettings::default()
|
||||
});
|
||||
let existing = AppSettings {
|
||||
webdav_sync: Some(WebDavSyncSettings {
|
||||
base_url: "https://dav.example.com".to_string(),
|
||||
username: "alice".to_string(),
|
||||
password: "secret".to_string(),
|
||||
..WebDavSyncSettings::default()
|
||||
}),
|
||||
..AppSettings::default()
|
||||
};
|
||||
|
||||
// Simulate frontend sending settings with cleared password
|
||||
let mut incoming = AppSettings::default();
|
||||
incoming.webdav_sync = Some(WebDavSyncSettings {
|
||||
base_url: "https://dav.example.com".to_string(),
|
||||
username: "alice".to_string(),
|
||||
password: "".to_string(),
|
||||
..WebDavSyncSettings::default()
|
||||
});
|
||||
let incoming = AppSettings {
|
||||
webdav_sync: Some(WebDavSyncSettings {
|
||||
base_url: "https://dav.example.com".to_string(),
|
||||
username: "alice".to_string(),
|
||||
password: "".to_string(),
|
||||
..WebDavSyncSettings::default()
|
||||
}),
|
||||
..AppSettings::default()
|
||||
};
|
||||
|
||||
let merged = merge_settings_for_save(incoming, &existing);
|
||||
|
||||
@@ -167,21 +177,25 @@ mod tests {
|
||||
/// work without panicking and keep the empty state.
|
||||
#[test]
|
||||
fn save_settings_should_handle_both_empty_passwords() {
|
||||
let mut existing = AppSettings::default();
|
||||
existing.webdav_sync = Some(WebDavSyncSettings {
|
||||
base_url: "https://dav.example.com".to_string(),
|
||||
username: "alice".to_string(),
|
||||
password: "".to_string(),
|
||||
..WebDavSyncSettings::default()
|
||||
});
|
||||
let existing = AppSettings {
|
||||
webdav_sync: Some(WebDavSyncSettings {
|
||||
base_url: "https://dav.example.com".to_string(),
|
||||
username: "alice".to_string(),
|
||||
password: "".to_string(),
|
||||
..WebDavSyncSettings::default()
|
||||
}),
|
||||
..AppSettings::default()
|
||||
};
|
||||
|
||||
let mut incoming = AppSettings::default();
|
||||
incoming.webdav_sync = Some(WebDavSyncSettings {
|
||||
base_url: "https://dav.example.com".to_string(),
|
||||
username: "alice".to_string(),
|
||||
password: "".to_string(),
|
||||
..WebDavSyncSettings::default()
|
||||
});
|
||||
let incoming = AppSettings {
|
||||
webdav_sync: Some(WebDavSyncSettings {
|
||||
base_url: "https://dav.example.com".to_string(),
|
||||
username: "alice".to_string(),
|
||||
password: "".to_string(),
|
||||
..WebDavSyncSettings::default()
|
||||
}),
|
||||
..AppSettings::default()
|
||||
};
|
||||
|
||||
let merged = merge_settings_for_save(incoming, &existing);
|
||||
|
||||
|
||||
@@ -342,7 +342,7 @@ mod tests {
|
||||
let cases = vec![
|
||||
serde_json::json!("hello"),
|
||||
serde_json::json!(42),
|
||||
serde_json::json!(3.14),
|
||||
serde_json::json!(3.5),
|
||||
serde_json::json!(true),
|
||||
serde_json::json!(null),
|
||||
];
|
||||
|
||||
@@ -791,8 +791,10 @@ mod tests {
|
||||
std::fs::create_dir_all(&test_home).expect("create test home");
|
||||
std::env::set_var("CC_SWITCH_TEST_HOME", &test_home);
|
||||
|
||||
let mut settings = AppSettings::default();
|
||||
settings.backup_interval_hours = Some(0);
|
||||
let settings = AppSettings {
|
||||
backup_interval_hours: Some(0),
|
||||
..AppSettings::default()
|
||||
};
|
||||
update_settings(settings).expect("disable auto backup");
|
||||
|
||||
let db = Database::memory()?;
|
||||
|
||||
@@ -760,8 +760,10 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn provider_meta_serializes_pricing_model_source() {
|
||||
let mut meta = ProviderMeta::default();
|
||||
meta.pricing_model_source = Some("response".to_string());
|
||||
let meta = ProviderMeta {
|
||||
pricing_model_source: Some("response".to_string()),
|
||||
..ProviderMeta::default()
|
||||
};
|
||||
|
||||
let value = serde_json::to_value(&meta).expect("serialize ProviderMeta");
|
||||
|
||||
|
||||
@@ -764,6 +764,7 @@ impl RequestForwarder {
|
||||
}
|
||||
|
||||
/// 转发单个请求(使用适配器)
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn forward(
|
||||
&self,
|
||||
app_type: &AppType,
|
||||
|
||||
@@ -894,9 +894,11 @@ mod tests {
|
||||
db.set_pricing_model_source(app_type, "response").await?;
|
||||
seed_pricing(&db)?;
|
||||
|
||||
let mut meta = ProviderMeta::default();
|
||||
meta.cost_multiplier = Some("2".to_string());
|
||||
meta.pricing_model_source = Some("request".to_string());
|
||||
let meta = ProviderMeta {
|
||||
cost_multiplier: Some("2".to_string()),
|
||||
pricing_model_source: Some("request".to_string()),
|
||||
..ProviderMeta::default()
|
||||
};
|
||||
insert_provider(&db, "provider-1", app_type, meta)?;
|
||||
|
||||
let state = build_state(db.clone());
|
||||
|
||||
Reference in New Issue
Block a user