1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use super::{GeoLocation, Movable, MultipleTargets, NextTarget, RailwayObject};
use crate::types::{NodeId, RailwayObjectId};
use geo::Coord;
use std::any::Any;
use std::collections::VecDeque;
use uom::si::f64::{Acceleration, Velocity};

/// A Train struct representing a train in the railway system.
#[derive(Default, Debug, Clone, PartialEq)]
pub struct Train {
    /// The unique identifier for the train.
    pub id: i64,
    /// The current position of the train, represented by a node ID.
    pub position: Option<NodeId>,
    /// The geographical location of the train, represented by a coordinate.
    pub geo_location: Option<Coord<f64>>,
    /// The next target node ID for the train to move towards.
    pub next_target: Option<NodeId>,
    /// A queue of target node IDs for the train to follow.
    pub targets: VecDeque<NodeId>,
    /// The current speed of the train
    pub speed: Velocity,
    /// The current speed of the train
    pub max_speed: Velocity,
    /// The current acceleration of the train
    pub acceleration: Acceleration,
}

/// Implements the `RailwayObject` trait for the `Train` struct.
impl RailwayObject for Train {
    fn id(&self) -> RailwayObjectId {
        self.id
    }

    fn position(&self) -> Option<NodeId> {
        self.position
    }

    fn set_position(&mut self, position: Option<NodeId>) {
        self.position = position;
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// Implements the `NextTarget` trait for the `Train` struct.
impl NextTarget for Train {
    fn next_target(&self) -> Option<NodeId> {
        self.next_target
    }

    fn set_next_target(&mut self, target: Option<NodeId>) {
        self.next_target = target;
    }
}

/// Implements the `MultipleTargets` trait for the `Train` struct.
impl MultipleTargets for Train {
    fn targets(&self) -> &VecDeque<NodeId> {
        &self.targets
    }

    fn add_target(&mut self, target: NodeId) {
        self.targets.push_back(target);
    }

    fn remove_target(&mut self) -> Option<NodeId> {
        self.targets.pop_front()
    }
}

/// Implements the `GeoLocation` trait for the `Train` struct.
impl GeoLocation for Train {
    fn geo_location(&self) -> Option<Coord<f64>> {
        self.geo_location
    }

    fn set_geo_location(&mut self, location: Option<Coord<f64>>) {
        self.geo_location = location;
    }
}

impl Movable for Train {
    fn max_speed(&self) -> Velocity {
        self.max_speed
    }

    fn set_max_speed(&mut self, max_speed: Velocity) {
        self.max_speed = max_speed;
    }

    fn speed(&self) -> Velocity {
        self.speed
    }

    fn set_speed(&mut self, speed: Velocity) {
        self.speed = speed;
    }

    fn acceleration(&self) -> Acceleration {
        self.acceleration
    }

    fn set_acceleration(&mut self, acceleration: Acceleration) {
        self.acceleration = acceleration;
    }
}

#[cfg(test)]
mod tests {
    use geo::coord;
    use uom::si::{acceleration::meter_per_second_squared, velocity::kilometer_per_hour};

    use super::*;

    #[test]
    fn test_train() {
        let mut train = Train {
            id: 1,
            position: Some(1),
            geo_location: Some(coord! { x:1.0, y: 2.0}),
            next_target: Some(2),
            targets: VecDeque::from(vec![2, 3, 4]),
            ..Default::default()
        };

        assert_eq!(train.id(), 1);
        assert_eq!(train.position(), Some(1));
        assert_eq!(train.geo_location(), Some(coord! {x:1.0, y:2.0}));
        assert_eq!(train.next_target(), Some(2));
        assert_eq!(train.targets(), &VecDeque::from(vec![2, 3, 4]));

        train.set_next_target(None);
        assert_eq!(train.next_target(), None);

        train.add_target(5);
        assert_eq!(train.targets(), &VecDeque::from(vec![2, 3, 4, 5]));

        let removed_target = train.remove_target();
        assert_eq!(removed_target, Some(2));
        assert_eq!(train.targets(), &VecDeque::from(vec![3, 4, 5]));
    }

    #[test]
    fn test_train_speed() {
        let mut train = Train {
            id: 1,
            position: Some(0),
            geo_location: Some(coord! { x:1.0, y: 2.0}),
            next_target: Some(2),
            targets: VecDeque::from(vec![2, 3, 4]),
            max_speed: Velocity::new::<kilometer_per_hour>(80.0),
            speed: Velocity::new::<kilometer_per_hour>(0.0),
            acceleration: Acceleration::new::<meter_per_second_squared>(0.0),
        };

        assert_eq!(train.speed(), Velocity::new::<kilometer_per_hour>(0.0));

        train.set_speed(Velocity::new::<kilometer_per_hour>(100.0));
        assert_eq!(train.speed(), Velocity::new::<kilometer_per_hour>(100.0));
    }

    #[test]
    fn test_train_acceleration() {
        let mut train = Train {
            id: 1,
            position: Some(0),
            geo_location: Some(coord! { x:1.0, y: 2.0}),
            next_target: Some(2),
            targets: VecDeque::from(vec![2, 3, 4]),
            max_speed: Velocity::new::<kilometer_per_hour>(80.0),
            speed: Velocity::new::<kilometer_per_hour>(0.0),
            acceleration: Acceleration::new::<meter_per_second_squared>(0.0),
        };

        assert_eq!(
            train.acceleration(),
            Acceleration::new::<meter_per_second_squared>(0.0)
        );

        train.set_acceleration(Acceleration::new::<meter_per_second_squared>(1.5));
        assert_eq!(
            train.acceleration(),
            Acceleration::new::<meter_per_second_squared>(1.5)
        );
    }
}