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
use chrono::{Date, FixedOffset};
use serde::{Deserialize, Serialize};
use super::*;
pub type Services = Vec<Service>;
pub type Service = Resource<ServiceAttributes>;
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
pub struct ServiceAttributes {
pub valid_days: Vec<Day>,
#[serde(with = "mbta_date_format")]
pub start_date: Date<FixedOffset>,
pub schedule_typicality: ScheduleTypicality,
pub schedule_type: Option<String>,
pub schedule_name: Option<String>,
pub removed_dates_notes: Vec<Option<String>>,
#[serde(with = "vec_mbta_date_format")]
pub removed_dates: Vec<Date<FixedOffset>>,
#[serde(with = "optional_mbta_date_format")]
pub rating_start_date: Option<Date<FixedOffset>>,
#[serde(with = "optional_mbta_date_format")]
pub rating_end_date: Option<Date<FixedOffset>>,
pub rating_description: Option<String>,
#[serde(with = "mbta_date_format")]
pub end_date: Date<FixedOffset>,
pub description: Option<String>,
pub added_dates_notes: Vec<Option<String>>,
#[serde(with = "vec_mbta_date_format")]
pub added_dates: Vec<Date<FixedOffset>>,
}
#[derive(Debug, PartialEq, Clone, Copy, Deserialize, Serialize)]
#[serde(try_from = "u8")]
#[serde(into = "u8")]
pub enum ScheduleTypicality {
Undefined,
Typical,
Extra,
Reduced,
Disrupted,
Atypical,
}
impl TryFrom<u8> for ScheduleTypicality {
type Error = String;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::Undefined),
1 => Ok(Self::Typical),
2 => Ok(Self::Extra),
3 => Ok(Self::Reduced),
4 => Ok(Self::Disrupted),
5 => Ok(Self::Atypical),
_ => Err(format!("invalid schedule typicality value: {}", value)),
}
}
}
impl From<ScheduleTypicality> for u8 {
fn from(value: ScheduleTypicality) -> Self {
match value {
ScheduleTypicality::Undefined => 0,
ScheduleTypicality::Typical => 1,
ScheduleTypicality::Extra => 2,
ScheduleTypicality::Reduced => 3,
ScheduleTypicality::Disrupted => 4,
ScheduleTypicality::Atypical => 5,
}
}
}
#[derive(Debug, PartialEq, Clone, Copy, Deserialize, Serialize)]
#[serde(try_from = "u8")]
#[serde(into = "u8")]
pub enum Day {
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday,
}
impl TryFrom<u8> for Day {
type Error = String;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
1 => Ok(Self::Monday),
2 => Ok(Self::Tuesday),
3 => Ok(Self::Wednesday),
4 => Ok(Self::Thursday),
5 => Ok(Self::Friday),
6 => Ok(Self::Saturday),
7 => Ok(Self::Sunday),
_ => Err(format!("invalid day value: {}", value)),
}
}
}
impl From<Day> for u8 {
fn from(value: Day) -> Self {
match value {
Day::Monday => 1,
Day::Tuesday => 2,
Day::Wednesday => 3,
Day::Thursday => 4,
Day::Friday => 5,
Day::Saturday => 6,
Day::Sunday => 7,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::*;
#[rstest]
#[case::zero(0, Ok(ScheduleTypicality::Undefined))]
#[case::one(1, Ok(ScheduleTypicality::Typical))]
#[case::two(2, Ok(ScheduleTypicality::Extra))]
#[case::three(3, Ok(ScheduleTypicality::Reduced))]
#[case::four(4, Ok(ScheduleTypicality::Disrupted))]
#[case::five(5, Ok(ScheduleTypicality::Atypical))]
#[case::invalid(6, Err("invalid schedule typicality value: 6".into()))]
fn test_schedule_typicality_try_from_u8(#[case] input: u8, #[case] expected: Result<ScheduleTypicality, String>) {
assert_eq!(ScheduleTypicality::try_from(input), expected);
}
#[rstest]
#[case::undefined(ScheduleTypicality::Undefined, 0)]
#[case::typical(ScheduleTypicality::Typical, 1)]
#[case::extra(ScheduleTypicality::Extra, 2)]
#[case::reduced(ScheduleTypicality::Reduced, 3)]
#[case::disrupted(ScheduleTypicality::Disrupted, 4)]
#[case::atypical(ScheduleTypicality::Atypical, 5)]
fn test_u8_from_schedule_typicality(#[case] input: ScheduleTypicality, #[case] expected: u8) {
assert_eq!(u8::from(input), expected);
}
#[rstest]
#[case::one(1, Ok(Day::Monday))]
#[case::two(2, Ok(Day::Tuesday))]
#[case::three(3, Ok(Day::Wednesday))]
#[case::four(4, Ok(Day::Thursday))]
#[case::five(5, Ok(Day::Friday))]
#[case::six(6, Ok(Day::Saturday))]
#[case::seven(7, Ok(Day::Sunday))]
#[case::invalid(8, Err("invalid day value: 8".into()))]
fn test_day_try_from_u8(#[case] input: u8, #[case] expected: Result<Day, String>) {
assert_eq!(Day::try_from(input), expected);
}
#[rstest]
#[case::monday(Day::Monday, 1)]
#[case::tuesday(Day::Tuesday, 2)]
#[case::wednesday(Day::Wednesday, 3)]
#[case::thursday(Day::Thursday, 4)]
#[case::friday(Day::Friday, 5)]
#[case::saturday(Day::Saturday, 6)]
#[case::sunday(Day::Sunday, 7)]
fn test_u8_from_day(#[case] input: Day, #[case] expected: u8) {
assert_eq!(u8::from(input), expected);
}
}