-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmain.rs
More file actions
64 lines (49 loc) · 1.65 KB
/
main.rs
File metadata and controls
64 lines (49 loc) · 1.65 KB
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
use cli_candlestick_chart::{Candle, Chart};
use std::error::Error;
use std::io::{self, BufRead};
mod utils;
use utils::hexa_to_rgb;
mod get_args;
use get_args::{get_args, CandlesRetrievalMode};
fn parse_candles_from_stdin() -> Vec<Candle> {
let stdin = io::stdin();
let mut stdin_lines = String::new();
for line in stdin.lock().lines() {
let line = line.expect("Could not read line from standard in");
stdin_lines.push_str(&line);
}
let candles: Vec<Candle> = serde_json::from_str(&stdin_lines).expect("Could not parse json");
candles
}
fn parse_candles_from_csv(filepath: &str) -> Result<Vec<Candle>, Box<dyn Error>> {
let mut rdr = csv::Reader::from_path(filepath)?;
let mut candles: Vec<Candle> = Vec::new();
for result in rdr.deserialize() {
let candle: Candle = result?;
candles.push(candle);
}
Ok(candles)
}
fn main() {
let options = get_args();
let candles = match options.mode {
CandlesRetrievalMode::Stdin => parse_candles_from_stdin(),
CandlesRetrievalMode::CsvFile => {
let filepath = options.file_path.expect("No file path provided.");
parse_candles_from_csv(&filepath).unwrap()
}
};
let mut chart = Chart::new(&candles);
if let Some(chart_name) = options.chart_name {
chart.set_name(chart_name);
}
if let Some(bear_color) = options.bear_color {
let (r, g, b) = hexa_to_rgb(bear_color);
chart.set_bear_color(r, g, b);
}
if let Some(bull_color) = options.bull_color {
let (r, g, b) = hexa_to_rgb(bull_color);
chart.set_bull_color(r, g, b);
}
chart.draw();
}