diff --git a/crates/caldav/src/calendar/methods/report/calendar_query/tests.rs b/crates/caldav/src/calendar/methods/report/calendar_query/tests.rs index c7f57a1..9652c10 100644 --- a/crates/caldav/src/calendar/methods/report/calendar_query/tests.rs +++ b/crates/caldav/src/calendar/methods/report/calendar_query/tests.rs @@ -1,9 +1,8 @@ +use super::FilterElement; use rstest::rstest; use rustical_ical::CalendarObject; use rustical_xml::XmlDocument; -use crate::calendar::methods::report::calendar_query::FilterElement; - const ICS_1: &str = r"BEGIN:VCALENDAR VERSION:2.0 PRODID:-//Example Corp.//CalDAV Client//EN 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 7ac2bc7..2a4a105 100644 --- a/crates/carddav/src/addressbook/methods/report/addressbook_query/elements.rs +++ b/crates/carddav/src/addressbook/methods/report/addressbook_query/elements.rs @@ -2,10 +2,11 @@ use crate::{ address_object::AddressObjectPropWrapperName, addressbook::methods::report::addressbook_query::PropFilterElement, }; +use derive_more::{From, Into}; use ical::property::Property; use rustical_dav::xml::{PropfindType, TextMatchElement}; use rustical_ical::{AddressObject, UtcDateTime}; -use rustical_xml::XmlDeserialize; +use rustical_xml::{ValueDeserialize, XmlDeserialize, XmlRootTag}; #[derive(XmlDeserialize, Clone, Debug, PartialEq, Eq)] #[allow(dead_code)] @@ -46,18 +47,34 @@ impl ParamFilterElement { } } -#[derive(XmlDeserialize, Clone, Debug, PartialEq, Eq)] -#[allow(dead_code)] +#[derive(Clone, Debug, PartialEq, Eq, Default, From, Into)] +pub struct Allof(pub bool); + +impl ValueDeserialize for Allof { + fn deserialize(val: &str) -> Result { + Ok(Self(match val { + "allof" => true, + "anyof" => false, + _ => { + return Err(rustical_xml::XmlError::InvalidVariant(format!( + "Invalid test parameter: {val}" + ))); + } + })) + } +} + // // // +#[derive(XmlDeserialize, XmlRootTag, Clone, Debug, PartialEq, Eq)] +#[xml(root = "filter", ns = "rustical_dav::namespace::NS_CARDDAV")] +#[allow(dead_code)] pub struct FilterElement { - #[xml(ty = "attr")] - pub anyof: Option, - #[xml(ty = "attr")] - pub allof: Option, + #[xml(ty = "attr", default = "Default::default")] + pub test: Allof, #[xml(ns = "rustical_dav::namespace::NS_CARDDAV", flatten)] pub(crate) prop_filter: Vec, } @@ -65,11 +82,7 @@ pub struct FilterElement { impl FilterElement { #[must_use] pub fn matches(&self, addr_object: &AddressObject) -> bool { - let allof = match (self.allof.is_some(), self.anyof.is_some()) { - (true, false) => true, - (false, _) => false, - (true, true) => panic!("wat"), - }; + let Allof(allof) = self.test; let mut results = self .prop_filter .iter() 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 a2b8d08..eb368a8 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,4 +1,4 @@ -use super::ParamFilterElement; +use super::{Allof, ParamFilterElement}; use ical::{parser::Component, property::Property}; use rustical_dav::xml::TextMatchElement; use rustical_ical::AddressObject; @@ -22,25 +22,17 @@ pub struct PropFilterElement { pub(crate) text_match: Vec, #[xml(ns = "rustical_dav::namespace::NS_CARDDAV", flatten)] pub(crate) param_filter: Vec, + #[xml(ty = "attr", default = "Default::default")] + pub test: Allof, #[xml(ty = "attr")] pub(crate) name: String, - - #[xml(ty = "attr")] - pub anyof: Option, - #[xml(ty = "attr")] - pub allof: Option, } impl PropFilterElement { #[must_use] pub fn match_property(&self, property: &Property) -> bool { - let allof = match (self.allof.is_some(), self.anyof.is_some()) { - (true, false) => true, - (false, _) => false, - (true, true) => panic!("wat"), - }; - + let Allof(allof) = self.test; let text_matches = self .text_match .iter() diff --git a/crates/carddav/src/addressbook/methods/report/mod.rs b/crates/carddav/src/addressbook/methods/report/mod.rs index 5ac0034..9c6a08b 100644 --- a/crates/carddav/src/addressbook/methods/report/mod.rs +++ b/crates/carddav/src/addressbook/methods/report/mod.rs @@ -158,7 +158,9 @@ mod tests { use super::*; use crate::{ address_object::AddressObjectPropName, - addressbook::methods::report::addressbook_query::{FilterElement, PropFilterElement}, + addressbook::methods::report::addressbook_query::{ + Allof, FilterElement, PropFilterElement, + }, }; use rustical_dav::xml::{PropElement, sync_collection::SyncLevel}; @@ -250,15 +252,13 @@ mod tests { vec![] )), filter: FilterElement { - anyof: None, - allof: None, + test: Allof::default(), prop_filter: vec![PropFilterElement { name: "FN".to_owned(), is_not_defined: None, text_match: vec![], param_filter: vec![], - allof: None, - anyof: None + test: Allof::default() }] } })