WIP: Complete work of propfind parsing

This commit is contained in:
Lennart
2025-06-04 18:11:25 +02:00
parent 5ad6ee2e99
commit e57a14cad1
43 changed files with 875 additions and 1036 deletions

View File

@@ -1,5 +1,4 @@
use super::ReportPropName;
use crate::Error;
use crate::{Error, calendar_object::resource::CalendarObjectPropWrapperName};
use actix_web::dev::{Path, ResourceDef};
use rustical_dav::xml::PropfindType;
use rustical_ical::CalendarObject;
@@ -11,7 +10,7 @@ use rustical_xml::XmlDeserialize;
// <!ELEMENT calendar-query ((DAV:allprop | DAV:propname | DAV:prop)?, href+)>
pub(crate) struct CalendarMultigetRequest {
#[xml(ty = "untagged")]
pub(crate) prop: PropfindType<ReportPropName>,
pub(crate) prop: PropfindType<CalendarObjectPropWrapperName>,
#[xml(flatten)]
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
pub(crate) href: Vec<String>,

View File

@@ -1,5 +1,4 @@
use super::ReportPropName;
use crate::Error;
use crate::{Error, calendar_object::resource::CalendarObjectPropWrapperName};
use rustical_dav::xml::PropfindType;
use rustical_ical::{CalendarObject, UtcDateTime};
use rustical_store::{CalendarStore, calendar_store::CalendarQuery};
@@ -171,7 +170,7 @@ impl From<&FilterElement> for CalendarQuery {
// <!ELEMENT calendar-query ((DAV:allprop | DAV:propname | DAV:prop)?, filter, timezone?)>
pub struct CalendarQueryRequest {
#[xml(ty = "untagged")]
pub prop: PropfindType<ReportPropName>,
pub prop: PropfindType<CalendarObjectPropWrapperName>,
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]
pub(crate) filter: Option<FilterElement>,
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]

View File

@@ -1,6 +1,8 @@
use crate::{
CalDavPrincipalUri, Error,
calendar_object::resource::{CalendarObjectPropWrapper, CalendarObjectResource},
calendar_object::resource::{
CalendarObjectPropWrapper, CalendarObjectPropWrapperName, CalendarObjectResource,
},
};
use actix_web::{
HttpRequest, Responder,
@@ -12,11 +14,11 @@ use calendar_query::{CalendarQueryRequest, get_objects_calendar_query};
use rustical_dav::{
resource::{PrincipalUri, Resource},
xml::{
MultistatusElement, PropElement, PropfindType, Propname, multistatus::ResponseElement,
MultistatusElement, PropfindType, multistatus::ResponseElement,
sync_collection::SyncCollectionRequest,
},
};
use rustical_ical::{CalendarObject, UtcDateTime};
use rustical_ical::CalendarObject;
use rustical_store::{CalendarStore, auth::User};
use rustical_xml::{XmlDeserialize, XmlDocument};
use sync_collection::handle_sync_collection;
@@ -26,34 +28,6 @@ mod calendar_multiget;
mod calendar_query;
mod sync_collection;
#[derive(XmlDeserialize, Clone, Debug, PartialEq)]
pub(crate) struct ExpandElement {
#[xml(ty = "attr")]
start: UtcDateTime,
#[xml(ty = "attr")]
end: UtcDateTime,
}
#[derive(XmlDeserialize, Clone, Debug, PartialEq)]
pub struct CalendarData {
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]
comp: Option<()>,
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]
expand: Option<ExpandElement>,
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]
limit_recurrence_set: Option<()>,
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]
limit_freebusy_set: Option<()>,
}
#[derive(XmlDeserialize, Clone, Debug, PartialEq)]
pub enum ReportPropName {
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]
CalendarData(CalendarData),
#[xml(other)]
Propname(Propname),
}
#[derive(XmlDeserialize, XmlDocument, Clone, Debug, PartialEq)]
pub(crate) enum ReportRequest {
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]
@@ -61,31 +35,15 @@ pub(crate) enum ReportRequest {
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]
CalendarQuery(CalendarQueryRequest),
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
SyncCollection(SyncCollectionRequest<ReportPropName>),
SyncCollection(SyncCollectionRequest<CalendarObjectPropWrapperName>),
}
impl ReportRequest {
fn props(&self) -> Vec<&str> {
let prop_element = match self {
fn props(&self) -> &PropfindType<CalendarObjectPropWrapperName> {
match &self {
ReportRequest::CalendarMultiget(CalendarMultigetRequest { prop, .. }) => prop,
ReportRequest::CalendarQuery(CalendarQueryRequest { prop, .. }) => prop,
ReportRequest::SyncCollection(SyncCollectionRequest { prop, .. }) => prop,
};
match prop_element {
PropfindType::Allprop => {
vec!["allprop"]
}
PropfindType::Propname => {
vec!["propname"]
}
PropfindType::Prop(PropElement(prop_tags)) => prop_tags
.iter()
.map(|propname| match propname {
ReportPropName::Propname(propname) => propname.name.as_str(),
ReportPropName::CalendarData(_) => "calendar-data",
})
.collect(),
}
}
}
@@ -97,7 +55,7 @@ fn objects_response(
principal: &str,
puri: &impl PrincipalUri,
user: &User,
props: &[&str],
prop: &PropfindType<CalendarObjectPropWrapperName>,
) -> Result<MultistatusElement<CalendarObjectPropWrapper, String>, Error> {
let mut responses = Vec::new();
for object in objects {
@@ -107,7 +65,7 @@ fn objects_response(
object,
principal: principal.to_owned(),
}
.propfind(&path, props, puri, user)?,
.propfind_typed(&path, prop, puri, user)?,
);
}
@@ -156,7 +114,7 @@ pub async fn route_report_calendar<C: CalendarStore>(
&principal,
puri.as_ref(),
&user,
&props,
props,
)?
}
ReportRequest::CalendarMultiget(cal_multiget) => {
@@ -175,13 +133,12 @@ pub async fn route_report_calendar<C: CalendarStore>(
&principal,
puri.as_ref(),
&user,
&props,
props,
)?
}
ReportRequest::SyncCollection(sync_collection) => {
handle_sync_collection(
sync_collection,
&props,
req.path(),
puri.as_ref(),
&user,
@@ -197,10 +154,11 @@ pub async fn route_report_calendar<C: CalendarStore>(
#[cfg(test)]
mod tests {
use super::*;
use crate::calendar_object::resource::{CalendarData, CalendarObjectPropName, ExpandElement};
use calendar_query::{CompFilterElement, FilterElement, TimeRangeElement};
use rustical_dav::xml::{PropElement, PropfindType, Propname};
use rustical_dav::xml::PropElement;
use rustical_ical::UtcDateTime;
use rustical_xml::ValueDeserialize;
use rustical_xml::{NamespaceOwned, ValueDeserialize};
#[test]
fn test_xml_calendar_data() {
@@ -222,13 +180,14 @@ mod tests {
report_request,
ReportRequest::CalendarMultiget(CalendarMultigetRequest {
prop: rustical_dav::xml::PropfindType::Prop(PropElement(vec![
ReportPropName::Propname(Propname{name: "getetag".to_owned(), ns: Some("DAV:".into())}),
ReportPropName::Propname(Propname{name: "displayname".to_owned(), ns: Some("DAV:".into())}),
ReportPropName::CalendarData(CalendarData { comp: None, expand: Some(ExpandElement {
CalendarObjectPropWrapperName::CalendarObject(CalendarObjectPropName::Getetag),
CalendarObjectPropWrapperName::CalendarObject(CalendarObjectPropName::CalendarData(
CalendarData { comp: None, expand: Some(ExpandElement {
start: <UtcDateTime as ValueDeserialize>::deserialize("20250426T220000Z").unwrap(),
end: <UtcDateTime as ValueDeserialize>::deserialize("20250503T220000Z").unwrap(),
}), limit_recurrence_set: None, limit_freebusy_set: None })
])),
}), limit_recurrence_set: None, limit_freebusy_set: None }
)),
], vec![(Some(NamespaceOwned(Vec::from("DAV:"))), "displayname".to_string())])),
href: vec![
"/caldav/user/user/6f787542-5256-401a-8db97003260da/ae7a998fdfd1d84a20391168962c62b".to_owned()
]
@@ -258,10 +217,12 @@ mod tests {
assert_eq!(
report_request,
ReportRequest::CalendarQuery(CalendarQueryRequest {
prop: PropfindType::Prop(PropElement(vec![ReportPropName::Propname(Propname {
name: "getetag".to_owned(),
ns: Some("DAV:".into())
})])),
prop: rustical_dav::xml::PropfindType::Prop(PropElement(
vec![CalendarObjectPropWrapperName::CalendarObject(
CalendarObjectPropName::Getetag
),],
vec![]
)),
filter: Some(FilterElement {
comp_filter: CompFilterElement {
is_not_defined: None,
@@ -308,9 +269,8 @@ mod tests {
report_request,
ReportRequest::CalendarMultiget(CalendarMultigetRequest {
prop: rustical_dav::xml::PropfindType::Prop(PropElement(vec![
ReportPropName::Propname(Propname{name: "getetag".to_owned(), ns: Some("DAV:".into())}),
ReportPropName::Propname(Propname{name: "displayname".to_owned(), ns: Some("DAV:".into())})
])),
CalendarObjectPropWrapperName::CalendarObject(CalendarObjectPropName::Getetag),
], vec![(Some(NamespaceOwned(Vec::from("DAV:"))), "displayname".to_string())])),
href: vec![
"/caldav/user/user/6f787542-5256-401a-8db97003260da/ae7a998fdfd1d84a20391168962c62b".to_owned()
]

View File

@@ -1,7 +1,8 @@
use super::ReportPropName;
use crate::{
Error,
calendar_object::resource::{CalendarObjectPropWrapper, CalendarObjectResource},
calendar_object::resource::{
CalendarObjectPropWrapper, CalendarObjectPropWrapperName, CalendarObjectResource,
},
};
use actix_web::http::StatusCode;
use rustical_dav::{
@@ -17,8 +18,7 @@ use rustical_store::{
};
pub async fn handle_sync_collection<C: CalendarStore>(
sync_collection: &SyncCollectionRequest<ReportPropName>,
props: &[&str],
sync_collection: &SyncCollectionRequest<CalendarObjectPropWrapperName>,
path: &str,
puri: &impl PrincipalUri,
user: &User,
@@ -39,7 +39,7 @@ pub async fn handle_sync_collection<C: CalendarStore>(
object,
principal: principal.to_owned(),
}
.propfind(&path, props, puri, user)?,
.propfind_typed(&path, &sync_collection.prop, puri, user)?,
);
}

View File

@@ -19,12 +19,12 @@ use rustical_dav_push::{DavPushExtension, DavPushExtensionProp};
use rustical_ical::CalDateTime;
use rustical_store::auth::User;
use rustical_store::{Calendar, CalendarStore, SubscriptionStore};
use rustical_xml::{EnumUnitVariants, EnumVariants};
use rustical_xml::{EnumVariants, PropName};
use rustical_xml::{XmlDeserialize, XmlSerialize};
use std::str::FromStr;
use std::sync::Arc;
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, EnumUnitVariants)]
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "CalendarPropName")]
pub enum CalendarProp {
// WebDAV (RFC 2518)
@@ -64,7 +64,7 @@ pub enum CalendarProp {
MaxDateTime(String),
}
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, EnumUnitVariants)]
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "CalendarPropWrapperName", untagged)]
pub enum CalendarPropWrapper {
Calendar(CalendarProp),

View File

@@ -9,9 +9,9 @@ use rustical_dav::{
resource::{PrincipalUri, Resource, ResourceService},
xml::Resourcetype,
};
use rustical_ical::CalendarObject;
use rustical_ical::{CalendarObject, UtcDateTime};
use rustical_store::{CalendarStore, auth::User};
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
use rustical_xml::{EnumVariants, PropName, XmlDeserialize, XmlSerialize};
use serde::Deserialize;
use std::sync::Arc;
@@ -25,7 +25,27 @@ impl<C: CalendarStore> CalendarObjectResourceService<C> {
}
}
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, EnumUnitVariants)]
#[derive(XmlDeserialize, Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) struct ExpandElement {
#[xml(ty = "attr")]
pub(crate) start: UtcDateTime,
#[xml(ty = "attr")]
pub(crate) end: UtcDateTime,
}
#[derive(XmlDeserialize, Clone, Debug, PartialEq, Default, Eq, Hash)]
pub struct CalendarData {
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]
pub(crate) comp: Option<()>,
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]
pub(crate) expand: Option<ExpandElement>,
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]
pub(crate) limit_recurrence_set: Option<()>,
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]
pub(crate) limit_freebusy_set: Option<()>,
}
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "CalendarObjectPropName")]
pub enum CalendarObjectProp {
// WebDAV (RFC 2518)
@@ -36,10 +56,11 @@ pub enum CalendarObjectProp {
// CalDAV (RFC 4791)
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]
#[xml(prop = "CalendarData")]
CalendarData(String),
}
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, EnumUnitVariants)]
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "CalendarObjectPropWrapperName", untagged)]
pub enum CalendarObjectPropWrapper {
CalendarObject(CalendarObjectProp),
@@ -73,8 +94,15 @@ impl Resource for CalendarObjectResource {
CalendarObjectPropName::Getetag => {
CalendarObjectProp::Getetag(self.object.get_etag())
}
CalendarObjectPropName::CalendarData => {
CalendarObjectProp::CalendarData(self.object.get_ics().to_owned())
CalendarObjectPropName::CalendarData(CalendarData { expand, .. }) => {
CalendarObjectProp::CalendarData(if let Some(expand) = expand.as_ref() {
self.object.expand_recurrence(
Some(expand.start.to_utc()),
Some(expand.end.to_utc()),
)?
} else {
self.object.get_ics().to_owned()
})
}
CalendarObjectPropName::Getcontenttype => {
CalendarObjectProp::Getcontenttype("text/calendar;charset=utf-8")

View File

@@ -8,7 +8,7 @@ use rustical_dav::resource::{PrincipalUri, Resource, ResourceService};
use rustical_dav::xml::{Resourcetype, ResourcetypeInner};
use rustical_store::auth::User;
use rustical_store::{CalendarStore, SubscriptionStore};
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
use rustical_xml::{EnumVariants, PropName, XmlDeserialize, XmlSerialize};
use std::sync::Arc;
#[derive(Clone)]
@@ -17,7 +17,7 @@ pub struct CalendarSetResource {
pub(crate) read_only: bool,
}
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, EnumUnitVariants)]
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "PrincipalPropWrapperName", untagged)]
pub enum PrincipalPropWrapper {
Common(CommonPropertiesProp),

View File

@@ -9,7 +9,7 @@ use rustical_dav::xml::{HrefElement, Resourcetype, ResourcetypeInner};
use rustical_store::auth::user::PrincipalType;
use rustical_store::auth::{AuthenticationProvider, User};
use rustical_store::{CalendarStore, SubscriptionStore};
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
use rustical_xml::{EnumVariants, PropName, XmlDeserialize, XmlSerialize};
use std::sync::Arc;
#[derive(Clone)]
@@ -21,7 +21,7 @@ pub struct PrincipalResource {
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone)]
pub struct CalendarHomeSet(#[xml(ty = "untagged", flatten)] Vec<HrefElement>);
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, EnumUnitVariants)]
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "PrincipalPropName")]
pub enum PrincipalProp {
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
@@ -42,7 +42,7 @@ pub enum PrincipalProp {
CalendarHomeSet(CalendarHomeSet),
}
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, EnumUnitVariants)]
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "PrincipalPropWrapperName", untagged)]
pub enum PrincipalPropWrapper {
Principal(PrincipalProp),

View File

@@ -10,7 +10,7 @@ use rustical_dav::{
};
use rustical_ical::AddressObject;
use rustical_store::{AddressbookStore, auth::User};
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
use rustical_xml::{EnumVariants, PropName, XmlDeserialize, XmlSerialize};
use serde::Deserialize;
use std::sync::Arc;
@@ -21,7 +21,7 @@ pub struct AddressObjectResourceService<AS: AddressbookStore> {
addr_store: Arc<AS>,
}
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, EnumUnitVariants)]
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "AddressObjectPropName")]
pub enum AddressObjectProp {
// WebDAV (RFC 2518)
@@ -35,7 +35,7 @@ pub enum AddressObjectProp {
AddressData(String),
}
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, EnumUnitVariants)]
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "AddressObjectPropWrapperName", untagged)]
pub enum AddressObjectPropWrapper {
AddressObject(AddressObjectProp),

View File

@@ -1,6 +1,8 @@
use crate::{
Error,
address_object::resource::{AddressObjectPropWrapper, AddressObjectResource},
address_object::resource::{
AddressObjectPropWrapper, AddressObjectPropWrapperName, AddressObjectResource,
},
};
use actix_web::{
dev::{Path, ResourceDef},
@@ -19,7 +21,7 @@ use rustical_xml::XmlDeserialize;
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
pub struct AddressbookMultigetRequest {
#[xml(ns = "rustical_dav::namespace::NS_DAV", ty = "untagged")]
pub(crate) prop: PropfindType,
pub(crate) prop: PropfindType<AddressObjectPropWrapperName>,
#[xml(ns = "rustical_dav::namespace::NS_DAV", flatten)]
pub(crate) href: Vec<String>,
}
@@ -59,7 +61,7 @@ pub async fn get_objects_addressbook_multiget<AS: AddressbookStore>(
pub async fn handle_addressbook_multiget<AS: AddressbookStore>(
addr_multiget: &AddressbookMultigetRequest,
props: &[&str],
prop: &PropfindType<AddressObjectPropWrapperName>,
path: &str,
puri: &impl PrincipalUri,
user: &User,
@@ -79,7 +81,7 @@ pub async fn handle_addressbook_multiget<AS: AddressbookStore>(
object,
principal: principal.to_owned(),
}
.propfind(&path, props, puri, user)?,
.propfind_typed(&path, prop, puri, user)?,
);
}

View File

@@ -1,10 +1,10 @@
use crate::{CardDavPrincipalUri, Error};
use crate::{CardDavPrincipalUri, Error, address_object::resource::AddressObjectPropWrapperName};
use actix_web::{
HttpRequest, Responder,
web::{Data, Path},
};
use addressbook_multiget::{AddressbookMultigetRequest, handle_addressbook_multiget};
use rustical_dav::xml::{PropElement, PropfindType, sync_collection::SyncCollectionRequest};
use rustical_dav::xml::{PropfindType, sync_collection::SyncCollectionRequest};
use rustical_store::{AddressbookStore, auth::User};
use rustical_xml::{XmlDeserialize, XmlDocument};
use sync_collection::handle_sync_collection;
@@ -18,27 +18,14 @@ pub(crate) enum ReportRequest {
#[xml(ns = "rustical_dav::namespace::NS_CARDDAV")]
AddressbookMultiget(AddressbookMultigetRequest),
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
SyncCollection(SyncCollectionRequest),
SyncCollection(SyncCollectionRequest<AddressObjectPropWrapperName>),
}
impl ReportRequest {
fn props(&self) -> Vec<&str> {
let prop_element = match self {
fn props(&self) -> &PropfindType<AddressObjectPropWrapperName> {
match self {
ReportRequest::AddressbookMultiget(AddressbookMultigetRequest { prop, .. }) => prop,
ReportRequest::SyncCollection(SyncCollectionRequest { prop, .. }) => prop,
};
match prop_element {
PropfindType::Allprop => {
vec!["allprop"]
}
PropfindType::Propname => {
vec!["propname"]
}
PropfindType::Prop(PropElement(prop_tags)) => prop_tags
.iter()
.map(|propname| propname.name.as_str())
.collect(),
}
}
}
@@ -58,13 +45,12 @@ pub async fn route_report_addressbook<AS: AddressbookStore>(
}
let request = ReportRequest::parse_str(&body)?;
let props = request.props();
Ok(match &request {
ReportRequest::AddressbookMultiget(addr_multiget) => {
handle_addressbook_multiget(
addr_multiget,
&props,
request.props(),
req.path(),
puri.as_ref(),
&user,
@@ -77,7 +63,6 @@ pub async fn route_report_addressbook<AS: AddressbookStore>(
ReportRequest::SyncCollection(sync_collection) => {
handle_sync_collection(
sync_collection,
&props,
req.path(),
puri.as_ref(),
&user,
@@ -92,9 +77,9 @@ pub async fn route_report_addressbook<AS: AddressbookStore>(
#[cfg(test)]
mod tests {
use rustical_dav::xml::{PropElement, Propname, sync_collection::SyncLevel};
use super::*;
use crate::address_object::resource::AddressObjectPropName;
use rustical_dav::xml::{PropElement, sync_collection::SyncLevel};
#[test]
fn test_xml_sync_collection() {
@@ -115,10 +100,12 @@ mod tests {
ReportRequest::SyncCollection(SyncCollectionRequest {
sync_token: "".to_owned(),
sync_level: SyncLevel::One,
prop: rustical_dav::xml::PropfindType::Prop(PropElement(vec![Propname {
name: "getetag".to_owned(),
ns: Some("DAV:".into())
}])),
prop: rustical_dav::xml::PropfindType::Prop(PropElement(
vec![AddressObjectPropWrapperName::AddressObject(
AddressObjectPropName::Getetag
)],
vec![]
)),
limit: None
})
)
@@ -141,9 +128,13 @@ mod tests {
report_request,
ReportRequest::AddressbookMultiget(AddressbookMultigetRequest {
prop: rustical_dav::xml::PropfindType::Prop(PropElement(vec![
Propname{name: "getetag".to_owned(), ns: Some("DAV:".into())},
Propname{name: "address-data".to_owned(), ns: Some("urn:ietf:params:xml:ns:carddav".into())}
])),
AddressObjectPropWrapperName::AddressObject(
AddressObjectPropName::Getetag
),
AddressObjectPropWrapperName::AddressObject(
AddressObjectPropName::AddressData
),
], vec![])),
href: vec![
"/carddav/user/user/6f787542-5256-401a-8db97003260da/ae7a998fdfd1d84a20391168962c62b".to_owned()
]

View File

@@ -1,6 +1,8 @@
use crate::{
Error,
address_object::resource::{AddressObjectPropWrapper, AddressObjectResource},
address_object::resource::{
AddressObjectPropWrapper, AddressObjectPropWrapperName, AddressObjectResource,
},
};
use actix_web::http::StatusCode;
use rustical_dav::{
@@ -16,8 +18,7 @@ use rustical_store::{
};
pub async fn handle_sync_collection<AS: AddressbookStore>(
sync_collection: &SyncCollectionRequest,
props: &[&str],
sync_collection: &SyncCollectionRequest<AddressObjectPropWrapperName>,
path: &str,
puri: &impl PrincipalUri,
user: &User,
@@ -38,7 +39,7 @@ pub async fn handle_sync_collection<AS: AddressbookStore>(
object,
principal: principal.to_owned(),
}
.propfind(&path, props, puri, user)?,
.propfind_typed(&path, &sync_collection.prop, puri, user)?,
);
}

View File

@@ -17,7 +17,7 @@ use rustical_dav::xml::{Resourcetype, ResourcetypeInner};
use rustical_dav_push::{DavPushExtension, DavPushExtensionProp};
use rustical_store::auth::User;
use rustical_store::{Addressbook, AddressbookStore, SubscriptionStore};
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
use rustical_xml::{EnumVariants, PropName, XmlDeserialize, XmlSerialize};
use std::str::FromStr;
use std::sync::Arc;
@@ -35,7 +35,7 @@ impl<A: AddressbookStore, S: SubscriptionStore> AddressbookResourceService<A, S>
}
}
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, EnumUnitVariants)]
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "AddressbookPropName")]
pub enum AddressbookProp {
// WebDAV (RFC 2518)
@@ -53,7 +53,7 @@ pub enum AddressbookProp {
MaxResourceSize(i64),
}
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, EnumUnitVariants)]
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "AddressbookPropWrapperName", untagged)]
pub enum AddressbookPropWrapper {
Addressbook(AddressbookProp),

View File

@@ -8,7 +8,7 @@ use rustical_dav::resource::{PrincipalUri, Resource, ResourceService};
use rustical_dav::xml::{HrefElement, Resourcetype, ResourcetypeInner};
use rustical_store::auth::{AuthenticationProvider, User};
use rustical_store::{AddressbookStore, SubscriptionStore};
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
use rustical_xml::{EnumVariants, PropName, XmlDeserialize, XmlSerialize};
use std::sync::Arc;
pub struct PrincipalResourceService<
@@ -53,7 +53,7 @@ pub struct PrincipalResource {
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone)]
pub struct AddressbookHomeSet(#[xml(ty = "untagged", flatten)] Vec<HrefElement>);
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, EnumUnitVariants)]
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "PrincipalPropName")]
pub enum PrincipalProp {
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
@@ -71,7 +71,7 @@ pub enum PrincipalProp {
PrincipalAddress(Option<HrefElement>),
}
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, EnumUnitVariants)]
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "PrincipalPropWrapperName", untagged)]
pub enum PrincipalPropWrapper {
Principal(PrincipalProp),

View File

@@ -4,9 +4,9 @@ use crate::{
resource::{PrincipalUri, Resource},
xml::{HrefElement, Resourcetype},
};
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
use rustical_xml::{EnumVariants, PropName, XmlDeserialize, XmlSerialize};
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumUnitVariants, EnumVariants)]
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, PropName, EnumVariants)]
#[xml(unit_variants_ident = "CommonPropertiesPropName")]
pub enum CommonPropertiesProp {
// WebDAV (RFC 2518)

View File

@@ -1,6 +1,6 @@
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
use rustical_xml::{EnumVariants, PropName, XmlDeserialize, XmlSerialize};
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumUnitVariants, EnumVariants)]
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, PropName, EnumVariants)]
#[xml(unit_variants_ident = "SyncTokenExtensionPropName")]
pub enum SyncTokenExtensionProp {
// Collection Synchronization (RFC 6578)

View File

@@ -5,9 +5,9 @@ use crate::resource::PrincipalUri;
use crate::resource::Resource;
use crate::resource::ResourceService;
use crate::xml::MultistatusElement;
use crate::xml::PropElement;
use crate::xml::PropfindElement;
use crate::xml::PropfindType;
use rustical_xml::PropName;
use rustical_xml::XmlDocument;
use tracing::instrument;
@@ -58,37 +58,36 @@ pub(crate) async fn route_propfind<R: ResourceService>(
}
// A request body is optional. If empty we MUST return all props
let propfind: PropfindElement = if !body.is_empty() {
PropfindElement::parse_str(&body).map_err(Error::XmlError)?
} else {
PropfindElement {
prop: PropfindType::Allprop,
}
};
// TODO: respect namespaces?
let props = match &propfind.prop {
PropfindType::Allprop => vec!["allprop"],
PropfindType::Propname => vec!["propname"],
PropfindType::Prop(PropElement(prop_tags)) => prop_tags
.iter()
.map(|propname| propname.name.as_str())
.collect(),
};
let propfind_self: PropfindElement<<<R::Resource as Resource>::Prop as PropName>::Names> =
if !body.is_empty() {
PropfindElement::parse_str(&body).map_err(Error::XmlError)?
} else {
PropfindElement {
prop: PropfindType::Allprop,
}
};
let propfind_member: PropfindElement<<<R::MemberType as Resource>::Prop as PropName>::Names> =
if !body.is_empty() {
PropfindElement::parse_str(&body).map_err(Error::XmlError)?
} else {
PropfindElement {
prop: PropfindType::Allprop,
}
};
let mut member_responses = Vec::new();
if depth != Depth::Zero {
for (subpath, member) in resource_service.get_members(path_components).await? {
member_responses.push(member.propfind(
member_responses.push(member.propfind_typed(
&format!("{}/{}", path.trim_end_matches('/'), subpath),
&props,
&propfind_member.prop,
puri,
&user,
)?);
}
}
let response = resource.propfind(path, &props, puri, &user)?;
let response = resource.propfind_typed(path, &propfind_self.prop, puri, &user)?;
Ok(MultistatusElement {
responses: vec![response],

View File

@@ -7,7 +7,8 @@ use crate::xml::TagList;
use crate::xml::multistatus::{PropstatElement, PropstatWrapper, ResponseElement};
use http::StatusCode;
use quick_xml::name::Namespace;
use rustical_xml::EnumUnitVariants;
use rustical_xml::NamespaceOwned;
use rustical_xml::PropName;
use rustical_xml::Unparsed;
use rustical_xml::XmlDeserialize;
use rustical_xml::XmlDocument;
@@ -111,13 +112,15 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
}) => {
match property {
SetPropertyPropWrapper::Valid(prop) => {
let propname: <<R::Resource as Resource>::Prop as EnumUnitVariants>::UnitVariants = prop.clone().into();
let propname: <<R::Resource as Resource>::Prop as PropName>::Names =
prop.clone().into();
let (ns, propname): (Option<Namespace>, &str) = propname.into();
match resource.set_prop(prop) {
Ok(()) => props_ok.push((ns, propname.to_owned())),
Err(Error::PropReadOnly) => {
props_conflict.push((ns, propname.to_owned()))
Ok(()) => {
props_ok.push((ns.map(NamespaceOwned::from), propname.to_owned()))
}
Err(Error::PropReadOnly) => props_conflict
.push((ns.map(NamespaceOwned::from), propname.to_owned())),
Err(err) => return Err(err.into()),
};
}
@@ -128,7 +131,7 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
.into_iter()
.find_map(|(ns, tag)| {
if tag == propname.as_str() {
Some((ns, tag.to_owned()))
Some((ns.map(NamespaceOwned::from), tag.to_owned()))
} else {
None
}
@@ -146,14 +149,12 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
}
Operation::Remove(remove_el) => {
let propname = remove_el.prop.0.0;
match <<R::Resource as Resource>::Prop as EnumUnitVariants>::UnitVariants::from_str(
&propname,
) {
match <<R::Resource as Resource>::Prop as PropName>::Names::from_str(&propname) {
Ok(prop) => match resource.remove_prop(&prop) {
Ok(()) => props_ok.push((None, propname)),
Err(Error::PropReadOnly) => props_conflict.push({
let (ns, tag) = prop.into();
(ns, tag.to_owned())
(ns.map(NamespaceOwned::from), tag.to_owned())
}),
Err(err) => return Err(err.into()),
},

View File

@@ -1,14 +1,14 @@
use crate::Principal;
use crate::privileges::UserPrivilegeSet;
use crate::xml::Resourcetype;
use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper};
use crate::xml::{PropElement, PropfindType, Resourcetype};
use crate::xml::{TagList, multistatus::ResponseElement};
use crate::{Error, Principal};
use headers::{ETag, IfMatch, IfNoneMatch};
use http::StatusCode;
use itertools::Itertools;
use quick_xml::name::Namespace;
pub use resource_service::ResourceService;
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
use rustical_xml::{EnumVariants, NamespaceOwned, PropName, XmlDeserialize, XmlSerialize};
use std::collections::HashSet;
use std::str::FromStr;
@@ -26,7 +26,7 @@ pub trait ResourcePropName: FromStr {}
impl<T: FromStr> ResourcePropName for T {}
pub trait Resource: Clone + 'static {
type Prop: ResourceProp + PartialEq + Clone + EnumVariants + EnumUnitVariants;
type Prop: ResourceProp + PartialEq + Clone + EnumVariants + PropName;
type Error: From<crate::Error>;
type Principal: Principal;
@@ -40,17 +40,14 @@ pub trait Resource: Clone + 'static {
&self,
principal_uri: &impl PrincipalUri,
principal: &Self::Principal,
prop: &<Self::Prop as EnumUnitVariants>::UnitVariants,
prop: &<Self::Prop as PropName>::Names,
) -> Result<Self::Prop, Self::Error>;
fn set_prop(&mut self, _prop: Self::Prop) -> Result<(), crate::Error> {
Err(crate::Error::PropReadOnly)
}
fn remove_prop(
&mut self,
_prop: &<Self::Prop as EnumUnitVariants>::UnitVariants,
) -> Result<(), crate::Error> {
fn remove_prop(&mut self, _prop: &<Self::Prop as PropName>::Names) -> Result<(), crate::Error> {
Err(crate::Error::PropReadOnly)
}
@@ -91,62 +88,45 @@ pub trait Resource: Clone + 'static {
principal: &Self::Principal,
) -> Result<UserPrivilegeSet, Self::Error>;
fn propfind(
fn propfind_typed(
&self,
path: &str,
props: &[&str],
prop: &PropfindType<<Self::Prop as PropName>::Names>,
principal_uri: &impl PrincipalUri,
principal: &Self::Principal,
) -> Result<ResponseElement<Self::Prop>, Self::Error> {
let mut props: HashSet<&str> = props.iter().cloned().collect();
// TODO: Support include element
let (props, invalid_props): (HashSet<<Self::Prop as PropName>::Names>, Vec<_>) = match prop
{
PropfindType::Propname => {
let props = Self::list_props()
.into_iter()
.map(|(ns, tag)| (ns.map(NamespaceOwned::from), tag.to_string()))
.collect_vec();
if props.contains(&"propname") {
if props.len() != 1 {
// propname MUST be the only queried prop per spec
return Err(
Error::BadRequest("propname MUST be the only queried prop".to_owned()).into(),
);
return Ok(ResponseElement {
href: path.to_owned(),
propstat: vec![PropstatWrapper::TagList(PropstatElement {
prop: TagList::from(props),
status: StatusCode::OK,
})],
..Default::default()
});
}
PropfindType::Allprop => (
Self::list_props()
.iter()
.map(|(_ns, name)| <Self::Prop as PropName>::Names::from_str(name).unwrap())
.collect(),
vec![],
),
PropfindType::Prop(PropElement(valid_tags, invalid_tags)) => (
valid_tags.iter().cloned().collect(),
invalid_tags.to_owned(),
),
};
let props = Self::list_props()
.into_iter()
.map(|(ns, tag)| (ns.to_owned(), tag.to_string()))
.collect_vec();
return Ok(ResponseElement {
href: path.to_owned(),
propstat: vec![PropstatWrapper::TagList(PropstatElement {
prop: TagList::from(props),
status: StatusCode::OK,
})],
..Default::default()
});
}
if props.contains(&"allprop") {
if props.len() != 1 {
// allprop MUST be the only queried prop per spec
return Err(
Error::BadRequest("allprop MUST be the only queried prop".to_owned()).into(),
);
}
props = Self::list_props()
.into_iter()
.map(|(_ns, tag)| tag)
.collect();
}
let mut valid_props = vec![];
let mut invalid_props = vec![];
for prop in props {
if let Ok(valid_prop) = <Self::Prop as EnumUnitVariants>::UnitVariants::from_str(prop) {
valid_props.push(valid_prop);
} else {
invalid_props.push(prop.to_string())
}
}
let prop_responses = valid_props
let prop_responses = props
.into_iter()
.map(|prop| self.get_prop(principal_uri, principal, &prop))
.collect::<Result<Vec<_>, Self::Error>>()?;
@@ -158,11 +138,7 @@ pub trait Resource: Clone + 'static {
if !invalid_props.is_empty() {
propstats.push(PropstatWrapper::TagList(PropstatElement {
status: StatusCode::NOT_FOUND,
prop: invalid_props
.into_iter()
.map(|tag| (None, tag))
.collect_vec()
.into(),
prop: invalid_props.into(),
}));
}
Ok(ResponseElement {

View File

@@ -4,7 +4,7 @@ mod resourcetype;
pub mod tag_list;
use derive_more::derive::From;
pub use multistatus::MultistatusElement;
pub use propfind::{PropElement, PropfindElement, PropfindType, Propname};
pub use propfind::{PropElement, PropfindElement, PropfindType};
pub use resourcetype::{Resourcetype, ResourcetypeInner};
use rustical_xml::{XmlDeserialize, XmlSerialize};
pub use tag_list::TagList;

View File

@@ -1,27 +1,85 @@
use quick_xml::events::Event;
use quick_xml::name::ResolveResult;
use rustical_xml::NamespaceOwned;
use rustical_xml::Unparsed;
use rustical_xml::XmlDeserialize;
use rustical_xml::XmlError;
use rustical_xml::XmlRootTag;
#[derive(Debug, Clone, XmlDeserialize, XmlRootTag, PartialEq)]
#[xml(root = b"propfind", ns = "crate::namespace::NS_DAV")]
pub struct PropfindElement {
pub struct PropfindElement<PN: XmlDeserialize> {
#[xml(ty = "untagged")]
pub prop: PropfindType,
pub prop: PropfindType<PN>,
}
#[derive(Debug, Clone, PartialEq)]
// pub struct PropElement<PN: XmlDeserialize = Propname>(#[xml(ty = "untagged", flatten)] pub Vec<PN>);
pub struct PropElement<PN: XmlDeserialize>(
// valid
pub Vec<PN>,
// invalid
pub Vec<(Option<NamespaceOwned>, String)>,
);
impl<PN: XmlDeserialize> XmlDeserialize for PropElement<PN> {
fn deserialize<R: std::io::BufRead>(
reader: &mut quick_xml::NsReader<R>,
start: &quick_xml::events::BytesStart,
empty: bool,
) -> Result<Self, XmlError> {
if empty {
return Ok(Self(vec![], vec![]));
}
let mut buf = Vec::new();
let mut valid_props = vec![];
let mut invalid_props = vec![];
loop {
let event = reader.read_event_into(&mut buf)?;
match &event {
Event::End(e) if e.name() == start.name() => {
break;
}
Event::Eof => return Err(XmlError::Eof),
// start of a child element
Event::Start(start) | Event::Empty(start) => {
let empty = matches!(event, Event::Empty(_));
let (ns, name) = reader.resolve_element(start.name());
let ns = match ns {
ResolveResult::Bound(ns) => Some(NamespaceOwned::from(ns)),
ResolveResult::Unknown(_ns) => todo!("handle error"),
ResolveResult::Unbound => None,
};
match PN::deserialize(reader, start, empty) {
Ok(propname) => valid_props.push(propname),
Err(XmlError::InvalidVariant(_)) => {
invalid_props
.push((ns, String::from_utf8_lossy(name.as_ref()).to_string()));
// Consume content
Unparsed::deserialize(reader, start, empty)?;
}
Err(err) => return Err(err),
}
}
Event::Text(_) | Event::CData(_) => {
return Err(XmlError::UnsupportedEvent("Not expecting text here"));
}
Event::Decl(_) | Event::Comment(_) | Event::DocType(_) | Event::PI(_) => { /* ignore */
}
Event::End(_end) => {
unreachable!(
"Unexpected closing tag for wrong element, should be handled by quick_xml"
);
}
}
}
Ok(Self(valid_props, invalid_props))
}
}
#[derive(Debug, Clone, XmlDeserialize, PartialEq)]
pub struct PropElement<PN: XmlDeserialize = Propname>(#[xml(ty = "untagged", flatten)] pub Vec<PN>);
#[derive(Debug, Clone, XmlDeserialize, PartialEq)]
pub struct Propname {
#[xml(ty = "namespace")]
pub ns: Option<NamespaceOwned>,
#[xml(ty = "tag_name")]
pub name: String,
}
#[derive(Debug, Clone, XmlDeserialize, PartialEq)]
pub enum PropfindType<PN: XmlDeserialize = Propname> {
pub enum PropfindType<PN: XmlDeserialize> {
#[xml(ns = "crate::namespace::NS_DAV")]
Propname,
#[xml(ns = "crate::namespace::NS_DAV")]

View File

@@ -1,6 +1,6 @@
use rustical_xml::{ValueDeserialize, ValueSerialize, XmlDeserialize};
use super::{PropfindType, Propname};
use super::PropfindType;
#[derive(Clone, Debug, PartialEq)]
pub enum SyncLevel {
@@ -37,7 +37,7 @@ impl ValueSerialize for SyncLevel {
// <!-- DAV:limit defined in RFC 5323, Section 5.17 -->
// <!-- DAV:prop defined in RFC 4918, Section 14.18 -->
#[xml(ns = "crate::namespace::NS_DAV")]
pub struct SyncCollectionRequest<PN: XmlDeserialize = Propname> {
pub struct SyncCollectionRequest<PN: XmlDeserialize> {
#[xml(ns = "crate::namespace::NS_DAV")]
pub sync_token: String,
#[xml(ns = "crate::namespace::NS_DAV")]

View File

@@ -1,10 +1,13 @@
use derive_more::derive::From;
use quick_xml::name::Namespace;
use rustical_xml::XmlSerialize;
use quick_xml::{
events::{BytesStart, Event},
name::Namespace,
};
use rustical_xml::{NamespaceOwned, XmlSerialize};
use std::collections::HashMap;
#[derive(Clone, Debug, PartialEq, From)]
pub struct TagList(Vec<(Option<Namespace<'static>>, String)>);
pub struct TagList(Vec<(Option<NamespaceOwned>, String)>);
impl XmlSerialize for TagList {
fn serialize<W: std::io::Write>(
@@ -14,22 +17,10 @@ impl XmlSerialize for TagList {
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> {
#[derive(Debug, XmlSerialize, PartialEq)]
struct Inner(#[xml(ty = "untagged", flatten)] Vec<Tag>);
#[derive(Debug, XmlSerialize, PartialEq)]
struct Tag(
#[xml(ty = "namespace")] Option<Namespace<'static>>,
#[xml(ty = "tag_name")] String,
);
Inner(
self.0
.iter()
.map(|(ns, tag)| Tag(ns.to_owned(), tag.to_owned()))
.collect(),
)
.serialize(ns, tag, namespaces, writer)
for (_ns, tag) in &self.0 {
writer.write_event(Event::Empty(BytesStart::new(tag)))?;
}
Ok(())
}
#[allow(refining_impl_trait)]

View File

@@ -1,87 +0,0 @@
use rustical_dav::xml::{PropElement, PropfindElement, PropfindType, Propname};
use rustical_xml::de::XmlDocument;
#[test]
fn propfind_allprop() {
let propfind = PropfindElement::parse_str(
r#"
<propfind xmlns="DAV:">
<allprop />
</propfind>
"#,
)
.unwrap();
assert_eq!(
propfind,
PropfindElement {
prop: PropfindType::Allprop
}
);
}
#[test]
fn propfind_propname() {
let propfind = PropfindElement::parse_str(
r#"
<propfind xmlns="DAV:">
<propname />
</propfind>
"#,
)
.unwrap();
assert_eq!(
propfind,
PropfindElement {
prop: PropfindType::Propname
}
);
}
#[test]
fn propfind_prop() {
let propfind = PropfindElement::parse_str(
r#"
<propfind xmlns="DAV:">
<prop>
<displayname />
<color />
</prop>
</propfind>
"#,
)
.unwrap();
assert_eq!(
propfind,
PropfindElement {
prop: PropfindType::Prop(PropElement(vec![
Propname {
name: "displayname".to_owned(),
ns: Some("DAV:".to_owned().into())
},
Propname {
name: "color".to_owned(),
ns: Some("DAV:".to_owned().into())
},
]))
}
);
}
/// Example taken from DAVx5
#[test]
fn propfind_decl() {
let propfind = PropfindElement::parse_str(
r#"
<?xml version='1.0' encoding='UTF-8' ?>
<propfind xmlns="DAV:" xmlns:CAL="urn:ietf:params:xml:ns:caldav" xmlns:CARD="urn:ietf:params:xml:ns:carddav">
<prop>
<CARD:max-resource-size />
<CARD:supported-address-data />
<supported-report-set />
<n0:getctag xmlns:n0="http://calendarserver.org/ns/" />
<sync-token />
</prop>
</propfind>
"#
).unwrap();
}

View File

@@ -1,8 +1,8 @@
use crate::{ContentUpdate, PropertyUpdate, SupportedTriggers, Transports, Trigger};
use rustical_dav::header::Depth;
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
use rustical_xml::{EnumVariants, PropName, XmlDeserialize, XmlSerialize};
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumUnitVariants, EnumVariants)]
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, PropName, EnumVariants)]
#[xml(unit_variants_ident = "DavPushExtensionPropName")]
pub enum DavPushExtensionProp {
// WebDav Push

View File

@@ -17,9 +17,7 @@ rustical_xml.workspace = true
ical.workspace = true
lazy_static.workspace = true
regex.workspace = true
strum.workspace = true
strum_macros.workspace = true
rrule = "0.14"
rrule.workspace = true
serde.workspace = true
sha2.workspace = true
actix-web = { workspace = true, optional = true }

View File

@@ -1,6 +1,6 @@
use crate::Error;
use crate::{CalDateTime, ComponentMut, parse_duration};
use chrono::{DateTime, Duration};
use chrono::{DateTime, Duration, Utc};
use ical::{
generator::IcalEvent,
parser::{Component, ical::component::IcalTimeZone},
@@ -89,8 +89,18 @@ impl EventObject {
Ok(Some(rrule_set))
}
pub fn expand_recurrence(&self) -> Result<Vec<IcalEvent>, Error> {
if let Some(rrule_set) = self.recurrence_ruleset()? {
pub fn expand_recurrence(
&self,
start: Option<DateTime<Utc>>,
end: Option<DateTime<Utc>>,
) -> Result<Vec<IcalEvent>, Error> {
if let Some(mut rrule_set) = self.recurrence_ruleset()? {
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 mut events = vec![];
let dates = rrule_set.all(2048).dates;
@@ -205,7 +215,7 @@ END:VEVENT\r\n",
let event = event.event().unwrap();
let events: Vec<String> = event
.expand_recurrence()
.expand_recurrence(None, None)
.unwrap()
.into_iter()
.map(|event| Emitter::generate(&event))

View File

@@ -1,6 +1,8 @@
use super::{EventObject, JournalObject, TodoObject};
use crate::CalDateTime;
use crate::Error;
use chrono::DateTime;
use chrono::Utc;
use ical::{
generator::{Emitter, IcalCalendar},
parser::{Component, ical::component::IcalTimeZone},
@@ -188,12 +190,16 @@ impl CalendarObject {
}
}
pub fn expand_recurrence(&self) -> Result<String, Error> {
pub fn expand_recurrence(
&self,
start: Option<DateTime<Utc>>,
end: Option<DateTime<Utc>>,
) -> Result<String, Error> {
// Only events can be expanded
match &self.data {
CalendarObjectComponent::Event(event) => {
let mut cal = self.cal.clone();
cal.events = event.expand_recurrence()?;
cal.events = event.expand_recurrence(start, end)?;
Ok(cal.generate())
}
_ => Ok(self.get_ics().to_string()),

View File

@@ -38,7 +38,7 @@ pub enum CalDateTimeError {
InvalidDurationFormat(String),
}
#[derive(Debug, Clone, Deref, PartialEq)]
#[derive(Debug, Clone, Deref, PartialEq, Eq, Hash)]
pub struct UtcDateTime(pub DateTime<Utc>);
impl ValueDeserialize for UtcDateTime {

View File

@@ -28,10 +28,8 @@ rand.workspace = true
uuid.workspace = true
clap.workspace = true
rustical_dav.workspace = true
strum.workspace = true
strum_macros.workspace = true
rustical_ical.workspace = true
rrule = "0.14"
rrule.workspace = true
[dev-dependencies]
rstest = { workspace = true }

View File

@@ -252,7 +252,7 @@ impl SqliteCalendarStore {
.fetch_all(executor)
.await.map_err(crate::Error::from)?
.into_iter()
.map(|row| row.try_into().map_err(rustical_store::Error::from))
.map(|row| row.try_into())
.collect()
}
@@ -286,7 +286,7 @@ impl SqliteCalendarStore {
.await
.map_err(crate::Error::from)?
.into_iter()
.map(|row| row.try_into().map_err(rustical_store::Error::from))
.map(|row| row.try_into())
.collect()
}

View File

@@ -1,7 +1,7 @@
use std::collections::HashMap;
use darling::{FromDeriveInput, FromField, FromMeta, FromVariant, util::Flag};
use syn::LitByteStr;
use syn::{Ident, LitByteStr};
#[derive(Debug, Default, FromMeta, Clone)]
pub struct TagAttrs {
@@ -16,6 +16,8 @@ pub struct VariantAttrs {
pub common: TagAttrs,
pub other: Flag,
pub skip_deserializing: Flag,
// This is actually only for the PropName trait
pub prop: Option<Ident>,
}
#[derive(Default, FromDeriveInput, Clone)]

View File

@@ -74,13 +74,13 @@ pub fn derive_enum_variants(input: proc_macro::TokenStream) -> proc_macro::Token
.into()
}
#[proc_macro_derive(EnumUnitVariants, attributes(xml))]
pub fn derive_enum_unit_variants(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
#[proc_macro_derive(PropName, attributes(xml))]
pub fn derive_enum_prop_name(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(input as DeriveInput);
match &input.data {
syn::Data::Struct(_) => panic!("Struct not supported"),
syn::Data::Enum(e) => Enum::parse(&input, e).impl_enum_unit_variants(),
syn::Data::Enum(e) => Enum::parse(&input, e).impl_enum_prop_name(),
syn::Data::Union(_) => panic!("Union not supported"),
}
.into()

View File

@@ -85,8 +85,8 @@ impl Variant {
) {
(_, Fields::Named(_), _) => {
panic!(
"struct variants are not supported, please use a tuple variant with a struct"
)
"struct variants are not supported, please use a tuple variant with a struct"
)
}
(false, Fields::Unnamed(FieldsUnnamed { unnamed, .. }), true) => {
if unnamed.len() != 1 {
@@ -165,16 +165,20 @@ impl Variant {
}
let field = unnamed.iter().next().unwrap();
quote! {
if let Ok(val) = <#field as ::rustical_xml::XmlDeserialize>::deserialize(reader, start, empty) {
return Ok(Self::#ident(val));
match <#field as ::rustical_xml::XmlDeserialize>::deserialize(reader, start, empty) {
Ok(val) => { return Ok(Self::#ident(val)) }
Err(::rustical_xml::XmlError::InvalidVariant(..)) => {}
Err(err) => { return Err(err) }
}
}
}
Fields::Unit => {
quote! {
// Make sure that content is still consumed
if let Ok(_) = <() as ::rustical_xml::XmlDeserialize>::deserialize(reader, start, empty) {
return Ok(Self::#ident);
match <() as ::rustical_xml::XmlDeserialize>::deserialize(reader, start, empty) {
Ok(val) => { return Ok(Self::#ident(val)) }
Err(::rustical_xml::XmlError::InvalidVariant(..)) => {}
Err(err) => { return Err(err) }
}
}
}

View File

@@ -1,400 +0,0 @@
use super::{attrs::EnumAttrs, Variant};
use crate::attrs::VariantAttrs;
use core::panic;
use darling::{FromDeriveInput, FromVariant};
use quote::quote;
use syn::{DataEnum, DeriveInput};
pub struct Enum {
attrs: EnumAttrs,
variants: Vec<Variant>,
ident: syn::Ident,
generics: syn::Generics,
}
impl Enum {
fn impl_de_untagged(&self) -> proc_macro2::TokenStream {
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
let name = &self.ident;
let variant_branches = self
.variants
.iter()
.filter_map(|variant| variant.untagged_branch());
quote! {
impl #impl_generics ::rustical_xml::XmlDeserialize for #name #type_generics #where_clause {
fn deserialize<R: ::std::io::BufRead>(
reader: &mut quick_xml::NsReader<R>,
start: &quick_xml::events::BytesStart,
empty: bool
) -> Result<Self, rustical_xml::XmlError> {
#(#variant_branches);*
Err(rustical_xml::XmlError::InvalidVariant("could not match".to_owned()))
}
}
}
}
fn impl_de_tagged(&self) -> proc_macro2::TokenStream {
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
let name = &self.ident;
let variant_branches = self.variants.iter().filter_map(Variant::tagged_branch);
quote! {
impl #impl_generics ::rustical_xml::XmlDeserialize for #name #type_generics #where_clause {
fn deserialize<R: std::io::BufRead>(
reader: &mut quick_xml::NsReader<R>,
start: &quick_xml::events::BytesStart,
empty: bool
) -> Result<Self, rustical_xml::XmlError> {
let (_ns, name) = reader.resolve_element(start.name());
match name.as_ref() {
#(#variant_branches),*
name => {
// Handle invalid variant name
Err(rustical_xml::XmlError::InvalidVariant(String::from_utf8_lossy(name).to_string()))
}
}
}
}
}
}
pub fn impl_de(&self) -> proc_macro2::TokenStream {
match self.attrs.untagged.is_present() {
true => self.impl_de_untagged(),
false => self.impl_de_tagged(),
}
}
pub fn parse(input: &DeriveInput, data: &DataEnum) -> Self {
let attrs = EnumAttrs::from_derive_input(input).unwrap();
Self {
variants: data
.variants
.iter()
.map(|variant| Variant {
attrs: VariantAttrs::from_variant(variant).unwrap(),
variant: variant.to_owned(),
})
.collect(),
attrs,
ident: input.ident.to_owned(),
generics: input.generics.to_owned(),
}
}
pub fn impl_se(&self) -> proc_macro2::TokenStream {
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
let ident = &self.ident;
let enum_untagged = self.attrs.untagged.is_present();
let variant_serializers = self.variants.iter().map(Variant::se_branch);
quote! {
impl #impl_generics ::rustical_xml::XmlSerialize for #ident #type_generics #where_clause {
fn serialize<W: ::std::io::Write>(
&self,
ns: Option<::quick_xml::name::Namespace>,
tag: Option<&[u8]>,
namespaces: &::std::collections::HashMap<::quick_xml::name::Namespace, &[u8]>,
writer: &mut ::quick_xml::Writer<W>
) -> ::std::io::Result<()> {
use ::quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
let prefix = ns
.map(|ns| namespaces.get(&ns))
.unwrap_or(None)
.map(|prefix| [*prefix, b":"].concat());
let has_prefix = prefix.is_some();
let tagname = tag.map(|tag| [&prefix.unwrap_or_default(), tag].concat());
let qname = tagname.as_ref().map(|tagname| ::quick_xml::name::QName(tagname));
const enum_untagged: bool = #enum_untagged;
if let Some(qname) = &qname {
let mut bytes_start = BytesStart::from(qname.to_owned());
if !has_prefix {
if let Some(ns) = &ns {
bytes_start.push_attribute((b"xmlns".as_ref(), ns.as_ref()));
}
}
writer.write_event(Event::Start(bytes_start))?;
}
#(#variant_serializers);*
if let Some(qname) = &qname {
writer.write_event(Event::End(BytesEnd::from(qname.to_owned())))?;
}
Ok(())
}
fn attributes<'a>(&self) -> Option<Vec<::quick_xml::events::attributes::Attribute<'a>>> {
None
}
}
}
}
pub fn impl_xml_document(&self) -> proc_macro2::TokenStream {
if self.attrs.untagged.is_present() {
panic!("XmlDocument only supported for untagged enums");
}
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
let ident = &self.ident;
quote! {
impl #impl_generics ::rustical_xml::XmlDocument for #ident #type_generics #where_clause {
fn parse<R: ::std::io::BufRead>(mut reader: ::quick_xml::NsReader<R>) -> Result<Self, ::rustical_xml::XmlError>
where
Self: ::rustical_xml::XmlDeserialize
{
use ::quick_xml::events::Event;
let mut buf = Vec::new();
loop {
let event = reader.read_event_into(&mut buf)?;
let empty = matches!(event, Event::Empty(_));
match event {
Event::Start(start) | Event::Empty(start) => {
return <Self as ::rustical_xml::XmlDeserialize>::deserialize(&mut reader, &start, empty);
}
Event::Eof => return Err(::rustical_xml::XmlError::Eof),
Event::Text(bytes_text) => {
return Err(::rustical_xml::XmlError::UnsupportedEvent("Text"));
}
Event::CData(cdata) => {
return Err(::rustical_xml::XmlError::UnsupportedEvent("CDATA"));
}
Event::Decl(_) => { /* <?xml ... ?> ignore this */ }
Event::Comment(_) => { /* ignore */ }
Event::DocType(_) => { /* ignore */ }
Event::PI(_) => {
return Err(::rustical_xml::XmlError::UnsupportedEvent("Processing instruction"));
}
Event::End(end) => {
unreachable!("Premature end of xml document, should be handled by quick_xml");
}
};
}
}
}
}
}
pub fn impl_enum_variants(&self) -> proc_macro2::TokenStream {
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
let ident = &self.ident;
if self.attrs.untagged.is_present() {
let untagged_variants = self.variants.iter().map(|variant| {
let ty = &variant.deserializer_type();
quote! { #ty::variant_names() }
});
quote! {
impl #impl_generics ::rustical_xml::EnumVariants for #ident #type_generics #where_clause {
const TAGGED_VARIANTS: &'static [(Option<::quick_xml::name::Namespace<'static>>, &'static str)] = &[];
fn variant_names() -> Vec<(Option<::quick_xml::name::Namespace<'static>>, &'static str)> {
[
#(#untagged_variants),*
].concat()
}
}
}
} else {
let tagged_variants = self.variants.iter().map(|variant| {
let ns = match &variant.attrs.common.ns {
Some(ns) => quote! { Some(#ns) },
None => quote! { None },
};
let b_xml_name = variant.xml_name().value();
let xml_name = String::from_utf8_lossy(&b_xml_name);
quote! {(#ns, #xml_name)}
});
quote! {
impl #impl_generics ::rustical_xml::EnumVariants for #ident #type_generics #where_clause {
const TAGGED_VARIANTS: &'static [(Option<::quick_xml::name::Namespace<'static>>, &'static str)] = &[
#(#tagged_variants),*
];
fn variant_names() -> Vec<(Option<::quick_xml::name::Namespace<'static>>, &'static str)> {
[Self::TAGGED_VARIANTS,].concat()
}
}
}
}
}
pub fn impl_enum_unit_variants(&self) -> proc_macro2::TokenStream {
let unit_enum_ident = self
.attrs
.unit_variants_ident
.as_ref()
.expect("unit_variants_ident no set");
let ident = &self.ident;
if self.attrs.untagged.is_present() {
let variant_branches: Vec<_> = self
.variants
.iter()
.map(|variant| {
let variant_type = variant.deserializer_type();
let variant_ident = &variant.variant.ident;
quote! {
#variant_ident (<#variant_type as ::rustical_xml::EnumUnitVariants>::UnitVariants)
}
})
.collect();
let variant_idents: Vec<_> = self
.variants
.iter()
.map(|variant| &variant.variant.ident)
.collect();
let unit_to_output_branches = variant_idents.iter().map(|variant_ident| {
quote! { #unit_enum_ident::#variant_ident(val) => val.into() }
});
let str_to_unit_branches = self.variants.iter().map(|variant| {
let variant_type = variant.deserializer_type();
let variant_ident = &variant.variant.ident;
quote! {
if let Ok(name) = <#variant_type as ::rustical_xml::EnumUnitVariants>::UnitVariants::from_str(val) {
return Ok(Self::#variant_ident(name))
}
}
});
let from_enum_to_unit_branches = variant_idents.iter().map(|variant_ident| {
quote! { #ident::#variant_ident(val) => #unit_enum_ident::#variant_ident(val.into()) }
});
quote! {
#[derive(Clone, Debug, PartialEq)]
pub enum #unit_enum_ident {
#(#variant_branches),*
}
impl ::rustical_xml::EnumUnitVariants for #ident {
type UnitVariants = #unit_enum_ident;
}
impl From<#unit_enum_ident> for (Option<::quick_xml::name::Namespace<'static>>, &'static str) {
fn from(val: #unit_enum_ident) -> Self {
match val {
#(#unit_to_output_branches),*
}
}
}
impl From<#ident> for #unit_enum_ident {
fn from(val: #ident) -> Self {
match val {
#(#from_enum_to_unit_branches),*
}
}
}
impl ::std::str::FromStr for #unit_enum_ident {
type Err = ::rustical_xml::FromStrError;
fn from_str(val: &str) -> Result<Self, Self::Err> {
#(#str_to_unit_branches);*
Err(::rustical_xml::FromStrError)
}
}
}
} else {
let tagged_variants: Vec<_> = self
.variants
.iter()
.filter(|variant| !variant.attrs.other.is_present())
.collect();
let variant_outputs: Vec<_> = tagged_variants
.iter()
.map(|variant| {
let ns = match &variant.attrs.common.ns {
Some(ns) => quote! { Some(#ns) },
None => quote! { None },
};
let b_xml_name = variant.xml_name().value();
let xml_name = String::from_utf8_lossy(&b_xml_name);
quote! {(#ns, #xml_name)}
})
.collect();
let variant_idents: Vec<_> = tagged_variants
.iter()
.map(|variant| &variant.variant.ident)
.collect();
let unit_to_output_branches =
variant_idents
.iter()
.zip(&variant_outputs)
.map(|(variant_ident, out)| {
quote! { #unit_enum_ident::#variant_ident => #out }
});
let from_enum_to_unit_branches = variant_idents.iter().map(|variant_ident| {
quote! { #ident::#variant_ident { .. } => #unit_enum_ident::#variant_ident }
});
let str_to_unit_branches = tagged_variants.iter().map(|variant| {
let variant_ident = &variant.variant.ident;
let b_xml_name = variant.xml_name().value();
let xml_name = String::from_utf8_lossy(&b_xml_name);
quote! { #xml_name => Ok(#unit_enum_ident::#variant_ident) }
});
quote! {
#[derive(Clone, Debug, PartialEq)]
pub enum #unit_enum_ident {
#(#variant_idents),*
}
impl ::rustical_xml::EnumUnitVariants for #ident {
type UnitVariants = #unit_enum_ident;
}
impl From<#unit_enum_ident> for (Option<::quick_xml::name::Namespace<'static>>, &'static str) {
fn from(val: #unit_enum_ident) -> Self {
match val {
#(#unit_to_output_branches),*
}
}
}
impl From<#ident> for #unit_enum_ident {
fn from(val: #ident) -> Self {
match val {
#(#from_enum_to_unit_branches),*
}
}
}
impl ::std::str::FromStr for #unit_enum_ident {
type Err = ::rustical_xml::FromStrError;
fn from_str(val: &str) -> Result<Self, Self::Err> {
match val {
#(#str_to_unit_branches),*,
_ => Err(::rustical_xml::FromStrError)
}
}
}
}
}
}
}

View File

@@ -0,0 +1,64 @@
use crate::Variant;
use super::Enum;
use quote::quote;
impl Enum {
fn impl_de_untagged(&self) -> proc_macro2::TokenStream {
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
let name = &self.ident;
let variant_branches = self
.variants
.iter()
.filter_map(|variant| variant.untagged_branch());
quote! {
impl #impl_generics ::rustical_xml::XmlDeserialize for #name #type_generics #where_clause {
fn deserialize<R: ::std::io::BufRead>(
reader: &mut quick_xml::NsReader<R>,
start: &quick_xml::events::BytesStart,
empty: bool
) -> Result<Self, rustical_xml::XmlError> {
#(#variant_branches);*
Err(rustical_xml::XmlError::InvalidVariant("could not match".to_owned()))
}
}
}
}
fn impl_de_tagged(&self) -> proc_macro2::TokenStream {
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
let name = &self.ident;
let variant_branches = self.variants.iter().filter_map(Variant::tagged_branch);
quote! {
impl #impl_generics ::rustical_xml::XmlDeserialize for #name #type_generics #where_clause {
fn deserialize<R: std::io::BufRead>(
reader: &mut quick_xml::NsReader<R>,
start: &quick_xml::events::BytesStart,
empty: bool
) -> Result<Self, rustical_xml::XmlError> {
let (_ns, name) = reader.resolve_element(start.name());
match name.as_ref() {
#(#variant_branches),*
name => {
// Handle invalid variant name
Err(rustical_xml::XmlError::InvalidVariant(String::from_utf8_lossy(name).to_string()))
}
}
}
}
}
}
pub fn impl_de(&self) -> proc_macro2::TokenStream {
match self.attrs.untagged.is_present() {
true => self.impl_de_untagged(),
false => self.impl_de_tagged(),
}
}
}

View File

@@ -0,0 +1,179 @@
use super::Enum;
use quote::quote;
impl Enum {
pub fn impl_enum_prop_name(&self) -> proc_macro2::TokenStream {
let unit_enum_ident = self
.attrs
.unit_variants_ident
.as_ref()
.expect("unit_variants_ident no set");
let ident = &self.ident;
if self.attrs.untagged.is_present() {
let variant_branches: Vec<_> = self
.variants
.iter()
.map(|variant| {
let variant_type = variant.deserializer_type();
let variant_ident = &variant.variant.ident;
quote! {
#variant_ident (<#variant_type as ::rustical_xml::PropName>::Names)
}
})
.collect();
let variant_idents: Vec<_> = self
.variants
.iter()
.map(|variant| &variant.variant.ident)
.collect();
let unit_to_output_branches = variant_idents.iter().map(|variant_ident| {
quote! { #unit_enum_ident::#variant_ident(val) => val.into() }
});
let str_to_unit_branches = self.variants.iter().map(|variant| {
let variant_type = variant.deserializer_type();
let variant_ident = &variant.variant.ident;
quote! {
if let Ok(name) = <#variant_type as ::rustical_xml::PropName>::Names::from_str(val) {
return Ok(Self::#variant_ident(name))
}
}
});
let from_enum_to_unit_branches = variant_idents.iter().map(|variant_ident| {
quote! { #ident::#variant_ident(val) => #unit_enum_ident::#variant_ident(val.into()) }
});
quote! {
#[derive(Clone, Debug, PartialEq, Hash, Eq, ::rustical_xml::XmlDeserialize)]
#[xml(untagged)]
pub enum #unit_enum_ident {
#(#variant_branches),*
}
impl ::rustical_xml::PropName for #ident {
type Names = #unit_enum_ident;
}
impl From<#unit_enum_ident> for (Option<::quick_xml::name::Namespace<'static>>, &'static str) {
fn from(val: #unit_enum_ident) -> Self {
match val {
#(#unit_to_output_branches),*
}
}
}
impl From<#ident> for #unit_enum_ident {
fn from(val: #ident) -> Self {
match val {
#(#from_enum_to_unit_branches),*
}
}
}
impl ::std::str::FromStr for #unit_enum_ident {
type Err = ::rustical_xml::FromStrError;
fn from_str(val: &str) -> Result<Self, Self::Err> {
#(#str_to_unit_branches);*
Err(::rustical_xml::FromStrError)
}
}
}
} else {
let tagged_variants: Vec<_> = self
.variants
.iter()
.filter(|variant| !variant.attrs.other.is_present())
.collect();
let prop_name_variants = tagged_variants.iter().map(|variant| {
let ident = &variant.variant.ident;
if let Some(proptype) = &variant.attrs.prop {
quote! {#ident(#proptype)}
} else {
quote! {#ident}
}
});
let unit_to_output_branches = tagged_variants.iter().map(|variant| {
let ns = match &variant.attrs.common.ns {
Some(ns) => quote! { Some(#ns) },
None => quote! { None },
};
let b_xml_name = variant.xml_name().value();
let xml_name = String::from_utf8_lossy(&b_xml_name);
let out = quote! {(#ns, #xml_name)};
let ident = &variant.variant.ident;
if variant.attrs.prop.is_some() {
quote! { #unit_enum_ident::#ident(..) => #out }
} else {
quote! { #unit_enum_ident::#ident => #out }
}
});
let from_enum_to_unit_branches = tagged_variants.iter().map(|variant| {
let variant_ident = &variant.variant.ident;
if variant.attrs.prop.is_some() {
quote! { #ident::#variant_ident { .. } => Self::#variant_ident (Default::default()) }
} else {
quote! { #ident::#variant_ident { .. } => Self::#variant_ident }
}
});
let str_to_unit_branches = tagged_variants.iter().map(|variant| {
let ident = &variant.variant.ident;
let b_xml_name = variant.xml_name().value();
let xml_name = String::from_utf8_lossy(&b_xml_name);
if variant.attrs.prop.is_some() {
quote! { #xml_name => Ok(Self::#ident (Default::default())) }
} else {
quote! { #xml_name => Ok(Self::#ident) }
}
});
quote! {
#[derive(Clone, Debug, PartialEq, Eq, Hash, ::rustical_xml::XmlDeserialize)]
pub enum #unit_enum_ident {
#(#prop_name_variants),*
}
impl ::rustical_xml::PropName for #ident {
type Names = #unit_enum_ident;
}
impl From<#unit_enum_ident> for (Option<::quick_xml::name::Namespace<'static>>, &'static str) {
fn from(val: #unit_enum_ident) -> Self {
match val {
#(#unit_to_output_branches),*
}
}
}
impl From<#ident> for #unit_enum_ident {
fn from(val: #ident) -> Self {
match val {
#(#from_enum_to_unit_branches),*
}
}
}
impl ::std::str::FromStr for #unit_enum_ident {
type Err = ::rustical_xml::FromStrError;
fn from_str(val: &str) -> Result<Self, Self::Err> {
match val {
#(#str_to_unit_branches),*,
_ => Err(::rustical_xml::FromStrError)
}
}
}
}
}
}
}

View File

@@ -0,0 +1,59 @@
use quote::quote;
use crate::Variant;
use super::Enum;
impl Enum {
pub fn impl_se(&self) -> proc_macro2::TokenStream {
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
let ident = &self.ident;
let enum_untagged = self.attrs.untagged.is_present();
let variant_serializers = self.variants.iter().map(Variant::se_branch);
quote! {
impl #impl_generics ::rustical_xml::XmlSerialize for #ident #type_generics #where_clause {
fn serialize<W: ::std::io::Write>(
&self,
ns: Option<::quick_xml::name::Namespace>,
tag: Option<&[u8]>,
namespaces: &::std::collections::HashMap<::quick_xml::name::Namespace, &[u8]>,
writer: &mut ::quick_xml::Writer<W>
) -> ::std::io::Result<()> {
use ::quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
let prefix = ns
.map(|ns| namespaces.get(&ns))
.unwrap_or(None)
.map(|prefix| [*prefix, b":"].concat());
let has_prefix = prefix.is_some();
let tagname = tag.map(|tag| [&prefix.unwrap_or_default(), tag].concat());
let qname = tagname.as_ref().map(|tagname| ::quick_xml::name::QName(tagname));
const enum_untagged: bool = #enum_untagged;
if let Some(qname) = &qname {
let mut bytes_start = BytesStart::from(qname.to_owned());
if !has_prefix {
if let Some(ns) = &ns {
bytes_start.push_attribute((b"xmlns".as_ref(), ns.as_ref()));
}
}
writer.write_event(Event::Start(bytes_start))?;
}
#(#variant_serializers);*
if let Some(qname) = &qname {
writer.write_event(Event::End(BytesEnd::from(qname.to_owned())))?;
}
Ok(())
}
fn attributes<'a>(&self) -> Option<Vec<::quick_xml::events::attributes::Attribute<'a>>> {
None
}
}
}
}
}

View File

@@ -0,0 +1,129 @@
use super::{Variant, attrs::EnumAttrs};
use crate::attrs::VariantAttrs;
use core::panic;
use darling::{FromDeriveInput, FromVariant};
use quote::quote;
use syn::{DataEnum, DeriveInput};
mod impl_de;
mod impl_prop_name;
mod impl_se;
pub struct Enum {
attrs: EnumAttrs,
variants: Vec<Variant>,
ident: syn::Ident,
generics: syn::Generics,
}
impl Enum {
pub fn parse(input: &DeriveInput, data: &DataEnum) -> Self {
let attrs = EnumAttrs::from_derive_input(input).unwrap();
Self {
variants: data
.variants
.iter()
.map(|variant| Variant {
attrs: VariantAttrs::from_variant(variant).unwrap(),
variant: variant.to_owned(),
})
.collect(),
attrs,
ident: input.ident.to_owned(),
generics: input.generics.to_owned(),
}
}
pub fn impl_xml_document(&self) -> proc_macro2::TokenStream {
if self.attrs.untagged.is_present() {
panic!("XmlDocument only supported for untagged enums");
}
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
let ident = &self.ident;
quote! {
impl #impl_generics ::rustical_xml::XmlDocument for #ident #type_generics #where_clause {
fn parse<R: ::std::io::BufRead>(mut reader: ::quick_xml::NsReader<R>) -> Result<Self, ::rustical_xml::XmlError>
where
Self: ::rustical_xml::XmlDeserialize
{
use ::quick_xml::events::Event;
let mut buf = Vec::new();
loop {
let event = reader.read_event_into(&mut buf)?;
let empty = matches!(event, Event::Empty(_));
match event {
Event::Start(start) | Event::Empty(start) => {
return <Self as ::rustical_xml::XmlDeserialize>::deserialize(&mut reader, &start, empty);
}
Event::Eof => return Err(::rustical_xml::XmlError::Eof),
Event::Text(bytes_text) => {
return Err(::rustical_xml::XmlError::UnsupportedEvent("Text"));
}
Event::CData(cdata) => {
return Err(::rustical_xml::XmlError::UnsupportedEvent("CDATA"));
}
Event::Decl(_) => { /* <?xml ... ?> ignore this */ }
Event::Comment(_) => { /* ignore */ }
Event::DocType(_) => { /* ignore */ }
Event::PI(_) => {
return Err(::rustical_xml::XmlError::UnsupportedEvent("Processing instruction"));
}
Event::End(end) => {
unreachable!("Premature end of xml document, should be handled by quick_xml");
}
};
}
}
}
}
}
pub fn impl_enum_variants(&self) -> proc_macro2::TokenStream {
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
let ident = &self.ident;
if self.attrs.untagged.is_present() {
let untagged_variants = self.variants.iter().map(|variant| {
let ty = &variant.deserializer_type();
quote! { #ty::variant_names() }
});
quote! {
impl #impl_generics ::rustical_xml::EnumVariants for #ident #type_generics #where_clause {
const TAGGED_VARIANTS: &'static [(Option<::quick_xml::name::Namespace<'static>>, &'static str)] = &[];
fn variant_names() -> Vec<(Option<::quick_xml::name::Namespace<'static>>, &'static str)> {
[
#(#untagged_variants),*
].concat()
}
}
}
} else {
let tagged_variants = self.variants.iter().map(|variant| {
let ns = match &variant.attrs.common.ns {
Some(ns) => quote! { Some(#ns) },
None => quote! { None },
};
let b_xml_name = variant.xml_name().value();
let xml_name = String::from_utf8_lossy(&b_xml_name);
quote! {(#ns, #xml_name)}
});
quote! {
impl #impl_generics ::rustical_xml::EnumVariants for #ident #type_generics #where_clause {
const TAGGED_VARIANTS: &'static [(Option<::quick_xml::name::Namespace<'static>>, &'static str)] = &[
#(#tagged_variants),*
];
fn variant_names() -> Vec<(Option<::quick_xml::name::Namespace<'static>>, &'static str)> {
[Self::TAGGED_VARIANTS,].concat()
}
}
}
}
}
}

View File

@@ -1,5 +1,6 @@
use quick_xml::name::Namespace;
use std::collections::HashMap;
use std::hash::Hash;
use std::str::FromStr;
pub mod de;
@@ -17,8 +18,8 @@ pub use se::XmlSerialize;
pub use se::XmlSerializeRoot;
pub use unparsed::Unparsed;
pub use value::{ParseValueError, ValueDeserialize, ValueSerialize};
pub use xml_derive::EnumUnitVariants;
pub use xml_derive::EnumVariants;
pub use xml_derive::PropName;
pub use xml_derive::XmlRootTag;
pub trait XmlRootTag {
@@ -37,6 +38,12 @@ pub trait EnumVariants {
fn variant_names() -> Vec<(Option<Namespace<'static>>, &'static str)>;
}
pub trait EnumUnitVariants: Sized {
type UnitVariants: Into<(Option<Namespace<'static>>, &'static str)> + From<Self> + FromStr;
pub trait PropName: Sized {
type Names: Into<(Option<Namespace<'static>>, &'static str)>
+ Clone
+ From<Self>
+ FromStr<Err: std::fmt::Debug>
+ Hash
+ Eq
+ XmlDeserialize;
}

View File

@@ -2,7 +2,7 @@ use std::str::FromStr;
use quick_xml::name::Namespace;
use rustical_xml::EnumVariants;
use xml_derive::EnumUnitVariants;
use xml_derive::PropName;
pub const NS_DAV: Namespace = Namespace(b"DAV:");
pub const NS_DAVPUSH: Namespace = Namespace(b"https://bitfire.at/webdav-push");
@@ -12,13 +12,13 @@ pub const NS_ICAL: Namespace = Namespace(b"http://apple.com/ns/ical/");
pub const NS_CALENDARSERVER: Namespace = Namespace(b"http://calendarserver.org/ns/");
pub const NS_NEXTCLOUD: Namespace = Namespace(b"http://nextcloud.com/ns");
#[derive(EnumVariants, EnumUnitVariants)]
#[derive(EnumVariants, PropName)]
#[xml(unit_variants_ident = "ExtensionsPropName")]
enum ExtensionProp {
Hello,
}
#[derive(EnumVariants, EnumUnitVariants)]
#[derive(EnumVariants, PropName)]
#[xml(unit_variants_ident = "CalendarPropName")]
enum CalendarProp {
// WebDAV (RFC 2518)
@@ -45,7 +45,7 @@ fn test_enum_tagged_variants() {
);
}
#[derive(EnumVariants, EnumUnitVariants)]
#[derive(EnumVariants, PropName)]
#[xml(untagged, unit_variants_ident = "UnionPropName")]
enum UnionProp {
Calendar(CalendarProp),