Refactoring that will hopefully make life easier

This commit is contained in:
Lennart
2024-07-28 20:54:31 +02:00
parent 33539e8c7a
commit 99ac654448
8 changed files with 338 additions and 139 deletions

View File

@@ -4,13 +4,12 @@ use crate::namespace::Namespace;
use crate::resource::InvalidProperty;
use crate::resource::Resource;
use crate::resource::ResourceService;
use crate::resource::{PropstatElement, PropstatResponseElement, PropstatType};
use crate::xml::multistatus::{PropstatElement, PropstatWrapper, ResponseElement};
use crate::xml::MultistatusElement;
use crate::xml::TagList;
use crate::xml::TagName;
use crate::Error;
use actix_web::http::StatusCode;
use actix_web::Responder;
use actix_web::{web::Path, HttpRequest};
use log::debug;
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
@@ -55,7 +54,7 @@ pub async fn route_proppatch<A: CheckAuthentication, R: ResourceService + ?Sized
body: String,
req: HttpRequest,
auth: AuthInfoExtractor<A>,
) -> Result<impl Responder, R::Error> {
) -> Result<MultistatusElement<PropstatWrapper<String>, PropstatWrapper<String>>, R::Error> {
let auth_info = auth.inner;
let path_components = path.into_inner();
let href = req.path().to_owned();
@@ -138,25 +137,25 @@ pub async fn route_proppatch<A: CheckAuthentication, R: ResourceService + ?Sized
}
Ok(MultistatusElement {
responses: vec![PropstatResponseElement {
responses: vec![ResponseElement {
href,
propstat: vec![
PropstatType::Normal(PropstatElement {
PropstatWrapper::TagList(PropstatElement {
prop: TagList::from(props_ok),
status: format!("HTTP/1.1 {}", StatusCode::OK),
}),
PropstatType::NotFound(PropstatElement {
PropstatWrapper::TagList(PropstatElement {
prop: TagList::from(props_not_found),
status: format!("HTTP/1.1 {}", StatusCode::NOT_FOUND),
}),
PropstatType::Conflict(PropstatElement {
PropstatWrapper::TagList(PropstatElement {
prop: TagList::from(props_conflict),
status: format!("HTTP/1.1 {}", StatusCode::CONFLICT),
}),
],
}],
// Dummy just for typing
member_responses: Vec::<String>::new(),
member_responses: vec![],
ns_dav: Namespace::Dav.as_str(),
ns_caldav: Namespace::CalDAV.as_str(),
ns_ical: Namespace::ICal.as_str(),

View File

@@ -1,4 +1,5 @@
use crate::xml::TagList;
use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper};
use crate::xml::{multistatus::ResponseElement, TagList};
use crate::Error;
use actix_web::{http::StatusCode, HttpRequest, ResponseError};
use async_trait::async_trait;
@@ -65,41 +66,6 @@ pub trait ResourceService: Sized {
}
}
#[derive(Serialize)]
pub struct PropTagWrapper<T: Serialize> {
#[serde(rename = "$value")]
prop: T,
}
#[derive(Serialize)]
#[serde(untagged)]
pub enum PropWrapper<T: Serialize> {
Prop(PropTagWrapper<T>),
TagList(TagList),
}
#[derive(Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct PropstatElement<T: Serialize> {
pub prop: T,
pub status: String,
}
#[derive(Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct PropstatResponseElement<T1: Serialize, T2: Serialize> {
pub href: String,
pub propstat: Vec<PropstatType<T1, T2>>,
}
#[derive(Serialize)]
#[serde(untagged)]
pub enum PropstatType<T1: Serialize, T2: Serialize> {
Normal(PropstatElement<T1>),
NotFound(PropstatElement<T2>),
Conflict(PropstatElement<T2>),
}
#[async_trait(?Send)]
pub trait HandlePropfind {
type Error: ResponseError + From<crate::Error> + From<anyhow::Error>;
@@ -121,7 +87,7 @@ impl<R: Resource> HandlePropfind for R {
prefix: &str,
path: String,
props: Vec<&str>,
) -> Result<PropstatResponseElement<PropWrapper<Vec<R::Prop>>, TagList>, R::Error> {
) -> Result<ResponseElement<PropstatWrapper<R::Prop>>, R::Error> {
let mut props = props;
if props.contains(&"propname") {
if props.len() != 1 {
@@ -134,10 +100,10 @@ impl<R: Resource> HandlePropfind for R {
.iter()
.map(|&prop| prop.to_string())
.collect();
return Ok(PropstatResponseElement {
return Ok(ResponseElement {
href: path,
propstat: vec![PropstatType::Normal(PropstatElement {
prop: PropWrapper::TagList(TagList::from(props)),
propstat: vec![PropstatWrapper::TagList(PropstatElement {
prop: TagList::from(props),
status: format!("HTTP/1.1 {}", StatusCode::OK),
})],
});
@@ -170,14 +136,14 @@ impl<R: Resource> HandlePropfind for R {
.map(|prop| self.get_prop(prefix, prop))
.collect::<Result<Vec<R::Prop>, R::Error>>()?;
let mut propstats = vec![PropstatType::Normal(PropstatElement {
let mut propstats = vec![PropstatWrapper::Normal(PropstatElement {
status: format!("HTTP/1.1 {}", StatusCode::OK),
prop: PropWrapper::Prop(PropTagWrapper {
prop: PropTagWrapper {
prop: prop_responses,
}),
},
})];
if !invalid_props.is_empty() {
propstats.push(PropstatType::NotFound(PropstatElement {
propstats.push(PropstatWrapper::TagList(PropstatElement {
status: format!("HTTP/1.1 {}", StatusCode::NOT_FOUND),
prop: invalid_props
.into_iter()
@@ -186,7 +152,7 @@ impl<R: Resource> HandlePropfind for R {
.into(),
}));
}
Ok(PropstatResponseElement {
Ok(ResponseElement {
href: path,
propstat: propstats,
})

View File

@@ -1,16 +1,60 @@
use crate::xml::TagList;
use actix_web::{
body::BoxBody, http::header::ContentType, HttpRequest, HttpResponse, Responder, ResponseError,
};
use log::debug;
use serde::Serialize;
// Intermediate struct because of a serde limitation, see following article:
// https://stackoverflow.com/questions/78444158/unsupportedcannot-serialize-enum-newtype-variant-exampledata
#[derive(Serialize)]
#[serde(rename = "multistatus")]
pub struct PropTagWrapper<T: Serialize> {
#[serde(rename = "$value")]
pub prop: Vec<T>,
}
// #[derive(Serialize)]
// #[serde(untagged)]
// pub enum PropWrapper<T: Serialize> {
// Prop(Vec<T>),
// TagList(TagList),
// }
// RFC 2518
// <!ELEMENT propstat (prop, status, responsedescription?) >
#[derive(Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct PropstatElement<PropType: Serialize> {
pub prop: PropType,
pub status: String,
}
#[derive(Serialize)]
#[serde(untagged)]
pub enum PropstatWrapper<T: Serialize> {
Normal(PropstatElement<PropTagWrapper<T>>),
TagList(PropstatElement<TagList>),
}
// RFC 2518
// <!ELEMENT response (href, ((href*, status)|(propstat+)),
// responsedescription?) >
#[derive(Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct ResponseElement<PropstatType: Serialize> {
pub href: String,
pub propstat: Vec<PropstatType>,
}
// RFC 2518
// <!ELEMENT multistatus (response+, responsedescription?) >
#[derive(Serialize)]
#[serde(rename = "multistatus", rename_all = "kebab-case")]
pub struct MultistatusElement<T1: Serialize, T2: Serialize> {
#[serde(rename = "response")]
pub responses: Vec<T1>,
pub responses: Vec<ResponseElement<T1>>,
#[serde(rename = "response")]
pub member_responses: Vec<T2>,
pub member_responses: Vec<ResponseElement<T2>>,
#[serde(rename = "@xmlns")]
pub ns_dav: &'static str,
#[serde(rename = "@xmlns:C")]