Something running

This commit is contained in:
Bianca Fürstenau 2025-02-22 07:09:34 +01:00
parent e03a16d13e
commit 49a9810fd8
19 changed files with 173 additions and 132 deletions

View file

@ -1,93 +1,16 @@
use chrono::offset::Utc;
use rusqlite::{types::ToSqlOutput, ToSql};
use tauri::{Manager, State};
use tauri_plugin_fs::FsExt;
use tokio::sync::Mutex;
use std::collections::HashMap;
use tokio::sync::Mutex;
use tauri::{Manager, State};
mod app_state;
mod data_door;
use crate::types::*;
pub mod app_state;
pub mod data_door;
use app_state::AppState;
#[derive(Clone, Copy, Debug)]
enum Store {
Aldi,
Edeka,
Dm,
Lidl,
Rewe,
Tegut,
}
#[derive(Clone, Copy, Debug)]
enum Account {
Sumpf,
Heinersyndikat,
}
struct Inventory {
acc: Account,
cash: i64,
vouchers: Vec<VoucherInventory>,
}
#[derive(Debug)]
struct VoucherInventory {
store: Store,
count: i64,
}
impl TryFrom<&str> for Store {
type Error = ();
fn try_from(s: &str) -> Result<Self, Self::Error> {
match s {
"aldi" => Ok(Store::Aldi),
"edeka" => Ok(Store::Edeka),
"dm" => Ok(Store::Dm),
"lidl" => Ok(Store::Lidl),
"rewe" => Ok(Store::Rewe),
"tegut" => Ok(Store::Tegut),
_ => Err(()),
}
}
}
impl TryFrom<&str> for Account {
type Error = ();
fn try_from(s: &str) -> Result<Self, Self::Error> {
match s {
"sumpf" => Ok(Account::Sumpf),
"hs" => Ok(Account::Heinersyndikat),
_ => Err(()),
}
}
}
impl ToSql for Store {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
match self {
Store::Aldi => 0.to_sql(),
Store::Edeka => 1.to_sql(),
Store::Dm => 2.to_sql(),
Store::Lidl => 3.to_sql(),
Store::Rewe => 4.to_sql(),
Store::Tegut => 5.to_sql(),
}
}
}
impl ToSql for Account {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
match self {
Account::Sumpf => 0.to_sql(),
Account::Heinersyndikat => 1.to_sql(),
}
}
}
fn parse_inventory(data: HashMap<String, String>) -> Result<Inventory, ()> {
let a = data.get("cafe-inventory-acc").ok_or(())?;
let acc: Account = Account::try_from(a.as_ref())?;
@ -114,7 +37,7 @@ fn parse_inventory(data: HashMap<String, String>) -> Result<Inventory, ()> {
}
#[tauri::command]
async fn inventory(
pub async fn inventory(
data: HashMap<String, String>,
state: State<'_, Mutex<AppState>>,
) -> Result<(), ()> {
@ -137,7 +60,7 @@ async fn inventory(
}
#[tauri::command]
async fn swap(
pub async fn swap(
store: &str,
acc: i64,
state: State<'_, Mutex<AppState>>,
@ -159,7 +82,7 @@ async fn swap(
}
#[tauri::command]
async fn count(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
pub async fn count(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
let state = state.lock().await;
let mut stmt =
state.db.prepare("SELECT COUNT(*) FROM swap")
@ -176,25 +99,3 @@ async fn count(state: State<'_, Mutex<AppState>>) -> Result<String, ()> {
let cnt: u64 = row.get(0).map_err(|e| println!("{:?}", e))?;
Ok(cnt.to_string())
}
pub fn run() {
let state = AppState::new();
tauri::Builder::default()
.plugin(tauri_plugin_fs::init())
.setup(|app| {
app.manage(Mutex::new(state));
let scope = app.fs_scope();
let path = app.path();
scope.allow_directory(path.temp_dir()?, false)?;
Ok(())
})
.invoke_handler(tauri::generate_handler![
swap,
count,
inventory,
data_door::pull_data,
data_door::push_data,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}