From dcfe07453535d96468ee22a497b41a0d4eea680f Mon Sep 17 00:00:00 2001 From: polv Date: Tue, 24 Feb 2026 19:25:37 +0700 Subject: [PATCH] is init yet from Rust --- src-core/src/api.rs | 1 - src-core/src/db.rs | 58 ++++------------------ src-core/src/lib.rs | 8 ++- src-core/src/models.rs | 8 --- src-core/src/service.rs | 18 +++---- src-react/src/components/LoadingDialog.tsx | 5 +- src-react/src/pages/SearchPage.tsx | 10 +++- src-tauri/src/command.rs | 13 ++--- src-tauri/src/lib.rs | 8 +-- 9 files changed, 38 insertions(+), 91 deletions(-) delete mode 100644 src-core/src/api.rs delete mode 100644 src-core/src/models.rs diff --git a/src-core/src/api.rs b/src-core/src/api.rs deleted file mode 100644 index 8b13789..0000000 --- a/src-core/src/api.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src-core/src/db.rs b/src-core/src/db.rs index f69feb1..36efec1 100644 --- a/src-core/src/db.rs +++ b/src-core/src/db.rs @@ -10,8 +10,6 @@ use std::{ use anyhow::Result; use rusqlite::Connection; -use crate::models::Entry; - mod yomitan; pub use yomitan::YomitanRow; @@ -25,10 +23,13 @@ pub(crate) struct Database { yomitan_attached: Arc, } +const USER_DBFILE: &str = "user.db"; +pub(crate) const YOMITAN_DBFILE: &str = "yomitan.db"; + impl Database { pub fn new(db_dir: impl AsRef) -> Result { let dir = db_dir.as_ref().to_path_buf(); - let conn = Connection::open(dir.join("user.db"))?; + let conn = Connection::open(dir.join(USER_DBFILE))?; conn.execute_batch( r" @@ -45,6 +46,10 @@ impl Database { }) } + pub fn is_yomitan_dbfile_exists(&self) -> bool { + self.dir.join(YOMITAN_DBFILE).exists() + } + pub fn yomitan(&self) -> Result { self.ensure_yomitan_attached()?; Ok(yomitan::YomitanDatabase::new(self.clone())) @@ -64,7 +69,7 @@ impl Database { current_dir()?.join(dir) }; let path = path - .join("yomitan.db") + .join(YOMITAN_DBFILE) .to_string_lossy() .replace(r"\", r"/"); @@ -83,49 +88,4 @@ impl Database { Ok(()) } - - pub fn init(&self) -> Result<()> { - let conn = self.conn.lock().unwrap(); - - conn.execute_batch( - " - CREATE TABLE IF NOT EXISTS entries ( - id INTEGER PRIMARY KEY, - word TEXT NOT NULL, - definition TEXT NOT NULL - )", - )?; - Ok(()) - } - - pub fn insert_entry(&self, word: &str, definition: &str) -> Result<()> { - let conn = self.conn.lock().unwrap(); - - conn.execute( - "INSERT INTO entries (word, definition) VALUES (?, ?)", - [word, definition], - )?; - Ok(()) - } - - pub fn fetch_all_entries(&self) -> Result> { - let conn = self.conn.lock().unwrap(); - - let mut stmt = conn.prepare("SELECT id, word, definition FROM entries")?; - - let rows = stmt.query_map([], |row| { - Ok(Entry { - id: row.get(0)?, - word: row.get(1)?, - definition: row.get(2)?, - }) - })?; - - let mut entries = Vec::new(); - for entry in rows { - entries.push(entry?); - } - - Ok(entries) - } } diff --git a/src-core/src/lib.rs b/src-core/src/lib.rs index 92bba00..b835447 100644 --- a/src-core/src/lib.rs +++ b/src-core/src/lib.rs @@ -1,11 +1,9 @@ -mod api; -pub mod db; +mod db; mod error; -mod models; -pub mod service; +mod service; +pub use db::{YomitanRow, YomitanZipImportProgress, YomitanZipImportResult}; pub use error::CJDicError; -pub use models::*; pub use service::AppService; pub fn add(left: u64, right: u64) -> u64 { diff --git a/src-core/src/models.rs b/src-core/src/models.rs deleted file mode 100644 index 9a019d0..0000000 --- a/src-core/src/models.rs +++ /dev/null @@ -1,8 +0,0 @@ -use serde::{Deserialize, Serialize}; - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Entry { - pub id: i64, - pub word: String, - pub definition: String, -} diff --git a/src-core/src/service.rs b/src-core/src/service.rs index 1d4b514..adce4b7 100644 --- a/src-core/src/service.rs +++ b/src-core/src/service.rs @@ -3,9 +3,11 @@ use std::path::{Path, PathBuf}; use rusqlite::Connection; use crate::{ - db::{Database, YomitanRow, YomitanWriter, YomitanZipImportProgress, YomitanZipImportResult}, + db::{ + Database, YOMITAN_DBFILE, YomitanRow, YomitanWriter, YomitanZipImportProgress, + YomitanZipImportResult, + }, error::CJDicError, - models::Entry, }; pub struct AppService { @@ -15,17 +17,11 @@ pub struct AppService { impl AppService { pub fn new>(db_dir: P) -> Result { let db = Database::new(db_dir)?; - db.init()?; Ok(Self { db }) } - pub fn add_entry(&self, word: &str, definition: &str) -> Result<(), CJDicError> { - self.db.insert_entry(word, definition)?; - Ok(()) - } - - pub fn list_entries(&self) -> Result, CJDicError> { - Ok(self.db.fetch_all_entries()?) + pub fn is_yomitan_setup_yet(&self) -> bool { + self.db.is_yomitan_dbfile_exists() } pub fn search_yomitan( @@ -42,7 +38,7 @@ impl AppService { } pub fn get_yomitan_writer(&self) -> Result { - let conn = Connection::open(self.db.dir.join("yomitan.db"))?; + let conn = Connection::open(self.db.dir.join(YOMITAN_DBFILE))?; let mut writer = YomitanWriter::new(conn)?; writer.create_schema()?; Ok(writer) diff --git a/src-react/src/components/LoadingDialog.tsx b/src-react/src/components/LoadingDialog.tsx index 64e3b41..7e08ea2 100644 --- a/src-react/src/components/LoadingDialog.tsx +++ b/src-react/src/components/LoadingDialog.tsx @@ -1,12 +1,15 @@ import { useEffect, useState } from "react"; import { Dialog, Portal } from "@chakra-ui/react"; +import { invoke } from "@tauri-apps/api/core"; function LoadingDialog() { const [isLoading, setIsLoading] = useState(false); useEffect(() => { - setIsLoading(true); + invoke("is_yomitan_setup_yet").then((is_setup) => + setIsLoading(!is_setup), + ); }, []); return ( diff --git a/src-react/src/pages/SearchPage.tsx b/src-react/src/pages/SearchPage.tsx index cdb432c..89cbf49 100644 --- a/src-react/src/pages/SearchPage.tsx +++ b/src-react/src/pages/SearchPage.tsx @@ -29,19 +29,25 @@ function SearchPage() { trySearch(); }, [q]); - async function trySearch() { + function trySearch() { if (!q.trim()) { setEntries([]); return; } + let willRunSearch = runSearch; + if (searchTimeout) { clearTimeout(searchTimeout); + } else { + // If new, just run immediately. No need for throttling. + runSearch(); + willRunSearch = async () => {}; } setSearchTimeout( setTimeout(() => { - runSearch(); + willRunSearch(); }, 250), ); } diff --git a/src-tauri/src/command.rs b/src-tauri/src/command.rs index 73f52f0..3d84aea 100644 --- a/src-tauri/src/command.rs +++ b/src-tauri/src/command.rs @@ -1,16 +1,9 @@ // Learn more about Tauri commands at https://tauri.app/develop/calling-rust/ -use cjdic2_core::{AppService, Entry, db::YomitanRow}; +use cjdic2_core::{AppService, YomitanRow}; #[tauri::command] -pub fn add_entry(name: &str, state: tauri::State) -> Result<(), String> { - state - .add_entry(name, "definition") - .map_err(|e| e.to_string()) -} - -#[tauri::command] -pub fn list_entries(state: tauri::State) -> Result, String> { - state.list_entries().map_err(|e| e.to_string()) +pub fn is_yomitan_setup_yet(state: tauri::State) -> bool { + state.is_yomitan_setup_yet() } #[tauri::command] diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index b6e7463..b6a32a2 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,4 +1,4 @@ -use std::{fs::create_dir_all, path::Path}; +use std::fs::create_dir_all; use crate::command::*; use cjdic2_core::*; @@ -15,15 +15,15 @@ pub fn run() { create_dir_all(&app_dir)?; // db_path is app_dir; - let db_dir = Path::new(r"D:\Projects\cjdic2\tmp\save-db"); + let db_dir = app_dir.as_path(); + // let db_dir = Path::new(r"D:\Projects\cjdic2\tmp\save-db"); let service = AppService::new(&db_dir)?; app.manage(service); Ok(()) }) .invoke_handler(tauri::generate_handler![ - add_entry, - list_entries, + is_yomitan_setup_yet, search_yomitan ]) .build(tauri::generate_context!())