From aed72b111b90ae19a975860d125aa4df98bee076 Mon Sep 17 00:00:00 2001 From: JSR Date: Fri, 20 Mar 2026 18:43:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E5=B9=B6=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=20Rust=20=E7=AB=AF=E5=89=A9=E4=BD=99=E6=94=B9=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-tauri/src/commands/app.rs | 20 +++++++--------- src-tauri/src/commands/database.rs | 37 +++++++++++++++--------------- src-tauri/src/commands/reason.rs | 16 ++++++------- src-tauri/src/commands/window.rs | 18 +++++++-------- src-tauri/src/db/connection.rs | 2 +- src-tauri/src/db/migration.rs | 5 +++- 6 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src-tauri/src/commands/app.rs b/src-tauri/src/commands/app.rs index a9dd617..65311cd 100644 --- a/src-tauri/src/commands/app.rs +++ b/src-tauri/src/commands/app.rs @@ -25,8 +25,8 @@ pub async fn register_url_protocol( { use std::process::Command; - let exe_path = std::env::current_exe() - .map_err(|e| format!("Failed to get executable path: {}", e))?; + let exe_path = + std::env::current_exe().map_err(|e| format!("Failed to get executable path: {}", e))?; let exe_path_str = exe_path.to_string_lossy(); @@ -45,17 +45,11 @@ pub async fn register_url_protocol( protocol, exe_path_str ); - let _ = Command::new("cmd") - .args(["/C", ®_command]) - .output(); + let _ = Command::new("cmd").args(["/C", ®_command]).output(); - let _ = Command::new("cmd") - .args(["/C", ®_command2]) - .output(); + let _ = Command::new("cmd").args(["/C", ®_command2]).output(); - let _ = Command::new("cmd") - .args(["/C", ®_command3]) - .output(); + let _ = Command::new("cmd").args(["/C", ®_command3]).output(); let _ = app; @@ -83,7 +77,9 @@ pub async fn register_url_protocol( #[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))] { let _ = app; - Ok(IpcResponse::error("URL protocol registration is not supported on this platform")) + Ok(IpcResponse::error( + "URL protocol registration is not supported on this platform", + )) } } diff --git a/src-tauri/src/commands/database.rs b/src-tauri/src/commands/database.rs index 1a2d4e8..cd4d4ce 100644 --- a/src-tauri/src/commands/database.rs +++ b/src-tauri/src/commands/database.rs @@ -5,8 +5,8 @@ use std::sync::Arc; use tauri::{AppHandle, Emitter, Manager, State}; use tokio::time::{timeout, Duration}; -use crate::db::connection::{create_postgres_connection, create_sqlite_connection}; use crate::db::connection::DatabaseType; +use crate::db::connection::{create_postgres_connection, create_sqlite_connection}; use crate::db::entities::{ reasons, reward_redemptions, reward_settings, score_events, student_tags, students, tags, }; @@ -331,16 +331,17 @@ async fn load_student_tag_pairs( .all(conn) .await .map_err(|e| e.to_string())?; - let tag_rows = tags::Entity::find().all(conn).await.map_err(|e| e.to_string())?; + let tag_rows = tags::Entity::find() + .all(conn) + .await + .map_err(|e| e.to_string())?; let link_rows = student_tags::Entity::find() .all(conn) .await .map_err(|e| e.to_string())?; - let student_name_map: std::collections::HashMap = student_rows - .into_iter() - .map(|s| (s.id, s.name)) - .collect(); + let student_name_map: std::collections::HashMap = + student_rows.into_iter().map(|s| (s.id, s.name)).collect(); let tag_name_map: std::collections::HashMap = tag_rows.into_iter().map(|t| (t.id, t.name)).collect(); @@ -486,7 +487,10 @@ async fn upsert_reason( } } -async fn upsert_tag(conn: &sea_orm::DatabaseConnection, data: &TagNormalized) -> Result { +async fn upsert_tag( + conn: &sea_orm::DatabaseConnection, + data: &TagNormalized, +) -> Result { let existing = tags::Entity::find() .filter(tags::Column::Name.eq(&data.name)) .one(conn) @@ -702,11 +706,9 @@ async fn ensure_student_tag_pair( id: sea_orm::ActiveValue::NotSet, student_id: Set(student_row.id), tag_id: Set(tag_row.id), - created_at: Set( - chrono::Utc::now() - .format("%Y-%m-%dT%H:%M:%S%.3fZ") - .to_string(), - ), + created_at: Set(chrono::Utc::now() + .format("%Y-%m-%dT%H:%M:%S%.3fZ") + .to_string()), } .insert(conn) .await @@ -717,10 +719,7 @@ async fn ensure_student_tag_pair( async fn current_remote_and_local_from_state( app_handle: &AppHandle, app_state: &Arc>, -) -> Result< - Option<(sea_orm::DatabaseConnection, sea_orm::DatabaseConnection)>, - String, -> { +) -> Result, String> { let (current_conn, db_type) = { let state_guard = app_state.read(); let db = state_guard.db.read().clone(); @@ -1053,7 +1052,8 @@ pub async fn db_switch_connection( ) -> Result, String> { check_admin_permission(&state)?; - let (db_type, saved_connection_string, saved_status, conn) = if connection_string.starts_with("postgres://") + let (db_type, saved_connection_string, saved_status, conn) = if connection_string + .starts_with("postgres://") || connection_string.starts_with("postgresql://") { let conn = timeout( @@ -1390,7 +1390,8 @@ pub async fn db_sync( let db_guard = state_guard.db.read(); if let Some(conn) = db_guard.as_ref() { let settings = state_guard.settings.read(); - let status_json = settings.get_value(crate::services::settings::SettingsKey::PgConnectionStatus); + let status_json = + settings.get_value(crate::services::settings::SettingsKey::PgConnectionStatus); let db_type = match status_json { crate::services::settings::SettingsValue::Json(json) => json .get("type") diff --git a/src-tauri/src/commands/reason.rs b/src-tauri/src/commands/reason.rs index 9748f5e..79d1a2f 100644 --- a/src-tauri/src/commands/reason.rs +++ b/src-tauri/src/commands/reason.rs @@ -117,14 +117,14 @@ pub async fn reason_create( updated_at: Set(now), }; - match new_reason.insert(conn).await { - Ok(inserted) => { - realtime_dual_write_sync(state.inner()).await?; - Ok(IpcResponse::success(inserted.id)) - } - Err(e) => Ok(IpcResponse::error(&format!( - "Failed to create reason: {}", - e + match new_reason.insert(conn).await { + Ok(inserted) => { + realtime_dual_write_sync(state.inner()).await?; + Ok(IpcResponse::success(inserted.id)) + } + Err(e) => Ok(IpcResponse::error(&format!( + "Failed to create reason: {}", + e ))), } } else { diff --git a/src-tauri/src/commands/window.rs b/src-tauri/src/commands/window.rs index 4160333..abf21e8 100644 --- a/src-tauri/src/commands/window.rs +++ b/src-tauri/src/commands/window.rs @@ -37,15 +37,15 @@ pub async fn window_maximize( #[cfg(desktop)] { if let Some(window) = app.get_webview_window("main") { - let is_maximized = window.is_maximized().map_err(|e| e.to_string())?; + let is_maximized = window.is_maximized().map_err(|e| e.to_string())?; - if is_maximized { - window.unmaximize().map_err(|e| e.to_string())?; - Ok(false) - } else { - window.maximize().map_err(|e| e.to_string())?; - Ok(true) - } + if is_maximized { + window.unmaximize().map_err(|e| e.to_string())?; + Ok(false) + } else { + window.maximize().map_err(|e| e.to_string())?; + Ok(true) + } } else { Err("Window not found".to_string()) } @@ -83,7 +83,7 @@ pub async fn window_is_maximized( #[cfg(desktop)] { if let Some(window) = app.get_webview_window("main") { - window.is_maximized().map_err(|e| e.to_string()) + window.is_maximized().map_err(|e| e.to_string()) } else { Ok(false) } diff --git a/src-tauri/src/db/connection.rs b/src-tauri/src/db/connection.rs index d54b43c..bd4e97c 100644 --- a/src-tauri/src/db/connection.rs +++ b/src-tauri/src/db/connection.rs @@ -1,7 +1,7 @@ use sea_orm::{ConnectOptions, Database, DatabaseConnection, DbErr}; -use std::time::Duration; use std::str::FromStr; use std::sync::Arc; +use std::time::Duration; use tokio::sync::RwLock; use tracing::{info, warn}; diff --git a/src-tauri/src/db/migration.rs b/src-tauri/src/db/migration.rs index cd1e72b..4380077 100644 --- a/src-tauri/src/db/migration.rs +++ b/src-tauri/src/db/migration.rs @@ -125,7 +125,10 @@ impl Migration { let db_backend = Self::get_db_backend(sqlite); let alter_sql = "ALTER TABLE students ADD COLUMN reward_points INTEGER DEFAULT 0"; let result = conn - .execute(Statement::from_string(db_backend.clone(), alter_sql.to_string())) + .execute(Statement::from_string( + db_backend.clone(), + alter_sql.to_string(), + )) .await; match result {