~whynothugo/valarmd

076a9a6d754dca0e54eb60e01f4635c2761ba99d — Hugo Osvaldo Barrera 1 year, 9 months ago 2089823
Refactor for missing timezones branch
1 files changed, 28 insertions(+), 19 deletions(-)

M valarmd/src/main.rs
M valarmd/src/main.rs => valarmd/src/main.rs +28 -19
@@ 1,7 1,7 @@
use chrono::{DateTime, Local, TimeZone, Utc};
use icalendar::parser::{read_calendar, unfold, Component, ParseString, Property};
use icalendar::{CalendarDateTime, DatePerhapsTime};
use log::{error, warn};
use log::{debug, error, warn};
use std::fs::{read_dir, read_to_string};
use thiserror::Error;



@@ 263,6 263,7 @@ enum CommponentError {
fn extract_alarms(
    component: &Component,
    filter: &AlarmFilter,
    timezones: &[Component],
) -> Result<Vec<Alarm>, CommponentError> {
    let mut potential_alarms = component
        .components


@@ 330,6 331,8 @@ fn main() {
                continue;
            }
            let content = read_to_string(entry.path()).unwrap();
            let mut components_with_alarms = Vec::new();
            let mut timezones = Vec::new();

            // TODO: timezones should be scoped here, per file.



@@ 338,36 341,42 @@ fn main() {
            // TODO: ignore "DONE" or "CANCELLED" todos.

            // FIXME: `unfold` results in copying the entire thing in memory.
            match read_calendar(&unfold(&content)) {
            let unfolded_content = &unfold(&content);
            match read_calendar(unfolded_content) {
                Ok(calendar) => {
                    for component in calendar.components {
                        if component.name == "VTIMEZONE" {
                            // TODO: we'll need these eventually
                            // Probably best to map then my TZID for later usage.
                            continue;
                        } else if component.name == "VEVENT" || component.name == "VTODO" {
                            // TODO: pass "name" for logging.
                            match &mut extract_alarms(&component, &filter) {
                                Ok(new_alarms) => alarms.append(new_alarms),
                                Err(err) => {
                                    error!("Failed to extract alarms from component: {}.", err)
                                }
                            };
                        } else if component.name == "VJOURNAL" {
                            continue;
                        } else {
                            unreachable!("Unexpected component: {:?}", component.name);
                        match component.name.as_str() {
                            "VTIMEZONE" => {
                                timezones.push(component);
                            }
                            "VEVENT" | "VTODO" => {
                                components_with_alarms.push(component);
                            }
                            "VJOURNAL" => {}
                            _ => {
                                debug!("Unexpected component: {:?}", component.name);
                            }
                        }
                    }
                }
                Err(err) => {
                    error!(
                        "Failed to parse: {}: {:?}",
                        entry.path().to_string_lossy(),
                        &entry.path().to_string_lossy(),
                        err
                    );
                }
            }

            for component in components_with_alarms {
                // TODO: pass around timezomes here, since they might be relevant.
                match &mut extract_alarms(&component, &filter, &timezones) {
                    Ok(new_alarms) => alarms.append(new_alarms),
                    Err(err) => {
                        error!("Failed to extract alarms from component: {}.", err)
                    }
                };
            }
        }
    }