migrate to new ical-rs version

This commit is contained in:
Lennart K
2026-01-07 11:32:53 +01:00
parent d84158e8ad
commit 69acde10ba
23 changed files with 227 additions and 1498 deletions

29
Cargo.lock generated
View File

@@ -1768,25 +1768,10 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "ical"
version = "0.11.0"
source = "git+https://github.com/lennart-k/ical-rs?rev=7c2ab1f3#7c2ab1f3abdca768f22d8a36627eebbdd7947e29"
dependencies = [
"chrono",
"chrono-tz",
"derive_more",
"itertools 0.14.0",
"lazy_static",
"regex",
"rrule",
"thiserror 2.0.17",
]
[[package]] [[package]]
name = "ical" name = "ical"
version = "0.12.0-dev" version = "0.12.0-dev"
source = "git+https://github.com/lennart-k/ical-rs?branch=dev#d2226f6b92fa45dcc5a243adc57e6a07a67741a8" source = "git+https://github.com/lennart-k/ical-rs?branch=dev#5e61c25646c3785448d349e7d18b2833fc483c53"
dependencies = [ dependencies = [
"chrono", "chrono",
"chrono-tz", "chrono-tz",
@@ -3343,7 +3328,6 @@ dependencies = [
"figment", "figment",
"headers", "headers",
"http", "http",
"ical 0.12.0-dev",
"insta", "insta",
"opentelemetry", "opentelemetry",
"opentelemetry-otlp", "opentelemetry-otlp",
@@ -3391,7 +3375,7 @@ dependencies = [
"futures-util", "futures-util",
"headers", "headers",
"http", "http",
"ical 0.11.0", "ical",
"insta", "insta",
"percent-encoding", "percent-encoding",
"quick-xml", "quick-xml",
@@ -3430,7 +3414,7 @@ dependencies = [
"derive_more", "derive_more",
"futures-util", "futures-util",
"http", "http",
"ical 0.11.0", "ical",
"insta", "insta",
"percent-encoding", "percent-encoding",
"quick-xml", "quick-xml",
@@ -3463,7 +3447,7 @@ dependencies = [
"futures-util", "futures-util",
"headers", "headers",
"http", "http",
"ical 0.11.0", "ical",
"itertools 0.14.0", "itertools 0.14.0",
"log", "log",
"matchit 0.9.1", "matchit 0.9.1",
@@ -3547,7 +3531,7 @@ dependencies = [
"chrono", "chrono",
"chrono-tz", "chrono-tz",
"derive_more", "derive_more",
"ical 0.11.0", "ical",
"regex", "regex",
"rrule", "rrule",
"rstest", "rstest",
@@ -3588,7 +3572,7 @@ dependencies = [
"futures-core", "futures-core",
"headers", "headers",
"http", "http",
"ical 0.11.0", "ical",
"regex", "regex",
"rrule", "rrule",
"rstest", "rstest",
@@ -3615,6 +3599,7 @@ dependencies = [
"chrono", "chrono",
"criterion", "criterion",
"derive_more", "derive_more",
"ical",
"password-auth", "password-auth",
"password-hash", "password-hash",
"pbkdf2", "pbkdf2",

View File

@@ -107,7 +107,9 @@ strum = "0.27"
strum_macros = "0.27" strum_macros = "0.27"
serde_json = { version = "1.0", features = ["raw_value"] } serde_json = { version = "1.0", features = ["raw_value"] }
sqlx-sqlite = { version = "0.8", features = ["bundled"] } sqlx-sqlite = { version = "0.8", features = ["bundled"] }
ical = { git = "https://github.com/lennart-k/ical-rs", rev = "7c2ab1f3" } ical = { git = "https://github.com/lennart-k/ical-rs", branch = "dev", features = [
"chrono-tz",
] }
toml = "0.9" toml = "0.9"
tower = "0.5" tower = "0.5"
tower-http = { version = "0.6", features = [ tower-http = { version = "0.6", features = [
@@ -199,7 +201,3 @@ tower-http.workspace = true
axum-extra.workspace = true axum-extra.workspace = true
headers.workspace = true headers.workspace = true
http.workspace = true http.workspace = true
# TODO: Remove in next major release
ical_dev = { package = "ical", git = "https://github.com/lennart-k/ical-rs", branch = "dev", features = [
"chrono-tz",
] }

View File

@@ -5,11 +5,9 @@ use axum::extract::State;
use axum::{extract::Path, response::Response}; use axum::{extract::Path, response::Response};
use headers::{ContentType, HeaderMapExt}; use headers::{ContentType, HeaderMapExt};
use http::{HeaderValue, Method, StatusCode, header}; use http::{HeaderValue, Method, StatusCode, header};
use ical::builder::calendar::IcalCalendarBuilder;
use ical::generator::Emitter; use ical::generator::Emitter;
use ical::property::Property; use ical::property::ContentLine;
use percent_encoding::{CONTROLS, utf8_percent_encode}; use percent_encoding::{CONTROLS, utf8_percent_encode};
use rustical_ical::{CalendarObjectComponent, EventObject};
use rustical_store::{CalendarStore, SubscriptionStore, auth::Principal}; use rustical_store::{CalendarStore, SubscriptionStore, auth::Principal};
use std::collections::HashMap; use std::collections::HashMap;
use std::str::FromStr; use std::str::FromStr;
@@ -33,77 +31,79 @@ pub async fn route_get<C: CalendarStore, S: SubscriptionStore>(
return Err(crate::Error::Unauthorized); return Err(crate::Error::Unauthorized);
} }
let mut vtimezones = HashMap::new(); // let mut vtimezones = HashMap::new();
let objects = cal_store.get_objects(&principal, &calendar_id).await?; // let objects = cal_store.get_objects(&principal, &calendar_id).await?;
let mut ical_calendar_builder = IcalCalendarBuilder::version("2.0") todo!()
.gregorian()
.prodid("RustiCal");
if let Some(displayname) = calendar.meta.displayname {
ical_calendar_builder = ical_calendar_builder.set(Property {
name: "X-WR-CALNAME".to_owned(),
value: Some(displayname),
params: vec![],
});
}
if let Some(description) = calendar.meta.description {
ical_calendar_builder = ical_calendar_builder.set(Property {
name: "X-WR-CALDESC".to_owned(),
value: Some(description),
params: vec![],
});
}
if let Some(timezone_id) = calendar.timezone_id {
ical_calendar_builder = ical_calendar_builder.set(Property {
name: "X-WR-TIMEZONE".to_owned(),
value: Some(timezone_id),
params: vec![],
});
}
for object in &objects { // let mut ical_calendar_builder = IcalCalendarBuilder::version("2.0")
vtimezones.extend(object.get_vtimezones()); // .gregorian()
match object.get_data() { // .prodid("RustiCal");
CalendarObjectComponent::Event(EventObject { event, .. }, overrides) => { // if let Some(displayname) = calendar.meta.displayname {
ical_calendar_builder = ical_calendar_builder // ical_calendar_builder = ical_calendar_builder.set(ContentLine {
.add_event(event.clone()) // name: "X-WR-CALNAME".to_owned(),
.add_events(overrides.iter().map(|ev| ev.event.clone())); // value: Some(displayname),
} // params: vec![].into(),
CalendarObjectComponent::Todo(todo, overrides) => { // });
ical_calendar_builder = ical_calendar_builder // }
.add_todo(todo.clone()) // if let Some(description) = calendar.meta.description {
.add_todos(overrides.iter().cloned()); // ical_calendar_builder = ical_calendar_builder.set(ContentLine {
} // name: "X-WR-CALDESC".to_owned(),
CalendarObjectComponent::Journal(journal, overrides) => { // value: Some(description),
ical_calendar_builder = ical_calendar_builder // params: vec![].into(),
.add_journal(journal.clone()) // });
.add_journals(overrides.iter().cloned()); // }
} // if let Some(timezone_id) = calendar.timezone_id {
} // ical_calendar_builder = ical_calendar_builder.set(ContentLine {
} // name: "X-WR-TIMEZONE".to_owned(),
// value: Some(timezone_id),
ical_calendar_builder = ical_calendar_builder.add_timezones(vtimezones.into_values().cloned()); // params: vec![].into(),
// });
let ical_calendar = ical_calendar_builder // }
.build() //
.map_err(|parser_error| Error::IcalError(parser_error.into()))?; // for object in &objects {
// vtimezones.extend(object.get_vtimezones());
let mut resp = Response::builder().status(StatusCode::OK); // match object.get_data() {
let hdrs = resp.headers_mut().unwrap(); // CalendarObjectComponent::Event(EventObject { event, .. }, overrides) => {
hdrs.typed_insert(ContentType::from_str("text/calendar; charset=utf-8").unwrap()); // ical_calendar_builder = ical_calendar_builder
// .add_event(event.clone())
let filename = format!("{}_{}.ics", calendar.principal, calendar.id); // .add_events(overrides.iter().map(|ev| ev.event.clone()));
let filename = utf8_percent_encode(&filename, CONTROLS); // }
hdrs.insert( // CalendarObjectComponent::Todo(todo, overrides) => {
header::CONTENT_DISPOSITION, // ical_calendar_builder = ical_calendar_builder
HeaderValue::from_str(&format!( // .add_todo(todo.clone())
"attachement; filename*=UTF-8''{filename}; filename={filename}", // .add_todos(overrides.iter().cloned());
)) // }
.unwrap(), // CalendarObjectComponent::Journal(journal, overrides) => {
); // ical_calendar_builder = ical_calendar_builder
if matches!(method, Method::HEAD) { // .add_journal(journal.clone())
Ok(resp.body(Body::empty()).unwrap()) // .add_journals(overrides.iter().cloned());
} else { // }
Ok(resp.body(Body::new(ical_calendar.generate())).unwrap()) // }
} // }
//
// ical_calendar_builder = ical_calendar_builder.add_timezones(vtimezones.into_values().cloned());
//
// let ical_calendar = ical_calendar_builder
// .build()
// .map_err(|parser_error| Error::IcalError(parser_error.into()))?;
//
// let mut resp = Response::builder().status(StatusCode::OK);
// let hdrs = resp.headers_mut().unwrap();
// hdrs.typed_insert(ContentType::from_str("text/calendar; charset=utf-8").unwrap());
//
// let filename = format!("{}_{}.ics", calendar.principal, calendar.id);
// let filename = utf8_percent_encode(&filename, CONTROLS);
// hdrs.insert(
// header::CONTENT_DISPOSITION,
// HeaderValue::from_str(&format!(
// "attachement; filename*=UTF-8''{filename}; filename={filename}",
// ))
// .unwrap(),
// );
// if matches!(method, Method::HEAD) {
// Ok(resp.body(Body::empty()).unwrap())
// } else {
// Ok(resp.body(Body::new(ical_calendar.generate())).unwrap())
// }
} }

View File

@@ -5,10 +5,7 @@ use axum::{
response::{IntoResponse, Response}, response::{IntoResponse, Response},
}; };
use http::StatusCode; use http::StatusCode;
use ical::{ use ical::{generator::Emitter, parser::Component};
generator::Emitter,
parser::{Component, ComponentMut},
};
use rustical_dav::header::Overwrite; use rustical_dav::header::Overwrite;
use rustical_ical::{CalendarObject, CalendarObjectType}; use rustical_ical::{CalendarObject, CalendarObjectType};
use rustical_store::{ use rustical_store::{
@@ -53,58 +50,59 @@ pub async fn route_import<C: CalendarStore, S: SubscriptionStore>(
.get_property("X-WR-TIMEZONE") .get_property("X-WR-TIMEZONE")
.and_then(|prop| prop.value.clone()); .and_then(|prop| prop.value.clone());
// These properties should not appear in the expanded calendar objects // These properties should not appear in the expanded calendar objects
cal.remove_property("X-WR-CALNAME"); todo!();
cal.remove_property("X-WR-CALDESC"); // cal.remove_property("X-WR-CALNAME");
cal.remove_property("X-WR-TIMEZONE"); // cal.remove_property("X-WR-CALDESC");
let cal = cal.verify().unwrap(); // cal.remove_property("X-WR-TIMEZONE");
// Make sure timezone is valid // let cal = cal.verify().unwrap();
if let Some(timezone_id) = timezone_id.as_ref() { // // Make sure timezone is valid
assert!( // if let Some(timezone_id) = timezone_id.as_ref() {
vtimezones_rs::VTIMEZONES.contains_key(timezone_id), // assert!(
"Invalid calendar timezone id" // vtimezones_rs::VTIMEZONES.contains_key(timezone_id),
); // "Invalid calendar timezone id"
} // );
// }
// Extract necessary component types //
let mut cal_components = vec![]; // // Extract necessary component types
if !cal.events.is_empty() { // let mut cal_components = vec![];
cal_components.push(CalendarObjectType::Event); // if !cal.events.is_empty() {
} // cal_components.push(CalendarObjectType::Event);
if !cal.journals.is_empty() { // }
cal_components.push(CalendarObjectType::Journal); // if !cal.journals.is_empty() {
} // cal_components.push(CalendarObjectType::Journal);
if !cal.todos.is_empty() { // }
cal_components.push(CalendarObjectType::Todo); // if !cal.todos.is_empty() {
} // cal_components.push(CalendarObjectType::Todo);
// }
let expanded_cals = cal.expand_calendar(); //
// Janky way to convert between IcalCalendar and CalendarObject // let expanded_cals = cal.expand_calendar();
let objects = expanded_cals // // Janky way to convert between IcalCalendar and CalendarObject
.into_iter() // let objects = expanded_cals
.map(|cal| cal.generate()) // .into_iter()
.map(|ics| CalendarObject::from_ics(ics, None)) // .map(|cal| cal.generate())
.collect::<Result<Vec<_>, _>>()?; // .map(|ics| CalendarObject::from_ics(ics, None))
let new_cal = Calendar { // .collect::<Result<Vec<_>, _>>()?;
principal, // let new_cal = Calendar {
id: cal_id, // principal,
meta: CalendarMetadata { // id: cal_id,
displayname, // meta: CalendarMetadata {
order: 0, // displayname,
description, // order: 0,
color: None, // description,
}, // color: None,
timezone_id, // },
deleted_at: None, // timezone_id,
synctoken: 0, // deleted_at: None,
subscription_url: None, // synctoken: 0,
push_topic: uuid::Uuid::new_v4().to_string(), // subscription_url: None,
components: cal_components, // push_topic: uuid::Uuid::new_v4().to_string(),
}; // components: cal_components,
// };
let cal_store = resource_service.cal_store; //
cal_store // let cal_store = resource_service.cal_store;
.import_calendar(new_cal, objects, overwrite) // cal_store
.await?; // .import_calendar(new_cal, objects, overwrite)
// .await?;
Ok(StatusCode::OK.into_response()) //
// Ok(StatusCode::OK.into_response())
} }

View File

@@ -92,10 +92,11 @@ pub async fn route_mkcalendar<C: CalendarStore, S: SubscriptionStore>(
.ok_or_else(|| rustical_dav::Error::BadRequest("No timezone data provided".to_owned()))? .ok_or_else(|| rustical_dav::Error::BadRequest("No timezone data provided".to_owned()))?
.map_err(|_| rustical_dav::Error::BadRequest("Error parsing timezone".to_owned()))?; .map_err(|_| rustical_dav::Error::BadRequest("Error parsing timezone".to_owned()))?;
let timezone = calendar.timezones.first().ok_or_else(|| { let timezone = calendar.vtimezones.first().ok_or_else(|| {
rustical_dav::Error::BadRequest("No timezone data provided".to_owned()) rustical_dav::Error::BadRequest("No timezone data provided".to_owned())
})?; })?;
let timezone: chrono_tz::Tz = timezone.try_into().map_err(|_| { let timezone: Option<chrono_tz::Tz> = timezone.into();
let timezone = timezone.ok_or_else(|| {
rustical_dav::Error::BadRequest("Cannot translate VTIMEZONE into IANA TZID".to_owned()) rustical_dav::Error::BadRequest("Cannot translate VTIMEZONE into IANA TZID".to_owned())
})?; })?;

View File

@@ -1,9 +1,10 @@
use crate::calendar::methods::report::calendar_query::{ use crate::calendar::methods::report::calendar_query::{
TimeRangeElement, TimeRangeElement, prop_filter::PropFilterElement,
prop_filter::{PropFilterElement, PropFilterable}, };
use ical::{
component::IcalCalendarObject,
parser::{Component, ical::component::IcalTimeZone},
}; };
use ical::parser::ical::component::IcalTimeZone;
use rustical_ical::{CalendarObject, CalendarObjectComponent, CalendarObjectType};
use rustical_xml::XmlDeserialize; use rustical_xml::XmlDeserialize;
#[derive(XmlDeserialize, Clone, Debug, PartialEq)] #[derive(XmlDeserialize, Clone, Debug, PartialEq)]
@@ -24,9 +25,7 @@ pub struct CompFilterElement {
pub(crate) name: String, pub(crate) name: String,
} }
pub trait CompFilterable: PropFilterable + Sized { pub trait CompFilterable: Component + Sized {
fn get_comp_name(&self) -> &'static str;
fn match_time_range(&self, time_range: &TimeRangeElement) -> bool; fn match_time_range(&self, time_range: &TimeRangeElement) -> bool;
fn match_subcomponents(&self, comp_filter: &CompFilterElement) -> bool; fn match_subcomponents(&self, comp_filter: &CompFilterElement) -> bool;
@@ -68,11 +67,7 @@ pub trait CompFilterable: PropFilterable + Sized {
} }
} }
impl CompFilterable for CalendarObject { impl CompFilterable for IcalCalendarObject {
fn get_comp_name(&self) -> &'static str {
"VCALENDAR"
}
fn match_time_range(&self, _time_range: &TimeRangeElement) -> bool { fn match_time_range(&self, _time_range: &TimeRangeElement) -> bool {
// VCALENDAR has no concept of time range // VCALENDAR has no concept of time range
false false
@@ -83,7 +78,7 @@ impl CompFilterable for CalendarObject {
.get_vtimezones() .get_vtimezones()
.values() .values()
.map(|tz| tz.matches(comp_filter)) .map(|tz| tz.matches(comp_filter))
.chain([self.get_data().matches(comp_filter)]); .chain([self.matches(comp_filter)]);
if comp_filter.is_not_defined.is_some() { if comp_filter.is_not_defined.is_some() {
matches.all(|x| x) matches.all(|x| x)
@@ -94,10 +89,6 @@ impl CompFilterable for CalendarObject {
} }
impl CompFilterable for IcalTimeZone { impl CompFilterable for IcalTimeZone {
fn get_comp_name(&self) -> &'static str {
"VTIMEZONE"
}
fn match_time_range(&self, _time_range: &TimeRangeElement) -> bool { fn match_time_range(&self, _time_range: &TimeRangeElement) -> bool {
false false
} }
@@ -107,33 +98,6 @@ impl CompFilterable for IcalTimeZone {
} }
} }
impl CompFilterable for CalendarObjectComponent {
fn get_comp_name(&self) -> &'static str {
CalendarObjectType::from(self).as_str()
}
fn match_time_range(&self, time_range: &TimeRangeElement) -> bool {
if let Some(start) = &time_range.start
&& let Some(last_occurence) = self.get_last_occurence().unwrap_or(None)
&& **start > last_occurence.utc()
{
return false;
}
if let Some(end) = &time_range.end
&& let Some(first_occurence) = self.get_first_occurence().unwrap_or(None)
&& **end < first_occurence.utc()
{
return false;
}
true
}
fn match_subcomponents(&self, _comp_filter: &CompFilterElement) -> bool {
// TODO: Properly check subcomponents
true
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use chrono::{TimeZone, Utc}; use chrono::{TimeZone, Utc};

View File

@@ -1,8 +1,8 @@
use super::comp_filter::{CompFilterElement, CompFilterable}; use super::comp_filter::{CompFilterElement, CompFilterable};
use crate::calendar_object::CalendarObjectPropWrapperName; use crate::calendar_object::CalendarObjectPropWrapperName;
use ical::property::Property; use ical::{component::IcalCalendarObject, property::ContentLine};
use rustical_dav::xml::{PropfindType, TextMatchElement}; use rustical_dav::xml::{PropfindType, TextMatchElement};
use rustical_ical::{CalendarObject, UtcDateTime}; use rustical_ical::UtcDateTime;
use rustical_store::calendar_store::CalendarQuery; use rustical_store::calendar_store::CalendarQuery;
use rustical_xml::{XmlDeserialize, XmlRootTag}; use rustical_xml::{XmlDeserialize, XmlRootTag};
@@ -30,8 +30,8 @@ pub struct ParamFilterElement {
impl ParamFilterElement { impl ParamFilterElement {
#[must_use] #[must_use]
pub fn match_property(&self, prop: &Property) -> bool { pub fn match_property(&self, prop: &ContentLine) -> bool {
let Some(param) = prop.get_param(&self.name) else { let Some(param) = prop.params.get_param(&self.name) else {
return self.is_not_defined.is_some(); return self.is_not_defined.is_some();
}; };
if self.is_not_defined.is_some() { if self.is_not_defined.is_some() {
@@ -57,7 +57,7 @@ pub struct FilterElement {
impl FilterElement { impl FilterElement {
#[must_use] #[must_use]
pub fn matches(&self, cal_object: &CalendarObject) -> bool { pub fn matches(&self, cal_object: &IcalCalendarObject) -> bool {
cal_object.matches(&self.comp_filter) cal_object.matches(&self.comp_filter)
} }
} }

View File

@@ -11,7 +11,7 @@ mod tests;
pub use comp_filter::{CompFilterElement, CompFilterable}; pub use comp_filter::{CompFilterElement, CompFilterable};
pub use elements::*; pub use elements::*;
#[allow(unused_imports)] #[allow(unused_imports)]
pub use prop_filter::{PropFilterElement, PropFilterable}; pub use prop_filter::PropFilterElement;
pub async fn get_objects_calendar_query<C: CalendarStore>( pub async fn get_objects_calendar_query<C: CalendarStore>(
cal_query: &CalendarQueryRequest, cal_query: &CalendarQueryRequest,
@@ -23,7 +23,7 @@ pub async fn get_objects_calendar_query<C: CalendarStore>(
.calendar_query(principal, cal_id, cal_query.into()) .calendar_query(principal, cal_id, cal_query.into())
.await?; .await?;
if let Some(filter) = &cal_query.filter { if let Some(filter) = &cal_query.filter {
objects.retain(|object| filter.matches(object)); objects.retain(|object| filter.matches(object.get_inner()));
} }
Ok(objects) Ok(objects)
} }

View File

@@ -1,14 +1,7 @@
use super::{ParamFilterElement, TimeRangeElement}; use super::{ParamFilterElement, TimeRangeElement};
use ical::{ use ical::{parser::Component, property::ContentLine, types::CalDateTime};
generator::{IcalCalendar, IcalEvent},
parser::{
Component,
ical::component::{IcalJournal, IcalTimeZone, IcalTodo},
},
property::Property,
};
use rustical_dav::xml::TextMatchElement; use rustical_dav::xml::TextMatchElement;
use rustical_ical::{CalDateTime, CalendarObject, CalendarObjectComponent, UtcDateTime}; use rustical_ical::UtcDateTime;
use rustical_xml::XmlDeserialize; use rustical_xml::XmlDeserialize;
use std::collections::HashMap; use std::collections::HashMap;
@@ -31,7 +24,7 @@ pub struct PropFilterElement {
impl PropFilterElement { impl PropFilterElement {
#[must_use] #[must_use]
pub fn match_property(&self, property: &Property) -> bool { pub fn match_property(&self, property: &ContentLine) -> bool {
if let Some(TimeRangeElement { start, end }) = &self.time_range { if let Some(TimeRangeElement { start, end }) = &self.time_range {
// TODO: Respect timezones // TODO: Respect timezones
let Ok(timestamp) = CalDateTime::parse_prop(property, &HashMap::default()) else { let Ok(timestamp) = CalDateTime::parse_prop(property, &HashMap::default()) else {
@@ -68,7 +61,7 @@ impl PropFilterElement {
true true
} }
pub fn match_component(&self, comp: &impl PropFilterable) -> bool { pub fn match_component(&self, comp: &impl Component) -> bool {
let properties = comp.get_named_properties(&self.name); let properties = comp.get_named_properties(&self.name);
if self.is_not_defined.is_some() { if self.is_not_defined.is_some() {
return properties.is_empty(); return properties.is_empty();
@@ -79,53 +72,3 @@ impl PropFilterElement {
properties.iter().any(|prop| self.match_property(prop)) properties.iter().any(|prop| self.match_property(prop))
} }
} }
pub trait PropFilterable {
fn get_named_properties(&self, name: &str) -> Vec<&Property>;
}
impl PropFilterable for CalendarObject {
fn get_named_properties(&self, name: &str) -> Vec<&Property> {
Self::get_named_properties(self, name)
}
}
impl PropFilterable for IcalEvent {
fn get_named_properties(&self, name: &str) -> Vec<&Property> {
Component::get_named_properties(self, name)
}
}
impl PropFilterable for IcalTodo {
fn get_named_properties(&self, name: &str) -> Vec<&Property> {
Component::get_named_properties(self, name)
}
}
impl PropFilterable for IcalJournal {
fn get_named_properties(&self, name: &str) -> Vec<&Property> {
Component::get_named_properties(self, name)
}
}
impl PropFilterable for IcalCalendar {
fn get_named_properties(&self, name: &str) -> Vec<&Property> {
Component::get_named_properties(self, name)
}
}
impl PropFilterable for IcalTimeZone {
fn get_named_properties(&self, name: &str) -> Vec<&Property> {
Component::get_named_properties(self, name)
}
}
impl PropFilterable for CalendarObjectComponent {
fn get_named_properties(&self, name: &str) -> Vec<&Property> {
match self {
Self::Event(event, _) => PropFilterable::get_named_properties(&event.event, name),
Self::Todo(todo, _) => PropFilterable::get_named_properties(todo, name),
Self::Journal(journal, _) => PropFilterable::get_named_properties(journal, name),
}
}
}

View File

@@ -4,6 +4,7 @@ use crate::calendar::prop::{ReportMethod, SupportedCollationSet};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use derive_more::derive::{From, Into}; use derive_more::derive::{From, Into};
use ical::IcalParser; use ical::IcalParser;
use ical::types::CalDateTime;
use rustical_dav::extensions::{ use rustical_dav::extensions::{
CommonPropertiesExtension, CommonPropertiesProp, SyncTokenExtension, SyncTokenExtensionProp, CommonPropertiesExtension, CommonPropertiesProp, SyncTokenExtension, SyncTokenExtensionProp,
}; };
@@ -11,7 +12,6 @@ use rustical_dav::privileges::UserPrivilegeSet;
use rustical_dav::resource::{PrincipalUri, Resource, ResourceName}; use rustical_dav::resource::{PrincipalUri, Resource, ResourceName};
use rustical_dav::xml::{HrefElement, Resourcetype, ResourcetypeInner, SupportedReportSet}; use rustical_dav::xml::{HrefElement, Resourcetype, ResourcetypeInner, SupportedReportSet};
use rustical_dav_push::{DavPushExtension, DavPushExtensionProp}; use rustical_dav_push::{DavPushExtension, DavPushExtensionProp};
use rustical_ical::CalDateTime;
use rustical_store::Calendar; use rustical_store::Calendar;
use rustical_store::auth::Principal; use rustical_store::auth::Principal;
use rustical_xml::{EnumVariants, PropName}; use rustical_xml::{EnumVariants, PropName};
@@ -215,13 +215,13 @@ impl Resource for CalendarResource {
) )
})?; })?;
let timezone = calendar.timezones.first().ok_or_else(|| { let timezone = calendar.vtimezones.first().ok_or_else(|| {
rustical_dav::Error::BadRequest("No timezone data provided".to_owned()) rustical_dav::Error::BadRequest("No timezone data provided".to_owned())
})?; })?;
let timezone: chrono_tz::Tz = timezone.try_into().map_err(|_| { let timezone: Option<chrono_tz::Tz> = timezone.into();
let timezone = timezone.ok_or_else(|| {
rustical_dav::Error::BadRequest("No timezone data provided".to_owned()) rustical_dav::Error::BadRequest("No timezone data provided".to_owned())
})?; })?;
self.cal.timezone_id = Some(timezone.name().to_owned()); self.cal.timezone_id = Some(timezone.name().to_owned());
} }
Ok(()) Ok(())

View File

@@ -1,4 +1,4 @@
use std::io::BufReader; use std::{collections::HashMap, io::BufReader};
use crate::Error; use crate::Error;
use crate::addressbook::AddressbookResourceService; use crate::addressbook::AddressbookResourceService;
@@ -9,7 +9,7 @@ use axum::{
use http::StatusCode; use http::StatusCode;
use ical::{ use ical::{
parser::{Component, ComponentMut, vcard}, parser::{Component, ComponentMut, vcard},
property::Property, property::ContentLine,
}; };
use rustical_store::{Addressbook, AddressbookStore, SubscriptionStore, auth::Principal}; use rustical_store::{Addressbook, AddressbookStore, SubscriptionStore, auth::Principal};
use tracing::instrument; use tracing::instrument;
@@ -33,12 +33,12 @@ pub async fn route_import<AS: AddressbookStore, S: SubscriptionStore>(
let uid = card.get_uid(); let uid = card.get_uid();
if uid.is_none() { if uid.is_none() {
let mut card_mut = card.mutable(); let mut card_mut = card.mutable();
card_mut.set_property(Property { card_mut.add_content_line(ContentLine {
name: "UID".to_owned(), name: "UID".to_owned(),
value: Some(uuid::Uuid::new_v4().to_string()), value: Some(uuid::Uuid::new_v4().to_string()),
params: vec![], params: vec![].into(),
}); });
card = card_mut.verify().unwrap(); card = card_mut.build(&HashMap::new()).unwrap();
} }
objects.push(card.try_into().unwrap()); objects.push(card.try_into().unwrap());

View File

@@ -3,7 +3,7 @@ use crate::{
addressbook::methods::report::addressbook_query::PropFilterElement, addressbook::methods::report::addressbook_query::PropFilterElement,
}; };
use derive_more::{From, Into}; use derive_more::{From, Into};
use ical::property::Property; use ical::property::ContentLine;
use rustical_dav::xml::{PropfindType, TextMatchElement}; use rustical_dav::xml::{PropfindType, TextMatchElement};
use rustical_ical::{AddressObject, UtcDateTime}; use rustical_ical::{AddressObject, UtcDateTime};
use rustical_xml::{ValueDeserialize, XmlDeserialize, XmlRootTag}; use rustical_xml::{ValueDeserialize, XmlDeserialize, XmlRootTag};
@@ -32,8 +32,8 @@ pub struct ParamFilterElement {
impl ParamFilterElement { impl ParamFilterElement {
#[must_use] #[must_use]
pub fn match_property(&self, prop: &Property) -> bool { pub fn match_property(&self, prop: &ContentLine) -> bool {
let Some(param) = prop.get_param(&self.name) else { let Some(param) = prop.params.get_param(&self.name) else {
return self.is_not_defined.is_some(); return self.is_not_defined.is_some();
}; };
if self.is_not_defined.is_some() { if self.is_not_defined.is_some() {

View File

@@ -1,5 +1,5 @@
use super::{Allof, ParamFilterElement}; use super::{Allof, ParamFilterElement};
use ical::{parser::Component, property::Property}; use ical::{parser::Component, property::ContentLine};
use rustical_dav::xml::TextMatchElement; use rustical_dav::xml::TextMatchElement;
use rustical_ical::AddressObject; use rustical_ical::AddressObject;
use rustical_xml::XmlDeserialize; use rustical_xml::XmlDeserialize;
@@ -31,7 +31,7 @@ pub struct PropFilterElement {
impl PropFilterElement { impl PropFilterElement {
#[must_use] #[must_use]
pub fn match_property(&self, property: &Property) -> bool { pub fn match_property(&self, property: &ContentLine) -> bool {
if self.param_filter.is_empty() && self.text_match.is_empty() { if self.param_filter.is_empty() && self.text_match.is_empty() {
// Filter empty // Filter empty
return true; return true;
@@ -67,11 +67,11 @@ impl PropFilterElement {
} }
pub trait PropFilterable { pub trait PropFilterable {
fn get_named_properties(&self, name: &str) -> Vec<&Property>; fn get_named_properties(&self, name: &str) -> Vec<&ContentLine>;
} }
impl PropFilterable for AddressObject { impl PropFilterable for AddressObject {
fn get_named_properties(&self, name: &str) -> Vec<&Property> { fn get_named_properties(&self, name: &str) -> Vec<&ContentLine> {
self.get_vcard().get_named_properties(name) self.get_vcard().get_named_properties(name)
} }
} }

View File

@@ -1,4 +1,4 @@
use ical::property::Property; use ical::property::ContentLine;
use rustical_xml::{ValueDeserialize, XmlDeserialize}; use rustical_xml::{ValueDeserialize, XmlDeserialize};
use std::borrow::Cow; use std::borrow::Cow;
@@ -128,7 +128,7 @@ impl TextMatchElement {
negate_condition.0 ^ matches negate_condition.0 ^ matches
} }
#[must_use] #[must_use]
pub fn match_property(&self, property: &Property) -> bool { pub fn match_property(&self, property: &ContentLine) -> bool {
let text = property.value.as_deref().unwrap_or(""); let text = property.value.as_deref().unwrap_or("");
self.match_text(text) self.match_text(text)
} }

View File

@@ -1,11 +1,10 @@
use crate::{CalDateTime, LOCAL_DATE};
use crate::{CalendarObject, Error}; use crate::{CalendarObject, Error};
use chrono::Datelike;
use ical::generator::Emitter; use ical::generator::Emitter;
use ical::parser::{ use ical::parser::{
Component, Component,
vcard::{self, component::VcardContact}, vcard::{self, component::VcardContact},
}; };
use ical::types::CalDateTime;
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
use std::{collections::HashMap, io::BufReader}; use std::{collections::HashMap, io::BufReader};
@@ -65,14 +64,16 @@ impl AddressObject {
#[must_use] #[must_use]
pub fn get_anniversary(&self) -> Option<(CalDateTime, bool)> { pub fn get_anniversary(&self) -> Option<(CalDateTime, bool)> {
let prop = self.vcard.get_property("ANNIVERSARY")?.value.as_deref()?; // let prop = self.vcard.get_property("ANNIVERSARY")?.value.as_deref()?;
CalDateTime::parse_vcard(prop).ok() // CalDateTime::parse_vcard(prop).ok()
todo!()
} }
#[must_use] #[must_use]
pub fn get_birthday(&self) -> Option<(CalDateTime, bool)> { pub fn get_birthday(&self) -> Option<(CalDateTime, bool)> {
let prop = self.vcard.get_property("BDAY")?.value.as_deref()?; todo!()
CalDateTime::parse_vcard(prop).ok() // let prop = self.vcard.get_property("BDAY")?.value.as_deref()?;
// CalDateTime::parse_vcard(prop).ok()
} }
#[must_use] #[must_use]
@@ -82,90 +83,11 @@ impl AddressObject {
} }
pub fn get_anniversary_object(&self) -> Result<Option<CalendarObject>, Error> { pub fn get_anniversary_object(&self) -> Result<Option<CalendarObject>, Error> {
Ok( todo!();
if let Some((anniversary, contains_year)) = self.get_anniversary() {
let Some(fullname) = self.get_full_name() else {
return Ok(None);
};
let anniversary = anniversary.date();
let year = contains_year.then_some(anniversary.year());
let anniversary_start = anniversary.format(LOCAL_DATE);
let anniversary_end = anniversary
.succ_opt()
.unwrap_or(anniversary)
.format(LOCAL_DATE);
let uid = format!("{}-anniversary", self.get_id());
let year_suffix = year.map(|year| format!(" ({year})")).unwrap_or_default();
Some(CalendarObject::from_ics(
format!(
r"BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:-//github.com/lennart-k/rustical birthday calendar//EN
BEGIN:VEVENT
DTSTART;VALUE=DATE:{anniversary_start}
DTEND;VALUE=DATE:{anniversary_end}
UID:{uid}
RRULE:FREQ=YEARLY
SUMMARY:💍 {fullname}{year_suffix}
TRANSP:TRANSPARENT
BEGIN:VALARM
TRIGGER;VALUE=DURATION:-PT0M
ACTION:DISPLAY
DESCRIPTION:💍 {fullname}{year_suffix}
END:VALARM
END:VEVENT
END:VCALENDAR",
),
None,
)?)
} else {
None
},
)
} }
pub fn get_birthday_object(&self) -> Result<Option<CalendarObject>, Error> { pub fn get_birthday_object(&self) -> Result<Option<CalendarObject>, Error> {
Ok( todo!();
if let Some((birthday, contains_year)) = self.get_birthday() {
let Some(fullname) = self.get_full_name() else {
return Ok(None);
};
let birthday = birthday.date();
let year = contains_year.then_some(birthday.year());
let birthday_start = birthday.format(LOCAL_DATE);
let birthday_end = birthday.succ_opt().unwrap_or(birthday).format(LOCAL_DATE);
let uid = format!("{}-birthday", self.get_id());
let year_suffix = year.map(|year| format!(" ({year})")).unwrap_or_default();
Some(CalendarObject::from_ics(
format!(
r"BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:-//github.com/lennart-k/rustical birthday calendar//EN
BEGIN:VEVENT
DTSTART;VALUE=DATE:{birthday_start}
DTEND;VALUE=DATE:{birthday_end}
UID:{uid}
RRULE:FREQ=YEARLY
SUMMARY:🎂 {fullname}{year_suffix}
TRANSP:TRANSPARENT
BEGIN:VALARM
TRIGGER;VALUE=DURATION:-PT0M
ACTION:DISPLAY
DESCRIPTION:🎂 {fullname}{year_suffix}
END:VALARM
END:VEVENT
END:VCALENDAR",
),
None,
)?)
} else {
None
},
)
} }
/// Get significant dates associated with this address object /// Get significant dates associated with this address object

View File

@@ -1,386 +0,0 @@
use crate::CalDateTime;
use crate::Error;
use chrono::{DateTime, Duration, Utc};
use ical::parser::ComponentMut;
use ical::{generator::IcalEvent, parser::Component, property::Property};
use rrule::{RRule, RRuleSet};
use std::{collections::HashMap, str::FromStr};
#[derive(Debug, Clone, Default)]
pub struct EventObject {
pub event: IcalEvent,
// If a timezone is None that means that in the VCALENDAR object there's a timezone defined
// with that name but its not from the Olson DB
pub timezones: HashMap<String, Option<chrono_tz::Tz>>,
}
impl EventObject {
#[must_use]
pub fn get_uid(&self) -> &str {
self.event.get_uid()
}
pub fn get_dtstart(&self) -> Result<Option<CalDateTime>, Error> {
if let Some(dtstart) = self.event.get_dtstart() {
Ok(Some(CalDateTime::parse_prop(dtstart, &self.timezones)?))
} else {
Ok(None)
}
}
pub fn get_dtend(&self) -> Result<Option<CalDateTime>, Error> {
if let Some(dtend) = self.event.get_dtend() {
Ok(Some(CalDateTime::parse_prop(dtend, &self.timezones)?))
} else {
Ok(None)
}
}
pub fn get_last_occurence(&self) -> Result<Option<CalDateTime>, Error> {
if self.event.get_rrule().is_some() {
// TODO: understand recurrence rules
return Ok(None);
}
if let Some(dtend) = self.get_dtend()? {
return Ok(Some(dtend));
}
let duration = self.event.get_duration().unwrap_or(Duration::days(1));
let first_occurence = self.get_dtstart()?;
Ok(first_occurence.map(|first_occurence| first_occurence + duration))
}
pub fn recurrence_ruleset(&self) -> Result<Option<rrule::RRuleSet>, Error> {
let dtstart: DateTime<rrule::Tz> = if let Some(dtstart) = self.get_dtstart()? {
if let Some(dtend) = self.get_dtend()? {
// DTSTART and DTEND MUST have the same timezone
assert_eq!(dtstart.timezone(), dtend.timezone());
}
dtstart
.as_datetime()
.with_timezone(&dtstart.timezone().into())
} else {
return Ok(None);
};
let mut rrule_set = RRuleSet::new(dtstart);
// TODO: Make nice, this is just a bodge to get correct behaviour
let mut empty = true;
for prop in &self.event.properties {
rrule_set = match prop.name.as_str() {
"RRULE" => {
let rrule = RRule::from_str(prop.value.as_ref().ok_or_else(|| {
Error::RRuleError(rrule::ParseError::MissingDateGenerationRules.into())
})?)?
.validate(dtstart)
.unwrap();
empty = false;
rrule_set.rrule(rrule)
}
"RDATE" => {
let rdate = CalDateTime::parse_prop(prop, &self.timezones)?.into();
empty = false;
rrule_set.rdate(rdate)
}
"EXDATE" => {
let exdate = CalDateTime::parse_prop(prop, &self.timezones)?.into();
empty = false;
rrule_set.exdate(exdate)
}
_ => rrule_set,
}
}
if empty {
return Ok(None);
}
Ok(Some(rrule_set))
}
// The returned calendar components MUST NOT use recurrence
// properties (i.e., EXDATE, EXRULE, RDATE, and RRULE) and MUST NOT
// have reference to or include VTIMEZONE components. Date and local
// time with reference to time zone information MUST be converted
// into date with UTC time.
pub fn expand_recurrence(
&self,
start: Option<DateTime<Utc>>,
end: Option<DateTime<Utc>>,
overrides: &[Self],
) -> Result<Vec<IcalEvent>, Error> {
let mut events = vec![];
let dtstart = self.get_dtstart()?.expect("We must have a DTSTART here");
let computed_duration = self
.get_dtend()?
.map(|dtend| dtend.as_datetime().into_owned() - dtstart.as_datetime().as_ref());
let Some(mut rrule_set) = self.recurrence_ruleset()? else {
// If ruleset empty simply return main event AND all overrides
return Ok(std::iter::once(self.clone())
.chain(overrides.iter().cloned())
.map(|event| event.event)
.collect());
};
if let Some(start) = start {
rrule_set = rrule_set.after(start.with_timezone(&rrule::Tz::UTC));
}
if let Some(end) = end {
rrule_set = rrule_set.before(end.with_timezone(&rrule::Tz::UTC));
}
let dates = rrule_set.all(2048).dates;
'recurrence: for date in dates {
let date = CalDateTime::from(date.to_utc());
let recurrence_id = if dtstart.is_date() {
date.format_date()
} else {
date.format()
};
for ev_override in overrides {
if let Some(override_id) = &ev_override
.event
.get_recurrence_id()
.as_ref()
.expect("overrides have a recurrence id")
.value
&& override_id == &recurrence_id
{
// We have an override for this occurence
//
events.push(ev_override.event.clone());
continue 'recurrence;
}
}
let mut ev = self.event.clone().mutable();
ev.remove_property("RRULE");
ev.remove_property("RDATE");
ev.remove_property("EXDATE");
ev.remove_property("EXRULE");
let dtstart_prop = ev
.get_property("DTSTART")
.expect("We must have a DTSTART here")
.clone();
ev.remove_property("DTSTART");
ev.remove_property("DTEND");
ev.set_property(Property {
name: "RECURRENCE-ID".to_string(),
value: Some(recurrence_id.clone()),
params: vec![],
});
ev.set_property(Property {
name: "DTSTART".to_string(),
value: Some(recurrence_id),
params: vec![],
});
if let Some(duration) = computed_duration {
let dtend = date + duration;
let dtendformat = if dtstart.is_date() {
dtend.format_date()
} else {
dtend.format()
};
ev.set_property(Property {
name: "DTEND".to_string(),
value: Some(dtendformat),
params: dtstart_prop.params,
});
}
events.push(ev.verify()?);
}
Ok(events)
}
}
#[cfg(test)]
mod tests {
use crate::{CalDateTime, CalendarObject};
use chrono::{DateTime, Utc};
use ical::generator::Emitter;
use rstest::rstest;
const ICS_1: &str = r"BEGIN:VCALENDAR
CALSCALE:GREGORIAN
VERSION:2.0
BEGIN:VTIMEZONE
TZID:Europe/Berlin
X-LIC-LOCATION:Europe/Berlin
END:VTIMEZONE
BEGIN:VEVENT
UID:318ec6503573d9576818daf93dac07317058d95c
DTSTAMP:20250502T132758Z
DTSTART;TZID=Europe/Berlin:20250506T090000
DTEND;TZID=Europe/Berlin:20250506T092500
SEQUENCE:2
SUMMARY:weekly stuff
TRANSP:OPAQUE
RRULE:FREQ=WEEKLY;COUNT=4;INTERVAL=2;BYDAY=TU,TH,SU
END:VEVENT
END:VCALENDAR";
const EXPANDED_1: &[&str] = &[
"BEGIN:VEVENT\r
UID:318ec6503573d9576818daf93dac07317058d95c\r
DTSTAMP:20250502T132758Z\r
SEQUENCE:2\r
SUMMARY:weekly stuff\r
TRANSP:OPAQUE\r
RECURRENCE-ID:20250506T070000Z\r
DTSTART:20250506T070000Z\r
DTEND:20250506T072500Z\r
END:VEVENT\r\n",
"BEGIN:VEVENT\r
UID:318ec6503573d9576818daf93dac07317058d95c\r
DTSTAMP:20250502T132758Z\r
SEQUENCE:2\r
SUMMARY:weekly stuff\r
TRANSP:OPAQUE\r
RECURRENCE-ID:20250508T070000Z\r
DTSTART:20250508T070000Z\r
DTEND:20250508T072500Z\r
END:VEVENT\r\n",
"BEGIN:VEVENT\r
UID:318ec6503573d9576818daf93dac07317058d95c\r
DTSTAMP:20250502T132758Z\r
SEQUENCE:2\r
SUMMARY:weekly stuff\r
TRANSP:OPAQUE\r
RECURRENCE-ID:20250511T090000\r
DTSTART:20250511T070000Z\r
DTEND:20250511T072500Z\r
END:VEVENT\r\n",
"BEGIN:VEVENT\r
UID:318ec6503573d9576818daf93dac07317058d95c\r
DTSTAMP:20250502T132758Z\r
SEQUENCE:2\r
SUMMARY:weekly stuff\r
TRANSP:OPAQUE\r
RECURRENCE-ID:20250520T090000\r
DTSTA:20250520T070000Z\r
DTEND:20250520T072500Z\r
END:VEVENT\r\n",
];
const ICS_2: &str = r"BEGIN:VCALENDAR
CALSCALE:GREGORIAN
VERSION:2.0
BEGIN:VTIMEZONE
TZID:US/Eastern
END:VTIMEZONE
BEGIN:VEVENT
DTSTAMP:20060206T001121Z
DTSTART;TZID=US/Eastern:20060102T120000
DURATION:PT1H
RRULE:FREQ=DAILY;COUNT=5
SUMMARY:Event #2
UID:abcd2
END:VEVENT
BEGIN:VEVENT
DTSTAMP:20060206T001121Z
DTSTART;TZID=US/Eastern:20060104T140000
DURATION:PT1H
RECURRENCE-ID;TZID=US/Eastern:20060104T120000
SUMMARY:Event #2 bis
UID:abcd2
END:VEVENT
END:VCALENDAR
";
const EXPANDED_2: &[&str] = &[
"BEGIN:VEVENT\r
DTSTAMP:20060206T001121Z\r
DURATION:PT1H\r
SUMMARY:Event #2\r
UID:abcd2\r
RECURRENCE-ID:20060103T170000\r
DTSTART:20060103T170000\r
END:VEVENT\r\n",
"BEGIN:VEVENT\r
DTSTAMP:20060206T001121Z\r
DURATION:PT1H\r
SUMMARY:Event #2 bis\r
UID:abcd2\r
RECURRENCE-ID:20060104T170000\r
DTSTART:20060104T190000\r
END:VEVENT\r
END:VCALENDAR\r\n",
];
const ICS_3: &str = r"BEGIN:VCALENDAR
CALSCALE:GREGORIAN
VERSION:2.0
BEGIN:VTIMEZONE
TZID:US/Eastern
END:VTIMEZONE
BEGIN:VEVENT
ATTENDEE;PARTSTAT=ACCEPTED;ROLE=CHAIR:mailto:cyrus@example.com
ATTENDEE;PARTSTAT=NEEDS-ACTION:mailto:lisa@example.com
DTSTAMP:20060206T001220Z
DTSTART;TZID=US/Eastern:20060104T100000
DURATION:PT1H
LAST-MODIFIED:20060206T001330Z
ORGANIZER:mailto:cyrus@example.com
SEQUENCE:1
STATUS:TENTATIVE
SUMMARY:Event #3
UID:abcd3
END:VEVENT
END:VCALENDAR
";
const EXPANDED_3: &[&str] = &["BEGIN:VEVENT
ATTENDEE;PARTSTAT=ACCEPTED;ROLE=CHAIR:mailto:cyrus@example.com
ATTENDEE;PARTSTAT=NEEDS-ACTION:mailto:lisa@example.com
DTSTAMP:20060206T001220Z
DTSTART:20060104T150000
DURATION:PT1H
LAST-MODIFIED:20060206T001330Z
ORGANIZER:mailto:cyrus@example.com
SEQUENCE:1
STATUS:TENTATIVE
SUMMARY:Event #3
UID:abcd3
X-ABC-GUID:E1CX5Dr-0007ym-Hz@example.com
END:VEVENT"];
// The implementation never was entirely correct but will be fixed in v0.12
// #[rstest]
// #[case(ICS_1, EXPANDED_1, None, None)]
// // from https://datatracker.ietf.org/doc/html/rfc4791#section-7.8.3
// #[case(ICS_2, EXPANDED_2,
// Some(CalDateTime::parse("20060103T000000Z", Some(chrono_tz::US::Eastern)).unwrap().utc()),
// Some(CalDateTime::parse("20060105T000000Z", Some(chrono_tz::US::Eastern)).unwrap().utc())
// )]
// #[case(ICS_3, EXPANDED_3,
// Some(CalDateTime::parse("20060103T000000Z", Some(chrono_tz::US::Eastern)).unwrap().utc()),
// Some(CalDateTime::parse("20060105T000000Z", Some(chrono_tz::US::Eastern)).unwrap().utc())
// )]
// fn test_expand_recurrence(
// #[case] ics: &'static str,
// #[case] expanded: &[&str],
// #[case] from: Option<DateTime<Utc>>,
// #[case] to: Option<DateTime<Utc>>,
// ) {
// let event = CalendarObject::from_ics(ics.to_string(), None).unwrap();
// let crate::CalendarObjectComponent::Event(event, overrides) = event.get_data() else {
// panic!()
// };
//
// let events: Vec<String> = event
// .expand_recurrence(from, to, overrides)
// .unwrap()
// .into_iter()
// .map(|event| Emitter::generate(&event))
// .collect();
// assert_eq!(events.len(), expanded.len());
// for (output, reference) in events.iter().zip(expanded) {
// similar_asserts::assert_eq!(output, reference);
// }
// }
}

View File

@@ -1,5 +1,3 @@
mod event;
mod object; mod object;
pub use event::*;
pub use object::*; pub use object::*;

View File

@@ -1,18 +1,14 @@
use super::EventObject;
use crate::CalDateTime;
use crate::Error; use crate::Error;
use chrono::DateTime; use chrono::DateTime;
use chrono::Utc; use chrono::Utc;
use derive_more::Display; use derive_more::Display;
use ical::generator::{Emitter, IcalCalendar}; use ical::component::CalendarInnerData;
use ical::parser::ical::component::IcalJournal; use ical::component::IcalCalendarObject;
use ical::parser::ical::component::IcalTimeZone; use ical::parser::ComponentParser;
use ical::parser::ical::component::IcalTodo; use ical::types::CalDateTime;
use ical::property::Property;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
use std::{collections::HashMap, io::BufReader};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Display)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Display)]
// specified in https://datatracker.ietf.org/doc/html/rfc5545#section-3.6 // specified in https://datatracker.ietf.org/doc/html/rfc5545#section-3.6
@@ -25,6 +21,16 @@ pub enum CalendarObjectType {
Journal = 2, Journal = 2,
} }
impl From<&IcalCalendarObject> for CalendarObjectType {
fn from(value: &IcalCalendarObject) -> Self {
match value.get_inner() {
CalendarInnerData::Event(_, _) => Self::Event,
CalendarInnerData::Todo(_, _) => Self::Todo,
CalendarInnerData::Journal(_, _) => Self::Journal,
}
}
}
impl CalendarObjectType { impl CalendarObjectType {
#[must_use] #[must_use]
pub const fn as_str(&self) -> &'static str { pub const fn as_str(&self) -> &'static str {
@@ -57,241 +63,39 @@ impl rustical_xml::ValueDeserialize for CalendarObjectType {
} }
} }
#[derive(Debug, Clone)]
pub enum CalendarObjectComponent {
Event(EventObject, Vec<EventObject>),
Todo(IcalTodo, Vec<IcalTodo>),
Journal(IcalJournal, Vec<IcalJournal>),
}
impl CalendarObjectComponent {
#[must_use]
pub fn get_uid(&self) -> &str {
match &self {
// We've made sure before that the first component exists and all components share the
// same UID
Self::Todo(todo, _) => todo.get_uid(),
Self::Event(event, _) => event.event.get_uid(),
Self::Journal(journal, _) => journal.get_uid(),
}
}
}
impl From<&CalendarObjectComponent> for CalendarObjectType {
fn from(value: &CalendarObjectComponent) -> Self {
match value {
CalendarObjectComponent::Event(..) => Self::Event,
CalendarObjectComponent::Todo(..) => Self::Todo,
CalendarObjectComponent::Journal(..) => Self::Journal,
}
}
}
impl CalendarObjectComponent {
fn from_events(mut events: Vec<EventObject>) -> Result<Self, Error> {
// A calendar object does not necessarily have to contain a main VOBJECT
if events.is_empty() {
return Err(Error::MissingCalendar);
}
#[allow(clippy::option_if_let_else)]
let main_event = if let Some(main) = events
.extract_if(.., |event| event.event.get_recurrence_id().is_none())
.next()
{
main
} else {
events.remove(0)
};
let overrides = events;
for event in &overrides {
if event.get_uid() != main_event.get_uid() {
return Err(Error::InvalidData(
"Calendar object contains multiple UIDs".to_owned(),
));
}
if event.event.get_recurrence_id().is_none() {
return Err(Error::InvalidData(
"Calendar object can only contain one main component".to_owned(),
));
}
}
Ok(Self::Event(main_event, overrides))
}
fn from_todos(mut todos: Vec<IcalTodo>) -> Result<Self, Error> {
// A calendar object does not necessarily have to contain a main VOBJECT
if todos.is_empty() {
return Err(Error::MissingCalendar);
}
#[allow(clippy::option_if_let_else)]
let main_todo = if let Some(main) = todos
.extract_if(.., |todo| todo.get_recurrence_id().is_none())
.next()
{
main
} else {
todos.remove(0)
};
let overrides = todos;
for todo in &overrides {
if todo.get_uid() != main_todo.get_uid() {
return Err(Error::InvalidData(
"Calendar object contains multiple UIDs".to_owned(),
));
}
if todo.get_recurrence_id().is_none() {
return Err(Error::InvalidData(
"Calendar object can only contain one main component".to_owned(),
));
}
}
Ok(Self::Todo(main_todo, overrides))
}
fn from_journals(mut journals: Vec<IcalJournal>) -> Result<Self, Error> {
// A calendar object does not necessarily have to contain a main VOBJECT
if journals.is_empty() {
return Err(Error::MissingCalendar);
}
#[allow(clippy::option_if_let_else)]
let main_journal = if let Some(main) = journals
.extract_if(.., |journal| journal.get_recurrence_id().is_none())
.next()
{
main
} else {
journals.remove(0)
};
let overrides = journals;
for journal in &overrides {
if journal.get_uid() != main_journal.get_uid() {
return Err(Error::InvalidData(
"Calendar object contains multiple UIDs".to_owned(),
));
}
if journal.get_recurrence_id().is_none() {
return Err(Error::InvalidData(
"Calendar object can only contain one main component".to_owned(),
));
}
}
Ok(Self::Journal(main_journal, overrides))
}
pub fn get_first_occurence(&self) -> Result<Option<CalDateTime>, Error> {
match &self {
Self::Event(main_event, overrides) => Ok(overrides
.iter()
.chain(std::iter::once(main_event))
.map(super::event::EventObject::get_dtstart)
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.flatten()
.min()),
_ => Ok(None),
}
}
pub fn get_last_occurence(&self) -> Result<Option<CalDateTime>, Error> {
match &self {
Self::Event(main_event, overrides) => Ok(overrides
.iter()
.chain(std::iter::once(main_event))
.map(super::event::EventObject::get_last_occurence)
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.flatten()
.max()),
_ => Ok(None),
}
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct CalendarObject { pub struct CalendarObject {
data: CalendarObjectComponent,
properties: Vec<Property>,
id: String, id: String,
inner: IcalCalendarObject,
ics: String, ics: String,
vtimezones: HashMap<String, IcalTimeZone>,
} }
impl CalendarObject { impl CalendarObject {
pub fn from_ics(ics: String, id: Option<String>) -> Result<Self, Error> { pub fn from_ics(ics: String, id: Option<String>) -> Result<Self, Error> {
let mut parser = ical::IcalParser::new(BufReader::new(ics.as_bytes())); let mut parser: ComponentParser<_, IcalCalendarObject> =
let cal = parser.next().ok_or(Error::MissingCalendar)??; ComponentParser::new(ics.as_bytes());
let inner = parser.next().ok_or(Error::MissingCalendar)??;
if parser.next().is_some() { if parser.next().is_some() {
return Err(Error::InvalidData( return Err(Error::InvalidData(
"multiple calendars, only one allowed".to_owned(), "multiple calendars, only one allowed".to_owned(),
)); ));
} }
if u8::from(!cal.events.is_empty())
+ u8::from(!cal.todos.is_empty())
+ u8::from(!cal.journals.is_empty())
+ u8::from(!cal.free_busys.is_empty())
!= 1
{
// https://datatracker.ietf.org/doc/html/rfc4791#section-4.1
return Err(Error::InvalidData(
"iCalendar object must have exactly one component type".to_owned(),
));
}
let timezones: HashMap<String, Option<chrono_tz::Tz>> = cal
.timezones
.clone()
.into_iter()
.map(|timezone| (timezone.get_tzid().to_owned(), (&timezone).try_into().ok()))
.collect();
let vtimezones = cal
.timezones
.clone()
.into_iter()
.map(|timezone| (timezone.get_tzid().to_owned(), timezone))
.collect();
let data = if !cal.events.is_empty() {
CalendarObjectComponent::from_events(
cal.events
.into_iter()
.map(|event| EventObject {
event,
timezones: timezones.clone(),
})
.collect(),
)?
} else if !cal.todos.is_empty() {
CalendarObjectComponent::from_todos(cal.todos)?
} else if !cal.journals.is_empty() {
CalendarObjectComponent::from_journals(cal.journals)?
} else {
return Err(Error::InvalidData(
"iCalendar component type not supported :(".to_owned(),
));
};
Ok(Self { Ok(Self {
id: id.unwrap_or_else(|| data.get_uid().to_owned()), id: id.unwrap_or_else(|| inner.get_uid().to_owned()),
data, inner,
properties: cal.properties,
ics, ics,
vtimezones,
}) })
} }
#[must_use] #[must_use]
pub const fn get_vtimezones(&self) -> &HashMap<String, IcalTimeZone> { pub const fn get_inner(&self) -> &IcalCalendarObject {
&self.vtimezones &self.inner
}
#[must_use]
pub const fn get_data(&self) -> &CalendarObjectComponent {
&self.data
} }
#[must_use] #[must_use]
pub fn get_uid(&self) -> &str { pub fn get_uid(&self) -> &str {
self.data.get_uid() self.inner.get_uid()
} }
#[must_use] #[must_use]
@@ -319,15 +123,17 @@ impl CalendarObject {
#[must_use] #[must_use]
pub fn get_object_type(&self) -> CalendarObjectType { pub fn get_object_type(&self) -> CalendarObjectType {
(&self.data).into() (&self.inner).into()
} }
pub fn get_first_occurence(&self) -> Result<Option<CalDateTime>, Error> { pub fn get_first_occurence(&self) -> Option<CalDateTime> {
self.data.get_first_occurence() // TODO: Implement
None
} }
pub fn get_last_occurence(&self) -> Result<Option<CalDateTime>, Error> { pub fn get_last_occurence(&self) -> Option<CalDateTime> {
self.data.get_last_occurence() // TODO: Implement
None
} }
pub fn expand_recurrence( pub fn expand_recurrence(
@@ -335,32 +141,7 @@ impl CalendarObject {
start: Option<DateTime<Utc>>, start: Option<DateTime<Utc>>,
end: Option<DateTime<Utc>>, end: Option<DateTime<Utc>>,
) -> Result<String, Error> { ) -> Result<String, Error> {
// Only events can be expanded // Ok(self.inner.expand_recurrence(start, end)?)
match &self.data { todo!()
CalendarObjectComponent::Event(main_event, overrides) => {
let cal = IcalCalendar {
properties: self.properties.clone(),
events: main_event.expand_recurrence(start, end, overrides)?,
..Default::default()
};
Ok(cal.generate())
}
_ => Ok(self.get_ics().to_string()),
}
}
#[must_use]
pub fn get_property(&self, name: &str) -> Option<&Property> {
self.properties
.iter()
.find(|property| property.name == name)
}
#[must_use]
pub fn get_named_properties(&self, name: &str) -> Vec<&Property> {
self.properties
.iter()
.filter(|property| property.name == name)
.collect()
} }
} }

View File

@@ -1,9 +1,7 @@
#![warn(clippy::all, clippy::pedantic, clippy::nursery)] #![warn(clippy::all, clippy::pedantic, clippy::nursery)]
#![allow(clippy::missing_errors_doc, clippy::missing_panics_doc)] #![allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
mod timestamp; mod timestamp;
mod timezone;
pub use timestamp::*; pub use timestamp::*;
pub use timezone::*;
mod icalendar; mod icalendar;
pub use icalendar::*; pub use icalendar::*;

View File

@@ -1,13 +1,6 @@
use super::timezone::ICalTimezone; use chrono::{DateTime, NaiveDateTime, Utc};
use chrono::{DateTime, Datelike, Duration, Local, NaiveDate, NaiveDateTime, NaiveTime, Utc};
use chrono_tz::Tz;
use derive_more::derive::Deref; use derive_more::derive::Deref;
use ical::property::Property;
use rustical_xml::{ValueDeserialize, ValueSerialize}; use rustical_xml::{ValueDeserialize, ValueSerialize};
use std::{borrow::Cow, collections::HashMap, ops::Add, sync::LazyLock};
static RE_VCARD_DATE_MM_DD: LazyLock<regex::Regex> =
LazyLock::new(|| regex::Regex::new(r"^--(?<m>\d{2})(?<d>\d{2})$").unwrap());
const LOCAL_DATE_TIME: &str = "%Y%m%dT%H%M%S"; const LOCAL_DATE_TIME: &str = "%Y%m%dT%H%M%S";
const UTC_DATE_TIME: &str = "%Y%m%dT%H%M%SZ"; const UTC_DATE_TIME: &str = "%Y%m%dT%H%M%SZ";
@@ -54,375 +47,3 @@ impl ValueSerialize for UtcDateTime {
format!("{}", self.0.format(UTC_DATE_TIME)) format!("{}", self.0.format(UTC_DATE_TIME))
} }
} }
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CalDateTime {
// Form 1, example: 19980118T230000 -> Local
// Form 2, example: 19980119T070000Z -> UTC
// Form 3, example: TZID=America/New_York:19980119T020000 -> Olson
// https://en.wikipedia.org/wiki/Tz_database
DateTime(DateTime<ICalTimezone>),
Date(NaiveDate, ICalTimezone),
}
impl From<CalDateTime> for DateTime<rrule::Tz> {
fn from(value: CalDateTime) -> Self {
value
.as_datetime()
.into_owned()
.with_timezone(&value.timezone().into())
}
}
impl From<DateTime<rrule::Tz>> for CalDateTime {
fn from(value: DateTime<rrule::Tz>) -> Self {
Self::DateTime(value.with_timezone(&value.timezone().into()))
}
}
impl PartialOrd for CalDateTime {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for CalDateTime {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match (&self, &other) {
(Self::DateTime(a), Self::DateTime(b)) => a.cmp(b),
(Self::DateTime(a), Self::Date(..)) => a.cmp(&other.as_datetime()),
(Self::Date(..), Self::DateTime(b)) => self.as_datetime().as_ref().cmp(b),
(Self::Date(..), Self::Date(..)) => self.as_datetime().cmp(&other.as_datetime()),
}
}
}
impl From<DateTime<Local>> for CalDateTime {
fn from(value: DateTime<Local>) -> Self {
Self::DateTime(value.with_timezone(&ICalTimezone::Local))
}
}
impl From<DateTime<Utc>> for CalDateTime {
fn from(value: DateTime<Utc>) -> Self {
Self::DateTime(value.with_timezone(&ICalTimezone::Olson(chrono_tz::UTC)))
}
}
impl Add<Duration> for CalDateTime {
type Output = Self;
fn add(self, duration: Duration) -> Self::Output {
match self {
Self::DateTime(datetime) => Self::DateTime(datetime + duration),
Self::Date(date, tz) => Self::DateTime(
date.and_time(NaiveTime::default())
.and_local_timezone(tz)
.earliest()
.expect("Local timezone has constant offset")
+ duration,
),
}
}
}
impl CalDateTime {
pub fn parse_prop(
prop: &Property,
timezones: &HashMap<String, Option<chrono_tz::Tz>>,
) -> Result<Self, CalDateTimeError> {
let prop_value = prop
.value
.as_ref()
.ok_or_else(|| CalDateTimeError::InvalidDatetimeFormat("empty property".into()))?;
let timezone = if let Some(tzid) = prop.get_param("TZID") {
if let Some(timezone) = timezones.get(tzid) {
timezone.to_owned()
} else {
// TZID refers to timezone that does not exist
return Err(CalDateTimeError::InvalidTZID(tzid.to_string()));
}
} else {
// No explicit timezone specified.
// This is valid and will be localtime or UTC depending on the value
// We will stick to this default as documented in https://github.com/lennart-k/rustical/issues/102
None
};
Self::parse(prop_value, timezone)
}
#[must_use]
pub fn format(&self) -> String {
match self {
Self::DateTime(datetime) => match datetime.timezone() {
ICalTimezone::Olson(chrono_tz::UTC) => datetime.format(UTC_DATE_TIME).to_string(),
_ => datetime.format(LOCAL_DATE_TIME).to_string(),
},
Self::Date(date, _) => date.format(LOCAL_DATE).to_string(),
}
}
#[must_use]
pub fn format_date(&self) -> String {
match self {
Self::DateTime(datetime) => datetime.format(LOCAL_DATE).to_string(),
Self::Date(date, _) => date.format(LOCAL_DATE).to_string(),
}
}
#[must_use]
pub fn date(&self) -> NaiveDate {
match self {
Self::DateTime(datetime) => datetime.date_naive(),
Self::Date(date, _) => date.to_owned(),
}
}
#[must_use]
pub const fn is_date(&self) -> bool {
matches!(&self, Self::Date(_, _))
}
#[must_use]
pub fn as_datetime(&self) -> Cow<'_, DateTime<ICalTimezone>> {
match self {
Self::DateTime(datetime) => Cow::Borrowed(datetime),
Self::Date(date, tz) => Cow::Owned(
date.and_time(NaiveTime::default())
.and_local_timezone(tz.to_owned())
.earliest()
.expect("Midnight always exists"),
),
}
}
#[must_use]
pub fn with_timezone(&self, tz: &ICalTimezone) -> Self {
match self {
Self::DateTime(datetime) => Self::DateTime(datetime.with_timezone(tz)),
Self::Date(date, _) => Self::Date(date.to_owned(), tz.to_owned()),
}
}
pub fn parse(value: &str, timezone: Option<Tz>) -> Result<Self, CalDateTimeError> {
if let Ok(datetime) = NaiveDateTime::parse_from_str(value, LOCAL_DATE_TIME) {
if let Some(timezone) = timezone {
return Ok(Self::DateTime(
datetime
.and_local_timezone(timezone.into())
.earliest()
.ok_or(CalDateTimeError::LocalTimeGap)?,
));
}
return Ok(Self::DateTime(
datetime
.and_local_timezone(ICalTimezone::Local)
.earliest()
.ok_or(CalDateTimeError::LocalTimeGap)?,
));
}
if let Ok(datetime) = NaiveDateTime::parse_from_str(value, UTC_DATE_TIME) {
return Ok(datetime.and_utc().into());
}
let timezone = timezone.map_or(ICalTimezone::Local, ICalTimezone::Olson);
if let Ok(date) = NaiveDate::parse_from_str(value, LOCAL_DATE) {
return Ok(Self::Date(date, timezone));
}
if let Ok(date) = NaiveDate::parse_from_str(value, "%Y-%m-%d") {
return Ok(Self::Date(date, timezone));
}
if let Ok(date) = NaiveDate::parse_from_str(value, "%Y%m%d") {
return Ok(Self::Date(date, timezone));
}
Err(CalDateTimeError::InvalidDatetimeFormat(value.to_string()))
}
// Also returns whether the date contains a year
pub fn parse_vcard(value: &str) -> Result<(Self, bool), CalDateTimeError> {
if let Ok(datetime) = Self::parse(value, None) {
return Ok((datetime, true));
}
if let Some(captures) = RE_VCARD_DATE_MM_DD.captures(value) {
// Because 1972 is a leap year
let year = 1972;
// Cannot fail because of the regex
let month = captures.name("m").unwrap().as_str().parse().ok().unwrap();
let day = captures.name("d").unwrap().as_str().parse().ok().unwrap();
return Ok((
Self::Date(
NaiveDate::from_ymd_opt(year, month, day)
.ok_or_else(|| CalDateTimeError::ParseError(value.to_string()))?,
ICalTimezone::Local,
),
false,
));
}
Err(CalDateTimeError::InvalidDatetimeFormat(value.to_string()))
}
#[must_use]
pub fn utc(&self) -> DateTime<Utc> {
self.as_datetime().to_utc()
}
#[must_use]
pub fn timezone(&self) -> ICalTimezone {
match &self {
Self::DateTime(datetime) => datetime.timezone(),
Self::Date(_, tz) => tz.to_owned(),
}
}
}
impl From<CalDateTime> for DateTime<Utc> {
fn from(value: CalDateTime) -> Self {
value.utc()
}
}
impl Datelike for CalDateTime {
fn year(&self) -> i32 {
match &self {
Self::DateTime(datetime) => datetime.year(),
Self::Date(date, _) => date.year(),
}
}
fn month(&self) -> u32 {
match &self {
Self::DateTime(datetime) => datetime.month(),
Self::Date(date, _) => date.month(),
}
}
fn month0(&self) -> u32 {
match &self {
Self::DateTime(datetime) => datetime.month0(),
Self::Date(date, _) => date.month0(),
}
}
fn day(&self) -> u32 {
match &self {
Self::DateTime(datetime) => datetime.day(),
Self::Date(date, _) => date.day(),
}
}
fn day0(&self) -> u32 {
match &self {
Self::DateTime(datetime) => datetime.day0(),
Self::Date(date, _) => date.day0(),
}
}
fn ordinal(&self) -> u32 {
match &self {
Self::DateTime(datetime) => datetime.ordinal(),
Self::Date(date, _) => date.ordinal(),
}
}
fn ordinal0(&self) -> u32 {
match &self {
Self::DateTime(datetime) => datetime.ordinal0(),
Self::Date(date, _) => date.ordinal0(),
}
}
fn weekday(&self) -> chrono::Weekday {
match &self {
Self::DateTime(datetime) => datetime.weekday(),
Self::Date(date, _) => date.weekday(),
}
}
fn iso_week(&self) -> chrono::IsoWeek {
match &self {
Self::DateTime(datetime) => datetime.iso_week(),
Self::Date(date, _) => date.iso_week(),
}
}
fn with_year(&self, year: i32) -> Option<Self> {
match &self {
Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_year(year)?)),
Self::Date(date, tz) => Some(Self::Date(date.with_year(year)?, tz.to_owned())),
}
}
fn with_month(&self, month: u32) -> Option<Self> {
match &self {
Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_month(month)?)),
Self::Date(date, tz) => Some(Self::Date(date.with_month(month)?, tz.to_owned())),
}
}
fn with_month0(&self, month0: u32) -> Option<Self> {
match &self {
Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_month0(month0)?)),
Self::Date(date, tz) => Some(Self::Date(date.with_month0(month0)?, tz.to_owned())),
}
}
fn with_day(&self, day: u32) -> Option<Self> {
match &self {
Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_day(day)?)),
Self::Date(date, tz) => Some(Self::Date(date.with_day(day)?, tz.to_owned())),
}
}
fn with_day0(&self, day0: u32) -> Option<Self> {
match &self {
Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_day0(day0)?)),
Self::Date(date, tz) => Some(Self::Date(date.with_day0(day0)?, tz.to_owned())),
}
}
fn with_ordinal(&self, ordinal: u32) -> Option<Self> {
match &self {
Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_ordinal(ordinal)?)),
Self::Date(date, tz) => Some(Self::Date(date.with_ordinal(ordinal)?, tz.to_owned())),
}
}
fn with_ordinal0(&self, ordinal0: u32) -> Option<Self> {
match &self {
Self::DateTime(datetime) => Some(Self::DateTime(datetime.with_ordinal0(ordinal0)?)),
Self::Date(date, tz) => Some(Self::Date(date.with_ordinal0(ordinal0)?, tz.to_owned())),
}
}
}
#[cfg(test)]
mod tests {
use crate::CalDateTime;
use chrono::NaiveDate;
#[test]
fn test_vcard_date() {
assert_eq!(
CalDateTime::parse_vcard("19850412").unwrap(),
(
CalDateTime::Date(
NaiveDate::from_ymd_opt(1985, 4, 12).unwrap(),
crate::ICalTimezone::Local
),
true
)
);
assert_eq!(
CalDateTime::parse_vcard("1985-04-12").unwrap(),
(
CalDateTime::Date(
NaiveDate::from_ymd_opt(1985, 4, 12).unwrap(),
crate::ICalTimezone::Local
),
true
)
);
assert_eq!(
CalDateTime::parse_vcard("--0412").unwrap(),
(
CalDateTime::Date(
NaiveDate::from_ymd_opt(1972, 4, 12).unwrap(),
crate::ICalTimezone::Local
),
false
)
);
}
}

View File

@@ -1,92 +0,0 @@
use chrono::{Local, NaiveDate, NaiveDateTime, TimeZone};
use chrono_tz::Tz;
use derive_more::{Display, From};
#[derive(Debug, Clone, From, PartialEq, Eq)]
pub enum ICalTimezone {
Local,
Olson(Tz),
}
impl From<ICalTimezone> for rrule::Tz {
fn from(value: ICalTimezone) -> Self {
match value {
ICalTimezone::Local => Self::LOCAL,
ICalTimezone::Olson(tz) => Self::Tz(tz),
}
}
}
impl From<rrule::Tz> for ICalTimezone {
fn from(value: rrule::Tz) -> Self {
match value {
rrule::Tz::Local(_) => Self::Local,
rrule::Tz::Tz(tz) => Self::Olson(tz),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Display)]
pub enum CalTimezoneOffset {
Local(chrono::FixedOffset),
Olson(chrono_tz::TzOffset),
}
impl chrono::Offset for CalTimezoneOffset {
fn fix(&self) -> chrono::FixedOffset {
match self {
Self::Local(local) => local.fix(),
Self::Olson(olson) => olson.fix(),
}
}
}
impl TimeZone for ICalTimezone {
type Offset = CalTimezoneOffset;
fn from_offset(offset: &Self::Offset) -> Self {
match offset {
CalTimezoneOffset::Local(_) => Self::Local,
CalTimezoneOffset::Olson(offset) => Self::Olson(Tz::from_offset(offset)),
}
}
fn offset_from_local_date(&self, local: &NaiveDate) -> chrono::MappedLocalTime<Self::Offset> {
match self {
Self::Local => Local
.offset_from_local_date(local)
.map(CalTimezoneOffset::Local),
Self::Olson(tz) => tz
.offset_from_local_date(local)
.map(CalTimezoneOffset::Olson),
}
}
fn offset_from_local_datetime(
&self,
local: &NaiveDateTime,
) -> chrono::MappedLocalTime<Self::Offset> {
match self {
Self::Local => Local
.offset_from_local_datetime(local)
.map(CalTimezoneOffset::Local),
Self::Olson(tz) => tz
.offset_from_local_datetime(local)
.map(CalTimezoneOffset::Olson),
}
}
fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> Self::Offset {
match self {
Self::Local => CalTimezoneOffset::Local(Local.offset_from_utc_datetime(utc)),
Self::Olson(tz) => CalTimezoneOffset::Olson(tz.offset_from_utc_datetime(utc)),
}
}
fn offset_from_utc_date(&self, utc: &NaiveDate) -> Self::Offset {
match self {
Self::Local => CalTimezoneOffset::Local(Local.offset_from_utc_date(utc)),
Self::Olson(tz) => CalTimezoneOffset::Olson(tz.offset_from_utc_date(utc)),
}
}
}

View File

@@ -20,6 +20,7 @@ rstest.workspace = true
criterion.workspace = true criterion.workspace = true
[dependencies] [dependencies]
ical.workspace = true
tokio.workspace = true tokio.workspace = true
rustical_store.workspace = true rustical_store.workspace = true
async-trait.workspace = true async-trait.workspace = true

View File

@@ -3,8 +3,9 @@ use crate::BEGIN_IMMEDIATE;
use async_trait::async_trait; use async_trait::async_trait;
use chrono::TimeDelta; use chrono::TimeDelta;
use derive_more::derive::Constructor; use derive_more::derive::Constructor;
use ical::types::CalDateTime;
use regex::Regex; use regex::Regex;
use rustical_ical::{CalDateTime, CalendarObject, CalendarObjectType}; use rustical_ical::{CalendarObject, CalendarObjectType};
use rustical_store::calendar_store::CalendarQuery; use rustical_store::calendar_store::CalendarQuery;
use rustical_store::synctoken::format_synctoken; use rustical_store::synctoken::format_synctoken;
use rustical_store::{Calendar, CalendarMetadata, CalendarStore, CollectionMetadata, Error}; use rustical_store::{Calendar, CalendarMetadata, CalendarStore, CollectionMetadata, Error};
@@ -537,16 +538,12 @@ impl SqliteCalendarStore {
let first_occurence = object let first_occurence = object
.get_first_occurence() .get_first_occurence()
.ok()
.flatten()
.as_ref() .as_ref()
.map(CalDateTime::date); .map(CalDateTime::date_floor);
let last_occurence = object let last_occurence = object
.get_last_occurence() .get_last_occurence()
.ok()
.flatten()
.as_ref() .as_ref()
.map(CalDateTime::date); .map(CalDateTime::date_ceil);
let etag = object.get_etag(); let etag = object.get_etag();
let object_type = object.get_object_type() as u8; let object_type = object.get_object_type() as u8;