diff --git a/.gitignore b/.gitignore index a6ad858..e8ced61 100644 --- a/.gitignore +++ b/.gitignore @@ -46,3 +46,4 @@ target #.idea/ test.db* +/tmp diff --git a/src-core/examples/add-to-yomitan.rs b/src-core/examples/add-to-yomitan.rs index b97e45a..911db20 100644 --- a/src-core/examples/add-to-yomitan.rs +++ b/src-core/examples/add-to-yomitan.rs @@ -7,10 +7,13 @@ use std::{ use anyhow::{Context, Ok}; use cjdic2_core::db::Database; -fn main() -> Result<(), anyhow::Error> { - let db = Database::new("test.db")?; +mod common; +use common::get_db_path; - let zip_dir = Path::new("src-tauri/resources/yomitan"); +fn main() -> Result<(), anyhow::Error> { + let db = Database::new(get_db_path())?; + + let zip_dir = Path::new("tmp/yomitan"); println!("zip_dir: {:?}", absolute(zip_dir)); // Relative to workspace root for entry in diff --git a/src-core/examples/common/mod.rs b/src-core/examples/common/mod.rs new file mode 100644 index 0000000..509b2b2 --- /dev/null +++ b/src-core/examples/common/mod.rs @@ -0,0 +1,5 @@ +use std::path::PathBuf; + +pub fn get_db_path() -> PathBuf { + PathBuf::from("test.db") +} diff --git a/src-core/examples/search-yomitan.rs b/src-core/examples/search-yomitan.rs index f9b6059..639cac2 100644 --- a/src-core/examples/search-yomitan.rs +++ b/src-core/examples/search-yomitan.rs @@ -3,8 +3,11 @@ use std::time::Instant; use anyhow::Ok; use cjdic2_core::db::Database; +mod common; +use common::get_db_path; + fn main() -> Result<(), anyhow::Error> { - let db = Database::new("test.db")?; + let db = Database::new(get_db_path())?; let start = Instant::now(); let rs = db.yomitan().search_yomitan("擦る", "する", 10, 0)?; diff --git a/src-core/examples/yomitan-mod-schema.rs b/src-core/examples/yomitan-mod-schema.rs index d803efc..8fb4e7f 100644 --- a/src-core/examples/yomitan-mod-schema.rs +++ b/src-core/examples/yomitan-mod-schema.rs @@ -3,8 +3,11 @@ use std::time::Instant; use anyhow::Ok; use cjdic2_core::db::Database; +mod common; +use common::get_db_path; + fn main() -> Result<(), anyhow::Error> { - let db = Database::new("test.db")?; + let db = Database::new(get_db_path())?; let start = Instant::now(); db.yomitan().create_schema()?; diff --git a/src-core/src/db.rs b/src-core/src/db.rs index a71e0fa..8913471 100644 --- a/src-core/src/db.rs +++ b/src-core/src/db.rs @@ -8,6 +8,7 @@ use rusqlite::{Connection, Result}; use crate::models::Entry; mod yomitan; +pub use yomitan::{YomitanRow, YomitanZipImportResult}; #[derive(Clone)] pub struct Database { diff --git a/src-core/src/db/yomitan.rs b/src-core/src/db/yomitan.rs index aace2e9..f4a8db8 100644 --- a/src-core/src/db/yomitan.rs +++ b/src-core/src/db/yomitan.rs @@ -13,6 +13,26 @@ fn blake3_hex(s: &str) -> String { format!("{}", hash(s.as_bytes())) } +#[derive(Serialize, Debug)] +pub struct YomitanZipImportResult { + exists: bool, + load: bool, + error: Option, +} + +#[derive(Serialize, Debug)] +pub struct YomitanRow { + term: String, + reading: String, + def_tags: String, + rules: String, + score: i64, + glossary_json: String, + sequence: Option, + term_tags: String, + dict_title: String, +} + pub struct YomitanDatabase { db: Database, } @@ -453,23 +473,3 @@ fn intern( cache.insert(value.to_string(), id); Ok(id) } - -#[derive(Serialize, Debug)] -pub struct YomitanZipImportResult { - exists: bool, - load: bool, - error: Option, -} - -#[derive(Serialize, Debug)] -pub struct YomitanRow { - term: String, - reading: String, - def_tags: String, - rules: String, - score: i64, - glossary_json: String, - sequence: Option, - term_tags: String, - dict_title: String, -} diff --git a/src-core/src/service.rs b/src-core/src/service.rs index 69bdf16..f365dfe 100644 --- a/src-core/src/service.rs +++ b/src-core/src/service.rs @@ -1,6 +1,6 @@ use std::path::Path; -use crate::{db::Database, error::CJDicError, models::Entry}; +use crate::{db::Database, db::YomitanRow, error::CJDicError, models::Entry}; pub struct AppService { db: Database, @@ -22,7 +22,16 @@ impl AppService { Ok(self.db.fetch_all_entries()?) } - pub fn search_yomitan(&self, _term: &str, _reading: &str) -> Result<(), CJDicError> { - Ok(()) + pub fn search_yomitan( + &self, + q_term: &str, + q_reading: &str, + limit: u32, + offset: u32, + ) -> Result, CJDicError> { + Ok(self + .db + .yomitan() + .search_yomitan(q_term, q_reading, limit, offset)?) } } diff --git a/src-react/src/App.tsx b/src-react/src/App.tsx index e12ab3d..37aa416 100644 --- a/src-react/src/App.tsx +++ b/src-react/src/App.tsx @@ -8,20 +8,32 @@ import { invoke } from "@tauri-apps/api/core"; import { Provider } from "./components/ui/provider"; function App() { - const [greetMsg, setGreetMsg] = useState(""); - const [name, setName] = useState(""); + const [q, setQ] = useState(""); + const [entries, setEntries] = useState([]); useEffect(() => { - invoke("list_entries").then((r) => { - setGreetMsg(JSON.stringify(r)); - }); - }, [name]); + doSearch(); + }, [q]); - async function greet() { - await invoke("add_entry", { name }); - // Learn more about Tauri commands at https://tauri.app/develop/calling-rust/ + async function doSearch() { + if (!q.trim()) { + setEntries([]); + return; + } - setName(""); + const ender = q.endsWith(" ") ? "" : "*"; + + const qTerm = q.trim() + ender; + const qReading = q.trim() + ender; + + setEntries( + await invoke("search_yomitan", { + qTerm, + qReading, + limit: 10, + offset: 0, + }), + ); } return ( @@ -30,21 +42,24 @@ function App() {
{ e.preventDefault(); - greet(); }} > setName(e.currentTarget.value)} + value={q} + onChange={(e) => setQ(e.currentTarget.value)} autoComplete="off" - placeholder="Enter a name..." + placeholder="Search..." />
-

{greetMsg}

+
    + {entries.map((it, i) => ( +
  1. {JSON.stringify(it)}
  2. + ))} +
); diff --git a/src-tauri/resources/yomitan/.gitignore b/src-tauri/resources/yomitan/.gitignore deleted file mode 100644 index 2e67351..0000000 --- a/src-tauri/resources/yomitan/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -*.zip -# Pixiv_*.zip => Pixiv.zip from https://github.com/MarvNC/pixiv-yomitan/releases diff --git a/src-tauri/src/command.rs b/src-tauri/src/command.rs index 1ab764d..73f52f0 100644 --- a/src-tauri/src/command.rs +++ b/src-tauri/src/command.rs @@ -1,5 +1,5 @@ // Learn more about Tauri commands at https://tauri.app/develop/calling-rust/ -use cjdic2_core::{AppService, Entry}; +use cjdic2_core::{AppService, Entry, db::YomitanRow}; #[tauri::command] pub fn add_entry(name: &str, state: tauri::State) -> Result<(), String> { @@ -12,3 +12,16 @@ pub fn add_entry(name: &str, state: tauri::State) -> Result<(), Stri pub fn list_entries(state: tauri::State) -> Result, String> { state.list_entries().map_err(|e| e.to_string()) } + +#[tauri::command] +pub fn search_yomitan( + q_term: &str, + q_reading: &str, + limit: u32, + offset: u32, + state: tauri::State, +) -> Result, String> { + state + .search_yomitan(q_term, q_reading, limit, offset) + .map_err(|e| e.to_string()) +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 0f34906..190002a 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,3 +1,5 @@ +use std::{fs::create_dir_all, path::Path}; + use crate::command::*; use cjdic2_core::*; use tauri::Manager; @@ -9,19 +11,21 @@ pub fn run() { tauri::Builder::default() .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(".")) - }); - std::fs::create_dir_all(&app_dir)?; + let app_dir = app.path().app_config_dir()?; + create_dir_all(&app_dir)?; - let db_path = app_dir.join("cjdic.db"); + // let db_path = app_dir.join("cjdic.db"); + let db_path = Path::new(r"D:\Projects\cjdic2\test.db"); let service = AppService::new(&db_path)?; app.manage(service); Ok(()) }) - .invoke_handler(tauri::generate_handler![add_entry, list_entries]) + .invoke_handler(tauri::generate_handler![ + add_entry, + list_entries, + search_yomitan + ]) .build(tauri::generate_context!()) .expect("error while running tauri application") .run(|_app_handle, event| match event {