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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
use std::collections::HashMap;
use std::process;
use std::io::{self, Write};
struct Employees {
departments: HashMap<String, Vec<String>>,
}
impl Employees {
fn add_employee(&self, name: &Option<&String>, department: &Option<&String>) -> Result<(), &'static str> {
// parse name
let name = match name {
Some(s) => s,
None => {
return Err("Invalid or empty name.");
},
};
// parse department
let dept = match department {
Some(s) => s,
None => {
return Err("Invalid or empty department.");
},
};
// add name to department in departments
// and create department if it doesn't exist
employees.entry(dept.to_string())
.or_insert(Vec::new())
.push(name.to_string());
Ok(())
}
}
//fn list_employees() {
// let dept = match actions.get(1) {
// Some(s) => s,
// None => {
// ""
// }
// };
//
// if dept == "" {
// // print employees in all depts
// for each in employees.keys() {
// println!("{}", each);
// match employees.get(&each.to_string()) {
// Some(v) => {
// for every in v {
// println!(" {}", every);
// }
// },
// None => continue,
// };
// }
// }
// else {
// // print employees in single dept
// match employees.get(&dept.to_string()) {
// Some(v) => {
// for every in v {
// println!("{}", every);
// };
// },
// None => {
// eprintln!("List: Invalid department.");
// continue;
// }
// };
// }
//}
//
//fn list_employee(name) {
//}
//fn remove_employee(name) {
// let name = match actions.get(1) {
// Some(s) => s,
// None => {
// eprintln!("Remove: Invalid or empty name.");
// continue;
// },
// };
//
// let dept = match actions.get(2) {
// Some(s) => s,
// None => {
// eprintln!("Remove: Empty department.");
// continue;
// },
// };
//
// match employees.get_mut(&dept.to_string()) {
// Some(v) => {
// v.retain(|employee| employee != name);
// },
// None => {
// eprintln!("Remove: Invalid department.");
// continue;
// }
// };
//}
fn get_actions() -> Vec<String> {
print!("> ");
io::stdout().flush().unwrap();
// grab input
let mut input = String::new();
io::stdin().read_line(&mut input)
.expect("Failed to read input.");
// finish on eof
if input == "" {
println!("Bye.");
process::exit(0);
}
// collect all CLI arguments into vector of trimmed Strings
input.trim()
.split_whitespace()
.map(|action| action.to_string())
.collect()
}
fn main() {
println!("Welcome to employee manager.");
println!("Enter 'help' for help or Ctrl+D to exit.");
let mut departments: HashMap<String, Vec<String>> = HashMap::new();
// operating loop
loop {
let actions = get_actions();
// act on actions
let command = match actions.get(0) {
Some(c) => c,
None => {
eprintln!("Empty command. Enter Ctrl+D to exit.");
continue;
}
};
match command.as_str() {
// add employee to department
"add" => {
match add_employee(&actions.get(1), &actions.get(2), &mut departments) {
Ok(()) => {dbg!(&departments);},
Err(e) => eprintln!("{}", e),
};
},
// list employees from one or all departments
"list" => {
},
// remove employee from department
"remove" => {
},
// print help
"help" => {
println!("Valid commands include:");
println!("\tlist [department]");
println!("\tadd <name> <department>");
println!("\tremove <name> <department>");
println!("Enter 'help' for help or Ctrl+D to exit.");
},
// all else fails
_ => eprintln!("Invalid command."),
}
}
}
|