From 9092e97bc25ab7971be169c8682f9e41ead4e0e5 Mon Sep 17 00:00:00 2001 From: Jason Date: Sun, 8 Mar 2026 22:27:53 +0800 Subject: [PATCH] style: format Rust code with cargo fmt --- src-tauri/src/commands/config.rs | 4 ++- src-tauri/src/provider.rs | 5 ++- src-tauri/src/proxy/usage/parser.rs | 10 +++--- src-tauri/src/services/provider/live.rs | 42 ++++++++++++++++--------- src-tauri/src/services/provider/mod.rs | 13 ++++---- 5 files changed, 47 insertions(+), 27 deletions(-) diff --git a/src-tauri/src/commands/config.rs b/src-tauri/src/commands/config.rs index 8bb166f2..0fa6e4a2 100644 --- a/src-tauri/src/commands/config.rs +++ b/src-tauri/src/commands/config.rs @@ -260,7 +260,9 @@ pub async fn set_common_config_snippet( }; if matches!(app_type.as_str(), "claude" | "codex" | "gemini") { - if let Some(legacy_snippet) = old_snippet.as_deref().filter(|value| !value.trim().is_empty()) + if let Some(legacy_snippet) = old_snippet + .as_deref() + .filter(|value| !value.trim().is_empty()) { let app = AppType::from_str(&app_type).map_err(|e| e.to_string())?; crate::services::provider::ProviderService::migrate_legacy_common_config_usage( diff --git a/src-tauri/src/provider.rs b/src-tauri/src/provider.rs index 8ddbe9da..43e5dafe 100644 --- a/src-tauri/src/provider.rs +++ b/src-tauri/src/provider.rs @@ -198,7 +198,10 @@ pub struct ProviderMeta { #[serde(default, skip_serializing_if = "HashMap::is_empty")] pub custom_endpoints: HashMap, /// 是否在写入 live 时应用通用配置片段 - #[serde(rename = "commonConfigEnabled", skip_serializing_if = "Option::is_none")] + #[serde( + rename = "commonConfigEnabled", + skip_serializing_if = "Option::is_none" + )] pub common_config_enabled: Option, /// 用量查询脚本配置 #[serde(skip_serializing_if = "Option::is_none")] diff --git a/src-tauri/src/proxy/usage/parser.rs b/src-tauri/src/proxy/usage/parser.rs index f3ce1362..2541652c 100644 --- a/src-tauri/src/proxy/usage/parser.rs +++ b/src-tauri/src/proxy/usage/parser.rs @@ -111,8 +111,9 @@ impl TokenUsage { } // 从 message_delta 中处理缓存命中(cache_read_input_tokens) if usage.cache_read_tokens == 0 { - if let Some(cache_read) = - delta_usage.get("cache_read_input_tokens").and_then(|v| v.as_u64()) + if let Some(cache_read) = delta_usage + .get("cache_read_input_tokens") + .and_then(|v| v.as_u64()) { usage.cache_read_tokens = cache_read as u32; } @@ -120,8 +121,9 @@ impl TokenUsage { // 从 message_delta 中处理缓存创建(cache_creation_input_tokens) // 注: 现在 zhipu 没有返回 cache_creation_input_tokens 字段 if usage.cache_creation_tokens == 0 { - if let Some(cache_creation) = - delta_usage.get("cache_creation_input_tokens").and_then(|v| v.as_u64()) + if let Some(cache_creation) = delta_usage + .get("cache_creation_input_tokens") + .and_then(|v| v.as_u64()) { usage.cache_creation_tokens = cache_creation as u32; } diff --git a/src-tauri/src/services/provider/live.rs b/src-tauri/src/services/provider/live.rs index 8acc04a7..d096d96e 100644 --- a/src-tauri/src/services/provider/live.rs +++ b/src-tauri/src/services/provider/live.rs @@ -39,11 +39,11 @@ fn json_is_subset(target: &Value, source: &Value) -> bool { let Some(target_map) = target.as_object() else { return false; }; - source_map - .iter() - .all(|(key, source_value)| target_map.get(key).is_some_and(|target_value| { - json_is_subset(target_value, source_value) - })) + source_map.iter().all(|(key, source_value)| { + target_map + .get(key) + .is_some_and(|target_value| json_is_subset(target_value, source_value)) + }) } Value::Array(source_arr) => { let Some(target_arr) = target.as_array() else { @@ -163,9 +163,13 @@ fn toml_array_contains_subset(target: &toml_edit::Array, source: &toml_edit::Arr let target_items: Vec<&toml_edit::Value> = target.iter().collect(); source.iter().all(|source_item| { - if let Some((index, _)) = target_items.iter().enumerate().find(|(index, target_item)| { - !matched[*index] && toml_value_is_subset(target_item, source_item) - }) { + if let Some((index, _)) = target_items + .iter() + .enumerate() + .find(|(index, target_item)| { + !matched[*index] && toml_value_is_subset(target_item, source_item) + }) + { matched[index] = true; true } else { @@ -204,7 +208,9 @@ fn toml_item_is_subset(target: &Item, source: &Item) -> bool { } match (target.as_value(), source.as_value()) { - (Some(target_value), Some(source_value)) => toml_value_is_subset(target_value, source_value), + (Some(target_value), Some(source_value)) => { + toml_value_is_subset(target_value, source_value) + } _ => false, } } @@ -251,7 +257,9 @@ fn remove_toml_item(target: &mut Item, source: &Item) { toml_remove_array_items(target_arr, source_arr); remove_item = target_arr.is_empty(); } - (target_value, source_value) if toml_value_is_subset(target_value, source_value) => { + (target_value, source_value) + if toml_value_is_subset(target_value, source_value) => + { remove_item = true; } _ => {} @@ -370,7 +378,9 @@ pub(crate) fn remove_common_config_from_settings( DocumentMut::new() } else { config_toml.parse::().map_err(|e| { - AppError::Message(format!("Invalid Codex config.toml while removing common config: {e}")) + AppError::Message(format!( + "Invalid Codex config.toml while removing common config: {e}" + )) })? }; let source_doc = trimmed.parse::().map_err(|e| { @@ -421,7 +431,9 @@ fn apply_common_config_to_settings( DocumentMut::new() } else { config_toml.parse::().map_err(|e| { - AppError::Message(format!("Invalid Codex config.toml while applying common config: {e}")) + AppError::Message(format!( + "Invalid Codex config.toml while applying common config: {e}" + )) })? }; let source_doc = trimmed.parse::().map_err(|e| { @@ -791,7 +803,8 @@ pub(crate) fn sync_current_provider_for_app_to_live( if app_type.is_additive_mode() { sync_all_providers_to_live(state, app_type)?; } else { - let current_id = match crate::settings::get_effective_current_provider(&state.db, app_type)? { + let current_id = match crate::settings::get_effective_current_provider(&state.db, app_type)? + { Some(id) => id, None => return Ok(()), }; @@ -1326,8 +1339,7 @@ mod tests { }); let snippet = "[shared]\nreasoning = \"medium\"\n"; - let applied = - apply_common_config_to_settings(&AppType::Codex, &settings, snippet).unwrap(); + let applied = apply_common_config_to_settings(&AppType::Codex, &settings, snippet).unwrap(); let applied_config = applied["config"].as_str().unwrap_or_default(); assert!(applied_config.contains("[shared]")); assert!(applied_config.contains("reasoning = \"medium\"")); diff --git a/src-tauri/src/services/provider/mod.rs b/src-tauri/src/services/provider/mod.rs index 4c0bc695..1ecfd4e8 100644 --- a/src-tauri/src/services/provider/mod.rs +++ b/src-tauri/src/services/provider/mod.rs @@ -565,12 +565,13 @@ impl ProviderService { // Only backfill when switching to a different provider if let Ok(live_config) = read_live_settings(app_type.clone()) { if let Some(mut current_provider) = providers.get(¤t_id).cloned() { - current_provider.settings_config = strip_common_config_from_live_settings( - state.db.as_ref(), - &app_type, - ¤t_provider, - live_config, - ); + current_provider.settings_config = + strip_common_config_from_live_settings( + state.db.as_ref(), + &app_type, + ¤t_provider, + live_config, + ); if let Err(e) = state.db.save_provider(app_type.as_str(), ¤t_provider) {