From 8e4392cb517de39d514dbc6780210073656cb5dd Mon Sep 17 00:00:00 2001 From: Sijmen Date: Mon, 2 Jan 2023 18:40:51 +0100 Subject: [PATCH] We do a little bit of refactoring --- Cargo.lock | 7 +++++++ Cargo.toml | 2 +- src/main.rs | 56 +++++++++++++++++++++++++++++++---------------------- 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c39fc1c..f1e2308 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -70,6 +70,12 @@ dependencies = [ "libc", ] +[[package]] +name = "anyhow" +version = "1.0.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61" + [[package]] name = "async-stream" version = "0.3.3" @@ -1497,6 +1503,7 @@ dependencies = [ name = "rooster" version = "0.1.0" dependencies = [ + "anyhow", "figment", "icalendar", "log", diff --git a/Cargo.toml b/Cargo.toml index 7500be8..23ce5c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ sentry = "0.29.1" log = "0.4.17" serde = "1.0.152" figment = "0.10.8" -#anyhow = "1.0.68" +anyhow = "1.0.68" [profile.release] lto = "thin" diff --git a/src/main.rs b/src/main.rs index 362db25..6f4856e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,19 +43,37 @@ enum Source { Delft, } +impl Source { + fn from_url(url: &str) -> Option { + if url.starts_with("https://rooster.utwente.nl/ical") { + Some(Source::Twente) + } else if url.starts_with("https://mytimetable.tudelft.nl/ical") { + Some(Source::Delft) + } else { + None + } + } +} + +async fn get_calendar(url: &str) -> anyhow::Result { + let body = reqwest::get(url).await?.text().await?; + let calendar = body.parse::().map_err(anyhow::Error::msg)?; + Ok(calendar) +} + +fn get_event_kind(description: &str) -> &str { + let first_line = description.split_once("\\n").unwrap().0; + first_line.split_once(": ").unwrap().1 +} + #[get("/ical?")] async fn ics(url: &str) -> String { - let source = if url.starts_with("https://rooster.utwente.nl/ical") { - Source::Twente - } else if url.starts_with("https://mytimetable.tudelft.nl/ical") { - Source::Delft - } else { - return "Must be a rooster.utwente.nl or mytimetable.tudelft.nl URL".to_string(); + let source = match Source::from_url(url) { + Some(source) => source, + None => return "Must be a rooster.utwente.nl or mytimetable.tudelft.nl URL".to_string() }; - let body = reqwest::get(url).await.unwrap().text().await.unwrap(); - - let calendar = body.parse::().unwrap(); + let calendar = get_calendar(url).await.unwrap(); let mut new_calendar = Calendar::new(); for component in calendar.iter() { @@ -66,23 +84,14 @@ async fn ics(url: &str) -> String { let summary = event.get_summary().unwrap(); let subject = match source { - Source::Twente => { - let (subject, _) = summary.rsplit_once(' ').unwrap(); - subject - } - - Source::Delft => { - let (_, subject) = summary.split_once(" - ").unwrap(); - subject - } + Source::Twente => summary.rsplit_once(' ').unwrap().0, + Source::Delft => summary.split_once(" - ").unwrap().1, }; - let description = event.get_description().unwrap(); - let (kind, _) = description.split_once("\\n").unwrap(); - let (_, kind) = kind.split_once(": ").unwrap(); - let start = event.get_start().unwrap(); let end = event.get_end().unwrap(); + let description = event.get_description().unwrap(); + let kind = get_event_kind(&description); let mut hasher = DefaultHasher::new(); url.hash(&mut hasher); @@ -90,13 +99,14 @@ async fn ics(url: &str) -> String { format!("{end:?}").hash(&mut hasher); summary.hash(&mut hasher); description.hash(&mut hasher); - let mut new_event = Event::new(); + let mut new_event = Event::new(); new_event .summary(&format!("{kind} - {subject}")) .description(description) .starts(start) .ends(end); + if let Some(location) = event.get_location() { new_event.location(location); location.hash(&mut hasher);