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

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("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));


		}
		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
	}
}