summaryrefslogtreecommitdiff
path: root/src/exchange/mod.rs
blob: 94cdbca6aececd3716f50720a781c84d0793b1f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use std::io;
use parsers::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
		})
	}
}