diff --git a/Cargo.lock b/Cargo.lock index 80ac433..90cd229 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1768,25 +1768,10 @@ dependencies = [ "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]] name = "ical" 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 = [ "chrono", "chrono-tz", @@ -3343,7 +3328,6 @@ dependencies = [ "figment", "headers", "http", - "ical 0.12.0-dev", "insta", "opentelemetry", "opentelemetry-otlp", @@ -3391,7 +3375,7 @@ dependencies = [ "futures-util", "headers", "http", - "ical 0.11.0", + "ical", "insta", "percent-encoding", "quick-xml", @@ -3430,7 +3414,7 @@ dependencies = [ "derive_more", "futures-util", "http", - "ical 0.11.0", + "ical", "insta", "percent-encoding", "quick-xml", @@ -3463,7 +3447,7 @@ dependencies = [ "futures-util", "headers", "http", - "ical 0.11.0", + "ical", "itertools 0.14.0", "log", "matchit 0.9.1", @@ -3547,7 +3531,7 @@ dependencies = [ "chrono", "chrono-tz", "derive_more", - "ical 0.11.0", + "ical", "regex", "rrule", "rstest", @@ -3588,7 +3572,7 @@ dependencies = [ "futures-core", "headers", "http", - "ical 0.11.0", + "ical", "regex", "rrule", "rstest", @@ -3615,6 +3599,7 @@ dependencies = [ "chrono", "criterion", "derive_more", + "ical", "password-auth", "password-hash", "pbkdf2", diff --git a/Cargo.toml b/Cargo.toml index f3ac0f2..ead46ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -107,7 +107,9 @@ strum = "0.27" strum_macros = "0.27" serde_json = { version = "1.0", features = ["raw_value"] } 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" tower = "0.5" tower-http = { version = "0.6", features = [ @@ -199,7 +201,3 @@ tower-http.workspace = true axum-extra.workspace = true headers.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", -] } diff --git a/crates/caldav/src/calendar/methods/get.rs b/crates/caldav/src/calendar/methods/get.rs index f69843e..368870b 100644 --- a/crates/caldav/src/calendar/methods/get.rs +++ b/crates/caldav/src/calendar/methods/get.rs @@ -5,11 +5,9 @@ use axum::extract::State; use axum::{extract::Path, response::Response}; use headers::{ContentType, HeaderMapExt}; use http::{HeaderValue, Method, StatusCode, header}; -use ical::builder::calendar::IcalCalendarBuilder; use ical::generator::Emitter; -use ical::property::Property; +use ical::property::ContentLine; use percent_encoding::{CONTROLS, utf8_percent_encode}; -use rustical_ical::{CalendarObjectComponent, EventObject}; use rustical_store::{CalendarStore, SubscriptionStore, auth::Principal}; use std::collections::HashMap; use std::str::FromStr; @@ -33,77 +31,79 @@ pub async fn route_get( return Err(crate::Error::Unauthorized); } - let mut vtimezones = HashMap::new(); - let objects = cal_store.get_objects(&principal, &calendar_id).await?; + // let mut vtimezones = HashMap::new(); + // let objects = cal_store.get_objects(&principal, &calendar_id).await?; - let mut ical_calendar_builder = IcalCalendarBuilder::version("2.0") - .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![], - }); - } + todo!() - for object in &objects { - vtimezones.extend(object.get_vtimezones()); - match object.get_data() { - CalendarObjectComponent::Event(EventObject { event, .. }, overrides) => { - ical_calendar_builder = ical_calendar_builder - .add_event(event.clone()) - .add_events(overrides.iter().map(|ev| ev.event.clone())); - } - CalendarObjectComponent::Todo(todo, overrides) => { - ical_calendar_builder = ical_calendar_builder - .add_todo(todo.clone()) - .add_todos(overrides.iter().cloned()); - } - CalendarObjectComponent::Journal(journal, overrides) => { - ical_calendar_builder = ical_calendar_builder - .add_journal(journal.clone()) - .add_journals(overrides.iter().cloned()); - } - } - } - - 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()) - } + // let mut ical_calendar_builder = IcalCalendarBuilder::version("2.0") + // .gregorian() + // .prodid("RustiCal"); + // if let Some(displayname) = calendar.meta.displayname { + // ical_calendar_builder = ical_calendar_builder.set(ContentLine { + // name: "X-WR-CALNAME".to_owned(), + // value: Some(displayname), + // params: vec![].into(), + // }); + // } + // if let Some(description) = calendar.meta.description { + // ical_calendar_builder = ical_calendar_builder.set(ContentLine { + // name: "X-WR-CALDESC".to_owned(), + // value: Some(description), + // params: vec![].into(), + // }); + // } + // 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), + // params: vec![].into(), + // }); + // } + // + // for object in &objects { + // vtimezones.extend(object.get_vtimezones()); + // match object.get_data() { + // CalendarObjectComponent::Event(EventObject { event, .. }, overrides) => { + // ical_calendar_builder = ical_calendar_builder + // .add_event(event.clone()) + // .add_events(overrides.iter().map(|ev| ev.event.clone())); + // } + // CalendarObjectComponent::Todo(todo, overrides) => { + // ical_calendar_builder = ical_calendar_builder + // .add_todo(todo.clone()) + // .add_todos(overrides.iter().cloned()); + // } + // CalendarObjectComponent::Journal(journal, overrides) => { + // ical_calendar_builder = ical_calendar_builder + // .add_journal(journal.clone()) + // .add_journals(overrides.iter().cloned()); + // } + // } + // } + // + // 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()) + // } } diff --git a/crates/caldav/src/calendar/methods/import.rs b/crates/caldav/src/calendar/methods/import.rs index deb3b05..fe4f153 100644 --- a/crates/caldav/src/calendar/methods/import.rs +++ b/crates/caldav/src/calendar/methods/import.rs @@ -5,10 +5,7 @@ use axum::{ response::{IntoResponse, Response}, }; use http::StatusCode; -use ical::{ - generator::Emitter, - parser::{Component, ComponentMut}, -}; +use ical::{generator::Emitter, parser::Component}; use rustical_dav::header::Overwrite; use rustical_ical::{CalendarObject, CalendarObjectType}; use rustical_store::{ @@ -53,58 +50,59 @@ pub async fn route_import( .get_property("X-WR-TIMEZONE") .and_then(|prop| prop.value.clone()); // These properties should not appear in the expanded calendar objects - cal.remove_property("X-WR-CALNAME"); - cal.remove_property("X-WR-CALDESC"); - cal.remove_property("X-WR-TIMEZONE"); - let cal = cal.verify().unwrap(); - // Make sure timezone is valid - if let Some(timezone_id) = timezone_id.as_ref() { - assert!( - vtimezones_rs::VTIMEZONES.contains_key(timezone_id), - "Invalid calendar timezone id" - ); - } - - // Extract necessary component types - let mut cal_components = vec![]; - if !cal.events.is_empty() { - cal_components.push(CalendarObjectType::Event); - } - if !cal.journals.is_empty() { - cal_components.push(CalendarObjectType::Journal); - } - 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 objects = expanded_cals - .into_iter() - .map(|cal| cal.generate()) - .map(|ics| CalendarObject::from_ics(ics, None)) - .collect::, _>>()?; - let new_cal = Calendar { - principal, - id: cal_id, - meta: CalendarMetadata { - displayname, - order: 0, - description, - color: None, - }, - timezone_id, - deleted_at: None, - synctoken: 0, - subscription_url: None, - push_topic: uuid::Uuid::new_v4().to_string(), - components: cal_components, - }; - - let cal_store = resource_service.cal_store; - cal_store - .import_calendar(new_cal, objects, overwrite) - .await?; - - Ok(StatusCode::OK.into_response()) + todo!(); + // cal.remove_property("X-WR-CALNAME"); + // cal.remove_property("X-WR-CALDESC"); + // cal.remove_property("X-WR-TIMEZONE"); + // let cal = cal.verify().unwrap(); + // // Make sure timezone is valid + // if let Some(timezone_id) = timezone_id.as_ref() { + // assert!( + // vtimezones_rs::VTIMEZONES.contains_key(timezone_id), + // "Invalid calendar timezone id" + // ); + // } + // + // // Extract necessary component types + // let mut cal_components = vec![]; + // if !cal.events.is_empty() { + // cal_components.push(CalendarObjectType::Event); + // } + // if !cal.journals.is_empty() { + // cal_components.push(CalendarObjectType::Journal); + // } + // 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 objects = expanded_cals + // .into_iter() + // .map(|cal| cal.generate()) + // .map(|ics| CalendarObject::from_ics(ics, None)) + // .collect::, _>>()?; + // let new_cal = Calendar { + // principal, + // id: cal_id, + // meta: CalendarMetadata { + // displayname, + // order: 0, + // description, + // color: None, + // }, + // timezone_id, + // deleted_at: None, + // synctoken: 0, + // subscription_url: None, + // push_topic: uuid::Uuid::new_v4().to_string(), + // components: cal_components, + // }; + // + // let cal_store = resource_service.cal_store; + // cal_store + // .import_calendar(new_cal, objects, overwrite) + // .await?; + // + // Ok(StatusCode::OK.into_response()) } diff --git a/crates/caldav/src/calendar/methods/mkcalendar.rs b/crates/caldav/src/calendar/methods/mkcalendar.rs index 0847a57..71a8a09 100644 --- a/crates/caldav/src/calendar/methods/mkcalendar.rs +++ b/crates/caldav/src/calendar/methods/mkcalendar.rs @@ -92,10 +92,11 @@ pub async fn route_mkcalendar( .ok_or_else(|| rustical_dav::Error::BadRequest("No timezone data provided".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()) })?; - let timezone: chrono_tz::Tz = timezone.try_into().map_err(|_| { + let timezone: Option = timezone.into(); + let timezone = timezone.ok_or_else(|| { rustical_dav::Error::BadRequest("Cannot translate VTIMEZONE into IANA TZID".to_owned()) })?; diff --git a/crates/caldav/src/calendar/methods/report/calendar_query/comp_filter.rs b/crates/caldav/src/calendar/methods/report/calendar_query/comp_filter.rs index cc34a88..7e773c8 100644 --- a/crates/caldav/src/calendar/methods/report/calendar_query/comp_filter.rs +++ b/crates/caldav/src/calendar/methods/report/calendar_query/comp_filter.rs @@ -1,9 +1,10 @@ use crate::calendar::methods::report::calendar_query::{ - TimeRangeElement, - prop_filter::{PropFilterElement, PropFilterable}, + TimeRangeElement, prop_filter::PropFilterElement, +}; +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; #[derive(XmlDeserialize, Clone, Debug, PartialEq)] @@ -24,9 +25,7 @@ pub struct CompFilterElement { pub(crate) name: String, } -pub trait CompFilterable: PropFilterable + Sized { - fn get_comp_name(&self) -> &'static str; - +pub trait CompFilterable: Component + Sized { fn match_time_range(&self, time_range: &TimeRangeElement) -> bool; fn match_subcomponents(&self, comp_filter: &CompFilterElement) -> bool; @@ -68,11 +67,7 @@ pub trait CompFilterable: PropFilterable + Sized { } } -impl CompFilterable for CalendarObject { - fn get_comp_name(&self) -> &'static str { - "VCALENDAR" - } - +impl CompFilterable for IcalCalendarObject { fn match_time_range(&self, _time_range: &TimeRangeElement) -> bool { // VCALENDAR has no concept of time range false @@ -83,7 +78,7 @@ impl CompFilterable for CalendarObject { .get_vtimezones() .values() .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() { matches.all(|x| x) @@ -94,10 +89,6 @@ impl CompFilterable for CalendarObject { } impl CompFilterable for IcalTimeZone { - fn get_comp_name(&self) -> &'static str { - "VTIMEZONE" - } - fn match_time_range(&self, _time_range: &TimeRangeElement) -> bool { 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)] mod tests { use chrono::{TimeZone, Utc}; diff --git a/crates/caldav/src/calendar/methods/report/calendar_query/elements.rs b/crates/caldav/src/calendar/methods/report/calendar_query/elements.rs index d9ca17b..48f1c59 100644 --- a/crates/caldav/src/calendar/methods/report/calendar_query/elements.rs +++ b/crates/caldav/src/calendar/methods/report/calendar_query/elements.rs @@ -1,8 +1,8 @@ use super::comp_filter::{CompFilterElement, CompFilterable}; use crate::calendar_object::CalendarObjectPropWrapperName; -use ical::property::Property; +use ical::{component::IcalCalendarObject, property::ContentLine}; use rustical_dav::xml::{PropfindType, TextMatchElement}; -use rustical_ical::{CalendarObject, UtcDateTime}; +use rustical_ical::UtcDateTime; use rustical_store::calendar_store::CalendarQuery; use rustical_xml::{XmlDeserialize, XmlRootTag}; @@ -30,8 +30,8 @@ pub struct ParamFilterElement { impl ParamFilterElement { #[must_use] - pub fn match_property(&self, prop: &Property) -> bool { - let Some(param) = prop.get_param(&self.name) else { + pub fn match_property(&self, prop: &ContentLine) -> bool { + let Some(param) = prop.params.get_param(&self.name) else { return self.is_not_defined.is_some(); }; if self.is_not_defined.is_some() { @@ -57,7 +57,7 @@ pub struct FilterElement { impl FilterElement { #[must_use] - pub fn matches(&self, cal_object: &CalendarObject) -> bool { + pub fn matches(&self, cal_object: &IcalCalendarObject) -> bool { cal_object.matches(&self.comp_filter) } } diff --git a/crates/caldav/src/calendar/methods/report/calendar_query/mod.rs b/crates/caldav/src/calendar/methods/report/calendar_query/mod.rs index d46d271..b674142 100644 --- a/crates/caldav/src/calendar/methods/report/calendar_query/mod.rs +++ b/crates/caldav/src/calendar/methods/report/calendar_query/mod.rs @@ -11,7 +11,7 @@ mod tests; pub use comp_filter::{CompFilterElement, CompFilterable}; pub use elements::*; #[allow(unused_imports)] -pub use prop_filter::{PropFilterElement, PropFilterable}; +pub use prop_filter::PropFilterElement; pub async fn get_objects_calendar_query( cal_query: &CalendarQueryRequest, @@ -23,7 +23,7 @@ pub async fn get_objects_calendar_query( .calendar_query(principal, cal_id, cal_query.into()) .await?; if let Some(filter) = &cal_query.filter { - objects.retain(|object| filter.matches(object)); + objects.retain(|object| filter.matches(object.get_inner())); } Ok(objects) } diff --git a/crates/caldav/src/calendar/methods/report/calendar_query/prop_filter.rs b/crates/caldav/src/calendar/methods/report/calendar_query/prop_filter.rs index 406166d..889d0ee 100644 --- a/crates/caldav/src/calendar/methods/report/calendar_query/prop_filter.rs +++ b/crates/caldav/src/calendar/methods/report/calendar_query/prop_filter.rs @@ -1,14 +1,7 @@ use super::{ParamFilterElement, TimeRangeElement}; -use ical::{ - generator::{IcalCalendar, IcalEvent}, - parser::{ - Component, - ical::component::{IcalJournal, IcalTimeZone, IcalTodo}, - }, - property::Property, -}; +use ical::{parser::Component, property::ContentLine, types::CalDateTime}; use rustical_dav::xml::TextMatchElement; -use rustical_ical::{CalDateTime, CalendarObject, CalendarObjectComponent, UtcDateTime}; +use rustical_ical::UtcDateTime; use rustical_xml::XmlDeserialize; use std::collections::HashMap; @@ -31,7 +24,7 @@ pub struct PropFilterElement { impl PropFilterElement { #[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 { // TODO: Respect timezones let Ok(timestamp) = CalDateTime::parse_prop(property, &HashMap::default()) else { @@ -68,7 +61,7 @@ impl PropFilterElement { 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); if self.is_not_defined.is_some() { return properties.is_empty(); @@ -79,53 +72,3 @@ impl PropFilterElement { 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), - } - } -} diff --git a/crates/caldav/src/calendar/resource.rs b/crates/caldav/src/calendar/resource.rs index c1a59eb..cb37d71 100644 --- a/crates/caldav/src/calendar/resource.rs +++ b/crates/caldav/src/calendar/resource.rs @@ -4,6 +4,7 @@ use crate::calendar::prop::{ReportMethod, SupportedCollationSet}; use chrono::{DateTime, Utc}; use derive_more::derive::{From, Into}; use ical::IcalParser; +use ical::types::CalDateTime; use rustical_dav::extensions::{ CommonPropertiesExtension, CommonPropertiesProp, SyncTokenExtension, SyncTokenExtensionProp, }; @@ -11,7 +12,6 @@ use rustical_dav::privileges::UserPrivilegeSet; use rustical_dav::resource::{PrincipalUri, Resource, ResourceName}; use rustical_dav::xml::{HrefElement, Resourcetype, ResourcetypeInner, SupportedReportSet}; use rustical_dav_push::{DavPushExtension, DavPushExtensionProp}; -use rustical_ical::CalDateTime; use rustical_store::Calendar; use rustical_store::auth::Principal; 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()) })?; - let timezone: chrono_tz::Tz = timezone.try_into().map_err(|_| { + let timezone: Option = timezone.into(); + let timezone = timezone.ok_or_else(|| { rustical_dav::Error::BadRequest("No timezone data provided".to_owned()) })?; - self.cal.timezone_id = Some(timezone.name().to_owned()); } Ok(()) diff --git a/crates/carddav/src/addressbook/methods/import.rs b/crates/carddav/src/addressbook/methods/import.rs index ff7dff2..3f4bc2a 100644 --- a/crates/carddav/src/addressbook/methods/import.rs +++ b/crates/carddav/src/addressbook/methods/import.rs @@ -1,4 +1,4 @@ -use std::io::BufReader; +use std::{collections::HashMap, io::BufReader}; use crate::Error; use crate::addressbook::AddressbookResourceService; @@ -9,7 +9,7 @@ use axum::{ use http::StatusCode; use ical::{ parser::{Component, ComponentMut, vcard}, - property::Property, + property::ContentLine, }; use rustical_store::{Addressbook, AddressbookStore, SubscriptionStore, auth::Principal}; use tracing::instrument; @@ -33,12 +33,12 @@ pub async fn route_import( let uid = card.get_uid(); if uid.is_none() { let mut card_mut = card.mutable(); - card_mut.set_property(Property { + card_mut.add_content_line(ContentLine { name: "UID".to_owned(), 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()); diff --git a/crates/carddav/src/addressbook/methods/report/addressbook_query/elements.rs b/crates/carddav/src/addressbook/methods/report/addressbook_query/elements.rs index b294482..b89985b 100644 --- a/crates/carddav/src/addressbook/methods/report/addressbook_query/elements.rs +++ b/crates/carddav/src/addressbook/methods/report/addressbook_query/elements.rs @@ -3,7 +3,7 @@ use crate::{ addressbook::methods::report::addressbook_query::PropFilterElement, }; use derive_more::{From, Into}; -use ical::property::Property; +use ical::property::ContentLine; use rustical_dav::xml::{PropfindType, TextMatchElement}; use rustical_ical::{AddressObject, UtcDateTime}; use rustical_xml::{ValueDeserialize, XmlDeserialize, XmlRootTag}; @@ -32,8 +32,8 @@ pub struct ParamFilterElement { impl ParamFilterElement { #[must_use] - pub fn match_property(&self, prop: &Property) -> bool { - let Some(param) = prop.get_param(&self.name) else { + pub fn match_property(&self, prop: &ContentLine) -> bool { + let Some(param) = prop.params.get_param(&self.name) else { return self.is_not_defined.is_some(); }; if self.is_not_defined.is_some() { diff --git a/crates/carddav/src/addressbook/methods/report/addressbook_query/prop_filter.rs b/crates/carddav/src/addressbook/methods/report/addressbook_query/prop_filter.rs index e8de84d..eec0e96 100644 --- a/crates/carddav/src/addressbook/methods/report/addressbook_query/prop_filter.rs +++ b/crates/carddav/src/addressbook/methods/report/addressbook_query/prop_filter.rs @@ -1,5 +1,5 @@ use super::{Allof, ParamFilterElement}; -use ical::{parser::Component, property::Property}; +use ical::{parser::Component, property::ContentLine}; use rustical_dav::xml::TextMatchElement; use rustical_ical::AddressObject; use rustical_xml::XmlDeserialize; @@ -31,7 +31,7 @@ pub struct PropFilterElement { impl PropFilterElement { #[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() { // Filter empty return true; @@ -67,11 +67,11 @@ impl PropFilterElement { } 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 { - 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) } } diff --git a/crates/dav/src/xml/text_match.rs b/crates/dav/src/xml/text_match.rs index a8db120..2188fe5 100644 --- a/crates/dav/src/xml/text_match.rs +++ b/crates/dav/src/xml/text_match.rs @@ -1,4 +1,4 @@ -use ical::property::Property; +use ical::property::ContentLine; use rustical_xml::{ValueDeserialize, XmlDeserialize}; use std::borrow::Cow; @@ -128,7 +128,7 @@ impl TextMatchElement { negate_condition.0 ^ matches } #[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(""); self.match_text(text) } diff --git a/crates/ical/src/address_object.rs b/crates/ical/src/address_object.rs index b0c5450..9f63fb6 100644 --- a/crates/ical/src/address_object.rs +++ b/crates/ical/src/address_object.rs @@ -1,11 +1,10 @@ -use crate::{CalDateTime, LOCAL_DATE}; use crate::{CalendarObject, Error}; -use chrono::Datelike; use ical::generator::Emitter; use ical::parser::{ Component, vcard::{self, component::VcardContact}, }; +use ical::types::CalDateTime; use sha2::{Digest, Sha256}; use std::{collections::HashMap, io::BufReader}; @@ -65,14 +64,16 @@ impl AddressObject { #[must_use] pub fn get_anniversary(&self) -> Option<(CalDateTime, bool)> { - let prop = self.vcard.get_property("ANNIVERSARY")?.value.as_deref()?; - CalDateTime::parse_vcard(prop).ok() + // let prop = self.vcard.get_property("ANNIVERSARY")?.value.as_deref()?; + // CalDateTime::parse_vcard(prop).ok() + todo!() } #[must_use] pub fn get_birthday(&self) -> Option<(CalDateTime, bool)> { - let prop = self.vcard.get_property("BDAY")?.value.as_deref()?; - CalDateTime::parse_vcard(prop).ok() + todo!() + // let prop = self.vcard.get_property("BDAY")?.value.as_deref()?; + // CalDateTime::parse_vcard(prop).ok() } #[must_use] @@ -82,90 +83,11 @@ impl AddressObject { } pub fn get_anniversary_object(&self) -> Result, Error> { - Ok( - 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 - }, - ) + todo!(); } pub fn get_birthday_object(&self) -> Result, Error> { - Ok( - 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 - }, - ) + todo!(); } /// Get significant dates associated with this address object diff --git a/crates/ical/src/icalendar/event.rs b/crates/ical/src/icalendar/event.rs deleted file mode 100644 index 2b2d929..0000000 --- a/crates/ical/src/icalendar/event.rs +++ /dev/null @@ -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>, -} - -impl EventObject { - #[must_use] - pub fn get_uid(&self) -> &str { - self.event.get_uid() - } - - pub fn get_dtstart(&self) -> Result, 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, 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, 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, Error> { - let dtstart: DateTime = 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>, - end: Option>, - overrides: &[Self], - ) -> Result, 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>, - // #[case] to: Option>, - // ) { - // let event = CalendarObject::from_ics(ics.to_string(), None).unwrap(); - // let crate::CalendarObjectComponent::Event(event, overrides) = event.get_data() else { - // panic!() - // }; - // - // let events: Vec = 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); - // } - // } -} diff --git a/crates/ical/src/icalendar/mod.rs b/crates/ical/src/icalendar/mod.rs index 4883949..75ce6bc 100644 --- a/crates/ical/src/icalendar/mod.rs +++ b/crates/ical/src/icalendar/mod.rs @@ -1,5 +1,3 @@ -mod event; mod object; -pub use event::*; pub use object::*; diff --git a/crates/ical/src/icalendar/object.rs b/crates/ical/src/icalendar/object.rs index fa99059..f959d1a 100644 --- a/crates/ical/src/icalendar/object.rs +++ b/crates/ical/src/icalendar/object.rs @@ -1,18 +1,14 @@ -use super::EventObject; -use crate::CalDateTime; use crate::Error; use chrono::DateTime; use chrono::Utc; use derive_more::Display; -use ical::generator::{Emitter, IcalCalendar}; -use ical::parser::ical::component::IcalJournal; -use ical::parser::ical::component::IcalTimeZone; -use ical::parser::ical::component::IcalTodo; -use ical::property::Property; +use ical::component::CalendarInnerData; +use ical::component::IcalCalendarObject; +use ical::parser::ComponentParser; +use ical::types::CalDateTime; use serde::Deserialize; use serde::Serialize; use sha2::{Digest, Sha256}; -use std::{collections::HashMap, io::BufReader}; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Display)] // specified in https://datatracker.ietf.org/doc/html/rfc5545#section-3.6 @@ -25,6 +21,16 @@ pub enum CalendarObjectType { 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 { #[must_use] 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), - Todo(IcalTodo, Vec), - Journal(IcalJournal, Vec), -} - -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) -> Result { - // 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) -> Result { - // 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) -> Result { - // 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, Error> { - match &self { - Self::Event(main_event, overrides) => Ok(overrides - .iter() - .chain(std::iter::once(main_event)) - .map(super::event::EventObject::get_dtstart) - .collect::, _>>()? - .into_iter() - .flatten() - .min()), - _ => Ok(None), - } - } - - pub fn get_last_occurence(&self) -> Result, 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::, _>>()? - .into_iter() - .flatten() - .max()), - _ => Ok(None), - } - } -} - #[derive(Debug, Clone)] pub struct CalendarObject { - data: CalendarObjectComponent, - properties: Vec, id: String, + inner: IcalCalendarObject, ics: String, - vtimezones: HashMap, } impl CalendarObject { pub fn from_ics(ics: String, id: Option) -> Result { - let mut parser = ical::IcalParser::new(BufReader::new(ics.as_bytes())); - let cal = parser.next().ok_or(Error::MissingCalendar)??; + let mut parser: ComponentParser<_, IcalCalendarObject> = + ComponentParser::new(ics.as_bytes()); + let inner = parser.next().ok_or(Error::MissingCalendar)??; if parser.next().is_some() { return Err(Error::InvalidData( "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> = 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 { - id: id.unwrap_or_else(|| data.get_uid().to_owned()), - data, - properties: cal.properties, + id: id.unwrap_or_else(|| inner.get_uid().to_owned()), + inner, ics, - vtimezones, }) } #[must_use] - pub const fn get_vtimezones(&self) -> &HashMap { - &self.vtimezones - } - - #[must_use] - pub const fn get_data(&self) -> &CalendarObjectComponent { - &self.data + pub const fn get_inner(&self) -> &IcalCalendarObject { + &self.inner } #[must_use] pub fn get_uid(&self) -> &str { - self.data.get_uid() + self.inner.get_uid() } #[must_use] @@ -319,15 +123,17 @@ impl CalendarObject { #[must_use] pub fn get_object_type(&self) -> CalendarObjectType { - (&self.data).into() + (&self.inner).into() } - pub fn get_first_occurence(&self) -> Result, Error> { - self.data.get_first_occurence() + pub fn get_first_occurence(&self) -> Option { + // TODO: Implement + None } - pub fn get_last_occurence(&self) -> Result, Error> { - self.data.get_last_occurence() + pub fn get_last_occurence(&self) -> Option { + // TODO: Implement + None } pub fn expand_recurrence( @@ -335,32 +141,7 @@ impl CalendarObject { start: Option>, end: Option>, ) -> Result { - // Only events can be expanded - match &self.data { - 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() + // Ok(self.inner.expand_recurrence(start, end)?) + todo!() } } diff --git a/crates/ical/src/lib.rs b/crates/ical/src/lib.rs index 9c00d52..42f646f 100644 --- a/crates/ical/src/lib.rs +++ b/crates/ical/src/lib.rs @@ -1,9 +1,7 @@ #![warn(clippy::all, clippy::pedantic, clippy::nursery)] #![allow(clippy::missing_errors_doc, clippy::missing_panics_doc)] mod timestamp; -mod timezone; pub use timestamp::*; -pub use timezone::*; mod icalendar; pub use icalendar::*; diff --git a/crates/ical/src/timestamp.rs b/crates/ical/src/timestamp.rs index 2dcf037..bf053f1 100644 --- a/crates/ical/src/timestamp.rs +++ b/crates/ical/src/timestamp.rs @@ -1,13 +1,6 @@ -use super::timezone::ICalTimezone; -use chrono::{DateTime, Datelike, Duration, Local, NaiveDate, NaiveDateTime, NaiveTime, Utc}; -use chrono_tz::Tz; +use chrono::{DateTime, NaiveDateTime, Utc}; use derive_more::derive::Deref; -use ical::property::Property; use rustical_xml::{ValueDeserialize, ValueSerialize}; -use std::{borrow::Cow, collections::HashMap, ops::Add, sync::LazyLock}; - -static RE_VCARD_DATE_MM_DD: LazyLock = - LazyLock::new(|| regex::Regex::new(r"^--(?\d{2})(?\d{2})$").unwrap()); const LOCAL_DATE_TIME: &str = "%Y%m%dT%H%M%S"; 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)) } } - -#[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), - Date(NaiveDate, ICalTimezone), -} - -impl From for DateTime { - fn from(value: CalDateTime) -> Self { - value - .as_datetime() - .into_owned() - .with_timezone(&value.timezone().into()) - } -} - -impl From> for CalDateTime { - fn from(value: DateTime) -> Self { - Self::DateTime(value.with_timezone(&value.timezone().into())) - } -} - -impl PartialOrd for CalDateTime { - fn partial_cmp(&self, other: &Self) -> Option { - 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> for CalDateTime { - fn from(value: DateTime) -> Self { - Self::DateTime(value.with_timezone(&ICalTimezone::Local)) - } -} - -impl From> for CalDateTime { - fn from(value: DateTime) -> Self { - Self::DateTime(value.with_timezone(&ICalTimezone::Olson(chrono_tz::UTC))) - } -} - -impl Add 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>, - ) -> Result { - 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> { - 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) -> Result { - 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 { - 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 for DateTime { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 { - 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 - ) - ); - } -} diff --git a/crates/ical/src/timezone.rs b/crates/ical/src/timezone.rs deleted file mode 100644 index aea5c9e..0000000 --- a/crates/ical/src/timezone.rs +++ /dev/null @@ -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 for rrule::Tz { - fn from(value: ICalTimezone) -> Self { - match value { - ICalTimezone::Local => Self::LOCAL, - ICalTimezone::Olson(tz) => Self::Tz(tz), - } - } -} - -impl From 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 { - 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 { - 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)), - } - } -} diff --git a/crates/store_sqlite/Cargo.toml b/crates/store_sqlite/Cargo.toml index ae7a6d0..8c3b7fc 100644 --- a/crates/store_sqlite/Cargo.toml +++ b/crates/store_sqlite/Cargo.toml @@ -20,6 +20,7 @@ rstest.workspace = true criterion.workspace = true [dependencies] +ical.workspace = true tokio.workspace = true rustical_store.workspace = true async-trait.workspace = true diff --git a/crates/store_sqlite/src/calendar_store.rs b/crates/store_sqlite/src/calendar_store.rs index 5f1b130..d6bae17 100644 --- a/crates/store_sqlite/src/calendar_store.rs +++ b/crates/store_sqlite/src/calendar_store.rs @@ -3,8 +3,9 @@ use crate::BEGIN_IMMEDIATE; use async_trait::async_trait; use chrono::TimeDelta; use derive_more::derive::Constructor; +use ical::types::CalDateTime; use regex::Regex; -use rustical_ical::{CalDateTime, CalendarObject, CalendarObjectType}; +use rustical_ical::{CalendarObject, CalendarObjectType}; use rustical_store::calendar_store::CalendarQuery; use rustical_store::synctoken::format_synctoken; use rustical_store::{Calendar, CalendarMetadata, CalendarStore, CollectionMetadata, Error}; @@ -537,16 +538,12 @@ impl SqliteCalendarStore { let first_occurence = object .get_first_occurence() - .ok() - .flatten() .as_ref() - .map(CalDateTime::date); + .map(CalDateTime::date_floor); let last_occurence = object .get_last_occurence() - .ok() - .flatten() .as_ref() - .map(CalDateTime::date); + .map(CalDateTime::date_ceil); let etag = object.get_etag(); let object_type = object.get_object_type() as u8;