diff options
| author | Benedict Börger <benedict@0xb8000.de> | 2018-12-17 21:34:36 +0100 |
|---|---|---|
| committer | Benedict Börger <benedict@0xb8000.de> | 2018-12-17 21:34:36 +0100 |
| commit | 7fcdc3ecc0f077ff7ff4ec57c912beae4f974fdb (patch) | |
| tree | b0ecd5e6f008c2326b8dc96ca12c54632e2a8bff /src/exchange | |
[finz] inital commit
Diffstat (limited to 'src/exchange')
| -rw-r--r-- | src/exchange/mod.rs | 89 |
1 files changed, 89 insertions, 0 deletions
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<StockDatum> +} + +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<String>, header : &Vec<String>) -> Option<StockDatum> { + 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::<f32>(), + None => { + println!("error with high value"); + return None; + } + } + match map.get(&String::from("low")) { + Some(value) => low = value.parse::<f32>(), + None => { + println!("error with high value"); + return None; + } + } + match map.get(&String::from("start")) { + Some(value) => start = value.parse::<f32>(), + None => { + println!("error with high value"); + return None; + } + } + match map.get(&String::from("end")) { + Some(value) => end = value.parse::<f32>(), + 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<Stock, io::Error> { + 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 + }) + } +} |
