mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 14:02:29 +00:00
Rewrite occurence getters for Event
This commit is contained in:
@@ -2,9 +2,12 @@ use std::str::FromStr;
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use chrono::Duration;
|
||||
use ical::{generator::IcalEvent, parser::Component};
|
||||
use ical::{generator::IcalEvent, parser::Component, property::Property};
|
||||
|
||||
use crate::timestamp::{parse_duration, CalDateTime};
|
||||
use crate::{
|
||||
timestamp::{parse_duration, CalDateTime},
|
||||
Error,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct EventObject {
|
||||
@@ -12,47 +15,44 @@ pub struct EventObject {
|
||||
}
|
||||
|
||||
impl EventObject {
|
||||
pub fn get_first_occurence(&self) -> Result<CalDateTime> {
|
||||
pub fn get_first_occurence(&self) -> Result<Option<CalDateTime>, Error> {
|
||||
// This is safe since we enforce the event's existance in the constructor
|
||||
let dtstart = self
|
||||
.event
|
||||
.get_property("DTSTART")
|
||||
.ok_or(anyhow!("DTSTART property missing!"))?
|
||||
.value
|
||||
.to_owned()
|
||||
.ok_or(anyhow!("DTSTART property has no value!"))?;
|
||||
Ok(CalDateTime::from_str(&dtstart)?)
|
||||
let dtstart = if let Some(Property {
|
||||
value: Some(value), ..
|
||||
}) = self.event.get_property("DTSTART")
|
||||
{
|
||||
value
|
||||
} else {
|
||||
return Ok(None);
|
||||
};
|
||||
Ok(Some(CalDateTime::from_str(&dtstart)?))
|
||||
}
|
||||
|
||||
pub fn get_last_occurence(&self) -> Result<CalDateTime> {
|
||||
pub fn get_last_occurence(&self) -> Result<Option<CalDateTime>, Error> {
|
||||
// This is safe since we enforce the event's existence in the constructor
|
||||
if self.event.get_property("RRULE").is_some() {
|
||||
// TODO: understand recurrence rules
|
||||
return Err(anyhow!("event is recurring, we cannot handle that yet"));
|
||||
return Err(anyhow!("event is recurring, we cannot handle that yet").into());
|
||||
}
|
||||
|
||||
if let Some(dtend_prop) = self.event.get_property("DTEND") {
|
||||
let dtend = dtend_prop
|
||||
.value
|
||||
.to_owned()
|
||||
.ok_or(anyhow!("DTEND property has no value!"))?;
|
||||
return Ok(CalDateTime::from_str(&dtend)?);
|
||||
}
|
||||
if let Some(Property {
|
||||
value: Some(dtend), ..
|
||||
}) = self.event.get_property("DTEND")
|
||||
{
|
||||
return Ok(Some(CalDateTime::from_str(&dtend)?));
|
||||
};
|
||||
|
||||
if let Some(dtend_prop) = self.event.get_property("DURATION") {
|
||||
let duration = dtend_prop
|
||||
.value
|
||||
.to_owned()
|
||||
.ok_or(anyhow!("DURATION property has no value!"))?;
|
||||
let dtstart = self.get_first_occurence()?;
|
||||
return Ok(dtstart + parse_duration(&duration)?);
|
||||
}
|
||||
let duration = if let Some(Property {
|
||||
value: Some(duration),
|
||||
..
|
||||
}) = self.event.get_property("DURATION")
|
||||
{
|
||||
parse_duration(&duration)?
|
||||
} else {
|
||||
Duration::days(1)
|
||||
};
|
||||
|
||||
let dtstart = self.get_first_occurence()?;
|
||||
if let CalDateTime::Date(_) = dtstart {
|
||||
return Ok(dtstart + Duration::days(1));
|
||||
}
|
||||
|
||||
Err(anyhow!("help, couldn't determine any last occurence"))
|
||||
let first_occurence = self.get_first_occurence()?;
|
||||
return Ok(first_occurence.map(|first_occurence| first_occurence + duration));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user