use std::io; use Csv::CsvFile; use std::collections::HashMap; pub struct Stock { name : String, history_prices : Vec } struct StockDatum { //date: , high: f32, low: f32, start: f32, end: f32 } impl StockDatum { fn new(h : f32, l : f32, s : f32, e : f32) -> StockDatum { StockDatum { high : h, low : l, start : s, end : e } } } //struct StockBuilder /* reads in a stock from a data source */ impl Stock { fn parse_line(line : Vec, header : &Vec) -> Option { let map : HashMap<&String,String> = header.iter().zip(line.into_iter()).collect(); let (mut high, mut low, mut start, mut end); match map.get(&String::from("high")) { Some(value) => high = value.parse::(), None => { println!("error with high value"); return None; } } match map.get(&String::from("low")) { Some(value) => low = value.parse::(), None => { println!("error with high value"); return None; } } match map.get(&String::from("start")) { Some(value) => start = value.parse::(), None => { println!("error with high value"); return None; } } match map.get(&String::from("end")) { Some(value) => end = value.parse::(), None => { println!("error with high value"); return None; } } Some(StockDatum { high: high, low : low, start : start, end: end }) } pub fn from_csv(csv_file : CsvFile) -> Result { let mut datums = Vec::new(); for line in csv_file.content { match Stock::parse_line(line, &csv_file.header) { None => println!("Error parsing"), Some(datum) => datums.push(datum), }; } Ok(Stock { name : String::from("test"), history_prices : datums }) } }