DrCr/src-tauri/src/lib.rs

90 lines
2.6 KiB
Rust
Raw Normal View History

2024-11-15 19:35:11 +11:00
/*
DrCr: Web-based double-entry bookkeeping framework
Copyright (C) 20222024 Lee Yingtong Li (RunasSudo)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2024-11-17 22:31:34 +11:00
mod sql;
2024-11-17 17:45:16 +11:00
use tauri::{AppHandle, Builder, Manager, State};
use tauri_plugin_store::StoreExt;
2024-11-17 22:31:34 +11:00
use tokio::sync::Mutex;
2024-11-15 19:35:11 +11:00
2024-11-17 17:45:16 +11:00
use std::fs;
2024-11-15 19:35:11 +11:00
struct AppState {
db_filename: Option<String>,
2024-11-17 22:31:34 +11:00
sql_transactions: Vec<Option<crate::sql::SqliteTransaction>>,
2024-11-15 19:35:11 +11:00
}
2024-11-17 22:31:34 +11:00
// Filename state
2024-11-15 19:35:11 +11:00
#[tauri::command]
2024-11-17 22:31:34 +11:00
async fn get_open_filename(state: State<'_, Mutex<AppState>>) -> Result<Option<String>, tauri_plugin_sql::Error> {
let state = state.lock().await;
Ok(state.db_filename.clone())
2024-11-15 19:35:11 +11:00
}
#[tauri::command]
2024-11-17 22:31:34 +11:00
async fn set_open_filename(state: State<'_, Mutex<AppState>>, app: AppHandle, filename: Option<String>) -> Result<(), tauri_plugin_sql::Error> {
let mut state = state.lock().await;
2024-11-17 17:45:16 +11:00
state.db_filename = filename.clone();
// Persist in store
let store = app.store("store.json").expect("Error opening store");
store.set("db_filename", filename);
2024-11-17 22:31:34 +11:00
Ok(())
2024-11-15 19:35:11 +11:00
}
2024-11-17 22:31:34 +11:00
// Main method
2024-11-15 19:35:11 +11:00
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
Builder::default()
.setup(|app| {
2024-11-17 17:45:16 +11:00
// Get open filename
let store = app.store("store.json")?;
let db_filename = match store.get("db_filename") {
None => None,
Some(serde_json::Value::String(s)) => {
if fs::exists(&s)? {
Some(s)
} else {
None
}
},
_ => panic!("Unexpected db_filename in store")
};
2024-11-15 19:35:11 +11:00
app.manage(Mutex::new(AppState {
2024-11-17 22:31:34 +11:00
db_filename: db_filename,
sql_transactions: Vec::new(),
2024-11-15 19:35:11 +11:00
}));
2024-11-17 17:45:16 +11:00
2024-11-15 19:35:11 +11:00
Ok(())
})
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_sql::Builder::new().build())
2024-11-17 17:45:16 +11:00
.plugin(tauri_plugin_store::Builder::new().build())
2024-11-17 22:31:34 +11:00
.invoke_handler(tauri::generate_handler![
get_open_filename, set_open_filename,
sql::sql_transaction_begin, sql::sql_transaction_execute, sql::sql_transaction_select, sql::sql_transaction_rollback, sql::sql_transaction_commit
])
2024-11-15 19:35:11 +11:00
.run(tauri::generate_context!())
2024-11-17 17:45:16 +11:00
.expect("Error while running tauri application");
2024-11-15 19:35:11 +11:00
}