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
use chrono::{Datelike, Local, Months, NaiveDate, NaiveTime, NaiveWeek, Weekday};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::{deserializers::Deserializer, types::error::ScheduleParseError, utils::WeekRange};
/// Holds information for the entire schedule as it is currently known
#[derive(Debug)]
pub struct Schedule {
pub weeks: [ScheduleWeek; 53],
}
/// Contains information for a single week in the schedule
#[derive(Debug)]
pub struct ScheduleWeek {
pub week: NaiveWeek,
pub monday: ScheduleDay,
pub tuesday: ScheduleDay,
pub wednesday: ScheduleDay,
pub thursday: ScheduleDay,
pub friday: ScheduleDay,
}
/// Contains information for a single day in the schedule
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScheduleDay {
pub date: NaiveDate,
pub lessons: Vec<Lesson>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Lesson {
pub start: chrono::NaiveTime,
pub end: chrono::NaiveTime,
pub name: String,
pub room: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct RawOccasion {
weeks: u64,
excluding_weeks: u64,
cre_by_id: u64,
source: serde_json::Value,
external_ref: String,
subject_id: u64,
org_id: u64,
upd_date: String,
upd_by_type: i32,
exclude_class: u64,
start_time: String,
id: u64,
including_weeks: u64,
subject_name: String,
upd_by_id: u64,
cre_by_type: i32,
cre_date: String,
length: u64,
external_id: String,
room_name: String,
period_weeks: u64,
including_weeks_string: String,
day_id: u8,
name: String,
absence_type: u64,
guid: String,
excluding_weeks_string: String,
end_time: String,
weeks_string: String,
tmp_lesson: u64,
}
#[derive(Debug)]
pub struct Occasion {
pub id: u64,
pub uuid: Uuid,
pub start_time: NaiveTime,
pub end_time: NaiveTime,
pub name: String,
pub room_name: String,
pub week_day: Weekday,
pub weeks: Vec<u8>,
}
impl Schedule {
/// Create a new Schedule
///
/// Note that weeks are shifted down by one, i.e the first week of the year (week 1) will have
/// the index 0
///
/// If the start date is at the first half of the year, it means we are in the end of the school year.
/// And the later half of the weeks will be from last year.
///
/// If the start date is at the second half of the year, it means we are in the beginning of the school year.
/// And the first half of the weeks will be from next year.
///
/// # Arguments
///
/// * `start` - The date to use as origin when creating the schedule
///
/// # Returns
///
/// A new Schedule
///
/// # Examples
///
/// Example with the start date in the first half of the year
///
/// ```
/// # use chrono::{NaiveDate, Local, Datelike};
/// # use schoolsoft::schedule::Schedule;
/// let april = NaiveDate::from_ymd(2024, 4, 1);
/// let schedule = Schedule::from(april);
///
/// assert_eq!(schedule.weeks.len(), 53);
/// assert_eq!(schedule.weeks[10].monday.date.year_ce(), (true, 2024));
/// assert_eq!(schedule.weeks[40].monday.date.year_ce(), (true, 2023));
/// ```
///
/// Example with the start date in the second half of the year
///
/// ```
/// # use chrono::{NaiveDate, Local, Datelike};
/// # use schoolsoft::schedule::Schedule;
/// let september = NaiveDate::from_ymd(2024, 9, 1);
/// let schedule = Schedule::from(september);
///
/// assert_eq!(schedule.weeks.len(), 53);
/// assert_eq!(schedule.weeks[10].monday.date.year_ce(), (true, 2025));
/// assert_eq!(schedule.weeks[40].monday.date.year_ce(), (true, 2024));
/// ```
pub fn from(start: NaiveDate) -> Self {
let start_of_year = start.with_ordinal0(0).unwrap();
// Check if we are in the first or second half of the year.
let mut weeks = match start < start.with_ordinal(365 / 2).unwrap() {
true => {
// First half of the year
let this_year_weeks = start_of_year.iter_weeks().take(26);
// Second half of last year
let prev_year_weeks = start_of_year
.checked_sub_months(Months::new(12))
.unwrap()
.iter_weeks()
.skip(26)
.take(27);
// Combine the two
this_year_weeks.chain(prev_year_weeks)
}
false => {
// First half of next year
let next_year_weeks = start_of_year
.checked_add_months(Months::new(12))
.unwrap()
.iter_weeks()
.take(26);
// Second half of this year
let this_year_weeks = start_of_year.iter_weeks().skip(26).take(27);
// Combine the two
next_year_weeks.chain(this_year_weeks)
}
};
Schedule {
weeks: [0; 53].map(|_| {
ScheduleWeek::new_empty(weeks.next().unwrap().week(Weekday::Mon)).unwrap()
}),
}
}
}
impl ScheduleDay {
pub fn new(date: NaiveDate) -> Self {
Self {
date,
lessons: Vec::new(),
}
}
}
impl ScheduleWeek {
/// Create a new empty ScheduleWeek
///
/// # Arguments
/// * `start` - The first day of the week
///
/// # Examples
/// ```
/// # use chrono::{NaiveDate, Weekday, NaiveWeek, Datelike};
/// # use schoolsoft::schedule::ScheduleWeek;
/// let start = NaiveDate::from_ymd(2024, 2, 5);
/// let week = ScheduleWeek::new_empty(start.week(Weekday::Mon));
/// ```
///
/// # Returns
/// A new ScheduleWeek with no lessons
pub fn new_empty(start: NaiveWeek) -> Option<Self> {
let mut days = start.first_day().iter_days();
Some(ScheduleWeek {
week: start,
monday: ScheduleDay::new(days.next()?),
tuesday: ScheduleDay::new(days.next()?),
wednesday: ScheduleDay::new(days.next()?),
thursday: ScheduleDay::new(days.next()?),
friday: ScheduleDay::new(days.next()?),
})
}
}
impl TryFrom<RawOccasion> for Occasion {
type Error = ScheduleParseError;
fn try_from(value: RawOccasion) -> Result<Self, Self::Error> {
let mut weeks_mask = [false; 54];
let mut weeks = Vec::new();
// Base weeks
for week in WeekRange::from(value.weeks_string.as_str()) {
weeks_mask[week as usize] = true;
}
// Excluded weeks
for week in WeekRange::from(value.excluding_weeks_string.as_str()) {
weeks_mask[week as usize] = false;
}
// Included weeks
for week in WeekRange::from(value.including_weeks_string.as_str()) {
weeks_mask[week as usize] = true;
}
// Convert mask to vector of weeks
for (i, week) in weeks_mask.iter().enumerate() {
if *week {
weeks.push(i as u8);
}
}
let uuid = Uuid::parse_str(&value.guid).map_err(ScheduleParseError::UuidParseError)?;
let week_day =
Weekday::try_from(value.day_id).map_err(ScheduleParseError::DayOfWeekError)?;
let start_time = NaiveTime::parse_from_str(&value.start_time, "%Y-%m-%d %H:%M:%S%.f")
.map_err(ScheduleParseError::TimeParseError)?;
let end_time = NaiveTime::parse_from_str(&value.end_time, "%Y-%m-%d %H:%M:%S%.f")
.map_err(ScheduleParseError::TimeParseError)?;
Ok(Occasion {
id: value.id,
uuid,
start_time,
end_time,
name: value.name,
room_name: value.room_name,
week_day,
weeks,
})
}
}
impl Deserializer for Schedule {
type Error = ScheduleParseError;
fn deserialize(data: &str) -> Result<Self, Self::Error> {
let raw: Vec<RawOccasion> =
serde_json::from_str(data).map_err(ScheduleParseError::SerdeError)?;
let mut schedule = Schedule::from(Local::now().naive_local().date());
for raw_occasion in raw {
let occasion = Occasion::try_from(raw_occasion)?;
let lesson = Lesson {
start: occasion.start_time,
end: occasion.end_time,
name: occasion.name,
room: occasion.room_name,
};
for week in occasion.weeks {
let x = &mut schedule.weeks[(week-1) as usize];
match occasion.week_day {
Weekday::Mon => &mut x.monday,
Weekday::Tue => &mut x.tuesday,
Weekday::Wed => &mut x.wednesday,
Weekday::Thu => &mut x.thursday,
Weekday::Fri => &mut x.friday,
_ => continue,
}
.lessons
.push(lesson.clone());
}
}
Ok(schedule)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[allow(deprecated)]
fn test_schedule_week() {
let week =
ScheduleWeek::new_empty(NaiveDate::from_ymd(2024, 3, 25).week(Weekday::Mon)).unwrap();
assert_eq!(week.monday.date, NaiveDate::from_ymd(2024, 3, 25));
assert_eq!(week.tuesday.date, NaiveDate::from_ymd(2024, 3, 26));
assert_eq!(week.wednesday.date, NaiveDate::from_ymd(2024, 3, 27));
assert_eq!(week.thursday.date, NaiveDate::from_ymd(2024, 3, 28));
assert_eq!(week.friday.date, NaiveDate::from_ymd(2024, 3, 29));
}
}