2025-08-23 21:00:50 +08:00
|
|
|
use crate::config::get_app_config_path;
|
|
|
|
|
use crate::provider::ProviderManager;
|
2025-08-27 11:00:53 +08:00
|
|
|
use std::sync::Mutex;
|
2025-08-23 20:12:35 +08:00
|
|
|
|
|
|
|
|
/// 全局应用状态
|
|
|
|
|
pub struct AppState {
|
|
|
|
|
pub provider_manager: Mutex<ProviderManager>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AppState {
|
|
|
|
|
/// 创建新的应用状态
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
let config_path = get_app_config_path();
|
2025-08-27 11:00:53 +08:00
|
|
|
let provider_manager = ProviderManager::load_from_file(&config_path).unwrap_or_else(|e| {
|
|
|
|
|
log::warn!("加载配置失败: {}, 使用默认配置", e);
|
|
|
|
|
ProviderManager::default()
|
|
|
|
|
});
|
|
|
|
|
|
2025-08-23 20:12:35 +08:00
|
|
|
Self {
|
|
|
|
|
provider_manager: Mutex::new(provider_manager),
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-27 11:00:53 +08:00
|
|
|
|
2025-08-23 20:12:35 +08:00
|
|
|
/// 保存配置到文件
|
|
|
|
|
pub fn save(&self) -> Result<(), String> {
|
|
|
|
|
let config_path = get_app_config_path();
|
2025-08-27 11:00:53 +08:00
|
|
|
let manager = self
|
|
|
|
|
.provider_manager
|
|
|
|
|
.lock()
|
2025-08-23 20:12:35 +08:00
|
|
|
.map_err(|e| format!("获取锁失败: {}", e))?;
|
2025-08-27 11:00:53 +08:00
|
|
|
|
2025-08-23 20:12:35 +08:00
|
|
|
manager.save_to_file(&config_path)
|
|
|
|
|
}
|
2025-08-27 11:00:53 +08:00
|
|
|
|
2025-08-25 21:41:35 +08:00
|
|
|
// 保留按需扩展:若未来需要热加载,可在此实现
|
|
|
|
|
}
|