mirror of
https://github.com/patarapolw/cjdic2.git
synced 2026-11-09 17:39:35 +00:00
is init yet from Rust
This commit is contained in:
1 parent
9b5ae1cd8b
commit
dcfe074535
9 files changed
+38
-91
No files matched your search
@@ -1 +0,0 @@
|
||||
|
||||
+9
-49
@@ -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<AtomicBool>,
|
||||
}
|
||||
|
||||
const USER_DBFILE: &str = "user.db";
|
||||
pub(crate) const YOMITAN_DBFILE: &str = "yomitan.db";
|
||||
|
||||
impl Database {
|
||||
pub fn new(db_dir: impl AsRef<Path>) -> Result<Self> {
|
||||
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<yomitan::YomitanDatabase> {
|
||||
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<Vec<Entry>> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
+3
-5
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
+7
-11
@@ -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<P: AsRef<Path>>(db_dir: P) -> Result<Self, CJDicError> {
|
||||
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<Vec<Entry>, 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<YomitanWriter, CJDicError> {
|
||||
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)
|
||||
|
||||
@@ -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<boolean>("is_yomitan_setup_yet").then((is_setup) =>
|
||||
setIsLoading(!is_setup),
|
||||
);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
|
||||
@@ -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),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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<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())
|
||||
pub fn is_yomitan_setup_yet(state: tauri::State<AppService>) -> bool {
|
||||
state.is_yomitan_setup_yet()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
|
||||
@@ -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!())
|
||||
|
||||
Reference in new issue
Block a user