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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//! Train structs and data structures
use serde::{Deserialize, Serialize};

pub mod prelude {
    pub use crate::train::train_service_alert::{
        AffectedSegment, MrtLine, TrainServiceAlert, TrainServiceAlertMessage,
        TrainServiceAlertResp, TrainStatus,
    };
    pub use crate::train::StationCode;
}

/// List obtained from [Wikipedia](https://en.wikipedia.org/wiki/List_of_Singapore_MRT_stations)
/// and [Wikipedia](https://en.wikipedia.org/wiki/List_of_Singapore_LRT_stations)
/// Some of the stations are commented out to prevent misuse as they are technically
/// not constructed yet or it has not been announced
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Default)]
pub enum StationCode {
    NS1,
    NS2,
    NS3,
    NS4,
    NS5,
    NS6,
    NS7,
    NS8,
    NS9,
    NS10,
    NS11,
    NS12,
    NS13,
    NS14,
    NS15,
    NS16,
    NS17,
    NS18,
    NS19,
    NS20,
    NS21,
    NS22,
    NS23,
    NS24,
    NS25,
    NS26,
    NS27,
    NS28,
    EW1,
    EW2,
    EW3,
    EW4,
    EW5,
    EW6,
    EW7,
    EW8,
    EW9,
    EW10,
    EW11,
    EW12,
    EW13,
    EW14,
    EW15,
    EW16,
    EW17,
    EW18,
    EW19,
    EW20,
    EW21,
    EW22,
    EW23,
    EW24,
    EW25,
    EW26,
    EW27,
    EW28,
    EW29,
    EW30,
    EW31,
    EW32,
    EW33,
    CG,
    CG1,
    CG2,
    NE1,
    NE2, // reserved for possible future MRT station
    NE3,
    NE4,
    NE5,
    NE6,
    NE7,
    NE8,
    NE9,
    NE10,
    NE11,
    NE12,
    NE13,
    NE14,
    NE15,
    NE16,
    NE17,
    CC1,
    CC2,
    CC3,
    CC4,
    CC5,
    CC6,
    CC7,
    CC8,
    CC9,
    CC10,
    CC11,
    CC12,
    CC13,
    CC14,
    CC15,
    CC16,
    CC17,
    CC18, // bukit brown
    CC19,
    CC20,
    CC21,
    CC22,
    CC23,
    CC24,
    CC25,
    CC26,
    CC27,
    CC28,
    CC29,
    CC30, //  koppel
    CC31, //  cantonment
    CC32, //  prince edward road
    CC33,
    CC34,
    CE1,
    CE2,
    DT1,
    DT2,
    DT3,
    DT4, // hume
    DT5,
    DT6,
    DT7,
    DT8,
    DT9,
    DT10,
    DT11,
    DT12,
    DT13,
    DT14,
    DT15,
    DT16,
    DT17,
    DT18,
    DT19,
    DT20,
    DT21,
    DT22,
    DT23,
    DT24,
    DT25,
    DT26,
    DT27,
    DT28,
    DT29,
    DT30,
    DT31,
    DT32,
    DT33,
    DT34,
    DT35,
    DT36, // Xilin
    DT37, // Sungei Bedok
    TE1,
    TE2,
    TE3,
    TE4,   //  Springleaf
    TE5,   //  Lentor
    TE6,   //  Mayflower
    TE7,   //  Bright Hill
    TE8,   //  Upper Thomson
    TE9,   //  Caldecott
    TE10,  //  Mount Pleasant
    TE11,  //  Stevens
    TE12,  //  Napier
    TE13,  //  Orchard Boulevard
    TE14,  //  Orchard
    TE15,  //  Great World
    TE16,  //  Havelock
    TE17,  //  Outram Park
    TE18,  //  Maxwell
    TE19,  //  Shenton Way
    TE20,  //  Marina Bay
    TE21,  //  Marina South
    TE22,  //  Gardens By The Bay
    TE22A, //  Founders' Memorial
    TE23,  //  Tanjong Rhu
    TE24,  //  Katong Park
    TE25,  //  Tanjong Katong
    TE26,  //  Marine Parade
    TE27,  //  Marine Terrace
    TE28,  //  Siglap
    TE29,  //  Bayshore
    TE30,  //  Bedok South
    TE31,  //  Sungei Bedok
    BP1,
    BP2,
    BP3,
    BP4,
    BP5,
    BP6,
    BP7,
    BP8,
    BP9,
    BP10,
    BP11,
    BP12,
    BP13,
    BP14,
    STC,
    SE1,
    SE2,
    SE3,
    SE4,
    SE5,
    SW1,
    SW2,
    SW3,
    SW4,
    SW5,
    SW6,
    SW7,
    SW8,
    PTC,
    PE1,
    PE2,
    PE3,
    PE4,
    PE5,
    PE6,
    PE7,
    PW1,
    PW2,
    PW3,
    PW4,
    PW5,
    PW6,
    PW7,

    #[default]
    #[serde(other)]
    Unknown,
}

pub mod train_service_alert {
    use serde::{Deserialize, Serialize};
    use serde_repr::*;

    use crate::utils::de::{delimited, Sep, WrapErr};
    use std::ops::Deref;
    use std::str::FromStr;

    #[allow(clippy::upper_case_acronyms)]
    #[derive(Debug, Clone, PartialEq, Deserialize, Serialize, Default)]
    pub enum MrtLine {
        CCL,
        CEL,
        CGL,
        DTL,
        EWL,
        NEL,
        NSL,
        PEL,
        PWL,
        SEL,
        SWL,
        BPL,

        #[default]
        #[serde(other)]
        Unknown,
    }

    #[derive(Debug, Clone, PartialEq, Serialize_repr, Deserialize_repr, Default)]
    #[repr(u32)]
    pub enum TrainStatus {
        Normal = 1,
        Disrupted = 2,

        #[default]
        #[serde(other)]
        Unknown,
    }

    impl Sep for StringWrap {
        fn delimiter() -> &'static str {
            "-"
        }
    }

    impl Deref for StringWrap {
        type Target = String;

        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

    impl FromStr for StringWrap {
        type Err = WrapErr;

        fn from_str(s: &str) -> Result<Self, Self::Err> {
            Ok(StringWrap(s.to_string()))
        }
    }

    #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
    pub struct StringWrap(String);

    #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
    #[serde(rename_all = "PascalCase")]
    pub struct AffectedSegment {
        pub line: MrtLine,

        pub direction: String,

        #[serde(deserialize_with = "delimited")]
        pub stations: Vec<StringWrap>,

        pub free_public_bus: String,

        pub free_mrt_shuttle: String,

        #[serde(alias = "MRTShuttleDirection")]
        pub mrt_shuttle_dir: String,
    }

    #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
    #[serde(rename_all = "PascalCase")]
    pub struct TrainServiceAlertMessage {
        pub content: String,
        pub created_date: String,
    }

    #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
    #[serde(rename_all = "PascalCase")]
    pub struct TrainServiceAlert {
        pub status: TrainStatus,

        pub affected_segments: Vec<AffectedSegment>,

        pub message: Vec<TrainServiceAlertMessage>,
    }

    #[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
    pub struct TrainServiceAlertResp {
        pub value: TrainServiceAlert,
    }

    impl From<TrainServiceAlertResp> for TrainServiceAlert {
        fn from(data: TrainServiceAlertResp) -> Self {
            data.value
        }
    }
}