mirror of
https://github.com/patarapolw/cjdic2.git
synced 2026-11-09 17:39:35 +00:00
expose to Tauri to ensure it works
This commit is contained in:
1 parent
62cb1e5902
commit
098ee45b81
12 files changed
+108
-53
No files matched your search
@@ -46,3 +46,4 @@ target
|
|||||||
#.idea/
|
#.idea/
|
||||||
|
|
||||||
test.db*
|
test.db*
|
||||||
|
/tmp
|
||||||
@@ -7,10 +7,13 @@ use std::{
|
|||||||
use anyhow::{Context, Ok};
|
use anyhow::{Context, Ok};
|
||||||
use cjdic2_core::db::Database;
|
use cjdic2_core::db::Database;
|
||||||
|
|
||||||
fn main() -> Result<(), anyhow::Error> {
|
mod common;
|
||||||
let db = Database::new("test.db")?;
|
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
|
println!("zip_dir: {:?}", absolute(zip_dir)); // Relative to workspace root
|
||||||
|
|
||||||
for entry in
|
for entry in
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
pub fn get_db_path() -> PathBuf {
|
||||||
|
PathBuf::from("test.db")
|
||||||
|
}
|
||||||
@@ -3,8 +3,11 @@ use std::time::Instant;
|
|||||||
use anyhow::Ok;
|
use anyhow::Ok;
|
||||||
use cjdic2_core::db::Database;
|
use cjdic2_core::db::Database;
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
use common::get_db_path;
|
||||||
|
|
||||||
fn main() -> Result<(), anyhow::Error> {
|
fn main() -> Result<(), anyhow::Error> {
|
||||||
let db = Database::new("test.db")?;
|
let db = Database::new(get_db_path())?;
|
||||||
|
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let rs = db.yomitan().search_yomitan("擦る", "する", 10, 0)?;
|
let rs = db.yomitan().search_yomitan("擦る", "する", 10, 0)?;
|
||||||
|
|||||||
@@ -3,8 +3,11 @@ use std::time::Instant;
|
|||||||
use anyhow::Ok;
|
use anyhow::Ok;
|
||||||
use cjdic2_core::db::Database;
|
use cjdic2_core::db::Database;
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
use common::get_db_path;
|
||||||
|
|
||||||
fn main() -> Result<(), anyhow::Error> {
|
fn main() -> Result<(), anyhow::Error> {
|
||||||
let db = Database::new("test.db")?;
|
let db = Database::new(get_db_path())?;
|
||||||
|
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
db.yomitan().create_schema()?;
|
db.yomitan().create_schema()?;
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use rusqlite::{Connection, Result};
|
|||||||
use crate::models::Entry;
|
use crate::models::Entry;
|
||||||
|
|
||||||
mod yomitan;
|
mod yomitan;
|
||||||
|
pub use yomitan::{YomitanRow, YomitanZipImportResult};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Database {
|
pub struct Database {
|
||||||
|
|||||||
+20
-20
@@ -13,6 +13,26 @@ fn blake3_hex(s: &str) -> String {
|
|||||||
format!("{}", hash(s.as_bytes()))
|
format!("{}", hash(s.as_bytes()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Debug)]
|
||||||
|
pub struct YomitanZipImportResult {
|
||||||
|
exists: bool,
|
||||||
|
load: bool,
|
||||||
|
error: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Debug)]
|
||||||
|
pub struct YomitanRow {
|
||||||
|
term: String,
|
||||||
|
reading: String,
|
||||||
|
def_tags: String,
|
||||||
|
rules: String,
|
||||||
|
score: i64,
|
||||||
|
glossary_json: String,
|
||||||
|
sequence: Option<i64>,
|
||||||
|
term_tags: String,
|
||||||
|
dict_title: String,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct YomitanDatabase {
|
pub struct YomitanDatabase {
|
||||||
db: Database,
|
db: Database,
|
||||||
}
|
}
|
||||||
@@ -453,23 +473,3 @@ fn intern(
|
|||||||
cache.insert(value.to_string(), id);
|
cache.insert(value.to_string(), id);
|
||||||
Ok(id)
|
Ok(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
|
||||||
pub struct YomitanZipImportResult {
|
|
||||||
exists: bool,
|
|
||||||
load: bool,
|
|
||||||
error: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
|
||||||
pub struct YomitanRow {
|
|
||||||
term: String,
|
|
||||||
reading: String,
|
|
||||||
def_tags: String,
|
|
||||||
rules: String,
|
|
||||||
score: i64,
|
|
||||||
glossary_json: String,
|
|
||||||
sequence: Option<i64>,
|
|
||||||
term_tags: String,
|
|
||||||
dict_title: String,
|
|
||||||
}
|
|
||||||
+12
-3
@@ -1,6 +1,6 @@
|
|||||||
use std::path::Path;
|
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 {
|
pub struct AppService {
|
||||||
db: Database,
|
db: Database,
|
||||||
@@ -22,7 +22,16 @@ impl AppService {
|
|||||||
Ok(self.db.fetch_all_entries()?)
|
Ok(self.db.fetch_all_entries()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn search_yomitan(&self, _term: &str, _reading: &str) -> Result<(), CJDicError> {
|
pub fn search_yomitan(
|
||||||
Ok(())
|
&self,
|
||||||
|
q_term: &str,
|
||||||
|
q_reading: &str,
|
||||||
|
limit: u32,
|
||||||
|
offset: u32,
|
||||||
|
) -> Result<Vec<YomitanRow>, CJDicError> {
|
||||||
|
Ok(self
|
||||||
|
.db
|
||||||
|
.yomitan()
|
||||||
|
.search_yomitan(q_term, q_reading, limit, offset)?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+30
-15
@@ -8,20 +8,32 @@ import { invoke } from "@tauri-apps/api/core";
|
|||||||
import { Provider } from "./components/ui/provider";
|
import { Provider } from "./components/ui/provider";
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [greetMsg, setGreetMsg] = useState("");
|
const [q, setQ] = useState("");
|
||||||
const [name, setName] = useState("");
|
const [entries, setEntries] = useState<any[]>([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
invoke("list_entries").then((r) => {
|
doSearch();
|
||||||
setGreetMsg(JSON.stringify(r));
|
}, [q]);
|
||||||
});
|
|
||||||
}, [name]);
|
|
||||||
|
|
||||||
async function greet() {
|
async function doSearch() {
|
||||||
await invoke("add_entry", { name });
|
if (!q.trim()) {
|
||||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
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 (
|
return (
|
||||||
@@ -30,21 +42,24 @@ function App() {
|
|||||||
<form
|
<form
|
||||||
onSubmit={(e) => {
|
onSubmit={(e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
greet();
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Group attached w="full">
|
<Group attached w="full">
|
||||||
<Input
|
<Input
|
||||||
id="greet-input"
|
id="greet-input"
|
||||||
value={name}
|
value={q}
|
||||||
onChange={(e) => setName(e.currentTarget.value)}
|
onChange={(e) => setQ(e.currentTarget.value)}
|
||||||
autoComplete="off"
|
autoComplete="off"
|
||||||
placeholder="Enter a name..."
|
placeholder="Search..."
|
||||||
/>
|
/>
|
||||||
<Button type="submit">Search</Button>
|
<Button type="submit">Search</Button>
|
||||||
</Group>
|
</Group>
|
||||||
</form>
|
</form>
|
||||||
<p>{greetMsg}</p>
|
<ol>
|
||||||
|
{entries.map((it, i) => (
|
||||||
|
<li key={i}>{JSON.stringify(it)}</li>
|
||||||
|
))}
|
||||||
|
</ol>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Provider>
|
</Provider>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
*.zip
|
|
||||||
# Pixiv_*.zip => Pixiv.zip from https://github.com/MarvNC/pixiv-yomitan/releases
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
// 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]
|
#[tauri::command]
|
||||||
pub fn add_entry(name: &str, state: tauri::State<AppService>) -> Result<(), String> {
|
pub fn add_entry(name: &str, state: tauri::State<AppService>) -> Result<(), String> {
|
||||||
@@ -12,3 +12,16 @@ pub fn add_entry(name: &str, state: tauri::State<AppService>) -> Result<(), Stri
|
|||||||
pub fn list_entries(state: tauri::State<AppService>) -> Result<Vec<Entry>, String> {
|
pub fn list_entries(state: tauri::State<AppService>) -> Result<Vec<Entry>, String> {
|
||||||
state.list_entries().map_err(|e| e.to_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<AppService>,
|
||||||
|
) -> Result<Vec<YomitanRow>, String> {
|
||||||
|
state
|
||||||
|
.search_yomitan(q_term, q_reading, limit, offset)
|
||||||
|
.map_err(|e| e.to_string())
|
||||||
|
}
|
||||||
+11
-7
@@ -1,3 +1,5 @@
|
|||||||
|
use std::{fs::create_dir_all, path::Path};
|
||||||
|
|
||||||
use crate::command::*;
|
use crate::command::*;
|
||||||
use cjdic2_core::*;
|
use cjdic2_core::*;
|
||||||
use tauri::Manager;
|
use tauri::Manager;
|
||||||
@@ -9,19 +11,21 @@ pub fn run() {
|
|||||||
tauri::Builder::default()
|
tauri::Builder::default()
|
||||||
.plugin(tauri_plugin_opener::init())
|
.plugin(tauri_plugin_opener::init())
|
||||||
.setup(|app| {
|
.setup(|app| {
|
||||||
let cfg_dir = app.path().app_config_dir();
|
let app_dir = app.path().app_config_dir()?;
|
||||||
let app_dir = cfg_dir.unwrap_or_else(|_| {
|
create_dir_all(&app_dir)?;
|
||||||
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 db_path = app_dir.join("cjdic.db");
|
||||||
|
let db_path = Path::new(r"D:\Projects\cjdic2\test.db");
|
||||||
let service = AppService::new(&db_path)?;
|
let service = AppService::new(&db_path)?;
|
||||||
app.manage(service);
|
app.manage(service);
|
||||||
|
|
||||||
Ok(())
|
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!())
|
.build(tauri::generate_context!())
|
||||||
.expect("error while running tauri application")
|
.expect("error while running tauri application")
|
||||||
.run(|_app_handle, event| match event {
|
.run(|_app_handle, event| match event {
|
||||||
|
|||||||
Reference in new issue
Block a user