From ae4d5f0fc68bdd7a52ef34660291b7d9c2962734 Mon Sep 17 00:00:00 2001 From: Lennart <18233294+lennart-k@users.noreply.github.com> Date: Wed, 6 Nov 2024 11:01:13 +0100 Subject: [PATCH] some refactoring --- Cargo.lock | 17 ----- crates/dav/Cargo.toml | 1 - crates/dav/src/resource/invalid_property.rs | 9 +++ crates/dav/src/resource/mod.rs | 75 ++------------------- crates/dav/src/resource/resource_service.rs | 63 +++++++++++++++++ 5 files changed, 78 insertions(+), 87 deletions(-) create mode 100644 crates/dav/src/resource/invalid_property.rs create mode 100644 crates/dav/src/resource/resource_service.rs diff --git a/Cargo.lock b/Cargo.lock index 739da89..b4dba1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -986,16 +986,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" -[[package]] -name = "erased-serde" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24e2389d65ab4fab27dc2a5de7b191e1f6617d1f1c8855c0dc569c94a4cbb18d" -dependencies = [ - "serde", - "typeid", -] - [[package]] name = "errno" version = "0.3.9" @@ -2516,7 +2506,6 @@ dependencies = [ "actix-web", "async-trait", "derive_more 1.0.0", - "erased-serde", "futures-util", "itertools", "log", @@ -3426,12 +3415,6 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" -[[package]] -name = "typeid" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e13db2e0ccd5e14a544e8a246ba2312cd25223f616442d7f2cb0e3db614236e" - [[package]] name = "typenum" version = "1.17.0" diff --git a/crates/dav/Cargo.toml b/crates/dav/Cargo.toml index 8917d4b..5881329 100644 --- a/crates/dav/Cargo.toml +++ b/crates/dav/Cargo.toml @@ -20,4 +20,3 @@ log = { workspace = true } derive_more = { workspace = true } tracing = { workspace = true } tracing-actix-web = { workspace = true } -erased-serde = "0.4.5" diff --git a/crates/dav/src/resource/invalid_property.rs b/crates/dav/src/resource/invalid_property.rs new file mode 100644 index 0000000..b6216a8 --- /dev/null +++ b/crates/dav/src/resource/invalid_property.rs @@ -0,0 +1,9 @@ +pub trait InvalidProperty { + fn invalid_property(&self) -> bool; +} + +impl InvalidProperty for T { + fn invalid_property(&self) -> bool { + self == &T::default() + } +} diff --git a/crates/dav/src/resource/mod.rs b/crates/dav/src/resource/mod.rs index f4c4356..0b88496 100644 --- a/crates/dav/src/resource/mod.rs +++ b/crates/dav/src/resource/mod.rs @@ -1,4 +1,3 @@ -use crate::methods::{route_delete, route_propfind, route_proppatch}; use crate::privileges::UserPrivilegeSet; use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper}; use crate::xml::{multistatus::ResponseElement, TagList}; @@ -6,17 +5,19 @@ use crate::xml::{HrefElement, Resourcetype}; use crate::Error; use actix_web::dev::ResourceMap; use actix_web::error::UrlGenerationError; -use actix_web::http::Method; use actix_web::test::TestRequest; -use actix_web::web; -use actix_web::{http::StatusCode, HttpRequest, ResponseError}; -use async_trait::async_trait; +use actix_web::{http::StatusCode, ResponseError}; +pub use invalid_property::InvalidProperty; use itertools::Itertools; +pub use resource_service::ResourceService; use rustical_store::auth::User; use serde::{Deserialize, Serialize}; use std::str::FromStr; use strum::{EnumString, VariantNames}; +mod invalid_property; +mod resource_service; + pub trait ResourceProp: InvalidProperty + Serialize + for<'de> Deserialize<'de> {} impl Deserialize<'de>> ResourceProp for T {} @@ -234,67 +235,3 @@ pub trait Resource: Clone + 'static { }) } } - -pub trait InvalidProperty { - fn invalid_property(&self) -> bool; -} - -impl InvalidProperty for T { - fn invalid_property(&self) -> bool { - self == &T::default() - } -} - -#[async_trait(?Send)] -pub trait ResourceService: Sized + 'static { - type MemberType: Resource; - type PathComponents: for<'de> Deserialize<'de> + Sized + Clone + 'static; // defines how the resource URI maps to parameters, i.e. /{principal}/{calendar} -> (String, String) - type Resource: Resource; - type Error: ResponseError + From; - - async fn new( - req: &HttpRequest, - path_components: Self::PathComponents, - ) -> Result; - - async fn get_members( - &self, - _rmap: &ResourceMap, - ) -> Result, Self::Error> { - Ok(vec![]) - } - - async fn get_resource(&self) -> Result; - async fn save_resource(&self, _file: Self::Resource) -> Result<(), Self::Error> { - Err(crate::Error::Unauthorized.into()) - } - async fn delete_resource(&self, _use_trashbin: bool) -> Result<(), Self::Error> { - Err(crate::Error::Unauthorized.into()) - } - - #[inline] - fn resource_name() -> &'static str { - Self::Resource::resource_name() - } - - #[inline] - fn actix_resource() -> actix_web::Resource { - Self::actix_additional_routes( - web::resource("") - .name(Self::resource_name()) - .route( - web::method(Method::from_str("PROPFIND").unwrap()).to(route_propfind::), - ) - .route( - web::method(Method::from_str("PROPPATCH").unwrap()).to(route_proppatch::), - ) - .delete(route_delete::), - ) - } - - /// Hook for other resources to insert their additional methods (i.e. REPORT, MKCALENDAR) - #[inline] - fn actix_additional_routes(res: actix_web::Resource) -> actix_web::Resource { - res - } -} diff --git a/crates/dav/src/resource/resource_service.rs b/crates/dav/src/resource/resource_service.rs new file mode 100644 index 0000000..2fde81a --- /dev/null +++ b/crates/dav/src/resource/resource_service.rs @@ -0,0 +1,63 @@ +use std::str::FromStr; + +use actix_web::{dev::ResourceMap, http::Method, web, HttpRequest, ResponseError}; +use async_trait::async_trait; +use serde::Deserialize; + +use crate::methods::{route_delete, route_propfind, route_proppatch}; + +use super::Resource; + +#[async_trait(?Send)] +pub trait ResourceService: Sized + 'static { + type MemberType: Resource; + type PathComponents: for<'de> Deserialize<'de> + Sized + Clone + 'static; // defines how the resource URI maps to parameters, i.e. /{principal}/{calendar} -> (String, String) + type Resource: Resource; + type Error: ResponseError + From; + + async fn new( + req: &HttpRequest, + path_components: Self::PathComponents, + ) -> Result; + + async fn get_members( + &self, + _rmap: &ResourceMap, + ) -> Result, Self::Error> { + Ok(vec![]) + } + + async fn get_resource(&self) -> Result; + async fn save_resource(&self, _file: Self::Resource) -> Result<(), Self::Error> { + Err(crate::Error::Unauthorized.into()) + } + async fn delete_resource(&self, _use_trashbin: bool) -> Result<(), Self::Error> { + Err(crate::Error::Unauthorized.into()) + } + + #[inline] + fn resource_name() -> &'static str { + Self::Resource::resource_name() + } + + #[inline] + fn actix_resource() -> actix_web::Resource { + Self::actix_additional_routes( + web::resource("") + .name(Self::resource_name()) + .route( + web::method(Method::from_str("PROPFIND").unwrap()).to(route_propfind::), + ) + .route( + web::method(Method::from_str("PROPPATCH").unwrap()).to(route_proppatch::), + ) + .delete(route_delete::), + ) + } + + /// Hook for other resources to insert their additional methods (i.e. REPORT, MKCALENDAR) + #[inline] + fn actix_additional_routes(res: actix_web::Resource) -> actix_web::Resource { + res + } +}