style: format Rust code with cargo fmt

This commit is contained in:
Jason
2026-03-08 22:27:53 +08:00
parent 032a8203fd
commit 9092e97bc2
5 changed files with 47 additions and 27 deletions

View File

@@ -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(

View File

@@ -198,7 +198,10 @@ pub struct ProviderMeta {
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub custom_endpoints: HashMap<String, crate::settings::CustomEndpoint>,
/// 是否在写入 live 时应用通用配置片段
#[serde(rename = "commonConfigEnabled", skip_serializing_if = "Option::is_none")]
#[serde(
rename = "commonConfigEnabled",
skip_serializing_if = "Option::is_none"
)]
pub common_config_enabled: Option<bool>,
/// 用量查询脚本配置
#[serde(skip_serializing_if = "Option::is_none")]

View File

@@ -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;
}

View File

@@ -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::<DocumentMut>().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::<DocumentMut>().map_err(|e| {
@@ -421,7 +431,9 @@ fn apply_common_config_to_settings(
DocumentMut::new()
} else {
config_toml.parse::<DocumentMut>().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::<DocumentMut>().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\""));

View File

@@ -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(&current_id).cloned() {
current_provider.settings_config = strip_common_config_from_live_settings(
state.db.as_ref(),
&app_type,
&current_provider,
live_config,
);
current_provider.settings_config =
strip_common_config_from_live_settings(
state.db.as_ref(),
&app_type,
&current_provider,
live_config,
);
if let Err(e) =
state.db.save_provider(app_type.as_str(), &current_provider)
{