Split up types

This commit is contained in:
Bianca Fürstenau 2025-03-07 01:41:31 +01:00
parent 127ccfb26b
commit 293053134f
5 changed files with 151 additions and 135 deletions

View file

@ -1,135 +0,0 @@
#[cfg(feature = "server")]
use rusqlite::{types::ToSqlOutput, ToSql};
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum Store {
Aldi,
Edeka,
Dm,
Lidl,
Rewe,
Tegut,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum Account {
Sumpf,
Heinersyndikat,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Inventory {
pub acc: Account,
pub cash: i64,
pub vouchers: Vec<VoucherInventory>,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct VoucherInventory {
pub store: Store,
pub count: i64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Cash(i64);
impl std::str::FromStr for Cash {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let split: Vec<&str> = s.split(".").collect();
let i = i64::from_str(split.get(0).ok_or(())?)
.map_err(|e| println!("{:?}", e))?;
let f = match split.get(1) {
None => 0,
Some(fs) => i64::from_str(&format!("00{}", fs)[0..2])
.map_err(|e| println!("{:?}", e))?,
};
Ok(Cash(i * 100 + f))
}
}
impl Into<String> for &Store {
fn into(self) -> String {
String::from(match *self {
Store::Aldi => "aldi",
Store::Edeka => "edeka",
Store::Dm => "dm",
Store::Lidl => "lidl",
Store::Rewe => "rewe",
Store::Tegut => "tegut",
})
}
}
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 Into<String> for &Account {
fn into(self) -> String {
String::from(match *self {
Account::Sumpf => "sumpf",
Account::Heinersyndikat => "hs",
})
}
}
impl std::fmt::Display for Account {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
Account::Sumpf => "Sumpf",
Account::Heinersyndikat => "Heinersyndikat",
}
.fmt(f)
}
}
#[cfg(feature = "server")]
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(),
}
}
}
#[cfg(feature = "server")]
impl ToSql for Account {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
match self {
Account::Sumpf => 0.to_sql(),
Account::Heinersyndikat => 1.to_sql(),
}
}
}

50
src/types/account.rs Normal file
View file

@ -0,0 +1,50 @@
#[cfg(feature = "server")]
use rusqlite::{types::ToSqlOutput, ToSql};
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum Account {
Sumpf,
Heinersyndikat,
}
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 Into<String> for &Account {
fn into(self) -> String {
String::from(match *self {
Account::Sumpf => "sumpf",
Account::Heinersyndikat => "hs",
})
}
}
impl std::fmt::Display for Account {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
Account::Sumpf => "Sumpf",
Account::Heinersyndikat => "Heinersyndikat",
}
.fmt(f)
}
}
#[cfg(feature = "server")]
impl ToSql for Account {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
match self {
Account::Sumpf => 0.to_sql(),
Account::Heinersyndikat => 1.to_sql(),
}
}
}

23
src/types/cash.rs Normal file
View file

@ -0,0 +1,23 @@
use serde::{Deserialize, Serialize};
/// An amount of cash,
/// measured as an integer multiple
/// of 0.01 €.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Cash(i64);
impl std::str::FromStr for Cash {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let split: Vec<&str> = s.split(".").collect();
let i = i64::from_str(split.get(0).ok_or(())?)
.map_err(|e| println!("{:?}", e))?;
let f = match split.get(1) {
None => 0,
Some(fs) => i64::from_str(&format!("00{}", fs)[0..2])
.map_err(|e| println!("{:?}", e))?,
};
Ok(Cash(i * 100 + f))
}
}

22
src/types/mod.rs Normal file
View file

@ -0,0 +1,22 @@
use serde::{Deserialize, Serialize};
mod store;
mod account;
mod cash;
pub use store::Store;
pub use account::Account;
pub use cash::Cash;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Inventory {
pub acc: Account,
pub cash: Cash,
pub vouchers: Vec<VoucherInventory>,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct VoucherInventory {
pub store: Store,
pub count: i64,
}

56
src/types/store.rs Normal file
View file

@ -0,0 +1,56 @@
#[cfg(feature = "server")]
use rusqlite::{types::ToSqlOutput, ToSql};
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum Store {
Aldi,
Edeka,
Dm,
Lidl,
Rewe,
Tegut,
}
impl Into<String> for &Store {
fn into(self) -> String {
String::from(match *self {
Store::Aldi => "aldi",
Store::Edeka => "edeka",
Store::Dm => "dm",
Store::Lidl => "lidl",
Store::Rewe => "rewe",
Store::Tegut => "tegut",
})
}
}
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(()),
}
}
}
#[cfg(feature = "server")]
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(),
}
}
}