departments 0.1.1

Departments stat
Documentation
use simply_colored::*;
use std::collections::HashMap;
// use std::fmt;
use text_io;

mod employees;

/// Using a hash map and vectors, create a text interface to allow a user to add employee names to a department
/// in a company; for example, “Add Sally to Engineering” or “Add Amir to Sales.” Then let the user retrieve a
/// list of all people in a department or all people in the company by department, sorted alphabetically.
fn main() {
    // let mut input_buff = String::new();
    // let mut deps_employees: HashMap<String, Vec<String>> = HashMap::new();
    let mut deps_employees: HashMap<String, employees::Employees> = HashMap::new();
    loop {
        let mut input_buff = String::new();
        println!("{BG_BLUE}Drop a line:{BG_DIM_BLACK}");
        if departments::get_line(&mut input_buff).is_err() || input_buff.trim().is_empty() {
            break;
        }

        let dep = parse_department(&input_buff).unwrap();
        let employee = parse_employee(&input_buff).unwrap();

        deps_employees
            .entry(dep)
            .and_modify(|v: &mut employees::Employees| v.0.push(employee.clone()))
            .or_insert(employees::Employees(vec![employee]));
    }

    println!(
        "Press Enter to show all people in the company by department
      or enter department name to show the department's employees"
    );

    loop {
        let prompt_reply: String = text_io::read!("{}\n");
        let prompt_reply = prompt_reply.trim();

        if prompt_reply.is_empty() {
            print_deps_employees(&deps_employees);
            break;
        }

        if let Some(employees) = deps_employees.get(prompt_reply) {
            println!("Employees in '{}': {}", prompt_reply, employees);
        } else {
            println!("Not found!");
        }
    }
}

fn parse_department(input_buff: &str) -> Option<String> {
    match input_buff.split_whitespace().nth(3) {
        Some(dep_name) => Some(dep_name.to_owned()),
        None => None,
    }
}

fn parse_employee(input_buff: &str) -> Option<String> {
    match input_buff.split_whitespace().nth(1) {
        Some(dep_name) => Some(dep_name.to_owned()),
        None => None,
    }
}

fn print_deps_employees(deps_employees: &HashMap<String, employees::Employees>) {
    for (dep, employees) in deps_employees.iter() {
        println!("Dep. `{}`: {}", dep, employees);
    }
}