update file struct

This commit is contained in:
Julian Freeman
2026-03-30 19:22:11 -04:00
parent 626a9c21b0
commit 6b64f36cfb
3 changed files with 50 additions and 12 deletions

View File

@@ -16,6 +16,12 @@ pub struct AppSettings {
pub repo_url: String,
}
#[derive(Clone, Serialize, Deserialize)]
pub struct EssentialsRepo {
pub version: String,
pub essentials: Vec<Software>,
}
impl Default for AppSettings {
fn default() -> Self {
Self {
@@ -99,15 +105,15 @@ async fn sync_essentials(app: AppHandle) -> Result<bool, String> {
Ok(response) => {
if response.status().is_success() {
let content = response.text().await.map_err(|e| e.to_string())?;
// 验证 JSON 格式
let validation: Result<Vec<Software>, _> = serde_json::from_str(&content);
// 验证 JSON 格式(新格式:{ version: string, essentials: Vec<Software> }
let validation: Result<EssentialsRepo, _> = serde_json::from_str(&content);
if validation.is_ok() {
let path = get_essentials_path(&app);
fs::write(path, content).map_err(|e| e.to_string())?;
emit_log(&app, "sync-essentials", "Result", "Essentials list updated successfully.", "success");
Ok(true)
} else {
emit_log(&app, "sync-essentials", "Error", "Invalid JSON format from repository.", "error");
emit_log(&app, "sync-essentials", "Error", "Invalid JSON format from repository. Expected { version, essentials }.", "error");
Err("Invalid JSON format".to_string())
}
} else {
@@ -118,7 +124,7 @@ async fn sync_essentials(app: AppHandle) -> Result<bool, String> {
}
Err(e) => {
emit_log(&app, "sync-essentials", "Skipped", &format!("Network issue: {}. Using local cache.", e), "info");
Ok(false) // 静默失败,返回 false 表示未更新但可继续
Ok(false)
}
}
}
@@ -132,14 +138,14 @@ async fn initialize_app(app: AppHandle) -> Result<bool, String> {
}
#[tauri::command]
fn get_essentials(app: AppHandle) -> Vec<Software> {
fn get_essentials(app: AppHandle) -> Option<EssentialsRepo> {
let file_path = get_essentials_path(&app);
if !file_path.exists() {
return vec![];
return None;
}
let content = fs::read_to_string(file_path).unwrap_or_else(|_| "[]".to_string());
serde_json::from_str(&content).unwrap_or_default()
let content = fs::read_to_string(file_path).unwrap_or_default();
serde_json::from_str(&content).ok()
}
#[tauri::command]