This repository has been archived on 2024-05-23. You can view files and clone it, but cannot push or open issues or pull requests.
cal8tor/src/ics.rs

48 lines
1.4 KiB
Rust
Raw Normal View History

2022-08-16 10:32:46 +02:00
use ics::{
properties::{Class, Description, DtEnd, DtStart, Location, Summary, Transp},
Event, ICalendar,
};
2022-08-16 11:54:47 +02:00
pub fn export(courses: Vec<crate::timetable::models::Course>, filename: &str) {
2022-08-16 10:32:46 +02:00
let mut calendar = ICalendar::new("2.0", "cal8tor");
2022-08-16 11:54:47 +02:00
// Create events which contains the information regarding the course
for course in courses {
2022-08-16 14:22:04 +02:00
let mut event = Event::new(uuid::Uuid::new_v4().to_string(), dt_ical(chrono::Utc::now()));
2022-08-16 10:32:46 +02:00
2022-08-16 11:54:47 +02:00
// Public event
event.push(Class::public());
2022-08-16 14:22:04 +02:00
2022-08-16 11:54:47 +02:00
// Consume actual time
event.push(Transp::opaque());
2022-08-16 14:22:04 +02:00
2022-08-16 11:54:47 +02:00
// Professor's name
2022-08-16 14:06:58 +02:00
if course.professor.is_some() {
event.push(Description::new(course.professor.unwrap()));
}
2022-08-16 14:22:04 +02:00
2022-08-16 11:54:47 +02:00
// Start time of the course
event.push(DtStart::new(dt_ical(course.dtstart.unwrap())));
2022-08-16 14:22:04 +02:00
2022-08-16 11:54:47 +02:00
// End time of the course
event.push(DtEnd::new(dt_ical(course.dtend.unwrap())));
2022-08-16 14:22:04 +02:00
2022-08-16 11:54:47 +02:00
// Room location
event.push(Location::new(course.room));
2022-08-16 14:22:04 +02:00
2022-08-16 11:54:47 +02:00
// Course's name
event.push(Summary::new(course.name));
2022-08-16 10:32:46 +02:00
2022-08-16 11:54:47 +02:00
// Add the course to the calendar
calendar.add_event(event);
}
2022-08-16 10:32:46 +02:00
calendar.save_file(filename).unwrap();
2022-08-16 10:32:46 +02:00
}
2022-08-16 11:54:47 +02:00
/// Transform the datetime from chrono to the ICS format
2022-08-16 12:16:23 +02:00
/// See <https://github.com/hummingly/ics/issues/17#issue-985662287>
2022-08-16 11:54:47 +02:00
fn dt_ical(dt: chrono::DateTime<chrono::Utc>) -> String {
format!("{}", dt.format("%Y%m%dT%H%M%SZ"))
}