use std::collections::HashMap; use rocket_contrib::templates::Template; use crate::parsers::ini::IniFile; use crate::parsers::csv::CsvFile; #[derive(Serialize)] pub struct AssetContext { accounts : Vec } #[derive(Serialize)] pub struct Account { name : String, category : String, balance : f32 } #[get("/asset")] pub fn asset_handler() -> rocket_contrib::templates::Template { let asset_file = "data/asset.ini"; let asset_config = IniFile::from_file(asset_file); let ini_file; match asset_config { Ok(file) => ini_file = file, Err(e) => panic!("could not read asset file") } let mut acc = Vec::new(); for (account_name, config) in ini_file.sections { let mut category = String::from(""); if let Some(cat) = config.get("Category") { if let Some(c) = cat.get(0) { category = c.to_string(); } } let mut all_trans = Vec::new(); if let Some(transaction_files) = config.get("TransactionFile") { for file in transaction_files { let transactions = CsvFile::from_file(file, ";", true); let mut t : Vec ; match transactions { Ok(trans) => t = crate::banking::account::Transaction::from_sparkasse_csv_file(trans), Err(e) => panic!("could not read file {:?}", e) } all_trans.append(& mut t); } } let balance = all_trans.iter().fold(0.0, |acc, x| acc + x.amount).abs(); let tmp = Account { name : account_name, category : category.to_string(), balance : balance }; acc.push(tmp); } let context = AssetContext { accounts : acc }; Template::render("asset", context) }