+simple_enums.rs

This commit is contained in:
Erik Zscheile 2019-12-22 22:30:26 +01:00
parent 04ec2f320e
commit 685538982d
2 changed files with 127 additions and 91 deletions

View file

@ -1,98 +1,20 @@
use chrono::naive::NaiveDate;
use std::collections::HashSet;
use string_cache::DefaultAtom;
use std::{collections::HashSet, fmt};
/// simple typed helper enums
mod simple_enums;
use simple_enums::*;
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
enum KontoDaten {
None,
Old {
knr: u64,
blz: u32,
},
New {
iban: String,
bic: String,
},
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
enum Waehrung {
EUR,
USD,
}
impl fmt::Display for Waehrung {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", match self {
Waehrung::EUR => "EUR",
Waehrung::USD => "USD",
})
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct WaehrungParseError;
impl std::error::Error for WaehrungParseError {}
impl fmt::Display for WaehrungParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "invalid currency")
}
}
impl std::str::FromStr for Waehrung {
type Err = WaehrungParseError;
fn from_str(s: &str) -> Result<Self, WaehrungParseError> {
Ok(match s {
"EUR" => Waehrung::EUR,
"USD" => Waehrung::USD,
_ => return Err(WaehrungParseError),
})
}
Old { knr: u64, blz: u32 },
New { iban: String, bic: String },
}
type TransactionValue = fixed::types::U21F11;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
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",
})
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct TransactionDirectionParseError;
impl std::error::Error for TransactionDirectionParseError {}
impl fmt::Display for TransactionDirectionParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "invalid H/S")
}
}
impl std::str::FromStr for TransactionDirection {
type Err = TransactionDirectionParseError;
fn from_str(s: &str) -> Result<Self, TransactionDirectionParseError> {
Ok(match s {
"H" => TransactionDirection::Haben,
"S" => TransactionDirection::Soll,
_ => return Err(TransactionDirectionParseError),
})
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
struct TransactionLine {
d_buchungs: NaiveDate,
@ -171,7 +93,24 @@ fn main() {
let mut recsit = rdr.records().skip(8);
assert_eq!(recsit.next().unwrap().unwrap(), vec!["Buchungstag", "Valuta", "Auftraggeber/Zahlungsempfänger", "Empfänger/Zahlungspflichtiger", "Konto-Nr.", "IBAN", "BLZ", "BIC", "Vorgang/Verwendungszweck", "Kundenreferenz", "Währung", "Umsatz", " "]);
assert_eq!(
recsit.next().unwrap().unwrap(),
vec![
"Buchungstag",
"Valuta",
"Auftraggeber/Zahlungsempfänger",
"Empfänger/Zahlungspflichtiger",
"Konto-Nr.",
"IBAN",
"BLZ",
"BIC",
"Vorgang/Verwendungszweck",
"Kundenreferenz",
"Währung",
"Umsatz",
" "
]
);
println!("\nData:");
for result in recsit {
@ -183,7 +122,12 @@ fn main() {
}
// decode KontoDaten
let r_e: &[u8] = &[record[4].is_empty() as u8, record[5].is_empty() as u8, record[6].is_empty() as u8, record[7].is_empty() as u8];
let r_e: &[u8] = &[
record[4].is_empty() as u8,
record[5].is_empty() as u8,
record[6].is_empty() as u8,
record[7].is_empty() as u8,
];
let konto_data = match &*r_e {
&[0, 1, 0, 1] => KontoDaten::Old {
knr: record[4].parse().expect("invalid Konto-Nr."),
@ -194,18 +138,28 @@ fn main() {
bic: record[7].to_string(),
},
&[1, 1, 1, 1] => KontoDaten::None,
_ => unimplemented!("unsupported KontoData encoding: {:?} :in: {:?}", &r_e, &record),
_ => unimplemented!(
"unsupported KontoData encoding: {:?} :in: {:?}",
&r_e,
&record
),
};
let tl = TransactionLine {
d_buchungs: NaiveDate::parse_from_str(&record[0], "%d.%m.%Y").expect("invalid date format"),
d_valuta: NaiveDate::parse_from_str(&record[1], "%d.%m.%Y").expect("invalid date format"),
d_buchungs: NaiveDate::parse_from_str(&record[0], "%d.%m.%Y")
.expect("invalid date format"),
d_valuta: NaiveDate::parse_from_str(&record[1], "%d.%m.%Y")
.expect("invalid date format"),
p_other: DefaultAtom::from(&record[3]),
konto_data,
verwendungszw: record[8].to_string(),
kref: record[9].to_string(),
waehrung: record[10].parse().unwrap(),
umsatz: record[11].replace('.', "").replace(',', ".").parse().expect("invalid Umsatz"),
umsatz: record[11]
.replace('.', "")
.replace(',', ".")
.parse()
.expect("invalid Umsatz"),
direction: record[12].parse().unwrap(),
};
println!("{:?}", tl);

82
src/simple_enums.rs Normal file
View file

@ -0,0 +1,82 @@
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 std::fmt;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Waehrung {
EUR,
USD,
}
impl fmt::Display for Waehrung {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl std::str::FromStr for Waehrung {
type Err = parser_error::Waehrung;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"EUR" => Waehrung::EUR,
"USD" => Waehrung::USD,
_ => return Err(parser_error::Waehrung),
})
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
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),
})
}
}