elevator_core/components/
route.rs1use serde::{Deserialize, Serialize};
4
5use crate::entity::EntityId;
6use crate::ids::GroupId;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
10#[non_exhaustive]
11pub enum TransportMode {
12 #[serde(alias = "Elevator")]
14 Group(GroupId),
15 Line(EntityId),
17 Walk,
19}
20
21#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct RouteLeg {
24 pub from: EntityId,
26 pub to: EntityId,
28 pub via: TransportMode,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
34pub struct Route {
35 pub legs: Vec<RouteLeg>,
37 pub current_leg: usize,
39}
40
41impl Route {
42 #[must_use]
44 pub fn direct(from: EntityId, to: EntityId, group: GroupId) -> Self {
45 Self {
46 legs: vec![RouteLeg {
47 from,
48 to,
49 via: TransportMode::Group(group),
50 }],
51 current_leg: 0,
52 }
53 }
54
55 #[must_use]
57 pub fn current(&self) -> Option<&RouteLeg> {
58 self.legs.get(self.current_leg)
59 }
60
61 pub const fn advance(&mut self) -> bool {
63 self.current_leg += 1;
64 self.current_leg < self.legs.len()
65 }
66
67 #[must_use]
69 pub const fn is_complete(&self) -> bool {
70 self.current_leg >= self.legs.len()
71 }
72
73 #[must_use]
75 pub fn current_destination(&self) -> Option<EntityId> {
76 self.current().map(|leg| leg.to)
77 }
78
79 #[must_use]
81 pub fn final_destination(&self) -> Option<EntityId> {
82 self.legs.last().map(|leg| leg.to)
83 }
84}