mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
dav: Refactor proppatch, remove InvalidProperty trait
This commit is contained in:
@@ -24,7 +24,7 @@ use serde::Serialize;
|
|||||||
use sha2::{Digest, Sha256};
|
use sha2::{Digest, Sha256};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use strum::{EnumDiscriminants, EnumString, VariantNames};
|
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
|
||||||
|
|
||||||
pub struct CalendarResourceService<C: CalendarStore + ?Sized> {
|
pub struct CalendarResourceService<C: CalendarStore + ?Sized> {
|
||||||
cal_store: Arc<C>,
|
cal_store: Arc<C>,
|
||||||
@@ -32,10 +32,10 @@ pub struct CalendarResourceService<C: CalendarStore + ?Sized> {
|
|||||||
calendar_id: String,
|
calendar_id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, XmlDeserialize, Serialize, PartialEq, EnumDiscriminants)]
|
#[derive(XmlDeserialize, Serialize, PartialEq, EnumDiscriminants, Clone)]
|
||||||
#[strum_discriminants(
|
#[strum_discriminants(
|
||||||
name(CalendarPropName),
|
name(CalendarPropName),
|
||||||
derive(EnumString, VariantNames),
|
derive(EnumString, VariantNames, IntoStaticStr),
|
||||||
strum(serialize_all = "kebab-case")
|
strum(serialize_all = "kebab-case")
|
||||||
)]
|
)]
|
||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(rename_all = "kebab-case")]
|
||||||
@@ -90,11 +90,6 @@ pub enum CalendarProp {
|
|||||||
Getctag(String),
|
Getctag(String),
|
||||||
#[serde(rename = "CS:source", alias = "source")]
|
#[serde(rename = "CS:source", alias = "source")]
|
||||||
Source(Option<HrefElement>),
|
Source(Option<HrefElement>),
|
||||||
|
|
||||||
#[serde(other)]
|
|
||||||
#[strum_discriminants(strum(disabled))]
|
|
||||||
#[default]
|
|
||||||
Invalid,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, From, Into)]
|
#[derive(Clone, Debug, From, Into)]
|
||||||
@@ -168,9 +163,6 @@ impl Resource for CalendarResource {
|
|||||||
CalendarPropName::Source => {
|
CalendarPropName::Source => {
|
||||||
CalendarProp::Source(self.0.subscription_url.to_owned().map(HrefElement::from))
|
CalendarProp::Source(self.0.subscription_url.to_owned().map(HrefElement::from))
|
||||||
}
|
}
|
||||||
CalendarPropName::Invalid => {
|
|
||||||
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,7 +201,6 @@ impl Resource for CalendarResource {
|
|||||||
CalendarProp::Getctag(_) => Err(rustical_dav::Error::PropReadOnly),
|
CalendarProp::Getctag(_) => Err(rustical_dav::Error::PropReadOnly),
|
||||||
// Converting between a calendar subscription calendar and a normal one would be weird
|
// Converting between a calendar subscription calendar and a normal one would be weird
|
||||||
CalendarProp::Source(_) => Err(rustical_dav::Error::PropReadOnly),
|
CalendarProp::Source(_) => Err(rustical_dav::Error::PropReadOnly),
|
||||||
CalendarProp::Invalid => Err(rustical_dav::Error::PropReadOnly),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,7 +239,6 @@ impl Resource for CalendarResource {
|
|||||||
CalendarPropName::Getctag => Err(rustical_dav::Error::PropReadOnly),
|
CalendarPropName::Getctag => Err(rustical_dav::Error::PropReadOnly),
|
||||||
// Converting a calendar subscription calendar into a normal one would be weird
|
// Converting a calendar subscription calendar into a normal one would be weird
|
||||||
CalendarPropName::Source => Err(rustical_dav::Error::PropReadOnly),
|
CalendarPropName::Source => Err(rustical_dav::Error::PropReadOnly),
|
||||||
CalendarPropName::Invalid => Err(rustical_dav::Error::PropReadOnly),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ use rustical_store::{auth::User, CalendarObject, CalendarStore};
|
|||||||
use rustical_xml::XmlDeserialize;
|
use rustical_xml::XmlDeserialize;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use strum::{EnumDiscriminants, EnumString, VariantNames};
|
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
|
||||||
|
|
||||||
pub struct CalendarObjectResourceService<C: CalendarStore + ?Sized> {
|
pub struct CalendarObjectResourceService<C: CalendarStore + ?Sized> {
|
||||||
cal_store: Arc<C>,
|
cal_store: Arc<C>,
|
||||||
@@ -20,10 +20,10 @@ pub struct CalendarObjectResourceService<C: CalendarStore + ?Sized> {
|
|||||||
object_id: String,
|
object_id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, XmlDeserialize, Serialize, PartialEq, EnumDiscriminants)]
|
#[derive(XmlDeserialize, Serialize, PartialEq, EnumDiscriminants, Clone)]
|
||||||
#[strum_discriminants(
|
#[strum_discriminants(
|
||||||
name(CalendarObjectPropName),
|
name(CalendarObjectPropName),
|
||||||
derive(EnumString, VariantNames),
|
derive(EnumString, VariantNames, IntoStaticStr),
|
||||||
strum(serialize_all = "kebab-case")
|
strum(serialize_all = "kebab-case")
|
||||||
)]
|
)]
|
||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(rename_all = "kebab-case")]
|
||||||
@@ -35,12 +35,6 @@ pub enum CalendarObjectProp {
|
|||||||
// CalDAV (RFC 4791)
|
// CalDAV (RFC 4791)
|
||||||
#[serde(rename = "C:calendar-data")]
|
#[serde(rename = "C:calendar-data")]
|
||||||
CalendarData(String),
|
CalendarData(String),
|
||||||
|
|
||||||
#[serde(other)]
|
|
||||||
#[xml(other)]
|
|
||||||
#[strum_discriminants(strum(disabled))]
|
|
||||||
#[default]
|
|
||||||
Invalid,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, From, Into)]
|
#[derive(Clone, From, Into)]
|
||||||
@@ -73,14 +67,12 @@ impl Resource for CalendarObjectResource {
|
|||||||
CalendarObjectPropName::Getcontenttype => {
|
CalendarObjectPropName::Getcontenttype => {
|
||||||
CalendarObjectProp::Getcontenttype("text/calendar;charset=utf-8".to_owned())
|
CalendarObjectProp::Getcontenttype("text/calendar;charset=utf-8".to_owned())
|
||||||
}
|
}
|
||||||
CalendarObjectPropName::Invalid => {
|
|
||||||
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn resource_name() -> &'static str {
|
fn resource_name() -> &'static str {
|
||||||
|
let a: CalendarObjectPropName = CalendarObjectProp::Getetag("".to_owned()).into();
|
||||||
"caldav_calendar_object"
|
"caldav_calendar_object"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ use rustical_store::CalendarStore;
|
|||||||
use rustical_xml::XmlDeserialize;
|
use rustical_xml::XmlDeserialize;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use strum::{EnumDiscriminants, EnumString, VariantNames};
|
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
|
||||||
|
|
||||||
pub struct PrincipalResourceService<C: CalendarStore + ?Sized> {
|
pub struct PrincipalResourceService<C: CalendarStore + ?Sized> {
|
||||||
principal: String,
|
principal: String,
|
||||||
@@ -24,10 +24,10 @@ pub struct PrincipalResource {
|
|||||||
principal: String,
|
principal: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, XmlDeserialize, Serialize, PartialEq, EnumDiscriminants)]
|
#[derive(XmlDeserialize, Serialize, PartialEq, EnumDiscriminants, Clone)]
|
||||||
#[strum_discriminants(
|
#[strum_discriminants(
|
||||||
name(PrincipalPropName),
|
name(PrincipalPropName),
|
||||||
derive(EnumString, VariantNames),
|
derive(EnumString, VariantNames, IntoStaticStr),
|
||||||
strum(serialize_all = "kebab-case")
|
strum(serialize_all = "kebab-case")
|
||||||
)]
|
)]
|
||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(rename_all = "kebab-case")]
|
||||||
@@ -42,12 +42,6 @@ pub enum PrincipalProp {
|
|||||||
CalendarHomeSet(HrefElement),
|
CalendarHomeSet(HrefElement),
|
||||||
#[serde(rename = "C:calendar-user-address-set")]
|
#[serde(rename = "C:calendar-user-address-set")]
|
||||||
CalendarUserAddressSet(HrefElement),
|
CalendarUserAddressSet(HrefElement),
|
||||||
|
|
||||||
#[serde(other)]
|
|
||||||
#[xml(other)]
|
|
||||||
#[strum_discriminants(strum(disabled))]
|
|
||||||
#[default]
|
|
||||||
Invalid,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PrincipalResource {
|
impl PrincipalResource {
|
||||||
@@ -80,9 +74,6 @@ impl Resource for PrincipalResource {
|
|||||||
PrincipalPropName::CalendarUserAddressSet => {
|
PrincipalPropName::CalendarUserAddressSet => {
|
||||||
PrincipalProp::CalendarUserAddressSet(principal_href)
|
PrincipalProp::CalendarUserAddressSet(principal_href)
|
||||||
}
|
}
|
||||||
PrincipalPropName::Invalid => {
|
|
||||||
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use rustical_store::{auth::User, AddressObject, AddressbookStore};
|
|||||||
use rustical_xml::XmlDeserialize;
|
use rustical_xml::XmlDeserialize;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use strum::{EnumDiscriminants, EnumString, VariantNames};
|
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
|
||||||
|
|
||||||
use super::methods::{get_object, put_object};
|
use super::methods::{get_object, put_object};
|
||||||
|
|
||||||
@@ -21,10 +21,10 @@ pub struct AddressObjectResourceService<AS: AddressbookStore + ?Sized> {
|
|||||||
object_id: String,
|
object_id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, XmlDeserialize, Serialize, PartialEq, EnumDiscriminants)]
|
#[derive(XmlDeserialize, Serialize, PartialEq, EnumDiscriminants, Clone)]
|
||||||
#[strum_discriminants(
|
#[strum_discriminants(
|
||||||
name(AddressObjectPropName),
|
name(AddressObjectPropName),
|
||||||
derive(EnumString, VariantNames),
|
derive(EnumString, VariantNames, IntoStaticStr),
|
||||||
strum(serialize_all = "kebab-case")
|
strum(serialize_all = "kebab-case")
|
||||||
)]
|
)]
|
||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(rename_all = "kebab-case")]
|
||||||
@@ -36,12 +36,6 @@ pub enum AddressObjectProp {
|
|||||||
// CalDAV (RFC 4791)
|
// CalDAV (RFC 4791)
|
||||||
#[serde(rename = "CARD:address-data")]
|
#[serde(rename = "CARD:address-data")]
|
||||||
AddressData(String),
|
AddressData(String),
|
||||||
|
|
||||||
#[serde(other)]
|
|
||||||
#[xml(other)]
|
|
||||||
#[strum_discriminants(strum(disabled))]
|
|
||||||
#[default]
|
|
||||||
Invalid,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, From, Into)]
|
#[derive(Clone, From, Into)]
|
||||||
@@ -74,9 +68,6 @@ impl Resource for AddressObjectResource {
|
|||||||
AddressObjectPropName::Getcontenttype => {
|
AddressObjectPropName::Getcontenttype => {
|
||||||
AddressObjectProp::Getcontenttype("text/vcard;charset=utf-8".to_owned())
|
AddressObjectProp::Getcontenttype("text/vcard;charset=utf-8".to_owned())
|
||||||
}
|
}
|
||||||
AddressObjectPropName::Invalid => {
|
|
||||||
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ use rustical_xml::XmlDeserialize;
|
|||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use strum::{EnumDiscriminants, EnumString, VariantNames};
|
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
|
||||||
|
|
||||||
pub struct AddressbookResourceService<AS: AddressbookStore + ?Sized> {
|
pub struct AddressbookResourceService<AS: AddressbookStore + ?Sized> {
|
||||||
addr_store: Arc<AS>,
|
addr_store: Arc<AS>,
|
||||||
@@ -26,11 +26,11 @@ pub struct AddressbookResourceService<AS: AddressbookStore + ?Sized> {
|
|||||||
addressbook_id: String,
|
addressbook_id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, XmlDeserialize, Serialize, PartialEq, EnumDiscriminants)]
|
#[derive(XmlDeserialize, Serialize, PartialEq, EnumDiscriminants, Clone)]
|
||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(rename_all = "kebab-case")]
|
||||||
#[strum_discriminants(
|
#[strum_discriminants(
|
||||||
name(AddressbookPropName),
|
name(AddressbookPropName),
|
||||||
derive(EnumString, VariantNames),
|
derive(EnumString, VariantNames, IntoStaticStr),
|
||||||
strum(serialize_all = "kebab-case")
|
strum(serialize_all = "kebab-case")
|
||||||
)]
|
)]
|
||||||
pub enum AddressbookProp {
|
pub enum AddressbookProp {
|
||||||
@@ -61,12 +61,6 @@ pub enum AddressbookProp {
|
|||||||
|
|
||||||
// Didn't find the spec
|
// Didn't find the spec
|
||||||
Getctag(String),
|
Getctag(String),
|
||||||
|
|
||||||
#[serde(other)]
|
|
||||||
#[xml(other)]
|
|
||||||
#[strum_discriminants(strum(disabled))]
|
|
||||||
#[default]
|
|
||||||
Invalid,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, From, Into)]
|
#[derive(Clone, Debug, From, Into)]
|
||||||
@@ -107,9 +101,6 @@ impl Resource for AddressbookResource {
|
|||||||
}
|
}
|
||||||
AddressbookPropName::SyncToken => AddressbookProp::SyncToken(self.0.format_synctoken()),
|
AddressbookPropName::SyncToken => AddressbookProp::SyncToken(self.0.format_synctoken()),
|
||||||
AddressbookPropName::Getctag => AddressbookProp::Getctag(self.0.format_synctoken()),
|
AddressbookPropName::Getctag => AddressbookProp::Getctag(self.0.format_synctoken()),
|
||||||
AddressbookPropName::Invalid => {
|
|
||||||
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,7 +120,6 @@ impl Resource for AddressbookResource {
|
|||||||
AddressbookProp::SupportedAddressData(_) => Err(rustical_dav::Error::PropReadOnly),
|
AddressbookProp::SupportedAddressData(_) => Err(rustical_dav::Error::PropReadOnly),
|
||||||
AddressbookProp::SyncToken(_) => Err(rustical_dav::Error::PropReadOnly),
|
AddressbookProp::SyncToken(_) => Err(rustical_dav::Error::PropReadOnly),
|
||||||
AddressbookProp::Getctag(_) => Err(rustical_dav::Error::PropReadOnly),
|
AddressbookProp::Getctag(_) => Err(rustical_dav::Error::PropReadOnly),
|
||||||
AddressbookProp::Invalid => Err(rustical_dav::Error::PropReadOnly),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,7 +139,6 @@ impl Resource for AddressbookResource {
|
|||||||
AddressbookPropName::SupportedAddressData => Err(rustical_dav::Error::PropReadOnly),
|
AddressbookPropName::SupportedAddressData => Err(rustical_dav::Error::PropReadOnly),
|
||||||
AddressbookPropName::SyncToken => Err(rustical_dav::Error::PropReadOnly),
|
AddressbookPropName::SyncToken => Err(rustical_dav::Error::PropReadOnly),
|
||||||
AddressbookPropName::Getctag => Err(rustical_dav::Error::PropReadOnly),
|
AddressbookPropName::Getctag => Err(rustical_dav::Error::PropReadOnly),
|
||||||
AddressbookPropName::Invalid => Err(rustical_dav::Error::PropReadOnly),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ use rustical_store::AddressbookStore;
|
|||||||
use rustical_xml::XmlDeserialize;
|
use rustical_xml::XmlDeserialize;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use strum::{EnumDiscriminants, EnumString, VariantNames};
|
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};
|
||||||
|
|
||||||
pub struct PrincipalResourceService<A: AddressbookStore + ?Sized> {
|
pub struct PrincipalResourceService<A: AddressbookStore + ?Sized> {
|
||||||
principal: String,
|
principal: String,
|
||||||
@@ -24,10 +24,10 @@ pub struct PrincipalResource {
|
|||||||
principal: String,
|
principal: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, XmlDeserialize, Serialize, PartialEq, EnumDiscriminants)]
|
#[derive(XmlDeserialize, Serialize, PartialEq, EnumDiscriminants, Clone)]
|
||||||
#[strum_discriminants(
|
#[strum_discriminants(
|
||||||
name(PrincipalPropName),
|
name(PrincipalPropName),
|
||||||
derive(EnumString, VariantNames),
|
derive(EnumString, VariantNames, IntoStaticStr),
|
||||||
strum(serialize_all = "kebab-case")
|
strum(serialize_all = "kebab-case")
|
||||||
)]
|
)]
|
||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(rename_all = "kebab-case")]
|
||||||
@@ -42,12 +42,6 @@ pub enum PrincipalProp {
|
|||||||
AddressbookHomeSet(HrefElement),
|
AddressbookHomeSet(HrefElement),
|
||||||
#[serde(rename = "CARD:principal-address")]
|
#[serde(rename = "CARD:principal-address")]
|
||||||
PrincipalAddress(Option<HrefElement>),
|
PrincipalAddress(Option<HrefElement>),
|
||||||
|
|
||||||
#[serde(other)]
|
|
||||||
#[xml(other)]
|
|
||||||
#[strum_discriminants(strum(disabled))]
|
|
||||||
#[default]
|
|
||||||
Invalid,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PrincipalResource {
|
impl PrincipalResource {
|
||||||
@@ -80,9 +74,6 @@ impl Resource for PrincipalResource {
|
|||||||
PrincipalProp::AddressbookHomeSet(principal_href)
|
PrincipalProp::AddressbookHomeSet(principal_href)
|
||||||
}
|
}
|
||||||
PrincipalPropName::PrincipalAddress => PrincipalProp::PrincipalAddress(None),
|
PrincipalPropName::PrincipalAddress => PrincipalProp::PrincipalAddress(None),
|
||||||
PrincipalPropName::Invalid => {
|
|
||||||
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
pub trait InvalidProperty {
|
|
||||||
fn invalid_property(&self) -> bool;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: Default + PartialEq> InvalidProperty for T {
|
|
||||||
fn invalid_property(&self) -> bool {
|
|
||||||
self == &T::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
use crate::privileges::UserPrivilege;
|
use crate::privileges::UserPrivilege;
|
||||||
use crate::resource::InvalidProperty;
|
|
||||||
use crate::resource::Resource;
|
use crate::resource::Resource;
|
||||||
use crate::resource::ResourceService;
|
use crate::resource::ResourceService;
|
||||||
use crate::xml::multistatus::{PropstatElement, PropstatWrapper, ResponseElement};
|
use crate::xml::multistatus::{PropstatElement, PropstatWrapper, ResponseElement};
|
||||||
@@ -9,6 +8,7 @@ use crate::Error;
|
|||||||
use actix_web::http::StatusCode;
|
use actix_web::http::StatusCode;
|
||||||
use actix_web::{web::Path, HttpRequest};
|
use actix_web::{web::Path, HttpRequest};
|
||||||
use rustical_store::auth::User;
|
use rustical_store::auth::User;
|
||||||
|
use rustical_xml::Unparsed;
|
||||||
use rustical_xml::XmlDeserialize;
|
use rustical_xml::XmlDeserialize;
|
||||||
use rustical_xml::XmlDocument;
|
use rustical_xml::XmlDocument;
|
||||||
use rustical_xml::XmlRootTag;
|
use rustical_xml::XmlRootTag;
|
||||||
@@ -16,6 +16,21 @@ use std::str::FromStr;
|
|||||||
use tracing::instrument;
|
use tracing::instrument;
|
||||||
use tracing_actix_web::RootSpan;
|
use tracing_actix_web::RootSpan;
|
||||||
|
|
||||||
|
#[derive(XmlDeserialize, Clone, Debug)]
|
||||||
|
#[xml(untagged)]
|
||||||
|
enum SetPropertyPropWrapper<T: XmlDeserialize> {
|
||||||
|
Valid(T),
|
||||||
|
Invalid(Unparsed),
|
||||||
|
}
|
||||||
|
|
||||||
|
// We are <prop>
|
||||||
|
#[derive(XmlDeserialize, Clone, Debug)]
|
||||||
|
struct SetPropertyPropWrapperWrapper<T: XmlDeserialize> {
|
||||||
|
#[xml(ty = "untagged")]
|
||||||
|
property: SetPropertyPropWrapper<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// We are <set>
|
||||||
#[derive(XmlDeserialize, Clone, Debug)]
|
#[derive(XmlDeserialize, Clone, Debug)]
|
||||||
struct SetPropertyElement<T: XmlDeserialize> {
|
struct SetPropertyElement<T: XmlDeserialize> {
|
||||||
prop: T,
|
prop: T,
|
||||||
@@ -47,10 +62,6 @@ enum Operation<T: XmlDeserialize> {
|
|||||||
#[derive(XmlDeserialize, XmlRootTag, Clone, Debug)]
|
#[derive(XmlDeserialize, XmlRootTag, Clone, Debug)]
|
||||||
#[xml(root = b"propertyupdate")]
|
#[xml(root = b"propertyupdate")]
|
||||||
struct PropertyupdateElement<T: XmlDeserialize> {
|
struct PropertyupdateElement<T: XmlDeserialize> {
|
||||||
// #[xml(flatten)]
|
|
||||||
// set: Vec<T>,
|
|
||||||
// #[xml(flatten)]
|
|
||||||
// remove: Vec<TagName>,
|
|
||||||
#[xml(ty = "untagged", flatten)]
|
#[xml(ty = "untagged", flatten)]
|
||||||
operations: Vec<Operation<T>>,
|
operations: Vec<Operation<T>>,
|
||||||
}
|
}
|
||||||
@@ -68,21 +79,9 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
|
|||||||
let resource_service = R::new(&req, path_components.clone()).await?;
|
let resource_service = R::new(&req, path_components.clone()).await?;
|
||||||
|
|
||||||
// Extract operations
|
// Extract operations
|
||||||
let PropertyupdateElement::<<R::Resource as Resource>::Prop> { operations } =
|
let PropertyupdateElement::<SetPropertyPropWrapperWrapper<<R::Resource as Resource>::Prop>> {
|
||||||
XmlDocument::parse_str(&body).map_err(Error::XmlDeserializationError)?;
|
operations,
|
||||||
|
} = 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> = 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.name,
|
|
||||||
// If we can't remove a nonexisting property then that's no big deal
|
|
||||||
Operation::Remove(remove_el) => remove_el.prop.property.name,
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let mut resource = resource_service.get_resource().await?;
|
let mut resource = resource_service.get_resource().await?;
|
||||||
let privileges = resource.get_user_privileges(&user)?;
|
let privileges = resource.get_user_privileges(&user)?;
|
||||||
@@ -94,27 +93,34 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
|
|||||||
let mut props_conflict = Vec::new();
|
let mut props_conflict = Vec::new();
|
||||||
let mut props_not_found = Vec::new();
|
let mut props_not_found = Vec::new();
|
||||||
|
|
||||||
for (operation, propname) in operations.into_iter().zip(propnames) {
|
for operation in operations.into_iter() {
|
||||||
match operation {
|
match operation {
|
||||||
Operation::Set(SetPropertyElement { prop }) => {
|
Operation::Set(SetPropertyElement { prop }) => {
|
||||||
if prop.invalid_property() {
|
match prop.property {
|
||||||
if <R::Resource as Resource>::list_props().contains(&propname.as_str()) {
|
SetPropertyPropWrapper::Valid(prop) => {
|
||||||
// This happens in following cases:
|
let propname: <R::Resource as Resource>::PropName = prop.clone().into();
|
||||||
// - read-only properties with #[serde(skip_deserializing)]
|
let propname: &str = propname.into();
|
||||||
// - internal properties
|
match resource.set_prop(prop) {
|
||||||
props_conflict.push(propname)
|
Ok(()) => props_ok.push(propname.to_owned()),
|
||||||
} else {
|
Err(Error::PropReadOnly) => props_conflict.push(propname.to_owned()),
|
||||||
props_not_found.push(propname);
|
Err(err) => return Err(err.into()),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
SetPropertyPropWrapper::Invalid(invalid) => {
|
||||||
|
let propname = invalid.tag_name();
|
||||||
|
if <R::Resource as Resource>::list_props().contains(&propname.as_str()) {
|
||||||
|
// This happens in following cases:
|
||||||
|
// - read-only properties with #[serde(skip_deserializing)]
|
||||||
|
// - internal properties
|
||||||
|
props_conflict.push(propname)
|
||||||
|
} else {
|
||||||
|
props_not_found.push(propname);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
match resource.set_prop(prop) {
|
|
||||||
Ok(()) => props_ok.push(propname),
|
|
||||||
Err(Error::PropReadOnly) => props_conflict.push(propname),
|
|
||||||
Err(err) => return Err(err.into()),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
Operation::Remove(_remove_el) => {
|
Operation::Remove(remove_el) => {
|
||||||
|
let propname = remove_el.prop.property.name;
|
||||||
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),
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ use actix_web::dev::ResourceMap;
|
|||||||
use actix_web::error::UrlGenerationError;
|
use actix_web::error::UrlGenerationError;
|
||||||
use actix_web::test::TestRequest;
|
use actix_web::test::TestRequest;
|
||||||
use actix_web::{http::StatusCode, ResponseError};
|
use actix_web::{http::StatusCode, ResponseError};
|
||||||
pub use invalid_property::InvalidProperty;
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
pub use resource_service::ResourceService;
|
pub use resource_service::ResourceService;
|
||||||
use rustical_store::auth::User;
|
use rustical_store::auth::User;
|
||||||
@@ -16,12 +15,11 @@ use serde::Serialize;
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use strum::{EnumString, VariantNames};
|
use strum::{EnumString, VariantNames};
|
||||||
|
|
||||||
mod invalid_property;
|
|
||||||
mod methods;
|
mod methods;
|
||||||
mod resource_service;
|
mod resource_service;
|
||||||
|
|
||||||
pub trait ResourceProp: InvalidProperty + Serialize + XmlDeserialize {}
|
pub trait ResourceProp: Serialize + XmlDeserialize {}
|
||||||
impl<T: InvalidProperty + Serialize + XmlDeserialize> ResourceProp for T {}
|
impl<T: Serialize + XmlDeserialize> ResourceProp for T {}
|
||||||
|
|
||||||
pub trait ResourcePropName: FromStr + VariantNames {}
|
pub trait ResourcePropName: FromStr + VariantNames {}
|
||||||
impl<T: FromStr + VariantNames> ResourcePropName for T {}
|
impl<T: FromStr + VariantNames> ResourcePropName for T {}
|
||||||
@@ -29,7 +27,7 @@ impl<T: FromStr + VariantNames> ResourcePropName for T {}
|
|||||||
pub trait ResourceType: Serialize + XmlDeserialize {}
|
pub trait ResourceType: Serialize + XmlDeserialize {}
|
||||||
impl<T: Serialize + XmlDeserialize> ResourceType for T {}
|
impl<T: Serialize + XmlDeserialize> ResourceType for T {}
|
||||||
|
|
||||||
#[derive(XmlDeserialize, Serialize, PartialEq, Default)]
|
#[derive(XmlDeserialize, Serialize, PartialEq)]
|
||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(rename_all = "kebab-case")]
|
||||||
pub enum CommonPropertiesProp {
|
pub enum CommonPropertiesProp {
|
||||||
// WebDAV (RFC 2518)
|
// WebDAV (RFC 2518)
|
||||||
@@ -43,10 +41,6 @@ pub enum CommonPropertiesProp {
|
|||||||
// WebDAV Access Control Protocol (RFC 3477)
|
// WebDAV Access Control Protocol (RFC 3477)
|
||||||
CurrentUserPrivilegeSet(UserPrivilegeSet),
|
CurrentUserPrivilegeSet(UserPrivilegeSet),
|
||||||
Owner(HrefElement),
|
Owner(HrefElement),
|
||||||
|
|
||||||
#[serde(other)]
|
|
||||||
#[default]
|
|
||||||
Invalid,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@@ -67,8 +61,8 @@ pub enum CommonPropertiesPropName {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait Resource: Clone + 'static {
|
pub trait Resource: Clone + 'static {
|
||||||
type PropName: ResourcePropName;
|
type PropName: ResourcePropName + From<Self::Prop> + Into<&'static str>;
|
||||||
type Prop: ResourceProp + PartialEq;
|
type Prop: ResourceProp + PartialEq + Clone;
|
||||||
type Error: ResponseError + From<crate::Error>;
|
type Error: ResponseError + From<crate::Error>;
|
||||||
type PrincipalResource: Resource;
|
type PrincipalResource: Resource;
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use rustical_xml::XmlDeserialize;
|
|||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::any::type_name;
|
use std::any::type_name;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use strum::{EnumString, VariantNames};
|
use strum::{EnumString, IntoStaticStr, VariantNames};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct RootResource<PR: Resource>(PhantomData<PR>);
|
pub struct RootResource<PR: Resource>(PhantomData<PR>);
|
||||||
@@ -19,15 +19,17 @@ impl<PR: Resource> Default for RootResource<PR> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(EnumString, VariantNames, Clone)]
|
#[derive(EnumString, VariantNames, Clone, IntoStaticStr)]
|
||||||
#[strum(serialize_all = "kebab-case")]
|
#[strum(serialize_all = "kebab-case")]
|
||||||
pub enum RootResourcePropName {}
|
pub enum RootResourcePropName {}
|
||||||
|
|
||||||
#[derive(XmlDeserialize, Serialize, Default, Clone, PartialEq)]
|
#[derive(XmlDeserialize, Serialize, Clone, PartialEq)]
|
||||||
pub enum RootResourceProp {
|
pub enum RootResourceProp {}
|
||||||
#[serde(other)]
|
|
||||||
#[default]
|
impl From<RootResourceProp> for RootResourcePropName {
|
||||||
Invalid,
|
fn from(_value: RootResourceProp) -> Self {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<PR: Resource> Resource for RootResource<PR> {
|
impl<PR: Resource> Resource for RootResource<PR> {
|
||||||
|
|||||||
@@ -5,11 +5,13 @@ use darling::FromDeriveInput;
|
|||||||
use quote::quote;
|
use quote::quote;
|
||||||
use syn::{DataStruct, DeriveInput};
|
use syn::{DataStruct, DeriveInput};
|
||||||
|
|
||||||
fn invalid_field_branch(allow: bool) -> proc_macro2::TokenStream {
|
fn invalid_field_branch(ident: &syn::Ident, allow: bool) -> proc_macro2::TokenStream {
|
||||||
|
let ident = ident.to_string();
|
||||||
if allow {
|
if allow {
|
||||||
quote! {}
|
quote! {}
|
||||||
} else {
|
} else {
|
||||||
quote! { return Err(XmlDeError::InvalidFieldName(format!("[{ns:?}]{tag}", tag = String::from_utf8_lossy(tag)))) }
|
quote! {
|
||||||
|
return Err(XmlDeError::InvalidFieldName(#ident, format!("[{ns:?}]{tag}", tag = String::from_utf8_lossy(tag)))) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +87,8 @@ impl NamedStruct {
|
|||||||
|
|
||||||
let builder_field_builds = self.fields.iter().map(Field::builder_field_build);
|
let builder_field_builds = self.fields.iter().map(Field::builder_field_build);
|
||||||
|
|
||||||
let invalid_field_branch = invalid_field_branch(self.attrs.allow_invalid.is_present());
|
let invalid_field_branch =
|
||||||
|
invalid_field_branch(ident, self.attrs.allow_invalid.is_present());
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
impl #impl_generics ::rustical_xml::XmlDeserialize for #ident #type_generics #where_clause {
|
impl #impl_generics ::rustical_xml::XmlDeserialize for #ident #type_generics #where_clause {
|
||||||
|
|||||||
@@ -28,8 +28,8 @@ pub enum XmlDeError {
|
|||||||
Other(String),
|
Other(String),
|
||||||
#[error("Invalid variant: {0}")]
|
#[error("Invalid variant: {0}")]
|
||||||
InvalidVariant(String),
|
InvalidVariant(String),
|
||||||
#[error("Invalid field name: {0}")]
|
#[error("Invalid field name in {0}: {1}")]
|
||||||
InvalidFieldName(String),
|
InvalidFieldName(&'static str, String),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
InvalidValue(#[from] crate::value::ParseValueError),
|
InvalidValue(#[from] crate::value::ParseValueError),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,13 @@ impl XmlDeserialize for () {
|
|||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Unparsed(BytesStart<'static>);
|
pub struct Unparsed(BytesStart<'static>);
|
||||||
|
|
||||||
|
impl Unparsed {
|
||||||
|
pub fn tag_name(&self) -> String {
|
||||||
|
// TODO: respect namespace?
|
||||||
|
String::from_utf8_lossy(self.0.local_name().as_ref()).to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl XmlDeserialize for Unparsed {
|
impl XmlDeserialize for Unparsed {
|
||||||
fn deserialize<R: BufRead>(
|
fn deserialize<R: BufRead>(
|
||||||
reader: &mut quick_xml::NsReader<R>,
|
reader: &mut quick_xml::NsReader<R>,
|
||||||
|
|||||||
Reference in New Issue
Block a user