mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 21:42:26 +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 anyhow::{anyhow, Result};
|
||||||
use chrono::Duration;
|
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)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct EventObject {
|
pub struct EventObject {
|
||||||
@@ -12,47 +15,44 @@ pub struct EventObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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
|
// This is safe since we enforce the event's existance in the constructor
|
||||||
let dtstart = self
|
let dtstart = if let Some(Property {
|
||||||
.event
|
value: Some(value), ..
|
||||||
.get_property("DTSTART")
|
}) = self.event.get_property("DTSTART")
|
||||||
.ok_or(anyhow!("DTSTART property missing!"))?
|
{
|
||||||
.value
|
value
|
||||||
.to_owned()
|
} else {
|
||||||
.ok_or(anyhow!("DTSTART property has no value!"))?;
|
return Ok(None);
|
||||||
Ok(CalDateTime::from_str(&dtstart)?)
|
};
|
||||||
|
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
|
// This is safe since we enforce the event's existence in the constructor
|
||||||
if self.event.get_property("RRULE").is_some() {
|
if self.event.get_property("RRULE").is_some() {
|
||||||
// TODO: understand recurrence rules
|
// 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") {
|
if let Some(Property {
|
||||||
let dtend = dtend_prop
|
value: Some(dtend), ..
|
||||||
.value
|
}) = self.event.get_property("DTEND")
|
||||||
.to_owned()
|
{
|
||||||
.ok_or(anyhow!("DTEND property has no value!"))?;
|
return Ok(Some(CalDateTime::from_str(&dtend)?));
|
||||||
return Ok(CalDateTime::from_str(&dtend)?);
|
};
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(dtend_prop) = self.event.get_property("DURATION") {
|
let duration = if let Some(Property {
|
||||||
let duration = dtend_prop
|
value: Some(duration),
|
||||||
.value
|
..
|
||||||
.to_owned()
|
}) = self.event.get_property("DURATION")
|
||||||
.ok_or(anyhow!("DURATION property has no value!"))?;
|
{
|
||||||
let dtstart = self.get_first_occurence()?;
|
parse_duration(&duration)?
|
||||||
return Ok(dtstart + parse_duration(&duration)?);
|
} else {
|
||||||
}
|
Duration::days(1)
|
||||||
|
};
|
||||||
|
|
||||||
let dtstart = self.get_first_occurence()?;
|
let first_occurence = self.get_first_occurence()?;
|
||||||
if let CalDateTime::Date(_) = dtstart {
|
return Ok(first_occurence.map(|first_occurence| first_occurence + duration));
|
||||||
return Ok(dtstart + Duration::days(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
Err(anyhow!("help, couldn't determine any last occurence"))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user