consume core in Tauri

This commit is contained in:
polv committed 2026-02-21 23:33:10 +07:00
1 parent 876b879a65
commit 59968e0d6c
14 files changed
+157 -75

No files matched your search

+14
View File
@@ -0,0 +1,14 @@
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
use cjdic2_core::{AppService, Entry};
#[tauri::command]
pub fn add_entry(name: &str, state: tauri::State<AppService>) -> Result<(), String> {
state
.add_entry(name, "definition")
.map_err(|e| e.to_string())
}
#[tauri::command]
pub fn list_entries(state: tauri::State<AppService>) -> Result<Vec<Entry>, String> {
state.list_entries().map_err(|e| e.to_string())
}
-29
View File
@@ -1,29 +0,0 @@
use serde::Serialize;
use crate::state;
#[derive(Serialize)]
pub struct SearchRow {
a: i32,
b: i32,
}
#[tauri::command]
pub fn get_db(state: tauri::State<state::AppState>) -> Result<Vec<SearchRow>, String> {
let conn = state.conn.lock().map_err(|e| e.to_string())?;
let mut stmt = conn.prepare("SELECT a, b FROM test").map_err(|e| e.to_string())?;
let rows = stmt.query_map([], |r| {
Ok(SearchRow {
a: r.get(0)?,
b: r.get(1)?,
})
}).map_err(|e| e.to_string())?;
let mut out = Vec::new();
for r in rows {
out.push(r.map_err(|e| e.to_string())?);
}
Ok(out)
}
+9 -29
View File
@@ -1,15 +1,8 @@
use crate::command::*;
use cjdic2_core::*;
use tauri::Manager;
use rusqlite::Connection;
use std::sync::Mutex;
mod db;
mod state;
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}
mod command;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
@@ -17,31 +10,18 @@ pub fn run() {
.plugin(tauri_plugin_opener::init())
.setup(|app| {
let cfg_dir = app.path().app_config_dir();
let app_dir = cfg_dir
.unwrap_or_else(|_| std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from(".")));
let app_dir = cfg_dir.unwrap_or_else(|_| {
std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."))
});
std::fs::create_dir_all(&app_dir)?;
let db_path = app_dir.join("cjdic.db");
let is_new = !db_path.exists();
let service = AppService::new(&db_path)?;
app.manage(service);
println!("{}", cjdic2_core::add(1, 2));
let conn = Connection::open(&db_path)?;
if is_new {
conn.execute_batch("CREATE TABLE test (a, b);")?;
let mut stmt = conn.prepare("INSERT INTO test VALUES (?1, ?2)")?;
stmt.insert([1, 2])?;
stmt.insert([3, 4])?;
}
app.manage(state::AppState{
conn: Mutex::new(conn),
});
Ok(())
})
.invoke_handler(tauri::generate_handler![greet, db::get_db])
.invoke_handler(tauri::generate_handler![add_entry, list_entries])
.build(tauri::generate_context!())
.expect("error while running tauri application")
.run(|_app_handle, event| match event {
-6
View File
@@ -1,6 +0,0 @@
use rusqlite::Connection;
use std::sync::Mutex;
pub struct AppState {
pub conn: Mutex<Connection>
}