refactor: Clean up code formatting in utils module

This commit is contained in:
PandaDEV 2025-01-04 11:58:52 +10:00
parent 60b670c7a7
commit 22fcd84b8d
No known key found for this signature in database
GPG key ID: 13EFF9BAF70EE75C
14 changed files with 485 additions and 394 deletions

View file

@ -1,8 +1,8 @@
use serde::{Deserialize, Serialize};
use serde::{ Deserialize, Serialize };
use serde_json;
use sqlx::Row;
use sqlx::SqlitePool;
use tauri::{Emitter, Manager};
use tauri::{ Emitter, Manager };
use tauri_plugin_aptabase::EventTracker;
#[derive(Deserialize, Serialize)]
@ -16,10 +16,10 @@ pub async fn initialize_settings(pool: &SqlitePool) -> Result<(), Box<dyn std::e
};
let json = serde_json::to_string(&default_keybind)?;
sqlx::query("INSERT INTO settings (key, value) VALUES ('keybind', ?)")
sqlx
::query("INSERT INTO settings (key, value) VALUES ('keybind', ?)")
.bind(json)
.execute(pool)
.await?;
.execute(pool).await?;
Ok(())
}
@ -28,23 +28,24 @@ pub async fn initialize_settings(pool: &SqlitePool) -> Result<(), Box<dyn std::e
pub async fn save_keybind(
app_handle: tauri::AppHandle,
pool: tauri::State<'_, SqlitePool>,
keybind: Vec<String>,
keybind: Vec<String>
) -> Result<(), String> {
app_handle
.emit("update-shortcut", &keybind)
.map_err(|e| e.to_string())?;
app_handle.emit("update-shortcut", &keybind).map_err(|e| e.to_string())?;
let json = serde_json::to_string(&keybind).map_err(|e| e.to_string())?;
sqlx::query("INSERT OR REPLACE INTO settings (key, value) VALUES ('keybind', ?)")
sqlx
::query("INSERT OR REPLACE INTO settings (key, value) VALUES ('keybind', ?)")
.bind(json)
.execute(&*pool)
.await
.execute(&*pool).await
.map_err(|e| e.to_string())?;
let _ = app_handle.track_event("keybind_saved", Some(serde_json::json!({
let _ = app_handle.track_event(
"keybind_saved",
Some(serde_json::json!({
"keybind": keybind
})));
}))
);
Ok(())
}
@ -52,12 +53,12 @@ pub async fn save_keybind(
#[tauri::command]
pub async fn get_setting(
pool: tauri::State<'_, SqlitePool>,
key: String,
key: String
) -> Result<String, String> {
let row = sqlx::query("SELECT value FROM settings WHERE key = ?")
let row = sqlx
::query("SELECT value FROM settings WHERE key = ?")
.bind(key)
.fetch_optional(&*pool)
.await
.fetch_optional(&*pool).await
.map_err(|e| e.to_string())?;
Ok(row.map(|r| r.get("value")).unwrap_or_default())
@ -68,18 +69,21 @@ pub async fn save_setting(
app_handle: tauri::AppHandle,
pool: tauri::State<'_, SqlitePool>,
key: String,
value: String,
value: String
) -> Result<(), String> {
sqlx::query("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)")
sqlx
::query("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)")
.bind(key.clone())
.bind(value)
.execute(&*pool)
.await
.execute(&*pool).await
.map_err(|e| e.to_string())?;
let _ = app_handle.track_event("setting_saved", Some(serde_json::json!({
let _ = app_handle.track_event(
"setting_saved",
Some(serde_json::json!({
"key": key
})));
}))
);
Ok(())
}
@ -88,15 +92,18 @@ pub async fn save_setting(
pub async fn get_keybind(app_handle: tauri::AppHandle) -> Result<Vec<String>, String> {
let pool = app_handle.state::<SqlitePool>();
let row = sqlx::query("SELECT value FROM settings WHERE key = 'keybind'")
.fetch_optional(&*pool)
.await
let row = sqlx
::query("SELECT value FROM settings WHERE key = 'keybind'")
.fetch_optional(&*pool).await
.map_err(|e| e.to_string())?;
let json = row.map(|r| r.get::<String, _>("value")).unwrap_or_else(|| {
serde_json::to_string(&vec!["MetaLeft".to_string(), "KeyV".to_string()])
.expect("Failed to serialize default keybind")
});
let json = row
.map(|r| r.get::<String, _>("value"))
.unwrap_or_else(|| {
serde_json
::to_string(&vec!["MetaLeft".to_string(), "KeyV".to_string()])
.expect("Failed to serialize default keybind")
});
serde_json::from_str::<Vec<String>>(&json).map_err(|e| e.to_string())
}