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 | |
[finz] inital commit
Diffstat (limited to 'src')
| -rw-r--r-- | src/Csv/mod.rs | 62 | ||||
| -rw-r--r-- | src/exchange/mod.rs | 89 | ||||
| -rw-r--r-- | src/lib.rs | 0 | ||||
| -rw-r--r-- | src/main.rs | 23 | ||||
| -rw-r--r-- | src/test.csv | 3 |
5 files changed, 177 insertions, 0 deletions
diff --git a/src/Csv/mod.rs b/src/Csv/mod.rs new file mode 100644 index 0000000..29e1aca --- /dev/null +++ b/src/Csv/mod.rs @@ -0,0 +1,62 @@ +use std::fs::File; +use std::io; +use std::io::BufRead; + +/** + Represents a CSV file + **/ + +pub struct CsvFile { + path : String, + delim : String, + pub header : Vec<String>, + pub content : Vec<Vec<String>>, + has_header : bool +} + +impl CsvFile { + pub fn new(path: &str, delim: &str, header: bool) -> CsvFile { + CsvFile { + path : String::from(path), + delim: String::from(delim), + header : Vec::new(), + content : Vec::new(), + has_header : header + } + + } + + // TODO nicht gleich viele spalten + fn parse_line(line : String, delim: &str) -> Vec<String> { + let mut columns : Vec<String> = Vec::new(); + for column in line.split(delim).collect::<Vec<&str>>() { + columns.push(column.to_string()); + } + columns + } + + + pub fn read_file(path: &str, delim: &str, header: bool) -> Result<CsvFile, io::Error> { + let fd = File::open(path)?; + let reader = io::BufReader::new(fd); + let mut column_names : Vec<String> = Vec::new(); + let mut lines : Vec<Vec<String>> = Vec::new(); + let mut first = true; + + for line in reader.lines() { + let line = line?; + if header && first { + column_names.append(&mut CsvFile::parse_line(line, delim)); + first = false; + } else { + lines.push(CsvFile::parse_line(line, delim)); + } + } + + Ok(CsvFile { path: String::from(path), + delim : String::from(delim), + header : column_names, + content : lines, + has_header: header } ) + } +} 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 + }) + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/lib.rs diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..4c0768c --- /dev/null +++ b/src/main.rs @@ -0,0 +1,23 @@ +mod exchange; +mod Csv; +//use exchange::Stock; +use Csv::CsvFile; + +fn main() { + println!("Welcome to your investment calculator!"); + + // TODO Compute how the money would have benn developed on historical data + //Stock::read_from_csv_file("~/hih"); + let config = CsvFile::read_file("test.csv", ";", true); + let config = match config { + Ok(c) => c, + Err(error) => panic!("Error reading CSV file: {}", error), + }; + let mut i = 0; + for line in config.content { + i += 1; + for f in line { + println!("{}: {}",i, f) + } + } +} diff --git a/src/test.csv b/src/test.csv new file mode 100644 index 0000000..8708e0f --- /dev/null +++ b/src/test.csv @@ -0,0 +1,3 @@ +iban;hoch;tief +6789;8.8;9.9 +1234;2.2;5.5 |
