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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
use std::{
env,
error,
fs::{File},
io::BufReader,
collections::HashMap,
};
use itertools::Itertools;
mod record;
mod drawing;
use record::Record;
pub fn run() -> Result<(), Box<dyn error::Error>> {
let records = load_contents()?;
drawing::ui(drawing::Category::Clear);
loop {
match drawing::ui(drawing::Category::Menu) {
'a' | 'A' => print_summary_by_project(&records),
'b' | 'B' => print_summary_by_time(&records),
'q' | 'Q' => {
drawing::ui(drawing::Category::Title("quit program"));
break;
},
_ => {
drawing::ui(drawing::Category::Wrong("Unsupported Option, please try again.\n"));
continue;
},
}
}
Ok(())
}
fn print_summary_by_time(records: &Vec<Record>) {
println!("placeholder");
}
fn print_summary_by_project(records: &Vec<Record>) {
let mut expenses: HashMap<String, HashMap<String, f64>> = HashMap::new();
let mut sum = HashMap::new();
/* group the project with separate currency */
for r in records.iter() {
/* exclude transfer and other type */
match r.record_type() {
"Expense" | "Fee" | "Refund" | "Income" => {
/* No project name will be ignore */
if let Some(project) = r.project() {
if project.is_empty() {
continue;
}
/* group projects with separate currency */
expenses
.entry(project)
.and_modify(|expense: &mut HashMap<String, f64>| {
expense
.entry(r.currency())
.and_modify(|v| *v += r.cal())
.or_insert(r.cal());
})
.or_insert(HashMap::from([(r.currency(), r.cal())]));
/* sum same project and currency amount */
let element = sum.entry(r.currency()).or_insert(0.0 as f64);
*element += r.cal();
}
},
_ => continue,
}
}
// print output
println!("print summary by project:");
for project in expenses.keys().sorted() {
println!("\n\t{project}");
print!("\t\t\t|");
for currency in expenses[project].keys().sorted() {
let expense = expenses[project][currency];
print!("\t{currency} {:.2}\t|", expense);
}
println!("");
}
println!("\n\n\tSummary");
print!("\t\t\t|");
for currency in sum.keys().sorted() {
print!("\t{currency} {:.2}\t|", sum[currency]);
}
println!("");
}
fn load_contents() -> Result<Vec<Record>, Box<dyn error::Error>> {
let args = env::args().skip(1).collect::<Vec<String>>();
let csv_file = check_csv(&args)?;
let mut reader = build_reader(csv_file)?;
let mut records = Vec::new();
for r in reader.records() {
let r = r.unwrap();
records.push(Record::new(r));
}
Ok(records)
}
fn build_reader(file_name: String)
-> Result<csv::Reader<BufReader<File>>, Box<dyn error::Error>>
{
let file = File::open(file_name)?;
let buf = BufReader::new(file);
let ret = csv::ReaderBuilder::new()
.flexible(true)
.from_reader(buf);
Ok(ret)
}
fn check_csv(args: &Vec<String>) -> Result<String, Box<dyn error::Error>> {
let n = args.len();
if n != 1 {
return Err(Box::from("Only 1 argument required\n\nUsage: moze_analyzer [csv_file]"));
}
if args[0].contains(".csv") {
Ok(args[0].clone())
} else {
Err(Box::from("Not csv file"))
}
}
|