This commit is contained in:
Michael Zhang 2023-06-11 15:08:15 -05:00
parent defee22098
commit 6efeb0498c
8 changed files with 75 additions and 22 deletions

3
rustfmt.toml Normal file
View file

@ -0,0 +1,3 @@
max_width = 80
tab_spaces = 2
wrap_comments = true

View file

@ -2,3 +2,5 @@
# will have compiled files and executables # will have compiled files and executables
/target/ /target/
*-shm
*-wal

4
src-tauri/Cargo.lock generated
View file

@ -1367,11 +1367,15 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
name = "houhou" name = "houhou"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"anyhow",
"clap",
"dirs", "dirs",
"serde", "serde",
"serde_json", "serde_json",
"sqlx",
"tauri", "tauri",
"tauri-build", "tauri-build",
"tokio",
] ]
[[package]] [[package]]

View file

@ -20,6 +20,11 @@ tauri = { version = "1.3", features = ["shell-open"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
dirs = "5.0.1" 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] [features]
# this feature is used for production builds or when `devPath` points to the filesystem # this feature is used for production builds or when `devPath` points to the filesystem

View file

@ -6,27 +6,27 @@ use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
struct Opt { struct Opt {
in_dir: PathBuf, in_dir: PathBuf,
out_file: PathBuf, out_file: PathBuf,
} }
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
let opt = Opt::parse(); let opt = Opt::parse();
// Open sqlite db // Open sqlite db
let uri = format!("sqlite:{}", opt.out_file.display()); let uri = format!("sqlite:{}", opt.out_file.display());
println!("Opening {}...", uri); println!("Opening {}...", uri);
let options = SqliteConnectOptions::from_str(&uri)?.create_if_missing(true); let options = SqliteConnectOptions::from_str(&uri)?.create_if_missing(true);
let pool = SqlitePoolOptions::new() let pool = SqlitePoolOptions::new()
.max_connections(5) .max_connections(5)
.connect_with(options) .connect_with(options)
.await?; .await?;
// Migrate that shit // Migrate that shit
sqlx::migrate!().run(&pool).await?; sqlx::migrate!().run(&pool).await?;
println!("Hello, world!"); println!("Hello, world!");
Ok(()) Ok(())
} }

16
src-tauri/src/kanji.rs Normal file
View file

@ -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<Vec<String>, ()> {
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)
}

View file

@ -1,15 +1,28 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!! // Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #![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 // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command] #[tauri::command]
fn greet(name: &str) -> String { 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() { #[tokio::main]
tauri::Builder::default() async fn main() -> Result<()> {
.invoke_handler(tauri::generate_handler![greet]) let kanji_pool = SqlitePool::connect("./KanjiDatabase.sqlite").await?;
.run(tauri::generate_context!())
.expect("error while running tauri application"); 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(())
} }

View file

@ -1,13 +1,23 @@
import { useState } from "react" import { useState } from "react"
import { Kanji } from "../types/Kanji" import { Kanji } from "../types/Kanji"
import { invoke } from '@tauri-apps/api/tauri'
import styles from "./KanjiPane.module.scss" import styles from "./KanjiPane.module.scss"
export default function KanjiPane() { export default function KanjiPane() {
const [selectedKanji, setSelectedKanji] = useState(null); const [selectedKanji, setSelectedKanji] = useState(null);
const fetchKanji = async () => {
const result = await invoke('get_kanji');
setSelectedKanji(result);
};
return <> return <>
{JSON.stringify(selectedKanji)} {JSON.stringify(selectedKanji)}
<div className={styles.kanjiDisplay}></div> <div className={styles.kanjiDisplay}>
</div>
<button onClick={fetchKanji}>Fetch</button>
</> </>
} }