Advertisement
nathanweetman

Rust Book Collections Exercise 3

Feb 24th, 2023
1,414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.63 KB | None | 0 0
  1. use std::io;
  2. use std::io::Write;
  3. use std::collections::HashMap;
  4.  
  5. struct Employee {
  6.     name: String,
  7.     department: String,
  8. }
  9.  
  10. enum Operation {
  11.     Add(Employee),
  12.     List(String),
  13.     ListAll,
  14. }
  15.  
  16. fn main() {
  17.  
  18.     let mut departments = HashMap::new();
  19.  
  20.     loop {
  21.         let mut line = String::new();
  22.         print!("> ");
  23.         if let Err(_) = io::stdout().lock().flush() {
  24.             println!("Could not flush stdout.");
  25.             std::process::exit(-1);
  26.         }
  27.         io::stdin().read_line(&mut line).expect("Failed to read line");
  28.         if line.len() == 0 || line.trim().to_lowercase() == "quit" {
  29.             break;
  30.         }
  31.  
  32.         match parse_cmd(line) {
  33.             Ok(Operation::Add(employee)) => {
  34.                 let employees = departments.entry(employee.department.clone()).or_insert(Vec::new());
  35.                 if employees.contains(&employee.name) {
  36.                     println!("Employee {} already exists in {}", employee.name, employee.department);
  37.                     break;
  38.                 } else {
  39.                     employees.push(employee.name.clone());
  40.                     println!("Added {} to {}.", employee.name, employee.department);
  41.                 }
  42.             }
  43.             Ok(Operation::List(department)) => {
  44.                 if let Some(employees) = departments.get(&department) {
  45.                     for (i, employee) in employees.iter().enumerate() {
  46.                         println!("{}: {}", i, employee);
  47.                     }
  48.                 } else {
  49.                     println!("Could not find department {}", department);
  50.                 }
  51.             }
  52.             Ok(Operation::ListAll) => {
  53.                 for (k, v) in departments.iter() {
  54.                     println!("department {}:", k);
  55.                     for (i, employee) in v.iter().enumerate() {
  56.                         println!("{}: {}", i, employee);
  57.                     }
  58.                     println!();
  59.                 }
  60.             }
  61.             Err(msg) => {
  62.                 println!("Parse Error: {}", msg);
  63.             },
  64.         }
  65.     }
  66. }
  67.  
  68. fn parse_cmd(cmd: String) -> Result<Operation, String> {
  69.     let cmd = cmd.to_lowercase();
  70.     let mut tokens = cmd.split_whitespace();
  71.     match tokens.next() {
  72.         Some("add") => {
  73.             // Add command
  74.             if tokens.clone().count() < 3 {
  75.                 return Err(String::from("Invalid add command."));
  76.             }
  77.             let employee_name;
  78.             let department;
  79.             // Employee name
  80.             employee_name = tokens.next().expect("Getting next token failed").to_string();
  81.             if let Some(to) = tokens.next() {
  82.                 if to != "to" {
  83.                     return Err(String::from("expected keyword \"to\""));
  84.                 }
  85.             }
  86.             // department name
  87.             department = tokens.next().expect("Getting next token failed").to_string();
  88.             Ok(Operation::Add(Employee {
  89.                 name: employee_name,
  90.                 department: department,
  91.             }))
  92.         },
  93.         Some("list") => {
  94.             // List command
  95.             if tokens.clone().count() < 1 {
  96.                 return Err(String::from("List what?"));
  97.             }
  98.             let arg = tokens.next().expect("Getting next token failed").to_string();
  99.             if arg == "all" {
  100.                 Ok(Operation::ListAll)
  101.             } else {
  102.                 Ok(Operation::List(arg))
  103.             }
  104.         },
  105.         Some(s) => {
  106.             Err(format!("\"{}\" is an invalid command.", s))
  107.         },
  108.         None => Err(String::from("No command given")),
  109.     }
  110. }
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement