refactor
This commit is contained in:
parent
49f48acaf1
commit
d4d0adaacc
2 changed files with 22 additions and 58 deletions
68
src/main.rs
68
src/main.rs
|
@ -277,38 +277,7 @@ fn google_url(
|
||||||
link.query_pairs_mut().append_pair("eid", &eid);
|
link.query_pairs_mut().append_pair("eid", &eid);
|
||||||
Ok(Some(link.to_string()))
|
Ok(Some(link.to_string()))
|
||||||
}
|
}
|
||||||
/*
|
|
||||||
impl EventInstance {
|
|
||||||
fn url(&self, google_id: Option<&str>) -> Result<Option<String>> {
|
|
||||||
if let Some(url) = self.ev.get_url() {
|
|
||||||
return Ok(Some(url.to_string()));
|
|
||||||
}
|
|
||||||
if let Some(google_id) = google_id {
|
|
||||||
return self.google_url(google_id);
|
|
||||||
}
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct EventWithUrl<'a> {
|
|
||||||
calendar: &'a ConfigIcal,
|
|
||||||
dtstart: DatePerhapsTime,
|
|
||||||
ev: &'a icalendar::Event,
|
|
||||||
url: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> EventWithUrl<'a> {
|
|
||||||
fn from_ei(calendar: &'a ConfigIcal, ei: EventInstance<'a>) -> Result<EventWithUrl<'a>> {
|
|
||||||
let url = ei.url(calendar.google_id.as_deref())?;
|
|
||||||
Ok(Self {
|
|
||||||
calendar,
|
|
||||||
dtstart: ei.dtstart,
|
|
||||||
ev: ei.ev,
|
|
||||||
url,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
fn ical_event_instances(
|
fn ical_event_instances(
|
||||||
config_ical: &ConfigIcal,
|
config_ical: &ConfigIcal,
|
||||||
params: &Parameters,
|
params: &Parameters,
|
||||||
|
@ -358,6 +327,9 @@ fn ical_event_instances(
|
||||||
struct ICal {
|
struct ICal {
|
||||||
/// The parsed ics file
|
/// The parsed ics file
|
||||||
cal: icalendar::Calendar,
|
cal: icalendar::Calendar,
|
||||||
|
|
||||||
|
/// The config used to load this calendar
|
||||||
|
config: ConfigIcal,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Used to link recurrence exceptions to the original events they replace
|
/// Used to link recurrence exceptions to the original events they replace
|
||||||
|
@ -368,15 +340,15 @@ struct RecurrenceKey<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ICal {
|
impl ICal {
|
||||||
fn read_from_str(s: &str) -> Result<Self> {
|
fn read_from_str(config: ConfigIcal, s: &str) -> Result<Self> {
|
||||||
let cal = s.parse().map_err(|s| anyhow!("parse error {s}"))?;
|
let cal = s.parse().map_err(|s| anyhow!("parse error {s}"))?;
|
||||||
let cal = Self { cal };
|
let cal = Self { cal, config };
|
||||||
Ok(cal)
|
Ok(cal)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_from_downloadable(dl: &Downloadable) -> Result<Self> {
|
fn read_from_downloadable(config: ConfigIcal) -> Result<Self> {
|
||||||
let s = std::fs::read_to_string(&dl.file_path)?;
|
let s = std::fs::read_to_string(&config.dl.file_path)?;
|
||||||
Self::read_from_str(&s)
|
Self::read_from_str(config, &s)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn events(&self) -> impl Iterator<Item = &icalendar::Event> {
|
fn events(&self) -> impl Iterator<Item = &icalendar::Event> {
|
||||||
|
@ -390,16 +362,12 @@ impl ICal {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an unsorted list of event instances for this calendar
|
/// Returns an unsorted list of event instances for this calendar
|
||||||
fn event_instances(
|
fn event_instances(&self, params: &Parameters) -> Result<Vec<EventInstance>> {
|
||||||
&self,
|
|
||||||
config_ical: &ConfigIcal,
|
|
||||||
params: &Parameters,
|
|
||||||
) -> Result<Vec<EventInstance>> {
|
|
||||||
let mut instances = vec![];
|
let mut instances = vec![];
|
||||||
let mut recurrence_exceptions = BTreeSet::new();
|
let mut recurrence_exceptions = BTreeSet::new();
|
||||||
|
|
||||||
for ev in self.events() {
|
for ev in self.events() {
|
||||||
let eis = match ical_event_instances(config_ical, params, ev)
|
let eis = match ical_event_instances(&self.config, params, ev)
|
||||||
.with_context(|| format!("Failed to process event with UID '{:?}'", ev.get_uid()))
|
.with_context(|| format!("Failed to process event with UID '{:?}'", ev.get_uid()))
|
||||||
{
|
{
|
||||||
Ok(x) => x,
|
Ok(x) => x,
|
||||||
|
@ -456,14 +424,14 @@ impl ICal {
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct Data {
|
struct Data {
|
||||||
icals: Vec<(ICal, ConfigIcal)>,
|
icals: Vec<ICal>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_data_from_disk(config: &Config) -> Result<Data> {
|
fn read_data_from_disk(config: &Config) -> Result<Data> {
|
||||||
let mut data = Data::default();
|
let mut data = Data::default();
|
||||||
for config in &config.icals {
|
for config_ical in &config.icals {
|
||||||
let cal = ICal::read_from_downloadable(&config.dl)?;
|
let cal = ICal::read_from_downloadable(config_ical.clone())?;
|
||||||
data.icals.push((cal, config.clone()));
|
data.icals.push(cal);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(data)
|
Ok(data)
|
||||||
|
@ -477,8 +445,8 @@ fn process_data<'a>(
|
||||||
let params = Parameters::new(now)?;
|
let params = Parameters::new(now)?;
|
||||||
|
|
||||||
let mut instances = vec![];
|
let mut instances = vec![];
|
||||||
for (ical, config) in &data.icals {
|
for ical in &data.icals {
|
||||||
for ei in ical.event_instances(config, ¶ms)? {
|
for ei in ical.event_instances(¶ms)? {
|
||||||
if let Some(uid) = &ei.uid
|
if let Some(uid) = &ei.uid
|
||||||
&& config_output.hide_uids.contains(uid)
|
&& config_output.hide_uids.contains(uid)
|
||||||
{
|
{
|
||||||
|
@ -489,7 +457,6 @@ fn process_data<'a>(
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// let ei = EventWithUrl::from_ei(config, ei)?;
|
|
||||||
instances.push(ei);
|
instances.push(ei);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -498,7 +465,6 @@ fn process_data<'a>(
|
||||||
Ok(instances)
|
Ok(instances)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Don't print to stdout / stderr
|
|
||||||
fn output_html(
|
fn output_html(
|
||||||
config: &ConfigOutput,
|
config: &ConfigOutput,
|
||||||
instances: &[EventInstance],
|
instances: &[EventInstance],
|
||||||
|
@ -533,8 +499,6 @@ fn output_html(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if last_date_printed != Some(date) {
|
if last_date_printed != Some(date) {
|
||||||
// println!("{date}");
|
|
||||||
|
|
||||||
// FIXME: De-dupe
|
// FIXME: De-dupe
|
||||||
if !day_list.is_empty() {
|
if !day_list.is_empty() {
|
||||||
html_list.push(maud::html! {
|
html_list.push(maud::html! {
|
||||||
|
|
12
src/tests.rs
12
src/tests.rs
|
@ -64,10 +64,10 @@ END:VEVENT
|
||||||
END:VCALENDAR
|
END:VCALENDAR
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let ical = ICal::read_from_str(s)?;
|
let ical = ICal::read_from_str(ConfigIcal::default(), s)?;
|
||||||
let now = dt_from_ts(1755000000);
|
let now = dt_from_ts(1755000000);
|
||||||
let params = Parameters::new(now)?;
|
let params = Parameters::new(now)?;
|
||||||
let instances = ical.event_instances(&ConfigIcal::default(), ¶ms)?;
|
let instances = ical.event_instances(¶ms)?;
|
||||||
assert_eq!(instances.len(), 1);
|
assert_eq!(instances.len(), 1);
|
||||||
|
|
||||||
let event = &instances[0];
|
let event = &instances[0];
|
||||||
|
@ -102,14 +102,14 @@ END:VEVENT
|
||||||
END:VCALENDAR
|
END:VCALENDAR
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let ical = ICal::read_from_str(s)?;
|
let ical = ICal::read_from_str(ConfigIcal::default(), s)?;
|
||||||
let params = Parameters {
|
let params = Parameters {
|
||||||
ignore_before: chicago_time(2025, 1, 1, 0, 0, 0),
|
ignore_before: chicago_time(2025, 1, 1, 0, 0, 0),
|
||||||
output_start: chicago_time(2025, 7, 1, 0, 0, 0),
|
output_start: chicago_time(2025, 7, 1, 0, 0, 0),
|
||||||
output_stop: chicago_time(2025, 10, 1, 0, 0, 0),
|
output_stop: chicago_time(2025, 10, 1, 0, 0, 0),
|
||||||
tz: chrono_tz::America::Chicago,
|
tz: chrono_tz::America::Chicago,
|
||||||
};
|
};
|
||||||
let instances = ical.event_instances(&ConfigIcal::default(), ¶ms)?;
|
let instances = ical.event_instances(¶ms)?;
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
[instances[0].dtstart, instances[1].dtstart,],
|
[instances[0].dtstart, instances[1].dtstart,],
|
||||||
|
@ -188,14 +188,14 @@ END:VEVENT
|
||||||
END:VCALENDAR
|
END:VCALENDAR
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let ical = ICal::read_from_str(s)?;
|
let ical = ICal::read_from_str(ConfigIcal::default(), s)?;
|
||||||
let params = Parameters {
|
let params = Parameters {
|
||||||
ignore_before: chicago_time(2025, 1, 1, 0, 0, 0),
|
ignore_before: chicago_time(2025, 1, 1, 0, 0, 0),
|
||||||
output_start: chicago_time(2025, 7, 1, 0, 0, 0),
|
output_start: chicago_time(2025, 7, 1, 0, 0, 0),
|
||||||
output_stop: chicago_time(2025, 10, 1, 0, 0, 0),
|
output_stop: chicago_time(2025, 10, 1, 0, 0, 0),
|
||||||
tz: chrono_tz::America::Chicago,
|
tz: chrono_tz::America::Chicago,
|
||||||
};
|
};
|
||||||
let instances = ical.event_instances(&ConfigIcal::default(), ¶ms)?;
|
let instances = ical.event_instances(¶ms)?;
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
[
|
[
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue