We do a little bit of refactoring

This commit is contained in:
Sijmen 2023-01-02 18:40:51 +01:00
parent fe16cde78f
commit 8e4392cb51
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
3 changed files with 41 additions and 24 deletions

7
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -43,19 +43,37 @@ enum Source {
Delft,
}
impl Source {
fn from_url(url: &str) -> Option<Self> {
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<Calendar> {
let body = reqwest::get(url).await?.text().await?;
let calendar = body.parse::<Calendar>().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?<url>")]
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::<Calendar>().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);