mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 07:02:24 +00:00
Migrate propfind and report to rustical_xml
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user