add unit tests finally
This commit is contained in:
parent
a4e4c89e15
commit
3044deb89f
2 changed files with 214 additions and 30 deletions
70
src/main.rs
70
src/main.rs
|
@ -6,6 +6,9 @@ use icalendar::{Component as _, EventLike as _};
|
|||
use serde::Deserialize;
|
||||
use std::{io::Write as _, path::PathBuf, str::FromStr as _, time::Duration};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
#[derive(Clone, Deserialize)]
|
||||
struct ConfigIcal {
|
||||
/// Disk location to cache the ics file for debugging
|
||||
|
@ -82,8 +85,30 @@ struct Parameters {
|
|||
tz: chrono_tz::Tz,
|
||||
}
|
||||
|
||||
impl Parameters {
|
||||
fn new(now: DateTime<chrono_tz::Tz>) -> Result<Self> {
|
||||
// Snap the cutoffs to midnight so we won't present half of a day
|
||||
let midnight = chrono::NaiveTime::default();
|
||||
let output_start = (now - Duration::from_secs(86_400 * 2))
|
||||
.with_time(midnight)
|
||||
.single()
|
||||
.context("output_start doesn't map to a single time in our timezone")?;
|
||||
let output_stop = (now + Duration::from_secs(86_400 * 45))
|
||||
.with_time(midnight)
|
||||
.single()
|
||||
.context("output_stop doesn't map to a single time in our timezone")?;
|
||||
|
||||
Ok(Parameters {
|
||||
ignore_before: now - Duration::from_secs(86_400 * 365 * 2),
|
||||
output_start,
|
||||
output_stop,
|
||||
tz: now.timezone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Similar to `icalendar::DatePerhapsTime` but doesn't allow Floating, and naive dates are stored as local midnight with an "all day" flag
|
||||
#[derive(Clone, Copy, Ord, PartialOrd, Eq, PartialEq)]
|
||||
#[derive(Clone, Copy, Debug, Ord, PartialOrd, Eq, PartialEq)]
|
||||
struct DatePerhapsTime {
|
||||
dt: DateTime<chrono_tz::Tz>,
|
||||
all_day: bool,
|
||||
|
@ -267,19 +292,20 @@ fn event_instances<'a>(
|
|||
struct ICal {
|
||||
/// The parsed ics file
|
||||
cal: icalendar::Calendar,
|
||||
|
||||
/// The config used to download the ics file
|
||||
config: ConfigIcal,
|
||||
}
|
||||
|
||||
impl ICal {
|
||||
fn read(config: ConfigIcal) -> Result<Self> {
|
||||
let s = std::fs::read_to_string(&config.file_path)?;
|
||||
fn read_from_str(s: &str) -> Result<Self> {
|
||||
let cal = s.parse().map_err(|s| anyhow!("parse error {s}"))?;
|
||||
let cal = Self { cal, config };
|
||||
let cal = Self { cal };
|
||||
Ok(cal)
|
||||
}
|
||||
|
||||
fn read_from_config(config: &ConfigIcal) -> Result<Self> {
|
||||
let s = std::fs::read_to_string(&config.file_path)?;
|
||||
Self::read_from_str(&s)
|
||||
}
|
||||
|
||||
fn events(&self) -> impl Iterator<Item = &icalendar::Event> {
|
||||
self.cal.components.iter().filter_map(|comp| {
|
||||
if let icalendar::CalendarComponent::Event(ev) = comp {
|
||||
|
@ -320,42 +346,26 @@ impl ICal {
|
|||
|
||||
#[derive(Default)]
|
||||
struct Data {
|
||||
icals: Vec<ICal>,
|
||||
icals: Vec<(ICal, ConfigIcal)>,
|
||||
}
|
||||
|
||||
fn read_data_from_disk(config: &Config) -> Result<Data> {
|
||||
let mut data = Data::default();
|
||||
for cfg in &config.icals {
|
||||
let cal = ICal::read(cfg.clone())?;
|
||||
data.icals.push(cal);
|
||||
for config in &config.icals {
|
||||
let cal = ICal::read_from_config(config)?;
|
||||
data.icals.push((cal, config.clone()));
|
||||
}
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
fn process_data(data: &Data, now: DateTime<chrono_tz::Tz>) -> Result<Vec<EventWithUrl<'_>>> {
|
||||
// Snap the cutoffs to midnight so we won't present half of a day
|
||||
let midnight = chrono::NaiveTime::default();
|
||||
let output_start = (now - Duration::from_secs(86_400 * 2))
|
||||
.with_time(midnight)
|
||||
.single()
|
||||
.context("output_start doesn't map to a single time in our timezone")?;
|
||||
let output_stop = (now + Duration::from_secs(86_400 * 45))
|
||||
.with_time(midnight)
|
||||
.single()
|
||||
.context("output_stop doesn't map to a single time in our timezone")?;
|
||||
|
||||
let params = Parameters {
|
||||
ignore_before: now - Duration::from_secs(86_400 * 365 * 2),
|
||||
output_start,
|
||||
output_stop,
|
||||
tz: now.timezone(),
|
||||
};
|
||||
let params = Parameters::new(now)?;
|
||||
|
||||
let mut instances = vec![];
|
||||
for ical in &data.icals {
|
||||
for (ical, config) in &data.icals {
|
||||
for ei in ical.event_instances(¶ms)? {
|
||||
let ei = EventWithUrl::from_ei(&ical.config, ei)?;
|
||||
let ei = EventWithUrl::from_ei(config, ei)?;
|
||||
instances.push(ei);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue