summaryrefslogtreecommitdiff
path: root/src/banking/asset.rs
blob: 642f1536d4efe9ee9426329ebaac25bc2f6b549b (plain)
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
use crate::banking::account::Account;
use crate::parsers::ini::IniFile;


pub struct AssetIterator<'a> {
	asset : &'a Asset,
	index : usize 
}


impl<'a> Iterator for AssetIterator<'a> {
	type Item = &'a crate::banking::account::Account;

	fn next(&mut self) -> Option<Self::Item> {
		if self.index < self.asset.accounts.len() {
			let tmp = &self.asset.accounts[self.index];
			self.index = self.index + 1;
			return Some(tmp);
		}
		None
	}
}

pub struct Asset {
	accounts : Vec<Account>	
}

impl Asset {
	pub fn from_ini_file(file_name : &str) -> Asset {
		// read in ini file
		let t = crate::parsers::ini::IniFile::from_file(file_name);
		let file;
		match t {
			Ok(f) => file = f,
			Err(e) => panic!("asset: from_ini_file: could not read ini file: {}", e)
		}

		let mut accounts = Vec::new();
		for (account_name, config) in file.sections {
			let mut tmp;
			match config.get("Institut") {
				Some(i) => tmp = i,
				None => panic!("asset: parse ini file: could not find \"Institute\" key for account {}", account_name)
			}
			let mut institute = String::from("");
			if let Some(i) = tmp.get(0) {
				institute = i.to_string();
			}
			let trans_files;
			match config.get("TransactionFile") {
				Some(i) => trans_files = i,
				None => panic!("asset: ini file: no \"TransactionFile\" for account: {}", account_name)
			}
			match config.get("Category") {
				Some(i) => tmp = i,
				None => panic!("asset: ini file: no \"Category\" for account: {}", account_name)
			}
			let mut category = String::from("");
			if let Some(i) = tmp.get(0) {
				category = i.to_string();
			}
			match config.get("IBAN") {
				Some(i) => tmp = i,
				None => panic!("asset: no name for account {}", account_name)
			}
			let mut iban = String::from("");
			if let Some(i) = tmp.get(0) {
				iban = i.to_string();
			}
			match config.get("GroupFile") {
				Some(i) => tmp = i,
				None => println!("no groupfile for account {}", account_name)
			}
			let mut groupFile = String::from("");
			if let Some(i) = tmp.get(0) {
				groupFile = i.to_string();
			}
			accounts.push(Account::new(account_name, iban, trans_files.to_vec(), institute, groupFile, category));


		}
		Asset { accounts : accounts }
	}

	pub fn get_account_by_name(self, name : &str) -> Option<Account> {
		for account in self.accounts {
			if account.name == name {
				return Some(account);
			}
		}
		None
	}

	pub fn iter<'a>(&'a self) -> AssetIterator<'a> {
		AssetIterator {
			asset : self,
			index : 0
		}
	}
}