enhance dtend of all-day events

This commit is contained in:
Astro 2019-10-11 21:43:24 +02:00
parent 6da61433d9
commit e466c0a048
2 changed files with 25 additions and 7 deletions

View File

@ -33,17 +33,32 @@ impl<'a> GetValue<'a, String> for Object {
} }
// TODO: TZID, offset // TODO: TZID, offset
// TODO: enum for all day events? impl<'a> GetValue<'a, Timestamp> for Object {
impl<'a> GetValue<'a, NaiveDateTime> for Object { fn get(&self, key: &'_ str) -> Option<Timestamp> {
fn get(&self, key: &'_ str) -> Option<NaiveDateTime> {
let s = self.get(key)?; let s = self.get(key)?;
NaiveDateTime::parse_from_str(s, "%Y%m%dT%H%M%SZ") NaiveDateTime::parse_from_str(s, "%Y%m%dT%H%M%SZ")
.or_else(|_| NaiveDateTime::parse_from_str(s, "%Y%m%dT%H%M%S")) .or_else(|_| NaiveDateTime::parse_from_str(s, "%Y%m%dT%H%M%S"))
.map(Timestamp::DateTime)
.or_else(|_| .or_else(|_|
NaiveDate::parse_from_str(s, "%Y%m%d") 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)) .map_err(|e| println!("Cannot parse date: {:?}", e))
.ok() .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,
}
}
}

View File

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