diff --git a/elevator/.gitignore b/elevator/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/elevator/.gitignore @@ -0,0 +1 @@ +/target diff --git a/elevator/Cargo.lock b/elevator/Cargo.lock new file mode 100644 index 0000000..18872f4 --- /dev/null +++ b/elevator/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "elevator" +version = "0.1.0" diff --git a/elevator/Cargo.toml b/elevator/Cargo.toml new file mode 100644 index 0000000..f7735f5 --- /dev/null +++ b/elevator/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "elevator" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/elevator/src/main.rs b/elevator/src/main.rs new file mode 100644 index 0000000..25c6f04 --- /dev/null +++ b/elevator/src/main.rs @@ -0,0 +1,59 @@ +#[derive(Debug)] +/// An event in the elevator system that the controller must react to. +enum Event { + Arrived(i32), + Called(i32, Direction), + Sent(i32), + DoorOpened, + DoorClosed, +} + +/// A direction of travel. +#[derive(Debug)] +enum Direction { + Up, + Down, +} + +/// The car has arrived on the given floor. +fn car_arrived(floor: i32) -> Event { + Event::Arrived(floor) +} + +/// The car doors have opened. +fn car_door_opened() -> Event { + Event::DoorOpened +} + +/// The car doors have closed. +fn car_door_closed() -> Event { + Event::DoorClosed +} + +/// A directional button was pressed in an elevator lobby on the given floor. +fn lobby_call_button_pressed(floor: i32, dir: Direction) -> Event { + Event::Called(floor, dir) +} + +/// A floor button was pressed in the elevator car. +fn car_floor_button_pressed(floor: i32) -> Event { + Event::Sent(floor) +} + +fn main() { + println!( + "A ground floor passenger has pressed the up button: {:?}", + lobby_call_button_pressed(0, Direction::Up) + ); + println!( + "The car has arrived on the ground floor: {:?}", + car_arrived(0) + ); + println!("The car door opened: {:?}", car_door_opened()); + println!( + "A passenger has pressed the 3rd floor button: {:?}", + car_floor_button_pressed(3) + ); + println!("The car door closed: {:?}", car_door_closed()); + println!("The car has arrived on the 3rd floor: {:?}", car_arrived(3)); +}