celcat/fetchable/
calendar.rs

1use std::marker::PhantomData;
2
3use chrono::NaiveDateTime;
4use serde::{Deserialize, Deserializer, Serialize};
5
6use super::Fetchable;
7use crate::entities::{CourseId, ModuleId, ResourceType};
8
9#[derive(Debug, Clone, PartialEq, Deserialize)]
10#[serde(rename_all = "camelCase")]
11#[non_exhaustive]
12pub struct Course {
13    pub id: CourseId,
14    pub start: NaiveDateTime,
15    pub end: Option<NaiveDateTime>,
16    pub all_day: bool,
17    pub description: String,
18    pub background_color: String,
19    pub text_color: String,
20    pub department: Option<String>,
21    pub faculty: Option<String>,
22    pub event_category: Option<String>,
23    pub sites: Option<Vec<String>>,
24    pub modules: Option<Vec<ModuleId>>,
25    pub register_status: i64, // TODO: values?
26    pub student_mark: f64,
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
30#[serde(rename_all = "camelCase")]
31#[non_exhaustive]
32pub enum CalView {
33    Month,
34    AgendaWeek,
35    AgendaDay,
36    ListWeek,
37}
38
39#[derive(Debug, Clone, Serialize)]
40#[serde(rename_all = "camelCase")]
41pub struct CalendarDataRequest<T: ResourceType> {
42    pub start: NaiveDateTime,
43    pub end: NaiveDateTime,
44    pub res_type: T,
45    pub cal_view: CalView,
46    pub federation_ids: T::Id, // TODO: array
47    pub colour_scheme: i64,    // TODO: values?
48}
49
50#[derive(Debug, Clone)]
51pub struct CalendarData<T: ResourceType> {
52    pub courses: Vec<Course>,
53    pub request: PhantomData<T>,
54}
55
56impl<T> Fetchable for CalendarData<T>
57where
58    T: ResourceType,
59{
60    type Request = CalendarDataRequest<T>;
61
62    const METHOD_NAME: &'static str = "GetCalendarData";
63}
64
65impl<'de, T> Deserialize<'de> for CalendarData<T>
66where
67    T: ResourceType,
68{
69    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
70    where
71        D: Deserializer<'de>,
72    {
73        Vec::<Course>::deserialize(deserializer).map(|cs| CalendarData {
74            courses: cs,
75            request: PhantomData,
76        })
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83    use crate::entities::Student;
84    use chrono::NaiveDate;
85    use serde_json::{from_value, json};
86
87    #[test]
88    fn deserialize_course() {
89        assert_eq!(
90            from_value::<Course>(json!({
91                "id": "-1347128091:-662573064:1:42367:4",
92                "start": "2021-09-22T14:30:00",
93                "end": "2021-09-22T17:45:00",
94                "allDay": false,
95                "description": "Some description",
96                "backgroundColor": "#FF0000",
97                "textColor": "#ffffff",
98                "department": "1 : UFR DROIT",
99                "faculty": null,
100                "eventCategory": "CM",
101                "sites": [
102                    "CHENES"
103                ],
104                "modules": [
105                    "1BAIJU1M"
106                ],
107                "registerStatus": 2,
108                "studentMark": 0,
109                "custom1": null,
110                "custom2": null,
111                "custom3": null
112            }))
113            .unwrap(),
114            Course {
115                id: CourseId("-1347128091:-662573064:1:42367:4".to_owned()),
116                start: NaiveDate::from_ymd(2021, 9, 22).and_hms(14, 30, 0),
117                end: Some(NaiveDate::from_ymd(2021, 9, 22).and_hms(17, 45, 0)),
118                all_day: false,
119                description: "Some description".to_owned(),
120                background_color: "#FF0000".to_owned(),
121                text_color: "#ffffff".to_owned(),
122                department: Some("1 : UFR DROIT".to_owned()),
123                faculty: None,
124                event_category: Some("CM".to_owned()),
125                sites: Some(vec!["CHENES".to_owned()]),
126                modules: Some(vec![ModuleId("1BAIJU1M".to_owned())]),
127                register_status: 2,
128                student_mark: 0.0,
129            }
130        );
131    }
132
133    #[test]
134    fn deserialize_calendar_data() {
135        from_value::<CalendarData<Student>>(json!([])).unwrap();
136    }
137}