refactored to make it more input-agnostic
This commit is contained in:
parent
dfbf23ed6a
commit
92c30167df
2 changed files with 40 additions and 24 deletions
47
src/main.rs
47
src/main.rs
|
@ -74,7 +74,7 @@ struct CliAuto {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(clap::Parser)]
|
#[derive(clap::Parser)]
|
||||||
struct CliIcsDebug {
|
struct CliDebugOutput {
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
config: Utf8PathBuf,
|
config: Utf8PathBuf,
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,7 @@ struct CliIcsDebug {
|
||||||
#[derive(clap::Subcommand)]
|
#[derive(clap::Subcommand)]
|
||||||
enum Commands {
|
enum Commands {
|
||||||
Auto(CliAuto),
|
Auto(CliAuto),
|
||||||
IcsDebug(CliIcsDebug),
|
DebugOutput(CliDebugOutput),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(clap::Parser)]
|
#[derive(clap::Parser)]
|
||||||
|
@ -154,17 +154,26 @@ struct EventInstance {
|
||||||
calendar_ui: CalendarUi,
|
calendar_ui: CalendarUi,
|
||||||
dtstart: DatePerhapsTime,
|
dtstart: DatePerhapsTime,
|
||||||
location: Option<String>,
|
location: Option<String>,
|
||||||
recurrence_id: Option<icalendar::DatePerhapsTime>,
|
recurrence_id: Option<DatePerhapsTime>,
|
||||||
summary: Option<String>,
|
summary: Option<String>,
|
||||||
uid: Option<String>,
|
uid: Option<String>,
|
||||||
url: Option<String>,
|
url: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Used to link recurrence exceptions to the original events they replace
|
impl EventInstance {
|
||||||
#[derive(Eq, Ord, PartialOrd, PartialEq)]
|
fn filter(&self, config_output: &ConfigOutput) -> bool {
|
||||||
struct RecurrenceKey<'a> {
|
if let Some(uid) = &self.uid
|
||||||
recurrence_id: DatePerhapsTime,
|
&& config_output.hide_uids.contains(uid)
|
||||||
uid: &'a str,
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if let Some(summary) = &self.summary
|
||||||
|
&& config_output.hide_summaries.contains(summary)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -191,17 +200,11 @@ fn process_data<'a>(
|
||||||
|
|
||||||
let mut instances = vec![];
|
let mut instances = vec![];
|
||||||
for ical in &data.icals {
|
for ical in &data.icals {
|
||||||
for ei in ical.event_instances(¶ms)? {
|
for ei in ical
|
||||||
if let Some(uid) = &ei.uid
|
.event_instances(¶ms)?
|
||||||
&& config_output.hide_uids.contains(uid)
|
.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);
|
instances.push(ei);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -412,7 +415,7 @@ async fn do_everything(cli: &CliAuto) -> Result<()> {
|
||||||
}
|
}
|
||||||
let bytes = resp.bytes().await?;
|
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::write(&temp_path, &bytes)?;
|
||||||
std::fs::rename(&temp_path, &dl.file_path)?;
|
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_subscriber::fmt::init();
|
||||||
tracing::info!("Started tracing");
|
tracing::info!("Started tracing");
|
||||||
let config = std::fs::read_to_string(&cli.config)?;
|
let config = std::fs::read_to_string(&cli.config)?;
|
||||||
|
@ -464,6 +467,6 @@ fn main() -> Result<()> {
|
||||||
|
|
||||||
match cli.command {
|
match cli.command {
|
||||||
Commands::Auto(x) => main_auto(x),
|
Commands::Auto(x) => main_auto(x),
|
||||||
Commands::IcsDebug(x) => main_ics_debug(x),
|
Commands::DebugOutput(x) => main_debug_output(x),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
//! 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 anyhow::{Context as _, Result, anyhow};
|
||||||
use base64::Engine as _;
|
use base64::Engine as _;
|
||||||
use chrono::TimeZone as _;
|
use chrono::TimeZone as _;
|
||||||
|
@ -163,11 +163,17 @@ fn ical_event_instances(
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let recurrence_id = ev
|
||||||
|
.get_recurrence_id()
|
||||||
|
.as_ref()
|
||||||
|
.map(|x| normalize_date_perhaps_time(x, params.tz))
|
||||||
|
.transpose()?;
|
||||||
|
|
||||||
Ok::<_, anyhow::Error>(EventInstance {
|
Ok::<_, anyhow::Error>(EventInstance {
|
||||||
calendar_ui: config_ical.ui.clone(),
|
calendar_ui: config_ical.ui.clone(),
|
||||||
dtstart,
|
dtstart,
|
||||||
location: ev.get_location().map(|s| s.to_string()),
|
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()),
|
summary: ev.get_summary().map(|s| s.to_string()),
|
||||||
uid,
|
uid,
|
||||||
url,
|
url,
|
||||||
|
@ -177,6 +183,13 @@ fn ical_event_instances(
|
||||||
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 {
|
impl Calendar {
|
||||||
pub(crate) fn read_from_str(config: Config, s: &str) -> Result<Self> {
|
pub(crate) fn read_from_str(config: Config, 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}"))?;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue