-
Notifications
You must be signed in to change notification settings - Fork 2
/
trader_v2.rs
63 lines (54 loc) · 1.51 KB
/
trader_v2.rs
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
// trial run with ./trader_v2 data/mini_me.csv
use std::{
collections::HashSet,
path::Path
};
use book::{
utils::get_args,
file_utils::lines_from_file,
list_utils::{head,tail,ht}
};
use crypto::types::{
assets::{Asset, print_assets},
trades::{read_csv_swap,swap_d},
usd::mk_usd
};
fn usage() {
println!("\n./trader <trades CSV file>");
println!("\n\tprints the trades that occurred, ... and stuff.");
}
fn main() {
if let Some(filename) = head(get_args()) {
println!("Processing {}", filename);
parse_n_print(filename);
println!("Finito!"); // a little Italian flourish at the finito!
} else {
usage();
}
}
fn parse_n_print(file: impl AsRef<Path>) {
let lines = tail(lines_from_file(file));
println!("date,from,to");
let mut bag = HashSet::new();
cont(&mut bag, lines);
}
// mutually recursive functions, because what even are for-loops, anyway? :<
fn cont(bag: &mut HashSet<Asset>, lines: Vec<String>) {
if !lines.is_empty() {
let (line, rest) = ht(lines);
print_trades(bag, &line, rest);
} else {
println!("\nAssets:\n");
print_assets(bag);
}
}
fn print_trades(bag: &mut HashSet<Asset>, line_opt: &Option<String>,
lines: Vec<String>) {
if let Some(line) = line_opt {
let (mut new_bag, _) = match read_csv_swap(line) {
Ok(trade) => { swap_d(bag, trade, true) },
Err(msg) => { println!("ERROR: {}", msg); (bag.clone(), mk_usd(0.0)) }
};
cont(&mut new_bag, lines);
}
}