feat(codex): validate non-empty config.toml with toml crate (syntax check in save/import)

This commit is contained in:
Jason
2025-08-31 17:13:25 +08:00
parent 417dcc1d37
commit 5edc3e07a4
3 changed files with 14 additions and 2 deletions

1
src-tauri/Cargo.lock generated
View File

@@ -562,6 +562,7 @@ dependencies = [
"tauri-build",
"tauri-plugin-log",
"tauri-plugin-opener",
"toml 0.8.2",
]
[[package]]

View File

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

View File

@@ -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::<toml::Table>(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<Value, String> {
// 读取 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::<toml::Table>(&s)
.map_err(|e| format!("config.toml 语法错误: {}", e))?;
}
s
} else {
String::new()
};