Migrate propfind and report to rustical_xml

This commit is contained in:
Lennart
2024-12-23 16:44:26 +01:00
parent 8e0a25b223
commit 72844aa94e
28 changed files with 528 additions and 335 deletions

View File

@@ -19,6 +19,9 @@ pub enum Error {
#[error("prop is read-only")]
PropReadOnly,
#[error(transparent)]
NewXmlDeserializationError(#[from] rustical_xml::XmlDeError),
#[error(transparent)]
XmlDeserializationError(#[from] quick_xml::DeError),
@@ -33,6 +36,7 @@ impl actix_web::error::ResponseError for Error {
Self::NotFound => StatusCode::NOT_FOUND,
Self::BadRequest(_) => StatusCode::BAD_REQUEST,
Self::Unauthorized => StatusCode::UNAUTHORIZED,
Self::NewXmlDeserializationError(_) => StatusCode::BAD_REQUEST,
Self::XmlDeserializationError(_) => StatusCode::BAD_REQUEST,
Self::XmlSerializationError(_) => StatusCode::BAD_REQUEST,
Error::PropReadOnly => StatusCode::CONFLICT,

View File

@@ -6,22 +6,16 @@ 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 crate::Error;
use actix_web::web::Path;
use actix_web::HttpRequest;
use rustical_store::auth::User;
use serde::Deserialize;
use rustical_xml::de::XmlDocument;
use tracing::instrument;
use tracing_actix_web::RootSpan;
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
struct PropfindElement {
#[serde(rename = "$value")]
prop: PropfindType,
}
#[instrument(parent = root_span.id(), skip(path_components, req, root_span))]
#[allow(clippy::type_complexity)]
pub(crate) async fn route_propfind<R: ResourceService>(
@@ -48,7 +42,7 @@ 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() {
quick_xml::de::from_str(&body).map_err(Error::XmlDeserializationError)?
PropfindElement::parse_str(&body).map_err(Error::NewXmlDeserializationError)?
} else {
PropfindElement {
prop: PropfindType::Allprop,
@@ -58,7 +52,10 @@ pub(crate) async fn route_propfind<R: ResourceService>(
let props = match propfind.prop {
PropfindType::Allprop => vec!["allprop".to_owned()],
PropfindType::Propname => vec!["propname".to_owned()],
PropfindType::Prop(PropElement { prop: prop_tags }) => prop_tags.into_inner(),
PropfindType::Prop(PropElement { prop: prop_tags }) => prop_tags
.into_iter()
.map(|propname| propname.name)
.collect(),
};
let props: Vec<&str> = props.iter().map(String::as_str).collect();

View File

@@ -4,7 +4,7 @@ mod resourcetype;
pub mod tag_list;
pub mod tag_name;
pub use propfind::{PropElement, PropfindType};
pub use propfind::{PropElement, PropfindElement, PropfindType, Propname};
use derive_more::derive::From;
pub use multistatus::MultistatusElement;

View File

@@ -1,15 +1,26 @@
use super::TagList;
use serde::Deserialize;
use rustical_xml::XmlDeserialize;
use rustical_xml::XmlRootTag;
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
pub struct PropElement {
#[serde(flatten)]
pub prop: TagList,
#[derive(Debug, Clone, XmlDeserialize, XmlRootTag, PartialEq)]
#[xml(root = b"propfind", ns = b"DAV:")]
pub struct PropfindElement {
#[xml(ty = "untagged")]
pub prop: PropfindType,
}
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
#[derive(Debug, Clone, XmlDeserialize, PartialEq)]
pub struct PropElement {
#[xml(ty = "untagged", flatten)]
pub prop: Vec<Propname>,
}
#[derive(Debug, Clone, XmlDeserialize, PartialEq)]
pub struct Propname {
#[xml(ty = "tag_name")]
pub name: String,
}
#[derive(Debug, Clone, XmlDeserialize, PartialEq)]
pub enum PropfindType {
Propname,
Allprop,

View File

@@ -1,44 +1,10 @@
use derive_more::derive::From;
use serde::ser::SerializeMap;
use serde::{
de::{MapAccess, Visitor},
Deserialize, Serialize,
};
use serde::Serialize;
#[derive(Clone, Debug, PartialEq, From)]
pub struct TagList(Vec<String>);
struct TagListVisitor;
impl<'de> Visitor<'de> for TagListVisitor {
type Value = TagList;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("TagList")
}
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
where
A: MapAccess<'de>,
{
let mut tags = Vec::new();
while let Some(key) = map.next_key::<String>()? {
tags.push(key);
}
Ok(TagList(tags))
}
}
impl<'de> Deserialize<'de> for TagList {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
deserializer.deserialize_map(TagListVisitor)
}
}
impl Serialize for TagList {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where

View File

@@ -0,0 +1,87 @@
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 {
prop: vec![
Propname {
name: "displayname".to_owned()
},
Propname {
name: "color".to_owned()
},
]
})
}
);
}
/// 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,48 +0,0 @@
use rustical_dav::xml::TagList;
use serde::{Deserialize, Serialize};
const INPUT: &str = r#"<Document>
<prop>
<nicename/>
<anotherprop/>
</prop>
</Document>"#;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "kebab-case")]
struct PropElement {
#[serde(flatten)]
tags: TagList,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "kebab-case")]
struct Document {
prop: PropElement,
}
fn expected_output() -> Document {
Document {
prop: PropElement {
tags: vec!["nicename".to_owned(), "anotherprop".to_owned()].into(),
},
}
}
#[test]
fn test_tagname_deserialize() {
let result: Document = quick_xml::de::from_str(INPUT).unwrap();
assert_eq!(result, expected_output());
}
#[test]
fn test_tagname_serialize() {
let mut result = String::new();
let mut ser = quick_xml::se::Serializer::new(&mut result);
ser.indent(' ', 4);
let to_serialize = &expected_output();
to_serialize.serialize(ser).unwrap();
assert_eq!(result, INPUT);
}