Files
cc-switch/src-tauri/src/claude_plugin.rs

134 lines
3.8 KiB
Rust
Raw Normal View History

use std::fs;
use std::path::PathBuf;
refactor(backend): complete phase 1 - unified error handling (100%) Completed the remaining migrations for Phase 1 of backend refactoring plan, achieving 100% coverage of unified error handling with AppError. ## Changes ### Fully Migrated Modules (Result<T, String> → Result<T, AppError>) - **claude_plugin.rs** (35 lines changed) - Migrated 7 public functions: claude_config_path, ensure_claude_dir_exists, read_claude_config, write_claude_config, clear_claude_config, claude_config_status, is_claude_config_applied - Used AppError::io(), AppError::JsonSerialize, AppError::Config - Simplified error handling with helper functions - **settings.rs** (14 lines changed) - Migrated AppSettings::save() and update_settings() - Used AppError::io() for file operations - Used AppError::JsonSerialize for JSON serialization - **import_export.rs** (67 lines changed) - Migrated 8 functions: create_backup, cleanup_old_backups, sync_current_providers_to_live, sync_current_provider_for_app, sync_codex_live, sync_claude_live, export_config_to_file, import_config_from_file, sync_current_providers_live - Used AppError::io(), AppError::json(), AppError::Config - Added proper error context with file paths and provider IDs - Used AppError::Message for temporary bridge with String-based APIs ### Adapted Interface Calls - **commands.rs** (30 lines changed) - Updated 15 Tauri command handlers to use .map_err(|e| e.to_string()) - Changed from implicit Into::into to explicit e.to_string() - Maintained Result<T, String> interface for Tauri (frontend compatibility) - Affected commands: Claude MCP (5), Claude plugin (5), settings (1) - **mcp.rs** (2 lines changed) - Updated claude_mcp::set_mcp_servers_map call - Changed from .map_err(Into::into) to .map_err(|e| e.to_string()) ## Statistics - Files changed: 5 - Lines changed: +82/-66 (net +16) - Compilation: ✅ Success (8.42s, 0 warnings) - Tests: ✅ 4/4 passed ## Benefits - **Type Safety**: All infrastructure modules now use strongly-typed AppError - **Error Context**: File paths and operation types preserved in error chain - **Code Quality**: Removed ~30 instances of .map_err(|e| format!("...", e)) - **Maintainability**: Consistent error handling pattern across codebase - **Debugging**: Error source chain preserved with #[source] attribute ## Phase 1 Status: ✅ 100% Complete All modules migrated: - ✅ config.rs (Phase 1.1) - ✅ claude_mcp.rs (Phase 1.1) - ✅ codex_config.rs (Phase 1.1) - ✅ app_config.rs (Phase 1.1) - ✅ store.rs (Phase 1.1) - ✅ claude_plugin.rs (Phase 1.2) - ✅ settings.rs (Phase 1.2) - ✅ import_export.rs (Phase 1.2) - ✅ commands.rs (interface adaptation complete) - ✅ mcp.rs (interface adaptation complete) Ready for Phase 2: Splitting commands.rs by domain. Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 16:48:08 +08:00
use crate::error::AppError;
const CLAUDE_DIR: &str = ".claude";
const CLAUDE_CONFIG_FILE: &str = "config.json";
refactor(backend): complete phase 1 - unified error handling (100%) Completed the remaining migrations for Phase 1 of backend refactoring plan, achieving 100% coverage of unified error handling with AppError. ## Changes ### Fully Migrated Modules (Result<T, String> → Result<T, AppError>) - **claude_plugin.rs** (35 lines changed) - Migrated 7 public functions: claude_config_path, ensure_claude_dir_exists, read_claude_config, write_claude_config, clear_claude_config, claude_config_status, is_claude_config_applied - Used AppError::io(), AppError::JsonSerialize, AppError::Config - Simplified error handling with helper functions - **settings.rs** (14 lines changed) - Migrated AppSettings::save() and update_settings() - Used AppError::io() for file operations - Used AppError::JsonSerialize for JSON serialization - **import_export.rs** (67 lines changed) - Migrated 8 functions: create_backup, cleanup_old_backups, sync_current_providers_to_live, sync_current_provider_for_app, sync_codex_live, sync_claude_live, export_config_to_file, import_config_from_file, sync_current_providers_live - Used AppError::io(), AppError::json(), AppError::Config - Added proper error context with file paths and provider IDs - Used AppError::Message for temporary bridge with String-based APIs ### Adapted Interface Calls - **commands.rs** (30 lines changed) - Updated 15 Tauri command handlers to use .map_err(|e| e.to_string()) - Changed from implicit Into::into to explicit e.to_string() - Maintained Result<T, String> interface for Tauri (frontend compatibility) - Affected commands: Claude MCP (5), Claude plugin (5), settings (1) - **mcp.rs** (2 lines changed) - Updated claude_mcp::set_mcp_servers_map call - Changed from .map_err(Into::into) to .map_err(|e| e.to_string()) ## Statistics - Files changed: 5 - Lines changed: +82/-66 (net +16) - Compilation: ✅ Success (8.42s, 0 warnings) - Tests: ✅ 4/4 passed ## Benefits - **Type Safety**: All infrastructure modules now use strongly-typed AppError - **Error Context**: File paths and operation types preserved in error chain - **Code Quality**: Removed ~30 instances of .map_err(|e| format!("...", e)) - **Maintainability**: Consistent error handling pattern across codebase - **Debugging**: Error source chain preserved with #[source] attribute ## Phase 1 Status: ✅ 100% Complete All modules migrated: - ✅ config.rs (Phase 1.1) - ✅ claude_mcp.rs (Phase 1.1) - ✅ codex_config.rs (Phase 1.1) - ✅ app_config.rs (Phase 1.1) - ✅ store.rs (Phase 1.1) - ✅ claude_plugin.rs (Phase 1.2) - ✅ settings.rs (Phase 1.2) - ✅ import_export.rs (Phase 1.2) - ✅ commands.rs (interface adaptation complete) - ✅ mcp.rs (interface adaptation complete) Ready for Phase 2: Splitting commands.rs by domain. Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 16:48:08 +08:00
fn claude_dir() -> Result<PathBuf, AppError> {
// 优先使用设置中的覆盖目录
if let Some(dir) = crate::settings::get_claude_override_dir() {
return Ok(dir);
}
refactor(backend): complete phase 1 - unified error handling (100%) Completed the remaining migrations for Phase 1 of backend refactoring plan, achieving 100% coverage of unified error handling with AppError. ## Changes ### Fully Migrated Modules (Result<T, String> → Result<T, AppError>) - **claude_plugin.rs** (35 lines changed) - Migrated 7 public functions: claude_config_path, ensure_claude_dir_exists, read_claude_config, write_claude_config, clear_claude_config, claude_config_status, is_claude_config_applied - Used AppError::io(), AppError::JsonSerialize, AppError::Config - Simplified error handling with helper functions - **settings.rs** (14 lines changed) - Migrated AppSettings::save() and update_settings() - Used AppError::io() for file operations - Used AppError::JsonSerialize for JSON serialization - **import_export.rs** (67 lines changed) - Migrated 8 functions: create_backup, cleanup_old_backups, sync_current_providers_to_live, sync_current_provider_for_app, sync_codex_live, sync_claude_live, export_config_to_file, import_config_from_file, sync_current_providers_live - Used AppError::io(), AppError::json(), AppError::Config - Added proper error context with file paths and provider IDs - Used AppError::Message for temporary bridge with String-based APIs ### Adapted Interface Calls - **commands.rs** (30 lines changed) - Updated 15 Tauri command handlers to use .map_err(|e| e.to_string()) - Changed from implicit Into::into to explicit e.to_string() - Maintained Result<T, String> interface for Tauri (frontend compatibility) - Affected commands: Claude MCP (5), Claude plugin (5), settings (1) - **mcp.rs** (2 lines changed) - Updated claude_mcp::set_mcp_servers_map call - Changed from .map_err(Into::into) to .map_err(|e| e.to_string()) ## Statistics - Files changed: 5 - Lines changed: +82/-66 (net +16) - Compilation: ✅ Success (8.42s, 0 warnings) - Tests: ✅ 4/4 passed ## Benefits - **Type Safety**: All infrastructure modules now use strongly-typed AppError - **Error Context**: File paths and operation types preserved in error chain - **Code Quality**: Removed ~30 instances of .map_err(|e| format!("...", e)) - **Maintainability**: Consistent error handling pattern across codebase - **Debugging**: Error source chain preserved with #[source] attribute ## Phase 1 Status: ✅ 100% Complete All modules migrated: - ✅ config.rs (Phase 1.1) - ✅ claude_mcp.rs (Phase 1.1) - ✅ codex_config.rs (Phase 1.1) - ✅ app_config.rs (Phase 1.1) - ✅ store.rs (Phase 1.1) - ✅ claude_plugin.rs (Phase 1.2) - ✅ settings.rs (Phase 1.2) - ✅ import_export.rs (Phase 1.2) - ✅ commands.rs (interface adaptation complete) - ✅ mcp.rs (interface adaptation complete) Ready for Phase 2: Splitting commands.rs by domain. Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 16:48:08 +08:00
let home = dirs::home_dir()
.ok_or_else(|| AppError::Config("无法获取用户主目录".into()))?;
Ok(home.join(CLAUDE_DIR))
}
refactor(backend): complete phase 1 - unified error handling (100%) Completed the remaining migrations for Phase 1 of backend refactoring plan, achieving 100% coverage of unified error handling with AppError. ## Changes ### Fully Migrated Modules (Result<T, String> → Result<T, AppError>) - **claude_plugin.rs** (35 lines changed) - Migrated 7 public functions: claude_config_path, ensure_claude_dir_exists, read_claude_config, write_claude_config, clear_claude_config, claude_config_status, is_claude_config_applied - Used AppError::io(), AppError::JsonSerialize, AppError::Config - Simplified error handling with helper functions - **settings.rs** (14 lines changed) - Migrated AppSettings::save() and update_settings() - Used AppError::io() for file operations - Used AppError::JsonSerialize for JSON serialization - **import_export.rs** (67 lines changed) - Migrated 8 functions: create_backup, cleanup_old_backups, sync_current_providers_to_live, sync_current_provider_for_app, sync_codex_live, sync_claude_live, export_config_to_file, import_config_from_file, sync_current_providers_live - Used AppError::io(), AppError::json(), AppError::Config - Added proper error context with file paths and provider IDs - Used AppError::Message for temporary bridge with String-based APIs ### Adapted Interface Calls - **commands.rs** (30 lines changed) - Updated 15 Tauri command handlers to use .map_err(|e| e.to_string()) - Changed from implicit Into::into to explicit e.to_string() - Maintained Result<T, String> interface for Tauri (frontend compatibility) - Affected commands: Claude MCP (5), Claude plugin (5), settings (1) - **mcp.rs** (2 lines changed) - Updated claude_mcp::set_mcp_servers_map call - Changed from .map_err(Into::into) to .map_err(|e| e.to_string()) ## Statistics - Files changed: 5 - Lines changed: +82/-66 (net +16) - Compilation: ✅ Success (8.42s, 0 warnings) - Tests: ✅ 4/4 passed ## Benefits - **Type Safety**: All infrastructure modules now use strongly-typed AppError - **Error Context**: File paths and operation types preserved in error chain - **Code Quality**: Removed ~30 instances of .map_err(|e| format!("...", e)) - **Maintainability**: Consistent error handling pattern across codebase - **Debugging**: Error source chain preserved with #[source] attribute ## Phase 1 Status: ✅ 100% Complete All modules migrated: - ✅ config.rs (Phase 1.1) - ✅ claude_mcp.rs (Phase 1.1) - ✅ codex_config.rs (Phase 1.1) - ✅ app_config.rs (Phase 1.1) - ✅ store.rs (Phase 1.1) - ✅ claude_plugin.rs (Phase 1.2) - ✅ settings.rs (Phase 1.2) - ✅ import_export.rs (Phase 1.2) - ✅ commands.rs (interface adaptation complete) - ✅ mcp.rs (interface adaptation complete) Ready for Phase 2: Splitting commands.rs by domain. Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 16:48:08 +08:00
pub fn claude_config_path() -> Result<PathBuf, AppError> {
Ok(claude_dir()?.join(CLAUDE_CONFIG_FILE))
}
refactor(backend): complete phase 1 - unified error handling (100%) Completed the remaining migrations for Phase 1 of backend refactoring plan, achieving 100% coverage of unified error handling with AppError. ## Changes ### Fully Migrated Modules (Result<T, String> → Result<T, AppError>) - **claude_plugin.rs** (35 lines changed) - Migrated 7 public functions: claude_config_path, ensure_claude_dir_exists, read_claude_config, write_claude_config, clear_claude_config, claude_config_status, is_claude_config_applied - Used AppError::io(), AppError::JsonSerialize, AppError::Config - Simplified error handling with helper functions - **settings.rs** (14 lines changed) - Migrated AppSettings::save() and update_settings() - Used AppError::io() for file operations - Used AppError::JsonSerialize for JSON serialization - **import_export.rs** (67 lines changed) - Migrated 8 functions: create_backup, cleanup_old_backups, sync_current_providers_to_live, sync_current_provider_for_app, sync_codex_live, sync_claude_live, export_config_to_file, import_config_from_file, sync_current_providers_live - Used AppError::io(), AppError::json(), AppError::Config - Added proper error context with file paths and provider IDs - Used AppError::Message for temporary bridge with String-based APIs ### Adapted Interface Calls - **commands.rs** (30 lines changed) - Updated 15 Tauri command handlers to use .map_err(|e| e.to_string()) - Changed from implicit Into::into to explicit e.to_string() - Maintained Result<T, String> interface for Tauri (frontend compatibility) - Affected commands: Claude MCP (5), Claude plugin (5), settings (1) - **mcp.rs** (2 lines changed) - Updated claude_mcp::set_mcp_servers_map call - Changed from .map_err(Into::into) to .map_err(|e| e.to_string()) ## Statistics - Files changed: 5 - Lines changed: +82/-66 (net +16) - Compilation: ✅ Success (8.42s, 0 warnings) - Tests: ✅ 4/4 passed ## Benefits - **Type Safety**: All infrastructure modules now use strongly-typed AppError - **Error Context**: File paths and operation types preserved in error chain - **Code Quality**: Removed ~30 instances of .map_err(|e| format!("...", e)) - **Maintainability**: Consistent error handling pattern across codebase - **Debugging**: Error source chain preserved with #[source] attribute ## Phase 1 Status: ✅ 100% Complete All modules migrated: - ✅ config.rs (Phase 1.1) - ✅ claude_mcp.rs (Phase 1.1) - ✅ codex_config.rs (Phase 1.1) - ✅ app_config.rs (Phase 1.1) - ✅ store.rs (Phase 1.1) - ✅ claude_plugin.rs (Phase 1.2) - ✅ settings.rs (Phase 1.2) - ✅ import_export.rs (Phase 1.2) - ✅ commands.rs (interface adaptation complete) - ✅ mcp.rs (interface adaptation complete) Ready for Phase 2: Splitting commands.rs by domain. Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 16:48:08 +08:00
pub fn ensure_claude_dir_exists() -> Result<PathBuf, AppError> {
let dir = claude_dir()?;
if !dir.exists() {
refactor(backend): complete phase 1 - unified error handling (100%) Completed the remaining migrations for Phase 1 of backend refactoring plan, achieving 100% coverage of unified error handling with AppError. ## Changes ### Fully Migrated Modules (Result<T, String> → Result<T, AppError>) - **claude_plugin.rs** (35 lines changed) - Migrated 7 public functions: claude_config_path, ensure_claude_dir_exists, read_claude_config, write_claude_config, clear_claude_config, claude_config_status, is_claude_config_applied - Used AppError::io(), AppError::JsonSerialize, AppError::Config - Simplified error handling with helper functions - **settings.rs** (14 lines changed) - Migrated AppSettings::save() and update_settings() - Used AppError::io() for file operations - Used AppError::JsonSerialize for JSON serialization - **import_export.rs** (67 lines changed) - Migrated 8 functions: create_backup, cleanup_old_backups, sync_current_providers_to_live, sync_current_provider_for_app, sync_codex_live, sync_claude_live, export_config_to_file, import_config_from_file, sync_current_providers_live - Used AppError::io(), AppError::json(), AppError::Config - Added proper error context with file paths and provider IDs - Used AppError::Message for temporary bridge with String-based APIs ### Adapted Interface Calls - **commands.rs** (30 lines changed) - Updated 15 Tauri command handlers to use .map_err(|e| e.to_string()) - Changed from implicit Into::into to explicit e.to_string() - Maintained Result<T, String> interface for Tauri (frontend compatibility) - Affected commands: Claude MCP (5), Claude plugin (5), settings (1) - **mcp.rs** (2 lines changed) - Updated claude_mcp::set_mcp_servers_map call - Changed from .map_err(Into::into) to .map_err(|e| e.to_string()) ## Statistics - Files changed: 5 - Lines changed: +82/-66 (net +16) - Compilation: ✅ Success (8.42s, 0 warnings) - Tests: ✅ 4/4 passed ## Benefits - **Type Safety**: All infrastructure modules now use strongly-typed AppError - **Error Context**: File paths and operation types preserved in error chain - **Code Quality**: Removed ~30 instances of .map_err(|e| format!("...", e)) - **Maintainability**: Consistent error handling pattern across codebase - **Debugging**: Error source chain preserved with #[source] attribute ## Phase 1 Status: ✅ 100% Complete All modules migrated: - ✅ config.rs (Phase 1.1) - ✅ claude_mcp.rs (Phase 1.1) - ✅ codex_config.rs (Phase 1.1) - ✅ app_config.rs (Phase 1.1) - ✅ store.rs (Phase 1.1) - ✅ claude_plugin.rs (Phase 1.2) - ✅ settings.rs (Phase 1.2) - ✅ import_export.rs (Phase 1.2) - ✅ commands.rs (interface adaptation complete) - ✅ mcp.rs (interface adaptation complete) Ready for Phase 2: Splitting commands.rs by domain. Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 16:48:08 +08:00
fs::create_dir_all(&dir).map_err(|e| AppError::io(&dir, e))?;
}
Ok(dir)
}
refactor(backend): complete phase 1 - unified error handling (100%) Completed the remaining migrations for Phase 1 of backend refactoring plan, achieving 100% coverage of unified error handling with AppError. ## Changes ### Fully Migrated Modules (Result<T, String> → Result<T, AppError>) - **claude_plugin.rs** (35 lines changed) - Migrated 7 public functions: claude_config_path, ensure_claude_dir_exists, read_claude_config, write_claude_config, clear_claude_config, claude_config_status, is_claude_config_applied - Used AppError::io(), AppError::JsonSerialize, AppError::Config - Simplified error handling with helper functions - **settings.rs** (14 lines changed) - Migrated AppSettings::save() and update_settings() - Used AppError::io() for file operations - Used AppError::JsonSerialize for JSON serialization - **import_export.rs** (67 lines changed) - Migrated 8 functions: create_backup, cleanup_old_backups, sync_current_providers_to_live, sync_current_provider_for_app, sync_codex_live, sync_claude_live, export_config_to_file, import_config_from_file, sync_current_providers_live - Used AppError::io(), AppError::json(), AppError::Config - Added proper error context with file paths and provider IDs - Used AppError::Message for temporary bridge with String-based APIs ### Adapted Interface Calls - **commands.rs** (30 lines changed) - Updated 15 Tauri command handlers to use .map_err(|e| e.to_string()) - Changed from implicit Into::into to explicit e.to_string() - Maintained Result<T, String> interface for Tauri (frontend compatibility) - Affected commands: Claude MCP (5), Claude plugin (5), settings (1) - **mcp.rs** (2 lines changed) - Updated claude_mcp::set_mcp_servers_map call - Changed from .map_err(Into::into) to .map_err(|e| e.to_string()) ## Statistics - Files changed: 5 - Lines changed: +82/-66 (net +16) - Compilation: ✅ Success (8.42s, 0 warnings) - Tests: ✅ 4/4 passed ## Benefits - **Type Safety**: All infrastructure modules now use strongly-typed AppError - **Error Context**: File paths and operation types preserved in error chain - **Code Quality**: Removed ~30 instances of .map_err(|e| format!("...", e)) - **Maintainability**: Consistent error handling pattern across codebase - **Debugging**: Error source chain preserved with #[source] attribute ## Phase 1 Status: ✅ 100% Complete All modules migrated: - ✅ config.rs (Phase 1.1) - ✅ claude_mcp.rs (Phase 1.1) - ✅ codex_config.rs (Phase 1.1) - ✅ app_config.rs (Phase 1.1) - ✅ store.rs (Phase 1.1) - ✅ claude_plugin.rs (Phase 1.2) - ✅ settings.rs (Phase 1.2) - ✅ import_export.rs (Phase 1.2) - ✅ commands.rs (interface adaptation complete) - ✅ mcp.rs (interface adaptation complete) Ready for Phase 2: Splitting commands.rs by domain. Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 16:48:08 +08:00
pub fn read_claude_config() -> Result<Option<String>, AppError> {
let path = claude_config_path()?;
if path.exists() {
refactor(backend): complete phase 1 - unified error handling (100%) Completed the remaining migrations for Phase 1 of backend refactoring plan, achieving 100% coverage of unified error handling with AppError. ## Changes ### Fully Migrated Modules (Result<T, String> → Result<T, AppError>) - **claude_plugin.rs** (35 lines changed) - Migrated 7 public functions: claude_config_path, ensure_claude_dir_exists, read_claude_config, write_claude_config, clear_claude_config, claude_config_status, is_claude_config_applied - Used AppError::io(), AppError::JsonSerialize, AppError::Config - Simplified error handling with helper functions - **settings.rs** (14 lines changed) - Migrated AppSettings::save() and update_settings() - Used AppError::io() for file operations - Used AppError::JsonSerialize for JSON serialization - **import_export.rs** (67 lines changed) - Migrated 8 functions: create_backup, cleanup_old_backups, sync_current_providers_to_live, sync_current_provider_for_app, sync_codex_live, sync_claude_live, export_config_to_file, import_config_from_file, sync_current_providers_live - Used AppError::io(), AppError::json(), AppError::Config - Added proper error context with file paths and provider IDs - Used AppError::Message for temporary bridge with String-based APIs ### Adapted Interface Calls - **commands.rs** (30 lines changed) - Updated 15 Tauri command handlers to use .map_err(|e| e.to_string()) - Changed from implicit Into::into to explicit e.to_string() - Maintained Result<T, String> interface for Tauri (frontend compatibility) - Affected commands: Claude MCP (5), Claude plugin (5), settings (1) - **mcp.rs** (2 lines changed) - Updated claude_mcp::set_mcp_servers_map call - Changed from .map_err(Into::into) to .map_err(|e| e.to_string()) ## Statistics - Files changed: 5 - Lines changed: +82/-66 (net +16) - Compilation: ✅ Success (8.42s, 0 warnings) - Tests: ✅ 4/4 passed ## Benefits - **Type Safety**: All infrastructure modules now use strongly-typed AppError - **Error Context**: File paths and operation types preserved in error chain - **Code Quality**: Removed ~30 instances of .map_err(|e| format!("...", e)) - **Maintainability**: Consistent error handling pattern across codebase - **Debugging**: Error source chain preserved with #[source] attribute ## Phase 1 Status: ✅ 100% Complete All modules migrated: - ✅ config.rs (Phase 1.1) - ✅ claude_mcp.rs (Phase 1.1) - ✅ codex_config.rs (Phase 1.1) - ✅ app_config.rs (Phase 1.1) - ✅ store.rs (Phase 1.1) - ✅ claude_plugin.rs (Phase 1.2) - ✅ settings.rs (Phase 1.2) - ✅ import_export.rs (Phase 1.2) - ✅ commands.rs (interface adaptation complete) - ✅ mcp.rs (interface adaptation complete) Ready for Phase 2: Splitting commands.rs by domain. Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 16:48:08 +08:00
let content = fs::read_to_string(&path).map_err(|e| AppError::io(&path, e))?;
Ok(Some(content))
} else {
Ok(None)
}
}
fn is_managed_config(content: &str) -> bool {
match serde_json::from_str::<serde_json::Value>(content) {
Ok(value) => value
.get("primaryApiKey")
.and_then(|v| v.as_str())
.map(|val| val == "any")
.unwrap_or(false),
Err(_) => false,
}
}
refactor(backend): complete phase 1 - unified error handling (100%) Completed the remaining migrations for Phase 1 of backend refactoring plan, achieving 100% coverage of unified error handling with AppError. ## Changes ### Fully Migrated Modules (Result<T, String> → Result<T, AppError>) - **claude_plugin.rs** (35 lines changed) - Migrated 7 public functions: claude_config_path, ensure_claude_dir_exists, read_claude_config, write_claude_config, clear_claude_config, claude_config_status, is_claude_config_applied - Used AppError::io(), AppError::JsonSerialize, AppError::Config - Simplified error handling with helper functions - **settings.rs** (14 lines changed) - Migrated AppSettings::save() and update_settings() - Used AppError::io() for file operations - Used AppError::JsonSerialize for JSON serialization - **import_export.rs** (67 lines changed) - Migrated 8 functions: create_backup, cleanup_old_backups, sync_current_providers_to_live, sync_current_provider_for_app, sync_codex_live, sync_claude_live, export_config_to_file, import_config_from_file, sync_current_providers_live - Used AppError::io(), AppError::json(), AppError::Config - Added proper error context with file paths and provider IDs - Used AppError::Message for temporary bridge with String-based APIs ### Adapted Interface Calls - **commands.rs** (30 lines changed) - Updated 15 Tauri command handlers to use .map_err(|e| e.to_string()) - Changed from implicit Into::into to explicit e.to_string() - Maintained Result<T, String> interface for Tauri (frontend compatibility) - Affected commands: Claude MCP (5), Claude plugin (5), settings (1) - **mcp.rs** (2 lines changed) - Updated claude_mcp::set_mcp_servers_map call - Changed from .map_err(Into::into) to .map_err(|e| e.to_string()) ## Statistics - Files changed: 5 - Lines changed: +82/-66 (net +16) - Compilation: ✅ Success (8.42s, 0 warnings) - Tests: ✅ 4/4 passed ## Benefits - **Type Safety**: All infrastructure modules now use strongly-typed AppError - **Error Context**: File paths and operation types preserved in error chain - **Code Quality**: Removed ~30 instances of .map_err(|e| format!("...", e)) - **Maintainability**: Consistent error handling pattern across codebase - **Debugging**: Error source chain preserved with #[source] attribute ## Phase 1 Status: ✅ 100% Complete All modules migrated: - ✅ config.rs (Phase 1.1) - ✅ claude_mcp.rs (Phase 1.1) - ✅ codex_config.rs (Phase 1.1) - ✅ app_config.rs (Phase 1.1) - ✅ store.rs (Phase 1.1) - ✅ claude_plugin.rs (Phase 1.2) - ✅ settings.rs (Phase 1.2) - ✅ import_export.rs (Phase 1.2) - ✅ commands.rs (interface adaptation complete) - ✅ mcp.rs (interface adaptation complete) Ready for Phase 2: Splitting commands.rs by domain. Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 16:48:08 +08:00
pub fn write_claude_config() -> Result<bool, AppError> {
// 增量写入:仅设置 primaryApiKey = "any",保留其它字段
let path = claude_config_path()?;
ensure_claude_dir_exists()?;
// 尝试读取并解析为对象
let mut obj = match read_claude_config()? {
Some(existing) => match serde_json::from_str::<serde_json::Value>(&existing) {
Ok(serde_json::Value::Object(map)) => serde_json::Value::Object(map),
_ => serde_json::json!({}),
},
None => serde_json::json!({}),
};
let mut changed = false;
if let Some(map) = obj.as_object_mut() {
let cur = map
.get("primaryApiKey")
.and_then(|v| v.as_str())
.unwrap_or("");
if cur != "any" {
map.insert(
"primaryApiKey".to_string(),
serde_json::Value::String("any".to_string()),
);
changed = true;
}
}
if changed || !path.exists() {
let serialized = serde_json::to_string_pretty(&obj)
refactor(backend): complete phase 1 - unified error handling (100%) Completed the remaining migrations for Phase 1 of backend refactoring plan, achieving 100% coverage of unified error handling with AppError. ## Changes ### Fully Migrated Modules (Result<T, String> → Result<T, AppError>) - **claude_plugin.rs** (35 lines changed) - Migrated 7 public functions: claude_config_path, ensure_claude_dir_exists, read_claude_config, write_claude_config, clear_claude_config, claude_config_status, is_claude_config_applied - Used AppError::io(), AppError::JsonSerialize, AppError::Config - Simplified error handling with helper functions - **settings.rs** (14 lines changed) - Migrated AppSettings::save() and update_settings() - Used AppError::io() for file operations - Used AppError::JsonSerialize for JSON serialization - **import_export.rs** (67 lines changed) - Migrated 8 functions: create_backup, cleanup_old_backups, sync_current_providers_to_live, sync_current_provider_for_app, sync_codex_live, sync_claude_live, export_config_to_file, import_config_from_file, sync_current_providers_live - Used AppError::io(), AppError::json(), AppError::Config - Added proper error context with file paths and provider IDs - Used AppError::Message for temporary bridge with String-based APIs ### Adapted Interface Calls - **commands.rs** (30 lines changed) - Updated 15 Tauri command handlers to use .map_err(|e| e.to_string()) - Changed from implicit Into::into to explicit e.to_string() - Maintained Result<T, String> interface for Tauri (frontend compatibility) - Affected commands: Claude MCP (5), Claude plugin (5), settings (1) - **mcp.rs** (2 lines changed) - Updated claude_mcp::set_mcp_servers_map call - Changed from .map_err(Into::into) to .map_err(|e| e.to_string()) ## Statistics - Files changed: 5 - Lines changed: +82/-66 (net +16) - Compilation: ✅ Success (8.42s, 0 warnings) - Tests: ✅ 4/4 passed ## Benefits - **Type Safety**: All infrastructure modules now use strongly-typed AppError - **Error Context**: File paths and operation types preserved in error chain - **Code Quality**: Removed ~30 instances of .map_err(|e| format!("...", e)) - **Maintainability**: Consistent error handling pattern across codebase - **Debugging**: Error source chain preserved with #[source] attribute ## Phase 1 Status: ✅ 100% Complete All modules migrated: - ✅ config.rs (Phase 1.1) - ✅ claude_mcp.rs (Phase 1.1) - ✅ codex_config.rs (Phase 1.1) - ✅ app_config.rs (Phase 1.1) - ✅ store.rs (Phase 1.1) - ✅ claude_plugin.rs (Phase 1.2) - ✅ settings.rs (Phase 1.2) - ✅ import_export.rs (Phase 1.2) - ✅ commands.rs (interface adaptation complete) - ✅ mcp.rs (interface adaptation complete) Ready for Phase 2: Splitting commands.rs by domain. Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 16:48:08 +08:00
.map_err(|e| AppError::JsonSerialize { source: e })?;
fs::write(&path, format!("{}\n", serialized))
refactor(backend): complete phase 1 - unified error handling (100%) Completed the remaining migrations for Phase 1 of backend refactoring plan, achieving 100% coverage of unified error handling with AppError. ## Changes ### Fully Migrated Modules (Result<T, String> → Result<T, AppError>) - **claude_plugin.rs** (35 lines changed) - Migrated 7 public functions: claude_config_path, ensure_claude_dir_exists, read_claude_config, write_claude_config, clear_claude_config, claude_config_status, is_claude_config_applied - Used AppError::io(), AppError::JsonSerialize, AppError::Config - Simplified error handling with helper functions - **settings.rs** (14 lines changed) - Migrated AppSettings::save() and update_settings() - Used AppError::io() for file operations - Used AppError::JsonSerialize for JSON serialization - **import_export.rs** (67 lines changed) - Migrated 8 functions: create_backup, cleanup_old_backups, sync_current_providers_to_live, sync_current_provider_for_app, sync_codex_live, sync_claude_live, export_config_to_file, import_config_from_file, sync_current_providers_live - Used AppError::io(), AppError::json(), AppError::Config - Added proper error context with file paths and provider IDs - Used AppError::Message for temporary bridge with String-based APIs ### Adapted Interface Calls - **commands.rs** (30 lines changed) - Updated 15 Tauri command handlers to use .map_err(|e| e.to_string()) - Changed from implicit Into::into to explicit e.to_string() - Maintained Result<T, String> interface for Tauri (frontend compatibility) - Affected commands: Claude MCP (5), Claude plugin (5), settings (1) - **mcp.rs** (2 lines changed) - Updated claude_mcp::set_mcp_servers_map call - Changed from .map_err(Into::into) to .map_err(|e| e.to_string()) ## Statistics - Files changed: 5 - Lines changed: +82/-66 (net +16) - Compilation: ✅ Success (8.42s, 0 warnings) - Tests: ✅ 4/4 passed ## Benefits - **Type Safety**: All infrastructure modules now use strongly-typed AppError - **Error Context**: File paths and operation types preserved in error chain - **Code Quality**: Removed ~30 instances of .map_err(|e| format!("...", e)) - **Maintainability**: Consistent error handling pattern across codebase - **Debugging**: Error source chain preserved with #[source] attribute ## Phase 1 Status: ✅ 100% Complete All modules migrated: - ✅ config.rs (Phase 1.1) - ✅ claude_mcp.rs (Phase 1.1) - ✅ codex_config.rs (Phase 1.1) - ✅ app_config.rs (Phase 1.1) - ✅ store.rs (Phase 1.1) - ✅ claude_plugin.rs (Phase 1.2) - ✅ settings.rs (Phase 1.2) - ✅ import_export.rs (Phase 1.2) - ✅ commands.rs (interface adaptation complete) - ✅ mcp.rs (interface adaptation complete) Ready for Phase 2: Splitting commands.rs by domain. Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 16:48:08 +08:00
.map_err(|e| AppError::io(&path, e))?;
Ok(true)
} else {
Ok(false)
}
}
refactor(backend): complete phase 1 - unified error handling (100%) Completed the remaining migrations for Phase 1 of backend refactoring plan, achieving 100% coverage of unified error handling with AppError. ## Changes ### Fully Migrated Modules (Result<T, String> → Result<T, AppError>) - **claude_plugin.rs** (35 lines changed) - Migrated 7 public functions: claude_config_path, ensure_claude_dir_exists, read_claude_config, write_claude_config, clear_claude_config, claude_config_status, is_claude_config_applied - Used AppError::io(), AppError::JsonSerialize, AppError::Config - Simplified error handling with helper functions - **settings.rs** (14 lines changed) - Migrated AppSettings::save() and update_settings() - Used AppError::io() for file operations - Used AppError::JsonSerialize for JSON serialization - **import_export.rs** (67 lines changed) - Migrated 8 functions: create_backup, cleanup_old_backups, sync_current_providers_to_live, sync_current_provider_for_app, sync_codex_live, sync_claude_live, export_config_to_file, import_config_from_file, sync_current_providers_live - Used AppError::io(), AppError::json(), AppError::Config - Added proper error context with file paths and provider IDs - Used AppError::Message for temporary bridge with String-based APIs ### Adapted Interface Calls - **commands.rs** (30 lines changed) - Updated 15 Tauri command handlers to use .map_err(|e| e.to_string()) - Changed from implicit Into::into to explicit e.to_string() - Maintained Result<T, String> interface for Tauri (frontend compatibility) - Affected commands: Claude MCP (5), Claude plugin (5), settings (1) - **mcp.rs** (2 lines changed) - Updated claude_mcp::set_mcp_servers_map call - Changed from .map_err(Into::into) to .map_err(|e| e.to_string()) ## Statistics - Files changed: 5 - Lines changed: +82/-66 (net +16) - Compilation: ✅ Success (8.42s, 0 warnings) - Tests: ✅ 4/4 passed ## Benefits - **Type Safety**: All infrastructure modules now use strongly-typed AppError - **Error Context**: File paths and operation types preserved in error chain - **Code Quality**: Removed ~30 instances of .map_err(|e| format!("...", e)) - **Maintainability**: Consistent error handling pattern across codebase - **Debugging**: Error source chain preserved with #[source] attribute ## Phase 1 Status: ✅ 100% Complete All modules migrated: - ✅ config.rs (Phase 1.1) - ✅ claude_mcp.rs (Phase 1.1) - ✅ codex_config.rs (Phase 1.1) - ✅ app_config.rs (Phase 1.1) - ✅ store.rs (Phase 1.1) - ✅ claude_plugin.rs (Phase 1.2) - ✅ settings.rs (Phase 1.2) - ✅ import_export.rs (Phase 1.2) - ✅ commands.rs (interface adaptation complete) - ✅ mcp.rs (interface adaptation complete) Ready for Phase 2: Splitting commands.rs by domain. Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 16:48:08 +08:00
pub fn clear_claude_config() -> Result<bool, AppError> {
let path = claude_config_path()?;
if !path.exists() {
return Ok(false);
}
let content = match read_claude_config()? {
Some(content) => content,
None => return Ok(false),
};
let mut value = match serde_json::from_str::<serde_json::Value>(&content) {
Ok(value) => value,
Err(_) => return Ok(false),
};
let obj = match value.as_object_mut() {
Some(obj) => obj,
None => return Ok(false),
};
if obj.remove("primaryApiKey").is_none() {
return Ok(false);
}
let serialized = serde_json::to_string_pretty(&value)
refactor(backend): complete phase 1 - unified error handling (100%) Completed the remaining migrations for Phase 1 of backend refactoring plan, achieving 100% coverage of unified error handling with AppError. ## Changes ### Fully Migrated Modules (Result<T, String> → Result<T, AppError>) - **claude_plugin.rs** (35 lines changed) - Migrated 7 public functions: claude_config_path, ensure_claude_dir_exists, read_claude_config, write_claude_config, clear_claude_config, claude_config_status, is_claude_config_applied - Used AppError::io(), AppError::JsonSerialize, AppError::Config - Simplified error handling with helper functions - **settings.rs** (14 lines changed) - Migrated AppSettings::save() and update_settings() - Used AppError::io() for file operations - Used AppError::JsonSerialize for JSON serialization - **import_export.rs** (67 lines changed) - Migrated 8 functions: create_backup, cleanup_old_backups, sync_current_providers_to_live, sync_current_provider_for_app, sync_codex_live, sync_claude_live, export_config_to_file, import_config_from_file, sync_current_providers_live - Used AppError::io(), AppError::json(), AppError::Config - Added proper error context with file paths and provider IDs - Used AppError::Message for temporary bridge with String-based APIs ### Adapted Interface Calls - **commands.rs** (30 lines changed) - Updated 15 Tauri command handlers to use .map_err(|e| e.to_string()) - Changed from implicit Into::into to explicit e.to_string() - Maintained Result<T, String> interface for Tauri (frontend compatibility) - Affected commands: Claude MCP (5), Claude plugin (5), settings (1) - **mcp.rs** (2 lines changed) - Updated claude_mcp::set_mcp_servers_map call - Changed from .map_err(Into::into) to .map_err(|e| e.to_string()) ## Statistics - Files changed: 5 - Lines changed: +82/-66 (net +16) - Compilation: ✅ Success (8.42s, 0 warnings) - Tests: ✅ 4/4 passed ## Benefits - **Type Safety**: All infrastructure modules now use strongly-typed AppError - **Error Context**: File paths and operation types preserved in error chain - **Code Quality**: Removed ~30 instances of .map_err(|e| format!("...", e)) - **Maintainability**: Consistent error handling pattern across codebase - **Debugging**: Error source chain preserved with #[source] attribute ## Phase 1 Status: ✅ 100% Complete All modules migrated: - ✅ config.rs (Phase 1.1) - ✅ claude_mcp.rs (Phase 1.1) - ✅ codex_config.rs (Phase 1.1) - ✅ app_config.rs (Phase 1.1) - ✅ store.rs (Phase 1.1) - ✅ claude_plugin.rs (Phase 1.2) - ✅ settings.rs (Phase 1.2) - ✅ import_export.rs (Phase 1.2) - ✅ commands.rs (interface adaptation complete) - ✅ mcp.rs (interface adaptation complete) Ready for Phase 2: Splitting commands.rs by domain. Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 16:48:08 +08:00
.map_err(|e| AppError::JsonSerialize { source: e })?;
fs::write(&path, format!("{}\n", serialized)).map_err(|e| AppError::io(&path, e))?;
Ok(true)
}
refactor(backend): complete phase 1 - unified error handling (100%) Completed the remaining migrations for Phase 1 of backend refactoring plan, achieving 100% coverage of unified error handling with AppError. ## Changes ### Fully Migrated Modules (Result<T, String> → Result<T, AppError>) - **claude_plugin.rs** (35 lines changed) - Migrated 7 public functions: claude_config_path, ensure_claude_dir_exists, read_claude_config, write_claude_config, clear_claude_config, claude_config_status, is_claude_config_applied - Used AppError::io(), AppError::JsonSerialize, AppError::Config - Simplified error handling with helper functions - **settings.rs** (14 lines changed) - Migrated AppSettings::save() and update_settings() - Used AppError::io() for file operations - Used AppError::JsonSerialize for JSON serialization - **import_export.rs** (67 lines changed) - Migrated 8 functions: create_backup, cleanup_old_backups, sync_current_providers_to_live, sync_current_provider_for_app, sync_codex_live, sync_claude_live, export_config_to_file, import_config_from_file, sync_current_providers_live - Used AppError::io(), AppError::json(), AppError::Config - Added proper error context with file paths and provider IDs - Used AppError::Message for temporary bridge with String-based APIs ### Adapted Interface Calls - **commands.rs** (30 lines changed) - Updated 15 Tauri command handlers to use .map_err(|e| e.to_string()) - Changed from implicit Into::into to explicit e.to_string() - Maintained Result<T, String> interface for Tauri (frontend compatibility) - Affected commands: Claude MCP (5), Claude plugin (5), settings (1) - **mcp.rs** (2 lines changed) - Updated claude_mcp::set_mcp_servers_map call - Changed from .map_err(Into::into) to .map_err(|e| e.to_string()) ## Statistics - Files changed: 5 - Lines changed: +82/-66 (net +16) - Compilation: ✅ Success (8.42s, 0 warnings) - Tests: ✅ 4/4 passed ## Benefits - **Type Safety**: All infrastructure modules now use strongly-typed AppError - **Error Context**: File paths and operation types preserved in error chain - **Code Quality**: Removed ~30 instances of .map_err(|e| format!("...", e)) - **Maintainability**: Consistent error handling pattern across codebase - **Debugging**: Error source chain preserved with #[source] attribute ## Phase 1 Status: ✅ 100% Complete All modules migrated: - ✅ config.rs (Phase 1.1) - ✅ claude_mcp.rs (Phase 1.1) - ✅ codex_config.rs (Phase 1.1) - ✅ app_config.rs (Phase 1.1) - ✅ store.rs (Phase 1.1) - ✅ claude_plugin.rs (Phase 1.2) - ✅ settings.rs (Phase 1.2) - ✅ import_export.rs (Phase 1.2) - ✅ commands.rs (interface adaptation complete) - ✅ mcp.rs (interface adaptation complete) Ready for Phase 2: Splitting commands.rs by domain. Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 16:48:08 +08:00
pub fn claude_config_status() -> Result<(bool, PathBuf), AppError> {
let path = claude_config_path()?;
Ok((path.exists(), path))
}
refactor(backend): complete phase 1 - unified error handling (100%) Completed the remaining migrations for Phase 1 of backend refactoring plan, achieving 100% coverage of unified error handling with AppError. ## Changes ### Fully Migrated Modules (Result<T, String> → Result<T, AppError>) - **claude_plugin.rs** (35 lines changed) - Migrated 7 public functions: claude_config_path, ensure_claude_dir_exists, read_claude_config, write_claude_config, clear_claude_config, claude_config_status, is_claude_config_applied - Used AppError::io(), AppError::JsonSerialize, AppError::Config - Simplified error handling with helper functions - **settings.rs** (14 lines changed) - Migrated AppSettings::save() and update_settings() - Used AppError::io() for file operations - Used AppError::JsonSerialize for JSON serialization - **import_export.rs** (67 lines changed) - Migrated 8 functions: create_backup, cleanup_old_backups, sync_current_providers_to_live, sync_current_provider_for_app, sync_codex_live, sync_claude_live, export_config_to_file, import_config_from_file, sync_current_providers_live - Used AppError::io(), AppError::json(), AppError::Config - Added proper error context with file paths and provider IDs - Used AppError::Message for temporary bridge with String-based APIs ### Adapted Interface Calls - **commands.rs** (30 lines changed) - Updated 15 Tauri command handlers to use .map_err(|e| e.to_string()) - Changed from implicit Into::into to explicit e.to_string() - Maintained Result<T, String> interface for Tauri (frontend compatibility) - Affected commands: Claude MCP (5), Claude plugin (5), settings (1) - **mcp.rs** (2 lines changed) - Updated claude_mcp::set_mcp_servers_map call - Changed from .map_err(Into::into) to .map_err(|e| e.to_string()) ## Statistics - Files changed: 5 - Lines changed: +82/-66 (net +16) - Compilation: ✅ Success (8.42s, 0 warnings) - Tests: ✅ 4/4 passed ## Benefits - **Type Safety**: All infrastructure modules now use strongly-typed AppError - **Error Context**: File paths and operation types preserved in error chain - **Code Quality**: Removed ~30 instances of .map_err(|e| format!("...", e)) - **Maintainability**: Consistent error handling pattern across codebase - **Debugging**: Error source chain preserved with #[source] attribute ## Phase 1 Status: ✅ 100% Complete All modules migrated: - ✅ config.rs (Phase 1.1) - ✅ claude_mcp.rs (Phase 1.1) - ✅ codex_config.rs (Phase 1.1) - ✅ app_config.rs (Phase 1.1) - ✅ store.rs (Phase 1.1) - ✅ claude_plugin.rs (Phase 1.2) - ✅ settings.rs (Phase 1.2) - ✅ import_export.rs (Phase 1.2) - ✅ commands.rs (interface adaptation complete) - ✅ mcp.rs (interface adaptation complete) Ready for Phase 2: Splitting commands.rs by domain. Co-authored-by: Claude <noreply@anthropic.com>
2025-10-27 16:48:08 +08:00
pub fn is_claude_config_applied() -> Result<bool, AppError> {
match read_claude_config()? {
Some(content) => Ok(is_managed_config(&content)),
None => Ok(false),
}
}