From 7fcdc3ecc0f077ff7ff4ec57c912beae4f974fdb Mon Sep 17 00:00:00 2001 From: Benedict Börger Date: Mon, 17 Dec 2018 21:34:36 +0100 Subject: [finz] inital commit --- src/exchange/mod.rs | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 src/exchange/mod.rs (limited to 'src/exchange/mod.rs') diff --git a/src/exchange/mod.rs b/src/exchange/mod.rs new file mode 100644 index 0000000..8d93ac0 --- /dev/null +++ b/src/exchange/mod.rs @@ -0,0 +1,89 @@ +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 + }) + } +} -- cgit v1.2.3-70-g09d2