summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--employees/.gitignore2
-rw-r--r--employees/Cargo.lock4
-rw-r--r--employees/Cargo.toml7
-rw-r--r--employees/src/main.rs181
4 files changed, 194 insertions, 0 deletions
diff --git a/employees/.gitignore b/employees/.gitignore
new file mode 100644
index 0000000..53eaa21
--- /dev/null
+++ b/employees/.gitignore
@@ -0,0 +1,2 @@
+/target
+**/*.rs.bk
diff --git a/employees/Cargo.lock b/employees/Cargo.lock
new file mode 100644
index 0000000..a6d8fc5
--- /dev/null
+++ b/employees/Cargo.lock
@@ -0,0 +1,4 @@
+[[package]]
+name = "employees"
+version = "0.1.0"
+
diff --git a/employees/Cargo.toml b/employees/Cargo.toml
new file mode 100644
index 0000000..016ef3b
--- /dev/null
+++ b/employees/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+name = "employees"
+version = "0.1.0"
+authors = ["Adam Carpenter <adam.carpenter@adp.com>"]
+edition = "2018"
+
+[dependencies]
diff --git a/employees/src/main.rs b/employees/src/main.rs
new file mode 100644
index 0000000..0900bed
--- /dev/null
+++ b/employees/src/main.rs
@@ -0,0 +1,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."),
+ }
+
+ }
+
+}