Solution

  1. #[derive(Debug)]
  2. /// An event in the elevator system that the controller must react to.
  3. enum Event {
  4.     /// A button was pressed.
  5.     ButtonPressed(Button),
  6.     /// The car has arrived at the given floor.
  7.     CarArrived(Floor),
  8.     /// The car's doors have opened.
  9.     CarDoorOpened,
  10.     /// The car's doors have closed.
  11.     CarDoorClosed,
  12. }
  13. /// A floor is represented as an integer.
  14. type Floor = i32;
  15. /// A direction of travel.
  16. #[derive(Debug)]
  17. enum Direction {
  18.     Up,
  19.     Down,
  20. }
  21. /// A user-accessible button.
  22. #[derive(Debug)]
  23. enum Button {
  24.     /// A button in the elevator lobby on the given floor.
  25.     LobbyCall(Direction, Floor),
  26.     /// A floor button within the car.
  27.     CarFloor(Floor),
  28. }
  29. /// The car has arrived on the given floor.
  30. fn car_arrived(floor: i32) -> Event {
  31.     Event::CarArrived(floor)
  32. }
  33. /// The car doors have opened.
  34. fn car_door_opened() -> Event {
  35.     Event::CarDoorOpened
  36. }
  37. /// The car doors have closed.
  38. fn car_door_closed() -> Event {
  39.     Event::CarDoorClosed
  40. }
  41. /// A directional button was pressed in an elevator lobby on the given floor.
  42. fn lobby_call_button_pressed(floor: i32, dir: Direction) -> Event {
  43.     Event::ButtonPressed(Button::LobbyCall(dir, floor))
  44. }
  45. /// A floor button was pressed in the elevator car.
  46. fn car_floor_button_pressed(floor: i32) -> Event {
  47.     Event::ButtonPressed(Button::CarFloor(floor))
  48. }
  49. fn main() {
  50.     println!(
  51.         "A ground floor passenger has pressed the up button: {:?}",
  52.         lobby_call_button_pressed(0, Direction::Up)
  53.     );
  54.     println!("The car has arrived on the ground floor: {:?}", car_arrived(0));
  55.     println!("The car door opened: {:?}", car_door_opened());
  56.     println!(
  57.         "A passenger has pressed the 3rd floor button: {:?}",
  58.         car_floor_button_pressed(3)
  59.     );
  60.     println!("The car door closed: {:?}", car_door_closed());
  61.     println!("The car has arrived on the 3rd floor: {:?}", car_arrived(3));
  62. }