1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#[macro_use]
extern crate log;
extern crate csv;
extern crate chrono;
extern crate bigdecimal;
extern crate encoding;
extern crate num;
use std::io::{self, BufReader, Read};
use chrono::prelude::*;
use bigdecimal::BigDecimal;
mod banks;
mod iban;
pub mod scr;
pub use iban::Iban;
pub trait Transaction {
fn get_identifier(&self) -> &str;
fn settlement_date(&self) -> Date<UTC>;
fn amount(&self) -> &BigDecimal;
fn currency(&self) -> &str;
fn date(&self) -> Date<UTC>;
fn origin_account(&self) -> &Iban;
fn counterparty_account(&self) -> &Iban;
fn counterparty_name(&self) -> &str;
fn first_message(&self) -> &str;
fn second_message(&self) -> &str;
}
pub trait Transactions<R: Read> {
fn get_origin_account(&self) -> Option<::Iban> {
None
}
fn read_transactions<'a>(&'a mut self) -> Box<Iterator<Item=Box<Transaction>> + 'a>;
}
pub trait Bank {
fn get_name() -> &'static str;
fn read_transaction_file<R: Read + 'static>(reader: BufReader<R>) -> io::Result<Box<Transactions<R>>>;
fn recognise_transaction_file<R: Read + 'static>(reader: &mut BufReader<R>) -> bool;
}
use std::io::{Seek, SeekFrom};
macro_rules! detect_bank {
($bank:ident, $reader:ident) => (
if banks::$bank::recognise_transaction_file(&mut $reader) {
$reader.seek(SeekFrom::Start(0)).unwrap();
return banks::$bank::read_transaction_file($reader);
}
)
}
pub fn read_transaction_file<R: Read + std::io::Seek + 'static>(mut reader: BufReader<R>) -> io::Result<Box<Transactions<R>>> {
detect_bank!(Argenta, reader);
Err(io::Error::new(io::ErrorKind::InvalidData, "Could not detect bank"))
}
struct GenericTransaction {
id: String,
settlement_date: Date<UTC>,
description: String,
amount: BigDecimal,
date: Date<UTC>,
counterparty_account: Iban,
origin_account: Iban,
counterparty_name: String,
currency: String,
message_1: String,
message_2: String,
}
impl ::Transaction for GenericTransaction {
fn get_identifier(&self) -> &str {
&self.id
}
fn settlement_date(&self) -> Date<UTC> {
self.settlement_date
}
fn amount(&self) -> &BigDecimal {
&self.amount
}
fn currency(&self) -> &str {
&self.currency
}
fn date(&self) -> Date<UTC> {
self.date
}
fn origin_account(&self) -> &Iban {
&self.origin_account
}
fn counterparty_account(&self) -> &Iban {
&self.counterparty_account
}
fn counterparty_name(&self) -> &str {
&self.counterparty_name
}
fn first_message(&self) -> &str {
&self.message_1
}
fn second_message(&self) -> &str {
&self.message_2
}
}