xml use tuple structs

This commit is contained in:
Lennart
2025-01-05 17:06:15 +01:00
parent 2eb6b1934d
commit 9ea9beb143
17 changed files with 57 additions and 141 deletions

View File

@@ -78,10 +78,9 @@ pub async fn handle_calendar_multiget<C: CalendarStore + ?Sized>(
PropfindType::Propname => { PropfindType::Propname => {
vec!["propname".to_owned()] vec!["propname".to_owned()]
} }
PropfindType::Prop(PropElement { prop: prop_tags }) => prop_tags PropfindType::Prop(PropElement(prop_tags)) => {
.into_iter() prop_tags.into_iter().map(|propname| propname.0).collect()
.map(|propname| propname.name) }
.collect(),
}; };
let props: Vec<&str> = props.iter().map(String::as_str).collect(); let props: Vec<&str> = props.iter().map(String::as_str).collect();

View File

@@ -207,10 +207,9 @@ pub async fn handle_calendar_query<C: CalendarStore + ?Sized>(
PropfindType::Propname => { PropfindType::Propname => {
vec!["propname".to_owned()] vec!["propname".to_owned()]
} }
PropfindType::Prop(PropElement { prop: prop_tags }) => prop_tags PropfindType::Prop(PropElement(prop_tags)) => {
.into_iter() prop_tags.into_iter().map(|propname| propname.0).collect()
.map(|propname| propname.name) }
.collect(),
}; };
let props: Vec<&str> = props.iter().map(String::as_str).collect(); let props: Vec<&str> = props.iter().map(String::as_str).collect();

View File

@@ -73,10 +73,9 @@ pub async fn handle_sync_collection<C: CalendarStore + ?Sized>(
PropfindType::Propname => { PropfindType::Propname => {
vec!["propname".to_owned()] vec!["propname".to_owned()]
} }
PropfindType::Prop(PropElement { prop: prop_tags }) => prop_tags PropfindType::Prop(PropElement(prop_tags)) => {
.into_iter() prop_tags.into_iter().map(|propname| propname.0).collect()
.map(|propname| propname.name) }
.collect(),
}; };
let props: Vec<&str> = props.iter().map(String::as_str).collect(); let props: Vec<&str> = props.iter().map(String::as_str).collect();

View File

@@ -100,25 +100,13 @@ impl Resource for CalendarResource {
fn get_resourcetype(&self) -> Resourcetype { fn get_resourcetype(&self) -> Resourcetype {
if self.0.subscription_url.is_none() { if self.0.subscription_url.is_none() {
Resourcetype(&[ Resourcetype(&[
ResourcetypeInner { ResourcetypeInner(rustical_dav::namespace::NS_DAV, "collection"),
ns: rustical_dav::namespace::NS_DAV, ResourcetypeInner(rustical_dav::namespace::NS_CALDAV, "calendar"),
name: "collection",
},
ResourcetypeInner {
ns: rustical_dav::namespace::NS_CALDAV,
name: "calendar",
},
]) ])
} else { } else {
Resourcetype(&[ Resourcetype(&[
ResourcetypeInner { ResourcetypeInner(rustical_dav::namespace::NS_DAV, "collection"),
ns: rustical_dav::namespace::NS_DAV, ResourcetypeInner(rustical_dav::namespace::NS_CALENDARSERVER, "subscribed"),
name: "collection",
},
ResourcetypeInner {
ns: rustical_dav::namespace::NS_CALENDARSERVER,
name: "subscribed",
},
]) ])
} }
} }

View File

@@ -59,14 +59,8 @@ impl Resource for PrincipalResource {
fn get_resourcetype(&self) -> Resourcetype { fn get_resourcetype(&self) -> Resourcetype {
Resourcetype(&[ Resourcetype(&[
ResourcetypeInner { ResourcetypeInner(rustical_dav::namespace::NS_DAV, "collection"),
ns: rustical_dav::namespace::NS_DAV, ResourcetypeInner(rustical_dav::namespace::NS_DAV, "principal"),
name: "collection",
},
ResourcetypeInner {
ns: rustical_dav::namespace::NS_DAV,
name: "principal",
},
]) ])
} }

View File

@@ -84,10 +84,9 @@ pub async fn handle_addressbook_multiget<AS: AddressbookStore + ?Sized>(
PropfindType::Propname => { PropfindType::Propname => {
vec!["propname".to_owned()] vec!["propname".to_owned()]
} }
PropfindType::Prop(PropElement { prop: prop_tags }) => prop_tags PropfindType::Prop(PropElement(prop_tags)) => {
.into_iter() prop_tags.into_iter().map(|propname| propname.0).collect()
.map(|propname| propname.name) }
.collect(),
}; };
let props: Vec<&str> = props.iter().map(String::as_str).collect(); let props: Vec<&str> = props.iter().map(String::as_str).collect();

View File

@@ -70,10 +70,9 @@ pub async fn handle_sync_collection<AS: AddressbookStore + ?Sized>(
PropfindType::Propname => { PropfindType::Propname => {
vec!["propname".to_owned()] vec!["propname".to_owned()]
} }
PropfindType::Prop(PropElement { prop: prop_tags }) => prop_tags PropfindType::Prop(PropElement(prop_tags)) => {
.into_iter() prop_tags.into_iter().map(|propname| propname.0).collect()
.map(|propname| propname.name) }
.collect(),
}; };
let props: Vec<&str> = props.iter().map(String::as_str).collect(); let props: Vec<&str> = props.iter().map(String::as_str).collect();

View File

@@ -72,14 +72,8 @@ impl Resource for AddressbookResource {
fn get_resourcetype(&self) -> Resourcetype { fn get_resourcetype(&self) -> Resourcetype {
Resourcetype(&[ Resourcetype(&[
ResourcetypeInner { ResourcetypeInner(rustical_dav::namespace::NS_DAV, "collection"),
ns: rustical_dav::namespace::NS_DAV, ResourcetypeInner(rustical_dav::namespace::NS_CARDDAV, "addressbook"),
name: "collection",
},
ResourcetypeInner {
ns: rustical_dav::namespace::NS_CARDDAV,
name: "addressbook",
},
]) ])
} }

View File

@@ -60,14 +60,8 @@ impl Resource for PrincipalResource {
fn get_resourcetype(&self) -> Resourcetype { fn get_resourcetype(&self) -> Resourcetype {
Resourcetype(&[ Resourcetype(&[
ResourcetypeInner { ResourcetypeInner(rustical_dav::namespace::NS_DAV, "collection"),
ns: rustical_dav::namespace::NS_DAV, ResourcetypeInner(rustical_dav::namespace::NS_DAV, "principal"),
name: "collection",
},
ResourcetypeInner {
ns: rustical_dav::namespace::NS_DAV,
name: "principal",
},
]) ])
} }

View File

@@ -13,7 +13,7 @@ use actix_web::web::Data;
use actix_web::web::Path; use actix_web::web::Path;
use actix_web::HttpRequest; use actix_web::HttpRequest;
use rustical_store::auth::User; use rustical_store::auth::User;
use rustical_xml::de::XmlDocument; use rustical_xml::XmlDocument;
use tracing::instrument; use tracing::instrument;
use tracing_actix_web::RootSpan; use tracing_actix_web::RootSpan;
@@ -53,9 +53,9 @@ pub(crate) async fn route_propfind<R: ResourceService>(
let props = match &propfind.prop { let props = match &propfind.prop {
PropfindType::Allprop => vec!["allprop"], PropfindType::Allprop => vec!["allprop"],
PropfindType::Propname => vec!["propname"], PropfindType::Propname => vec!["propname"],
PropfindType::Prop(PropElement { prop: prop_tags }) => prop_tags PropfindType::Prop(PropElement(prop_tags)) => prop_tags
.iter() .iter()
.map(|propname| propname.name.as_str()) .map(|propname| propname.0.as_str())
.collect(), .collect(),
}; };

View File

@@ -26,10 +26,9 @@ enum SetPropertyPropWrapper<T: XmlDeserialize> {
// We are <prop> // We are <prop>
#[derive(XmlDeserialize, Clone, Debug)] #[derive(XmlDeserialize, Clone, Debug)]
struct SetPropertyPropWrapperWrapper<T: XmlDeserialize> { struct SetPropertyPropWrapperWrapper<T: XmlDeserialize>(
#[xml(ty = "untagged")] #[xml(ty = "untagged")] SetPropertyPropWrapper<T>,
property: SetPropertyPropWrapper<T>, );
}
// We are <set> // We are <set>
#[derive(XmlDeserialize, Clone, Debug)] #[derive(XmlDeserialize, Clone, Debug)]
@@ -39,16 +38,10 @@ struct SetPropertyElement<T: XmlDeserialize> {
} }
#[derive(XmlDeserialize, Clone, Debug)] #[derive(XmlDeserialize, Clone, Debug)]
struct TagName { struct TagName(#[xml(ty = "tag_name")] String);
#[xml(ty = "tag_name")]
name: String,
}
#[derive(XmlDeserialize, Clone, Debug)] #[derive(XmlDeserialize, Clone, Debug)]
struct PropertyElement { struct PropertyElement(#[xml(ty = "untagged")] TagName);
#[xml(ty = "untagged")]
property: TagName,
}
#[derive(XmlDeserialize, Clone, Debug)] #[derive(XmlDeserialize, Clone, Debug)]
struct RemovePropertyElement { struct RemovePropertyElement {
@@ -67,10 +60,7 @@ enum Operation<T: XmlDeserialize> {
#[derive(XmlDeserialize, XmlRootTag, Clone, Debug)] #[derive(XmlDeserialize, XmlRootTag, Clone, Debug)]
#[xml(root = b"propertyupdate")] #[xml(root = b"propertyupdate")]
#[xml(ns = "crate::namespace::NS_DAV")] #[xml(ns = "crate::namespace::NS_DAV")]
struct PropertyupdateElement<T: XmlDeserialize> { struct PropertyupdateElement<T: XmlDeserialize>(#[xml(ty = "untagged", flatten)] Vec<Operation<T>>);
#[xml(ty = "untagged", flatten)]
operations: Vec<Operation<T>>,
}
#[instrument(parent = root_span.id(), skip(path, req, root_span, resource_service))] #[instrument(parent = root_span.id(), skip(path, req, root_span, resource_service))]
pub(crate) async fn route_proppatch<R: ResourceService>( pub(crate) async fn route_proppatch<R: ResourceService>(
@@ -84,9 +74,9 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
let href = req.path().to_owned(); let href = req.path().to_owned();
// Extract operations // Extract operations
let PropertyupdateElement::<SetPropertyPropWrapperWrapper<<R::Resource as Resource>::Prop>> { let PropertyupdateElement::<SetPropertyPropWrapperWrapper<<R::Resource as Resource>::Prop>>(
operations, operations,
} = XmlDocument::parse_str(&body).map_err(Error::XmlDeserializationError)?; ) = XmlDocument::parse_str(&body).map_err(Error::XmlDeserializationError)?;
let mut resource = resource_service.get_resource(&path).await?; let mut resource = resource_service.get_resource(&path).await?;
let privileges = resource.get_user_privileges(&user)?; let privileges = resource.get_user_privileges(&user)?;
@@ -100,8 +90,10 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
for operation in operations.into_iter() { for operation in operations.into_iter() {
match operation { match operation {
Operation::Set(SetPropertyElement { prop }) => { Operation::Set(SetPropertyElement {
match prop.property { prop: SetPropertyPropWrapperWrapper(property),
}) => {
match property {
SetPropertyPropWrapper::Valid(prop) => { SetPropertyPropWrapper::Valid(prop) => {
let propname: <R::Resource as Resource>::PropName = prop.clone().into(); let propname: <R::Resource as Resource>::PropName = prop.clone().into();
let propname: &str = propname.into(); let propname: &str = propname.into();
@@ -125,7 +117,7 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
} }
} }
Operation::Remove(remove_el) => { Operation::Remove(remove_el) => {
let propname = remove_el.prop.property.name; let propname = remove_el.prop.0 .0;
match <<R::Resource as Resource>::PropName as FromStr>::from_str(&propname) { match <<R::Resource as Resource>::PropName as FromStr>::from_str(&propname) {
Ok(prop) => match resource.remove_prop(&prop) { Ok(prop) => match resource.remove_prop(&prop) {
Ok(()) => props_ok.push(propname), Ok(()) => props_ok.push(propname),

View File

@@ -207,9 +207,7 @@ pub trait Resource: Clone + 'static {
let mut propstats = vec![PropstatWrapper::Normal(PropstatElement { let mut propstats = vec![PropstatWrapper::Normal(PropstatElement {
status: StatusCode::OK, status: StatusCode::OK,
prop: PropTagWrapper { prop: PropTagWrapper(prop_responses),
prop: prop_responses,
},
})]; })];
if !invalid_props.is_empty() { if !invalid_props.is_empty() {
propstats.push(PropstatWrapper::TagList(PropstatElement { propstats.push(PropstatWrapper::TagList(PropstatElement {

View File

@@ -39,10 +39,7 @@ impl<PR: Resource> Resource for RootResource<PR> {
type PrincipalResource = PR; type PrincipalResource = PR;
fn get_resourcetype(&self) -> Resourcetype { fn get_resourcetype(&self) -> Resourcetype {
Resourcetype(&[ResourcetypeInner { Resourcetype(&[ResourcetypeInner(crate::namespace::NS_DAV, "collection")])
ns: crate::namespace::NS_DAV,
name: "collection",
}])
} }
fn get_prop( fn get_prop(

View File

@@ -9,13 +9,8 @@ use actix_web::{
use quick_xml::name::Namespace; use quick_xml::name::Namespace;
use rustical_xml::{XmlRootTag, XmlSerialize, XmlSerializeRoot}; use rustical_xml::{XmlRootTag, XmlSerialize, XmlSerializeRoot};
// Intermediate struct because of a serde limitation, see following article:
// https://stackoverflow.com/questions/78444158/unsupportedcannot-serialize-enum-newtype-variant-exampledata
#[derive(XmlSerialize)] #[derive(XmlSerialize)]
pub struct PropTagWrapper<T: XmlSerialize> { pub struct PropTagWrapper<T: XmlSerialize>(#[xml(flatten, ty = "untagged")] pub Vec<T>);
#[xml(flatten, ty = "untagged")]
pub prop: Vec<T>,
}
// RFC 2518 // RFC 2518
// <!ELEMENT propstat (prop, status, responsedescription?) > // <!ELEMENT propstat (prop, status, responsedescription?) >

View File

@@ -4,21 +4,15 @@ use rustical_xml::XmlRootTag;
#[derive(Debug, Clone, XmlDeserialize, XmlRootTag, PartialEq)] #[derive(Debug, Clone, XmlDeserialize, XmlRootTag, PartialEq)]
#[xml(root = b"propfind", ns = "crate::namespace::NS_DAV")] #[xml(root = b"propfind", ns = "crate::namespace::NS_DAV")]
pub struct PropfindElement { pub struct PropfindElement {
#[xml(ty = "untagged")] #[xml(ns = "crate::namespace::NS_DAV")]
pub prop: PropfindType, pub prop: PropfindType,
} }
#[derive(Debug, Clone, XmlDeserialize, PartialEq)] #[derive(Debug, Clone, XmlDeserialize, PartialEq)]
pub struct PropElement { pub struct PropElement(#[xml(ty = "untagged", flatten)] pub Vec<Propname>);
#[xml(ty = "untagged", flatten)]
pub prop: Vec<Propname>,
}
#[derive(Debug, Clone, XmlDeserialize, PartialEq)] #[derive(Debug, Clone, XmlDeserialize, PartialEq)]
pub struct Propname { pub struct Propname(#[xml(ty = "tag_name")] pub String);
#[xml(ty = "tag_name")]
pub name: String,
}
#[derive(Debug, Clone, XmlDeserialize, PartialEq)] #[derive(Debug, Clone, XmlDeserialize, PartialEq)]
pub enum PropfindType { pub enum PropfindType {

View File

@@ -4,12 +4,10 @@ use rustical_xml::XmlSerialize;
pub struct Resourcetype(#[xml(flatten, ty = "untagged")] pub &'static [ResourcetypeInner]); pub struct Resourcetype(#[xml(flatten, ty = "untagged")] pub &'static [ResourcetypeInner]);
#[derive(Debug, Clone, PartialEq, XmlSerialize)] #[derive(Debug, Clone, PartialEq, XmlSerialize)]
pub struct ResourcetypeInner { pub struct ResourcetypeInner(
#[xml(ty = "namespace")] #[xml(ty = "namespace")] pub quick_xml::name::Namespace<'static>,
pub ns: quick_xml::name::Namespace<'static>, #[xml(ty = "tag_name")] pub &'static str,
#[xml(ty = "tag_name")] );
pub name: &'static str,
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@@ -29,14 +27,8 @@ mod tests {
let mut writer = quick_xml::Writer::new(&mut buf); let mut writer = quick_xml::Writer::new(&mut buf);
Document { Document {
resourcetype: Resourcetype(&[ resourcetype: Resourcetype(&[
ResourcetypeInner { ResourcetypeInner(crate::namespace::NS_DAV, "displayname"),
ns: crate::namespace::NS_DAV, ResourcetypeInner(crate::namespace::NS_CALENDARSERVER, "calendar-color"),
name: "displayname",
},
ResourcetypeInner {
ns: crate::namespace::NS_CALENDARSERVER,
name: "calendar-color",
},
]), ]),
} }
.serialize_root(&mut writer) .serialize_root(&mut writer)

View File

@@ -15,20 +15,12 @@ impl XmlSerialize for TagList {
writer: &mut quick_xml::Writer<W>, writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
#[derive(Debug, XmlSerialize, PartialEq)] #[derive(Debug, XmlSerialize, PartialEq)]
struct Inner { struct Inner(#[xml(ty = "untagged", flatten)] Vec<Tag>);
#[xml(ty = "untagged", flatten)]
tags: Vec<Tag>,
}
#[derive(Debug, XmlSerialize, PartialEq)] #[derive(Debug, XmlSerialize, PartialEq)]
struct Tag { struct Tag(#[xml(ty = "tag_name")] String);
#[xml(ty = "tag_name")] Inner(self.0.iter().map(|t| Tag(t.to_owned())).collect())
name: String, .serialize(ns, tag, namespaces, writer)
}
Inner {
tags: self.0.iter().map(|t| Tag { name: t.to_owned() }).collect(),
}
.serialize(ns, tag, namespaces, writer)
} }
#[allow(refining_impl_trait)] #[allow(refining_impl_trait)]
@@ -36,12 +28,3 @@ impl XmlSerialize for TagList {
None None
} }
} }
impl TagList {
pub fn inner(&self) -> &Vec<String> {
&self.0
}
pub fn into_inner(self) -> Vec<String> {
self.0
}
}