mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 08:12:24 +00:00
simplify resourcetype
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use crate::{
|
||||
extension::ResourceExtension,
|
||||
privileges::UserPrivilegeSet,
|
||||
resource::{InvalidProperty, Resource, ResourceType},
|
||||
xml::HrefElement,
|
||||
resource::{InvalidProperty, Resource},
|
||||
xml::{HrefElement, Resourcetype},
|
||||
};
|
||||
use actix_web::dev::ResourceMap;
|
||||
use rustical_store::auth::User;
|
||||
@@ -21,10 +21,10 @@ impl<R: Resource> Default for CommonPropertiesExtension<R> {
|
||||
|
||||
#[derive(Deserialize, Serialize, PartialEq)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum CommonPropertiesProp<RT: ResourceType> {
|
||||
pub enum CommonPropertiesProp {
|
||||
// WebDAV (RFC 2518)
|
||||
#[serde(skip_deserializing)]
|
||||
Resourcetype(RT),
|
||||
Resourcetype(Resourcetype),
|
||||
|
||||
// WebDAV Current Principal Extension (RFC 5397)
|
||||
CurrentUserPrincipal(HrefElement),
|
||||
@@ -37,7 +37,7 @@ pub enum CommonPropertiesProp<RT: ResourceType> {
|
||||
Invalid,
|
||||
}
|
||||
|
||||
impl<RT: ResourceType> InvalidProperty for CommonPropertiesProp<RT> {
|
||||
impl InvalidProperty for CommonPropertiesProp {
|
||||
fn invalid_property(&self) -> bool {
|
||||
matches!(self, Self::Invalid)
|
||||
}
|
||||
@@ -54,9 +54,9 @@ pub enum CommonPropertiesPropName {
|
||||
|
||||
impl<R: Resource> ResourceExtension<R> for CommonPropertiesExtension<R>
|
||||
where
|
||||
R::Prop: From<CommonPropertiesProp<R::ResourceType>>,
|
||||
R::Prop: From<CommonPropertiesProp>,
|
||||
{
|
||||
type Prop = CommonPropertiesProp<R::ResourceType>;
|
||||
type Prop = CommonPropertiesProp;
|
||||
type PropName = CommonPropertiesPropName;
|
||||
type Error = R::Error;
|
||||
|
||||
@@ -69,7 +69,7 @@ where
|
||||
) -> Result<Self::Prop, Self::Error> {
|
||||
Ok(match prop {
|
||||
CommonPropertiesPropName::Resourcetype => {
|
||||
CommonPropertiesProp::Resourcetype(R::ResourceType::default())
|
||||
CommonPropertiesProp::Resourcetype(Resourcetype(R::get_resourcetype()))
|
||||
}
|
||||
CommonPropertiesPropName::CurrentUserPrincipal => {
|
||||
CommonPropertiesProp::CurrentUserPrincipal(
|
||||
|
||||
@@ -29,10 +29,11 @@ impl<T: Serialize + for<'de> Deserialize<'de>> ResourceType for T {}
|
||||
|
||||
pub trait Resource: Clone + 'static {
|
||||
type PropName: ResourcePropName;
|
||||
type Prop: ResourceProp + From<CommonPropertiesProp<Self::ResourceType>> + PartialEq;
|
||||
type Prop: ResourceProp + From<CommonPropertiesProp> + PartialEq;
|
||||
type Error: ResponseError + From<crate::Error>;
|
||||
type PrincipalResource: Resource;
|
||||
type ResourceType: Default + Serialize + for<'de> Deserialize<'de>;
|
||||
|
||||
fn get_resourcetype() -> &'static [&'static str];
|
||||
|
||||
fn list_extensions() -> Vec<impl ResourceExtension<Self>> {
|
||||
vec![CommonPropertiesExtension::default()]
|
||||
@@ -122,7 +123,7 @@ pub trait Resource: Clone + 'static {
|
||||
Error::BadRequest("allprop MUST be the only queried prop".to_owned()).into(),
|
||||
);
|
||||
}
|
||||
props = Self::list_props();
|
||||
props = Self::list_props().to_vec();
|
||||
for extension in Self::list_extensions() {
|
||||
let ext_props: Vec<&str> = extension.list_props().into();
|
||||
props.extend(ext_props);
|
||||
|
||||
@@ -5,17 +5,10 @@ use actix_web::dev::ResourceMap;
|
||||
use actix_web::HttpRequest;
|
||||
use async_trait::async_trait;
|
||||
use rustical_store::auth::User;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::any::type_name;
|
||||
use std::marker::PhantomData;
|
||||
use strum::{EnumString, VariantNames};
|
||||
|
||||
#[derive(Deserialize, Serialize, Default, Debug, PartialEq)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub struct Resourcetype {
|
||||
collection: (),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RootResource<PR: Resource>(PhantomData<PR>);
|
||||
|
||||
@@ -31,11 +24,14 @@ pub enum RootResourcePropName {}
|
||||
|
||||
impl<PR: Resource> Resource for RootResource<PR> {
|
||||
type PropName = RootResourcePropName;
|
||||
type Prop = CommonPropertiesProp<Self::ResourceType>;
|
||||
type Prop = CommonPropertiesProp;
|
||||
type Error = PR::Error;
|
||||
type ResourceType = Resourcetype;
|
||||
type PrincipalResource = PR;
|
||||
|
||||
fn get_resourcetype() -> &'static [&'static str] {
|
||||
&["collection"]
|
||||
}
|
||||
|
||||
fn get_prop(
|
||||
&self,
|
||||
_rmap: &ResourceMap,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod multistatus;
|
||||
mod resourcetype;
|
||||
pub mod tag_list;
|
||||
pub mod tag_name;
|
||||
|
||||
@@ -7,6 +8,8 @@ pub use multistatus::MultistatusElement;
|
||||
pub use tag_list::TagList;
|
||||
pub use tag_name::TagName;
|
||||
|
||||
pub use resourcetype::Resourcetype;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, From, PartialEq)]
|
||||
|
||||
46
crates/dav/src/xml/resourcetype.rs
Normal file
46
crates/dav/src/xml/resourcetype.rs
Normal file
@@ -0,0 +1,46 @@
|
||||
use serde::de::{MapAccess, Visitor};
|
||||
use serde::ser::SerializeMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Resourcetype(pub &'static [&'static str]);
|
||||
|
||||
struct ResourcetypeVisitor;
|
||||
|
||||
impl<'de> Visitor<'de> for ResourcetypeVisitor {
|
||||
type Value = Resourcetype;
|
||||
|
||||
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>,
|
||||
{
|
||||
while map.next_key::<String>()?.is_some() {}
|
||||
Ok(Resourcetype(&[]))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Resourcetype {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_map(ResourcetypeVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for Resourcetype {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: serde::Serializer,
|
||||
{
|
||||
let mut map = serializer.serialize_map(Some(self.0.len()))?;
|
||||
for entry in self.0 {
|
||||
map.serialize_entry(entry, &())?;
|
||||
}
|
||||
map.end()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user