Refactor and add new commands and doc comments
This commit is contained in:
+1
-40
@@ -1,43 +1,4 @@
|
||||
mod transaction;
|
||||
pub use transaction::Transaction;
|
||||
pub mod csv;
|
||||
pub mod ynab;
|
||||
use csv::ReaderBuilder;
|
||||
use std::io::Read;
|
||||
|
||||
enum BankFormat {
|
||||
Bulder,
|
||||
}
|
||||
|
||||
impl BankFormat {
|
||||
fn from_str(format: &str) -> Result<BankFormat, String> {
|
||||
match format {
|
||||
"bulder" => Ok(BankFormat::Bulder),
|
||||
_ => Err(format!("Bank '{}' not found", format)),
|
||||
}
|
||||
}
|
||||
|
||||
fn reader<R: Read>(&self, input: R) -> csv::Reader<R> {
|
||||
match self {
|
||||
BankFormat::Bulder => ReaderBuilder::new()
|
||||
.has_headers(true)
|
||||
.delimiter(b';')
|
||||
.from_reader(input),
|
||||
}
|
||||
}
|
||||
|
||||
fn transform(&self, record: &csv::StringRecord) -> Result<Transaction, String> {
|
||||
match self {
|
||||
BankFormat::Bulder => Transaction::from_fields(&record[0], &record[9], &record[1]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_transactions<R: Read>(input: R, format: &str) -> Result<Vec<Transaction>, String> {
|
||||
let format = BankFormat::from_str(format).unwrap();
|
||||
format
|
||||
.reader(input)
|
||||
.records()
|
||||
.map(|r| r.map_err(|e| format!("parse error: {}", e)))
|
||||
.map(|r| r.and_then(|r| format.transform(&r)))
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user