From 0d2e07fb6544e7b149151e11953c3c29bdb7224d Mon Sep 17 00:00:00 2001 From: Lennart <18233294+lennart-k@users.noreply.github.com> Date: Mon, 4 Nov 2024 19:19:54 +0100 Subject: [PATCH] changes around extensions --- crates/caldav/src/calendar/resource.rs | 12 ++----- crates/caldav/src/calendar_object/resource.rs | 12 ++----- crates/caldav/src/principal/mod.rs | 12 ++----- crates/carddav/src/address_object/resource.rs | 12 ++----- crates/carddav/src/addressbook/resource.rs | 12 ++----- crates/carddav/src/principal/mod.rs | 9 ++--- crates/dav/src/extension.rs | 10 ++---- crates/dav/src/extensions/mod.rs | 35 +++++++++++-------- crates/dav/src/resource/mod.rs | 13 +++++-- crates/dav/src/resources/root.rs | 5 +-- 10 files changed, 53 insertions(+), 79 deletions(-) diff --git a/crates/caldav/src/calendar/resource.rs b/crates/caldav/src/calendar/resource.rs index 9208ebb..2a519a2 100644 --- a/crates/caldav/src/calendar/resource.rs +++ b/crates/caldav/src/calendar/resource.rs @@ -13,8 +13,7 @@ use actix_web::web; use actix_web::{web::Data, HttpRequest}; use async_trait::async_trait; use derive_more::derive::{From, Into, TryInto}; -use rustical_dav::extension::BoxedExtension; -use rustical_dav::extensions::{CommonPropertiesExtension, CommonPropertiesProp}; +use rustical_dav::extensions::CommonPropertiesProp; use rustical_dav::privileges::UserPrivilegeSet; use rustical_dav::resource::{InvalidProperty, Resource, ResourceService}; use rustical_store::auth::User; @@ -86,7 +85,7 @@ pub enum CalendarProp { #[serde(skip_deserializing, untagged)] #[from] #[try_into] - ExtCommonProperties(CommonPropertiesProp), + ExtCommonProperties(CommonPropertiesProp), #[serde(untagged)] Invalid, @@ -106,12 +105,7 @@ impl Resource for CalendarResource { type Prop = CalendarProp; type Error = Error; type ResourceType = Resourcetype; - - fn list_extensions() -> Vec> { - vec![BoxedExtension::from_ext(CommonPropertiesExtension::< - PrincipalResource, - >::default())] - } + type PrincipalResource = PrincipalResource; fn get_prop( &self, diff --git a/crates/caldav/src/calendar_object/resource.rs b/crates/caldav/src/calendar_object/resource.rs index 01b01e5..00c673f 100644 --- a/crates/caldav/src/calendar_object/resource.rs +++ b/crates/caldav/src/calendar_object/resource.rs @@ -4,8 +4,7 @@ use actix_web::{dev::ResourceMap, web::Data, HttpRequest}; use async_trait::async_trait; use derive_more::derive::{From, Into, TryInto}; use rustical_dav::{ - extension::BoxedExtension, - extensions::{CommonPropertiesExtension, CommonPropertiesProp}, + extensions::CommonPropertiesProp, privileges::UserPrivilegeSet, resource::{InvalidProperty, Resource, ResourceService}, }; @@ -44,7 +43,7 @@ pub enum CalendarObjectProp { #[serde(skip_deserializing, untagged)] #[from] #[try_into] - ExtCommonProperties(CommonPropertiesProp), + ExtCommonProperties(CommonPropertiesProp), #[serde(untagged)] Invalid, @@ -73,14 +72,9 @@ impl Resource for CalendarObjectResource { type PropName = CalendarObjectPropName; type Prop = CalendarObjectProp; type Error = Error; + type PrincipalResource = PrincipalResource; type ResourceType = Resourcetype; - fn list_extensions() -> Vec> { - vec![BoxedExtension::from_ext(CommonPropertiesExtension::< - PrincipalResource, - >::default())] - } - fn get_prop( &self, _rmap: &ResourceMap, diff --git a/crates/caldav/src/principal/mod.rs b/crates/caldav/src/principal/mod.rs index da4ed68..3642bac 100644 --- a/crates/caldav/src/principal/mod.rs +++ b/crates/caldav/src/principal/mod.rs @@ -5,8 +5,7 @@ use actix_web::web::Data; use actix_web::HttpRequest; use async_trait::async_trait; use derive_more::derive::{From, TryInto}; -use rustical_dav::extension::BoxedExtension; -use rustical_dav::extensions::{CommonPropertiesExtension, CommonPropertiesProp}; +use rustical_dav::extensions::CommonPropertiesProp; use rustical_dav::privileges::UserPrivilegeSet; use rustical_dav::resource::{InvalidProperty, Resource, ResourceService}; use rustical_dav::xml::HrefElement; @@ -49,7 +48,7 @@ pub enum PrincipalProp { #[serde(skip_deserializing, untagged)] #[from] #[try_into] - ExtCommonProperties(CommonPropertiesProp), + ExtCommonProperties(CommonPropertiesProp), #[serde(untagged)] Invalid, @@ -81,12 +80,7 @@ impl Resource for PrincipalResource { type Prop = PrincipalProp; type Error = Error; type ResourceType = Resourcetype; - - fn list_extensions() -> Vec> { - vec![BoxedExtension::from_ext(CommonPropertiesExtension::< - PrincipalResource, - >::default())] - } + type PrincipalResource = PrincipalResource; fn get_prop( &self, diff --git a/crates/carddav/src/address_object/resource.rs b/crates/carddav/src/address_object/resource.rs index 077d87f..aad4e17 100644 --- a/crates/carddav/src/address_object/resource.rs +++ b/crates/carddav/src/address_object/resource.rs @@ -3,8 +3,7 @@ use actix_web::{dev::ResourceMap, web::Data, HttpRequest}; use async_trait::async_trait; use derive_more::derive::{From, Into, TryInto}; use rustical_dav::{ - extension::BoxedExtension, - extensions::{CommonPropertiesExtension, CommonPropertiesProp}, + extensions::CommonPropertiesProp, privileges::UserPrivilegeSet, resource::{InvalidProperty, Resource, ResourceService}, }; @@ -45,7 +44,7 @@ pub enum AddressObjectProp { #[serde(skip_deserializing, untagged)] #[from] #[try_into] - ExtCommonProperties(CommonPropertiesProp), + ExtCommonProperties(CommonPropertiesProp), #[serde(untagged)] Invalid, @@ -75,12 +74,7 @@ impl Resource for AddressObjectResource { type Prop = AddressObjectProp; type Error = Error; type ResourceType = Resourcetype; - - fn list_extensions() -> Vec> { - vec![BoxedExtension::from_ext(CommonPropertiesExtension::< - PrincipalResource, - >::default())] - } + type PrincipalResource = PrincipalResource; fn get_prop( &self, diff --git a/crates/carddav/src/addressbook/resource.rs b/crates/carddav/src/addressbook/resource.rs index c93e17d..87366b4 100644 --- a/crates/carddav/src/addressbook/resource.rs +++ b/crates/carddav/src/addressbook/resource.rs @@ -10,8 +10,7 @@ use actix_web::web; use actix_web::{web::Data, HttpRequest}; use async_trait::async_trait; use derive_more::derive::{From, Into, TryInto}; -use rustical_dav::extension::BoxedExtension; -use rustical_dav::extensions::{CommonPropertiesExtension, CommonPropertiesProp}; +use rustical_dav::extensions::CommonPropertiesProp; use rustical_dav::privileges::UserPrivilegeSet; use rustical_dav::resource::{InvalidProperty, Resource, ResourceService}; use rustical_store::auth::User; @@ -71,7 +70,7 @@ pub enum AddressbookProp { #[serde(skip_deserializing, untagged)] #[from] #[try_into] - ExtCommonProperties(CommonPropertiesProp), + ExtCommonProperties(CommonPropertiesProp), #[serde(untagged)] Invalid, @@ -91,12 +90,7 @@ impl Resource for AddressbookResource { type Prop = AddressbookProp; type Error = Error; type ResourceType = Resourcetype; - - fn list_extensions() -> Vec> { - vec![BoxedExtension::from_ext(CommonPropertiesExtension::< - PrincipalResource, - >::default())] - } + type PrincipalResource = PrincipalResource; fn get_prop( &self, diff --git a/crates/carddav/src/principal/mod.rs b/crates/carddav/src/principal/mod.rs index e76e6b3..5670409 100644 --- a/crates/carddav/src/principal/mod.rs +++ b/crates/carddav/src/principal/mod.rs @@ -49,7 +49,7 @@ pub enum PrincipalProp { #[serde(skip_deserializing, untagged)] #[from] #[try_into] - ExtCommonProperties(CommonPropertiesProp), + ExtCommonProperties(CommonPropertiesProp), #[serde(untagged)] Invalid, @@ -81,12 +81,7 @@ impl Resource for PrincipalResource { type Prop = PrincipalProp; type Error = Error; type ResourceType = Resourcetype; - - fn list_extensions() -> Vec> { - vec![BoxedExtension::from_ext(CommonPropertiesExtension::< - PrincipalResource, - >::default())] - } + type PrincipalResource = PrincipalResource; fn get_prop( &self, diff --git a/crates/dav/src/extension.rs b/crates/dav/src/extension.rs index 980eae6..131392d 100644 --- a/crates/dav/src/extension.rs +++ b/crates/dav/src/extension.rs @@ -49,10 +49,8 @@ pub trait BoxableExtension { fn list_props(&self) -> &'static [&'static str]; } -impl< - R: Resource, - RE: ResourceExtension + TryFrom, Error: Into>, - > BoxableExtension for RE +impl, Error: Into>> + BoxableExtension for RE { fn get_prop( &self, @@ -114,9 +112,7 @@ impl< pub struct BoxedExtension(Box>); impl BoxedExtension { - pub fn from_ext + TryFrom> + 'static>( - ext: RE, - ) -> Self { + pub fn from_ext> + 'static>(ext: RE) -> Self { let boxed_ext: Box> = Box::new(ext); BoxedExtension(boxed_ext) } diff --git a/crates/dav/src/extensions/mod.rs b/crates/dav/src/extensions/mod.rs index 1074ee5..05525e1 100644 --- a/crates/dav/src/extensions/mod.rs +++ b/crates/dav/src/extensions/mod.rs @@ -1,7 +1,7 @@ use crate::{ extension::ResourceExtension, privileges::UserPrivilegeSet, - resource::{InvalidProperty, Resource}, + resource::{InvalidProperty, Resource, ResourceType}, xml::HrefElement, }; use actix_web::dev::ResourceMap; @@ -11,9 +11,9 @@ use std::marker::PhantomData; use strum::{EnumString, VariantNames}; #[derive(Clone)] -pub struct CommonPropertiesExtension(PhantomData); +pub struct CommonPropertiesExtension(PhantomData); -impl Default for CommonPropertiesExtension { +impl Default for CommonPropertiesExtension { fn default() -> Self { Self(PhantomData) } @@ -21,9 +21,10 @@ impl Default for CommonPropertiesExtension { #[derive(Deserialize, Serialize)] #[serde(rename_all = "kebab-case")] -pub enum CommonPropertiesProp { +pub enum CommonPropertiesProp { // WebDAV (RFC 2518) - Resourcetype(R::ResourceType), + #[serde(skip_deserializing)] + Resourcetype(RT), // WebDAV Current Principal Extension (RFC 5397) CurrentUserPrincipal(HrefElement), @@ -36,7 +37,7 @@ pub enum CommonPropertiesProp { Invalid, } -impl InvalidProperty for CommonPropertiesProp { +impl InvalidProperty for CommonPropertiesProp { fn invalid_property(&self) -> bool { matches!(self, Self::Invalid) } @@ -51,11 +52,11 @@ pub enum CommonPropertiesPropName { Owner, } -impl ResourceExtension for CommonPropertiesExtension +impl ResourceExtension for CommonPropertiesExtension where - R::Prop: From>, + R::Prop: From>, { - type Prop = CommonPropertiesProp; + type Prop = CommonPropertiesProp; type PropName = CommonPropertiesPropName; type Error = R::Error; @@ -72,17 +73,21 @@ where } CommonPropertiesPropName::CurrentUserPrincipal => { CommonPropertiesProp::CurrentUserPrincipal( - PR::get_url(rmap, &[&user.id]).unwrap().into(), + R::PrincipalResource::get_url(rmap, &[&user.id]) + .unwrap() + .into(), ) } CommonPropertiesPropName::CurrentUserPrivilegeSet => { CommonPropertiesProp::CurrentUserPrivilegeSet(resource.get_user_privileges(user)?) } - CommonPropertiesPropName::Owner => CommonPropertiesProp::Owner( - resource - .get_owner() - .map(|owner| PR::get_url(rmap, &[owner]).unwrap().into()), - ), + CommonPropertiesPropName::Owner => { + CommonPropertiesProp::Owner(resource.get_owner().map(|owner| { + R::PrincipalResource::get_url(rmap, &[owner]) + .unwrap() + .into() + })) + } }) } } diff --git a/crates/dav/src/resource/mod.rs b/crates/dav/src/resource/mod.rs index 3f79a8f..7679c58 100644 --- a/crates/dav/src/resource/mod.rs +++ b/crates/dav/src/resource/mod.rs @@ -1,4 +1,5 @@ -use crate::extension::BoxedExtension; +use crate::extension::{BoxableExtension, BoxedExtension}; +use crate::extensions::{CommonPropertiesExtension, CommonPropertiesProp}; use crate::methods::{route_delete, route_propfind, route_proppatch}; use crate::privileges::UserPrivilegeSet; use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper}; @@ -26,14 +27,20 @@ impl Deserialize<'de>> ResourceProp for T {} pub trait ResourcePropName: FromStr + VariantNames {} impl ResourcePropName for T {} +pub trait ResourceType: Serialize + for<'de> Deserialize<'de> {} +impl Deserialize<'de>> ResourceType for T {} + pub trait Resource: Clone + 'static { type PropName: ResourcePropName; - type Prop: ResourceProp; + type Prop: ResourceProp + From>; type Error: ResponseError + From; + type PrincipalResource: Resource; type ResourceType: Default + Serialize + for<'de> Deserialize<'de>; fn list_extensions() -> Vec> { - vec![] + vec![BoxedExtension::from_ext( + CommonPropertiesExtension::::default(), + )] } fn list_props() -> Vec<&'static str> { diff --git a/crates/dav/src/resources/root.rs b/crates/dav/src/resources/root.rs index d4be1cc..58275e6 100644 --- a/crates/dav/src/resources/root.rs +++ b/crates/dav/src/resources/root.rs @@ -30,13 +30,14 @@ impl Default for RootResource { impl Resource for RootResource { type PropName = CommonPropertiesPropName; - type Prop = CommonPropertiesProp>; + type Prop = CommonPropertiesProp; type Error = PR::Error; type ResourceType = Resourcetype; + type PrincipalResource = PR; fn list_extensions() -> Vec> { vec![BoxedExtension::from_ext( - CommonPropertiesExtension::::default(), + CommonPropertiesExtension::::default(), )] }