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 { 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 } 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("RiskCategory") { Some(i) => tmp = i, None => panic!("asset: ini file: no \"RiskCategory\" for account: {}", account_name) } let mut risk_category = String::from(""); if let Some(i) = tmp.get(0) { risk_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, risk_category)); } Asset { accounts : accounts } } pub fn get_account_by_name(self, name : &str) -> Option { 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 } } }