Add elevator

This commit is contained in:
Alex D. 2024-10-28 14:34:14 +00:00
parent 70fb45c9b9
commit 6a32f160b7
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
4 changed files with 73 additions and 0 deletions

1
elevator/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

7
elevator/Cargo.lock generated Normal file
View File

@ -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"

6
elevator/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "elevator"
version = "0.1.0"
edition = "2021"
[dependencies]

59
elevator/src/main.rs Normal file
View File

@ -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));
}