refactored to make it more input-agnostic

This commit is contained in:
_ 2025-08-14 03:33:12 +00:00
parent dfbf23ed6a
commit 92c30167df
2 changed files with 40 additions and 24 deletions

View file

@ -74,7 +74,7 @@ struct CliAuto {
}
#[derive(clap::Parser)]
struct CliIcsDebug {
struct CliDebugOutput {
#[arg(long)]
config: Utf8PathBuf,
}
@ -82,7 +82,7 @@ struct CliIcsDebug {
#[derive(clap::Subcommand)]
enum Commands {
Auto(CliAuto),
IcsDebug(CliIcsDebug),
DebugOutput(CliDebugOutput),
}
#[derive(clap::Parser)]
@ -154,17 +154,26 @@ struct EventInstance {
calendar_ui: CalendarUi,
dtstart: DatePerhapsTime,
location: Option<String>,
recurrence_id: Option<icalendar::DatePerhapsTime>,
recurrence_id: Option<DatePerhapsTime>,
summary: Option<String>,
uid: Option<String>,
url: Option<String>,
}
/// Used to link recurrence exceptions to the original events they replace
#[derive(Eq, Ord, PartialOrd, PartialEq)]
struct RecurrenceKey<'a> {
recurrence_id: DatePerhapsTime,
uid: &'a str,
impl EventInstance {
fn filter(&self, config_output: &ConfigOutput) -> bool {
if let Some(uid) = &self.uid
&& config_output.hide_uids.contains(uid)
{
return false;
}
if let Some(summary) = &self.summary
&& config_output.hide_summaries.contains(summary)
{
return false;
}
true
}
}
#[derive(Default)]
@ -191,17 +200,11 @@ fn process_data<'a>(
let mut instances = vec![];
for ical in &data.icals {
for ei in ical.event_instances(&params)? {
if let Some(uid) = &ei.uid
&& config_output.hide_uids.contains(uid)
for ei in ical
.event_instances(&params)?
.into_iter()
.filter(|x| x.filter(config_output))
{
continue;
}
if let Some(summary) = &ei.summary
&& config_output.hide_summaries.contains(summary)
{
continue;
}
instances.push(ei);
}
}
@ -412,7 +415,7 @@ async fn do_everything(cli: &CliAuto) -> Result<()> {
}
let bytes = resp.bytes().await?;
let temp_path = dl.file_path.with_extension(".ics.temp");
let temp_path = dl.file_path.with_extension(".wac_temp");
std::fs::write(&temp_path, &bytes)?;
std::fs::rename(&temp_path, &dl.file_path)?;
}
@ -443,7 +446,7 @@ fn main_auto(cli: CliAuto) -> Result<()> {
}
}
fn main_ics_debug(cli: CliIcsDebug) -> Result<()> {
fn main_debug_output(cli: CliDebugOutput) -> Result<()> {
tracing_subscriber::fmt::init();
tracing::info!("Started tracing");
let config = std::fs::read_to_string(&cli.config)?;
@ -464,6 +467,6 @@ fn main() -> Result<()> {
match cli.command {
Commands::Auto(x) => main_auto(x),
Commands::IcsDebug(x) => main_ics_debug(x),
Commands::DebugOutput(x) => main_debug_output(x),
}
}

View file

@ -1,6 +1,6 @@
//! Structs and functions specific to gathering input from ics files, which is a popular format that Google Calendar happens to put out
use super::{CalendarUi, DatePerhapsTime, Downloadable, EventInstance, Parameters, RecurrenceKey};
use super::{CalendarUi, DatePerhapsTime, Downloadable, EventInstance, Parameters};
use anyhow::{Context as _, Result, anyhow};
use base64::Engine as _;
use chrono::TimeZone as _;
@ -163,11 +163,17 @@ fn ical_event_instances(
None
};
let recurrence_id = ev
.get_recurrence_id()
.as_ref()
.map(|x| normalize_date_perhaps_time(x, params.tz))
.transpose()?;
Ok::<_, anyhow::Error>(EventInstance {
calendar_ui: config_ical.ui.clone(),
dtstart,
location: ev.get_location().map(|s| s.to_string()),
recurrence_id: ev.get_recurrence_id(),
recurrence_id,
summary: ev.get_summary().map(|s| s.to_string()),
uid,
url,
@ -177,6 +183,13 @@ fn ical_event_instances(
instances
}
/// Used to link recurrence exceptions to the original events they replace
#[derive(Eq, Ord, PartialOrd, PartialEq)]
struct RecurrenceKey<'a> {
recurrence_id: DatePerhapsTime,
uid: &'a str,
}
impl Calendar {
pub(crate) fn read_from_str(config: Config, s: &str) -> Result<Self> {
let cal = s.parse().map_err(|s| anyhow!("parse error {s}"))?;