diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..a3be6a8 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,3 @@ +max_width = 80 +tab_spaces = 2 +wrap_comments = true diff --git a/src-tauri/.gitignore b/src-tauri/.gitignore index f4dfb82..c68d73a 100644 --- a/src-tauri/.gitignore +++ b/src-tauri/.gitignore @@ -2,3 +2,5 @@ # will have compiled files and executables /target/ +*-shm +*-wal \ No newline at end of file diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index d6ed59b..b868cff 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1367,11 +1367,15 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" name = "houhou" version = "0.0.0" dependencies = [ + "anyhow", + "clap", "dirs", "serde", "serde_json", + "sqlx", "tauri", "tauri-build", + "tokio", ] [[package]] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 593e709..3860680 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -20,6 +20,11 @@ tauri = { version = "1.3", features = ["shell-open"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" dirs = "5.0.1" +anyhow = "1.0.71" +clap = { version = "4.3.2", features = ["derive"] } +sqlx = { version = "0.6.3", features = ["runtime-tokio-rustls", "sqlite"] } +tokio = { version = "1.28.2", features = ["full"] } + [features] # this feature is used for production builds or when `devPath` points to the filesystem diff --git a/src-tauri/database-maker/src/main.rs b/src-tauri/database-maker/src/main.rs index 48079f1..958f97b 100644 --- a/src-tauri/database-maker/src/main.rs +++ b/src-tauri/database-maker/src/main.rs @@ -6,27 +6,27 @@ use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions}; #[derive(Debug, Parser)] struct Opt { - in_dir: PathBuf, - out_file: PathBuf, + in_dir: PathBuf, + out_file: PathBuf, } #[tokio::main] async fn main() -> Result<()> { - let opt = Opt::parse(); + let opt = Opt::parse(); - // Open sqlite db - let uri = format!("sqlite:{}", opt.out_file.display()); - println!("Opening {}...", uri); - let options = SqliteConnectOptions::from_str(&uri)?.create_if_missing(true); - let pool = SqlitePoolOptions::new() - .max_connections(5) - .connect_with(options) - .await?; + // Open sqlite db + let uri = format!("sqlite:{}", opt.out_file.display()); + println!("Opening {}...", uri); + let options = SqliteConnectOptions::from_str(&uri)?.create_if_missing(true); + let pool = SqlitePoolOptions::new() + .max_connections(5) + .connect_with(options) + .await?; - // Migrate that shit - sqlx::migrate!().run(&pool).await?; + // Migrate that shit + sqlx::migrate!().run(&pool).await?; - println!("Hello, world!"); + println!("Hello, world!"); - Ok(()) + Ok(()) } diff --git a/src-tauri/src/kanji.rs b/src-tauri/src/kanji.rs new file mode 100644 index 0000000..9ac57ea --- /dev/null +++ b/src-tauri/src/kanji.rs @@ -0,0 +1,16 @@ +use sqlx::{Row, SqlitePool}; +use tauri::State; + +pub struct KanjiDb(pub SqlitePool); + +#[tauri::command] +pub async fn get_kanji(state: State<'_, KanjiDb>) -> Result, ()> { + let result = sqlx::query("SELECT * FROM KanjiSet LIMIT 5") + .fetch_all(&state.0) + .await + .map_err(|_| ())?; + + let result = result.into_iter().map(|row| row.get("Character")).collect(); + + Ok(result) +} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 523550d..4dd3a3e 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,15 +1,28 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] +mod kanji; + +use anyhow::{Context, Result}; +use sqlx::SqlitePool; + +use crate::kanji::KanjiDb; + // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command #[tauri::command] fn greet(name: &str) -> String { - format!("Hello, {}! You've been greeted from Rust!", name) + format!("Hello, {}! You've been greeted from Rust!", name) } -fn main() { - tauri::Builder::default() - .invoke_handler(tauri::generate_handler![greet]) - .run(tauri::generate_context!()) - .expect("error while running tauri application"); +#[tokio::main] +async fn main() -> Result<()> { + let kanji_pool = SqlitePool::connect("./KanjiDatabase.sqlite").await?; + + tauri::Builder::default() + .manage(KanjiDb(kanji_pool)) + .invoke_handler(tauri::generate_handler![greet, kanji::get_kanji]) + .run(tauri::generate_context!()) + .context("error while running tauri application")?; + + Ok(()) } diff --git a/src/panes/KanjiPane.tsx b/src/panes/KanjiPane.tsx index 3a3bb2a..a9faec3 100644 --- a/src/panes/KanjiPane.tsx +++ b/src/panes/KanjiPane.tsx @@ -1,13 +1,23 @@ import { useState } from "react" import { Kanji } from "../types/Kanji" +import { invoke } from '@tauri-apps/api/tauri' import styles from "./KanjiPane.module.scss" export default function KanjiPane() { const [selectedKanji, setSelectedKanji] = useState(null); + const fetchKanji = async () => { + const result = await invoke('get_kanji'); + setSelectedKanji(result); + }; + return <> {JSON.stringify(selectedKanji)} -
+
+ +
+ + } \ No newline at end of file