Migrate all deserialization business to rustical_xml

This commit is contained in:
Lennart
2024-12-25 10:21:36 +01:00
parent 5e2717e130
commit 80472289dc
15 changed files with 99 additions and 121 deletions

View File

@@ -20,10 +20,7 @@ pub enum Error {
PropReadOnly,
#[error(transparent)]
NewXmlDeserializationError(#[from] rustical_xml::XmlDeError),
#[error(transparent)]
XmlDeserializationError(#[from] quick_xml::DeError),
XmlDeserializationError(#[from] rustical_xml::XmlDeError),
#[error(transparent)]
XmlSerializationError(#[from] quick_xml::SeError),
@@ -36,7 +33,6 @@ 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

@@ -1,8 +1,8 @@
use serde::{Deserialize, Serialize};
use rustical_xml::XmlDeserialize;
use serde::Serialize;
use std::collections::HashSet;
#[derive(Debug, Clone, Serialize, Deserialize, Eq, Hash, PartialEq)]
#[serde(rename_all = "kebab-case")]
#[derive(Debug, Clone, Serialize, XmlDeserialize, Eq, Hash, PartialEq)]
pub enum UserPrivilege {
Read,
Write,
@@ -42,10 +42,9 @@ impl Serialize for UserPrivilegeSet {
}
}
// TODO: implement Deserialize once we need it
#[derive(Debug, Clone, Deserialize, Default, PartialEq)]
#[serde(rename_all = "kebab-case")]
#[derive(Debug, Clone, XmlDeserialize, Default, PartialEq)]
pub struct UserPrivilegeSet {
#[xml(flatten)]
privileges: HashSet<UserPrivilege>,
}

View File

@@ -42,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() {
PropfindElement::parse_str(&body).map_err(Error::NewXmlDeserializationError)?
PropfindElement::parse_str(&body).map_err(Error::XmlDeserializationError)?
} else {
PropfindElement {
prop: PropfindType::Allprop,

View File

@@ -5,47 +5,53 @@ use crate::resource::ResourceService;
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::{web::Path, HttpRequest};
use rustical_store::auth::User;
use serde::{Deserialize, Serialize};
use rustical_xml::XmlDeserialize;
use rustical_xml::XmlDocument;
use rustical_xml::XmlRootTag;
use std::str::FromStr;
use tracing::instrument;
use tracing_actix_web::RootSpan;
// https://docs.rs/quick-xml/latest/quick_xml/de/index.html#normal-enum-variant
#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
struct PropertyElement<T> {
#[serde(rename = "$value")]
#[derive(XmlDeserialize, Clone, Debug)]
struct SetPropertyElement<T: XmlDeserialize> {
prop: T,
}
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
struct SetPropertyElement<T> {
prop: PropertyElement<T>,
#[derive(XmlDeserialize, Clone, Debug)]
struct TagName {
#[xml(ty = "tag_name")]
name: String,
}
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
#[derive(XmlDeserialize, Clone, Debug)]
struct PropertyElement {
#[xml(ty = "untagged")]
property: TagName,
}
#[derive(XmlDeserialize, Clone, Debug)]
struct RemovePropertyElement {
prop: PropertyElement<TagName>,
prop: PropertyElement,
}
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
enum Operation<PropType> {
Set(SetPropertyElement<PropType>),
#[derive(XmlDeserialize, Clone, Debug)]
enum Operation<T: XmlDeserialize> {
Set(SetPropertyElement<T>),
Remove(RemovePropertyElement),
}
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
struct PropertyupdateElement<T> {
#[serde(rename = "$value", default = "Vec::new")]
#[derive(XmlDeserialize, XmlRootTag, Clone, Debug)]
#[xml(root = b"propertyupdate")]
struct PropertyupdateElement<T: XmlDeserialize> {
// #[xml(flatten)]
// set: Vec<T>,
// #[xml(flatten)]
// remove: Vec<TagName>,
#[xml(ty = "untagged", flatten)]
operations: Vec<Operation<T>>,
}
@@ -63,18 +69,18 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
// Extract operations
let PropertyupdateElement::<<R::Resource as Resource>::Prop> { operations } =
quick_xml::de::from_str(&body).map_err(Error::XmlDeserializationError)?;
XmlDocument::parse_str(&body).map_err(Error::XmlDeserializationError)?;
// Extract all set property names without verification
// Weird workaround because quick_xml doesn't allow untagged enums
let propnames: Vec<String> = quick_xml::de::from_str::<PropertyupdateElement<TagName>>(&body)
let propnames: Vec<String> = PropertyupdateElement::<TagName>::parse_str(&body)
.map_err(Error::XmlDeserializationError)?
.operations
.into_iter()
.map(|op_el| match op_el {
Operation::Set(set_el) => set_el.prop.prop.into(),
Operation::Set(set_el) => set_el.prop.name,
// If we can't remove a nonexisting property then that's no big deal
Operation::Remove(remove_el) => remove_el.prop.prop.into(),
Operation::Remove(remove_el) => remove_el.prop.property.name,
})
.collect();
@@ -90,9 +96,7 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
for (operation, propname) in operations.into_iter().zip(propnames) {
match operation {
Operation::Set(SetPropertyElement {
prop: PropertyElement { prop },
}) => {
Operation::Set(SetPropertyElement { prop }) => {
if prop.invalid_property() {
if <R::Resource as Resource>::list_props().contains(&propname.as_str()) {
// This happens in following cases:

View File

@@ -11,7 +11,8 @@ pub use invalid_property::InvalidProperty;
use itertools::Itertools;
pub use resource_service::ResourceService;
use rustical_store::auth::User;
use serde::{Deserialize, Serialize};
use rustical_xml::XmlDeserialize;
use serde::Serialize;
use std::str::FromStr;
use strum::{EnumString, VariantNames};
@@ -19,20 +20,21 @@ mod invalid_property;
mod methods;
mod resource_service;
pub trait ResourceProp: InvalidProperty + Serialize + for<'de> Deserialize<'de> {}
impl<T: InvalidProperty + Serialize + for<'de> Deserialize<'de>> ResourceProp for T {}
pub trait ResourceProp: InvalidProperty + Serialize + XmlDeserialize {}
impl<T: InvalidProperty + Serialize + XmlDeserialize> ResourceProp for T {}
pub trait ResourcePropName: FromStr + VariantNames {}
impl<T: FromStr + VariantNames> ResourcePropName for T {}
pub trait ResourceType: Serialize + for<'de> Deserialize<'de> {}
impl<T: Serialize + for<'de> Deserialize<'de>> ResourceType for T {}
pub trait ResourceType: Serialize + XmlDeserialize {}
impl<T: Serialize + XmlDeserialize> ResourceType for T {}
#[derive(Deserialize, Serialize, PartialEq, Default)]
#[derive(XmlDeserialize, Serialize, PartialEq, Default)]
#[serde(rename_all = "kebab-case")]
pub enum CommonPropertiesProp {
// WebDAV (RFC 2518)
#[serde(skip_deserializing)]
#[xml(skip_deserializing)]
Resourcetype(Resourcetype),
// WebDAV Current Principal Extension (RFC 5397)
@@ -40,7 +42,7 @@ pub enum CommonPropertiesProp {
// WebDAV Access Control Protocol (RFC 3477)
CurrentUserPrivilegeSet(UserPrivilegeSet),
Owner(Option<HrefElement>),
Owner(HrefElement),
#[serde(other)]
#[default]
@@ -97,11 +99,18 @@ pub trait Resource: Clone + 'static {
CommonPropertiesProp::CurrentUserPrivilegeSet(self.get_user_privileges(user)?)
}
CommonPropertiesPropName::Owner => {
CommonPropertiesProp::Owner(self.get_owner().map(|owner| {
// TODO: Reintroduce optional owner field
let owner = self.get_owner().unwrap_or(&user.id);
CommonPropertiesProp::Owner(
Self::PrincipalResource::get_url(rmap, [owner])
.unwrap()
.into()
}))
.into(),
)
// CommonPropertiesProp::Owner(self.get_owner().map(|owner| {
// Self::PrincipalResource::get_url(rmap, [owner])
// .unwrap()
// .into()
// }))
}
})
}

View File

@@ -4,7 +4,8 @@ use actix_web::dev::ResourceMap;
use actix_web::HttpRequest;
use async_trait::async_trait;
use rustical_store::auth::User;
use serde::{Deserialize, Serialize};
use rustical_xml::XmlDeserialize;
use serde::Serialize;
use std::any::type_name;
use std::marker::PhantomData;
use strum::{EnumString, VariantNames};
@@ -22,7 +23,7 @@ impl<PR: Resource> Default for RootResource<PR> {
#[strum(serialize_all = "kebab-case")]
pub enum RootResourcePropName {}
#[derive(Deserialize, Serialize, Default, Clone, PartialEq)]
#[derive(XmlDeserialize, Serialize, Default, Clone, PartialEq)]
pub enum RootResourceProp {
#[serde(other)]
#[default]

View File

@@ -2,20 +2,15 @@ pub mod multistatus;
mod propfind;
mod resourcetype;
pub mod tag_list;
pub mod tag_name;
pub use propfind::{PropElement, PropfindElement, PropfindType, Propname};
use derive_more::derive::From;
pub use multistatus::MultistatusElement;
pub use tag_list::TagList;
pub use tag_name::TagName;
pub use propfind::{PropElement, PropfindElement, PropfindType, Propname};
pub use resourcetype::Resourcetype;
use rustical_xml::XmlDeserialize;
use serde::Serialize;
pub use tag_list::TagList;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize, From, PartialEq)]
#[derive(XmlDeserialize, Debug, Clone, Serialize, From, PartialEq)]
pub struct HrefElement {
pub href: String,
}

View File

@@ -1,44 +0,0 @@
use serde::{
de::{VariantAccess, Visitor},
Deserialize,
};
#[derive(Debug, Clone, PartialEq)]
pub struct TagName(pub String);
impl From<TagName> for String {
fn from(value: TagName) -> Self {
value.0
}
}
impl From<String> for TagName {
fn from(value: String) -> Self {
Self(value)
}
}
impl<'de> Deserialize<'de> for TagName {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct __Visitor;
impl<'de> Visitor<'de> for __Visitor {
type Value = TagName;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("tagname")
}
fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
where
A: serde::de::EnumAccess<'de>,
{
let (name, variant): (String, _) = data.variant()?;
VariantAccess::unit_variant(variant)?;
Ok(TagName(name))
}
}
deserializer.deserialize_enum("doesn't matter", &[], __Visitor)
}
}