Exercise: Elevator Events
We will create a data structure to represent an event in an elevator control system. It is up to you to define the types and functions to construct various events. Use #[derive(Debug)] to allow the types to be formatted with {:?}.
This exercise only requires creating and populating data structures so that main runs without errors. The next part of the course will cover getting data out of these structures.
1. ```
#[derive(Debug)]
```
2. ```
/// An event in the elevator system that the controller must react to.
```
3. ```
enum Event {
```
4. ```
// TODO: add required variants
```
5. ```
}
```
7. ```
/// A direction of travel.
```
8. ```
#[derive(Debug)]
```
9. ```
enum Direction {
```
10. ```
Up,
```
11. ```
Down,
```
12. ```
}
```
14. ```
/// The car has arrived on the given floor.
```
15. ```
fn car_arrived(floor: i32) -> Event {
```
16. ```
todo!()
```
17. ```
}
```
19. ```
/// The car doors have opened.
```
20. ```
fn car_door_opened() -> Event {
```
21. ```
todo!()
```
22. ```
}
```
24. ```
/// The car doors have closed.
```
25. ```
fn car_door_closed() -> Event {
```
26. ```
todo!()
```
27. ```
}
```
29. ```
/// A directional button was pressed in an elevator lobby on the given floor.
```
30. ```
fn lobby_call_button_pressed(floor: i32, dir: Direction) -> Event {
```
31. ```
todo!()
```
32. ```
}
```
34. ```
/// A floor button was pressed in the elevator car.
```
35. ```
fn car_floor_button_pressed(floor: i32) -> Event {
```
36. ```
todo!()
```
37. ```
}
```
39. ```
fn main() {
```
40. ```
println!(
```
41. ```
"A ground floor passenger has pressed the up button: {:?}",
```
42. ```
lobby_call_button_pressed(0, Direction::Up)
```
43. ```
);
```
44. ```
println!("The car has arrived on the ground floor: {:?}", car_arrived(0));
```
45. ```
println!("The car door opened: {:?}", car_door_opened());
```
46. ```
println!(
```
47. ```
"A passenger has pressed the 3rd floor button: {:?}",
```
48. ```
car_floor_button_pressed(3)
```
49. ```
);
```
50. ```
println!("The car door closed: {:?}", car_door_closed());
```
51. ```
println!("The car has arrived on the 3rd floor: {:?}", car_arrived(3));
```
52. ```
}
```
