Initial commit
This commit is contained in:
commit
96033b4b9f
15 changed files with 6300 additions and 0 deletions
90
src-tauri/src/lib.rs
Normal file
90
src-tauri/src/lib.rs
Normal file
|
@ -0,0 +1,90 @@
|
|||
use rusqlite::{Connection, ToSql, types::ToSqlOutput};
|
||||
use rand::prelude::*;
|
||||
use tokio::sync::Mutex;
|
||||
use tauri::{Manager, State};
|
||||
use chrono::offset::Utc;
|
||||
|
||||
struct AppState {
|
||||
db: Connection,
|
||||
acc: i64,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum Store {
|
||||
Aldi,
|
||||
Rewe,
|
||||
}
|
||||
|
||||
impl TryFrom<&str> for Store {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(s: &str) -> Result<Self, Self::Error> {
|
||||
match s {
|
||||
"aldi" => Ok(Store::Aldi),
|
||||
"rewe" => Ok(Store::Rewe),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for Store {
|
||||
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
|
||||
match self {
|
||||
Store::Aldi => 0.to_sql(),
|
||||
Store::Rewe => 1.to_sql(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
||||
#[tauri::command]
|
||||
async fn swap(store: &str, state: State<'_, Mutex<AppState>>) -> Result<(), ()> {
|
||||
let state = state.lock().await;
|
||||
let mut rng = rand::rng();
|
||||
let store: Store = store.try_into()?;
|
||||
state.db.execute(
|
||||
"INSERT INTO swap VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
(
|
||||
rng.random::<i64>(),
|
||||
store,
|
||||
state.acc,
|
||||
Utc::now().timestamp(),
|
||||
false,
|
||||
),
|
||||
).map_err(|_| ())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn count(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
|
||||
let state = state.lock().await;
|
||||
let mut stmt = state.db.prepare("SELECT COUNT(*) FROM swap").unwrap();
|
||||
let mut rows = stmt.query([]).unwrap();
|
||||
let row = rows.next().unwrap().unwrap();
|
||||
let cnt: u64 = row.get_unwrap(0);
|
||||
Ok(cnt.to_string())
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
let mut rng = rand::rng();
|
||||
let db = Connection::open_in_memory().expect("Failed to create DB.");
|
||||
let acc = rng.random::<i64>();
|
||||
db.execute(
|
||||
"CREATE TABLE swap (
|
||||
rand INTEGER,
|
||||
store INTEGER,
|
||||
account INTEGER,
|
||||
time INTEGER,
|
||||
cancellation BOOL
|
||||
)",
|
||||
(),
|
||||
).unwrap();
|
||||
let state = AppState { db, acc };
|
||||
tauri::Builder::default()
|
||||
.setup(|app| {app.manage(Mutex::new(state)); Ok(())})
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.invoke_handler(tauri::generate_handler![swap, count])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
6
src-tauri/src/main.rs
Normal file
6
src-tauri/src/main.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
fn main() {
|
||||
bkbh_lib::run()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue