diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 9315a36..68bb71d 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -562,6 +562,7 @@ dependencies = [ "tauri-build", "tauri-plugin-log", "tauri-plugin-opener", + "toml 0.8.2", ] [[package]] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index ffe61c2..dcf7bb0 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -25,6 +25,7 @@ tauri = { version = "2.8.2", features = [] } tauri-plugin-log = "2" tauri-plugin-opener = "2" dirs = "5.0" +toml = "0.8" [target.'cfg(target_os = "macos")'.dependencies] objc2 = "0.5" diff --git a/src-tauri/src/codex_config.rs b/src-tauri/src/codex_config.rs index 7028b64..f3cb151 100644 --- a/src-tauri/src/codex_config.rs +++ b/src-tauri/src/codex_config.rs @@ -74,6 +74,11 @@ pub fn save_codex_provider_config( // 保存 config.toml if let Some(config) = settings_config.get("config") { if let Some(config_str) = config.as_str() { + // 若非空则进行 TOML 语法校验 + if !config_str.trim().is_empty() { + toml::from_str::(config_str) + .map_err(|e| format!("config.toml 格式错误: {}", e))?; + } fs::write(&config_path, config_str) .map_err(|e| format!("写入供应商 config.toml 失败: {}", e))?; } @@ -143,8 +148,13 @@ pub fn import_current_codex_config() -> Result { // 读取 config.toml(允许不存在或读取失败时为空) let config_str = if config_path.exists() { - fs::read_to_string(&config_path) - .map_err(|e| format!("读取 config.toml 失败: {}", e))? + let s = fs::read_to_string(&config_path) + .map_err(|e| format!("读取 config.toml 失败: {}", e))?; + if !s.trim().is_empty() { + toml::from_str::(&s) + .map_err(|e| format!("config.toml 语法错误: {}", e))?; + } + s } else { String::new() };