forked from HAWHHCalendarBot/parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.rs
More file actions
74 lines (63 loc) · 2.01 KB
/
events.rs
File metadata and controls
74 lines (63 loc) · 2.01 KB
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
use std::fs;
use std::path::Path;
use anyhow::Context as _;
use chrono::NaiveDateTime;
use serde::Deserialize;
use crate::generate_ics::{EventStatus, SoonToBeIcsEvent};
#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
pub struct EventEntry {
pub name: String,
pub location: String,
pub description: String,
pub start_time: NaiveDateTime,
pub end_time: NaiveDateTime,
}
pub const FOLDER: &str = "eventfiles";
pub fn read(name: &str) -> anyhow::Result<Vec<EventEntry>> {
let filename = name.replace('/', "-");
let path = Path::new(FOLDER).join(filename + ".json");
let content = fs::read_to_string(path).context("failed to read")?;
let event_entries: Vec<EventEntry> =
serde_json::from_str(&content).context("failed to parse")?;
Ok(event_entries)
}
impl From<EventEntry> for SoonToBeIcsEvent {
fn from(event: EventEntry) -> Self {
Self {
start_time: event.start_time,
end_time: event.end_time,
name: event.name.clone(),
pretty_name: event.name,
status: EventStatus::Confirmed,
alert_minutes_before: None,
description: event.description,
location: event.location,
}
}
}
#[test]
fn can_deserialize_event_entry() -> Result<(), serde_json::Error> {
use chrono::NaiveDate;
let test: EventEntry = serde_json::from_str(
r#"{"Name": "BTI1-TI", "Location": "1060", "Description": "Dozent: HTM", "StartTime": "2022-01-13T11:40:00", "EndTime": "2022-01-13T12:00:00"}"#,
)?;
assert_eq!(test.name, "BTI1-TI");
assert_eq!(test.location, "1060");
assert_eq!(test.description, "Dozent: HTM");
assert_eq!(
test.start_time,
NaiveDate::from_ymd_opt(2022, 1, 13)
.unwrap()
.and_hms_opt(11, 40, 0)
.unwrap()
);
assert_eq!(
test.end_time,
NaiveDate::from_ymd_opt(2022, 1, 13)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap()
);
Ok(())
}