Browse Source

enhance dtend of all-day events

master
Astro 3 years ago
parent
commit
e466c0a048
  1. 23
      src/ics/mod.rs
  2. 9
      src/main.rs

23
src/ics/mod.rs

@ -33,17 +33,32 @@ impl<'a> GetValue<'a, String> for Object {
}
// TODO: TZID, offset
// TODO: enum for all day events?
impl<'a> GetValue<'a, NaiveDateTime> for Object {
fn get(&self, key: &'_ str) -> Option<NaiveDateTime> {
impl<'a> GetValue<'a, Timestamp> for Object {
fn get(&self, key: &'_ str) -> Option<Timestamp> {
let s = self.get(key)?;
NaiveDateTime::parse_from_str(s, "%Y%m%dT%H%M%SZ")
.or_else(|_| NaiveDateTime::parse_from_str(s, "%Y%m%dT%H%M%S"))
.map(Timestamp::DateTime)
.or_else(|_|
NaiveDate::parse_from_str(s, "%Y%m%d")
.map(|date| date.and_hms(0, 0, 0))
.map(Timestamp::Date)
)
.map_err(|e| println!("Cannot parse date: {:?}", e))
.ok()
}
}
#[derive(Debug, Clone)]
pub enum Timestamp {
Date(NaiveDate),
DateTime(NaiveDateTime),
}
impl Timestamp {
pub fn or_hms(self, hour: u32, min: u32, sec: u32) -> NaiveDateTime {
match self {
Timestamp::Date(date) => date.and_hms(hour, min, sec),
Timestamp::DateTime(date_time) => date_time,
}
}
}

9
src/main.rs

@ -17,7 +17,7 @@ use schema::{calendars::dsl::calendars};
mod model;
use model::{Calendar, NewCalendar, Event};
mod ics;
use ics::{Parser, Object, GetValue};
use ics::{Parser, Object, Timestamp, GetValue};
fn extract_vevent_objs(results: &mut Vec<Object>, mut obj: Object) {
let children = replace(&mut obj.children, vec![]);
@ -34,7 +34,10 @@ fn obj_to_event(calendar: String, obj: &Object) -> Option<Event> {
return None;
}
let dtstart = obj.get("DTSTART")?;
let dtstart: Timestamp = obj.get("DTSTART")?;
let dtstart = dtstart.or_hms(0, 0, 0);
let dtend: Option<Timestamp> = obj.get("DTEND");
let dtend = dtend.map(|time| time.or_hms(23, 59, 59));
let summary = obj.get("SUMMARY")?;
let id = format!("{}{}{}{}",
obj.get("UID").unwrap_or(""),
@ -45,7 +48,7 @@ fn obj_to_event(calendar: String, obj: &Object) -> Option<Event> {
Some(Event {
calendar, id,
dtstart,
dtend: obj.get("DTEND"),
dtend,
summary,
location: obj.get("LOCATION"),
url: obj.get("URL"),

Loading…
Cancel
Save