89 lines
2.1 KiB
Rust
89 lines
2.1 KiB
Rust
pub mod parser_error {
|
|
use std::fmt;
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub struct Waehrung;
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub struct TransactionDirection;
|
|
|
|
impl std::error::Error for TransactionDirection {}
|
|
|
|
impl std::error::Error for Waehrung {}
|
|
|
|
impl fmt::Display for Waehrung {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "invalid currency")
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for TransactionDirection {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "invalid H/S")
|
|
}
|
|
}
|
|
}
|
|
|
|
use {
|
|
serde::{Deserialize, Serialize},
|
|
std::fmt,
|
|
};
|
|
|
|
#[derive(Clone, Copy, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
|
pub enum Waehrung {
|
|
Eur,
|
|
Usd,
|
|
}
|
|
|
|
impl fmt::Display for Waehrung {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "{}", match self {
|
|
Self::Eur => "EUR",
|
|
Self::Usd => "USD",
|
|
})
|
|
}
|
|
}
|
|
|
|
impl std::str::FromStr for Waehrung {
|
|
type Err = parser_error::Waehrung;
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
Ok(match s {
|
|
"EUR" => Self::Eur,
|
|
"USD" => Self::Usd,
|
|
_ => return Err(parser_error::Waehrung),
|
|
})
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
|
pub enum TransactionDirection {
|
|
Haben, // H = Gutschrift
|
|
Soll, // S = Belastung
|
|
}
|
|
|
|
impl fmt::Display for TransactionDirection {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(
|
|
f,
|
|
"{}",
|
|
match self {
|
|
TransactionDirection::Haben => "H",
|
|
TransactionDirection::Soll => "S",
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
impl std::str::FromStr for TransactionDirection {
|
|
type Err = parser_error::TransactionDirection;
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
Ok(match s {
|
|
"H" => TransactionDirection::Haben,
|
|
"S" => TransactionDirection::Soll,
|
|
_ => return Err(parser_error::TransactionDirection),
|
|
})
|
|
}
|
|
}
|