some refactoring

This commit is contained in:
Lennart
2024-11-06 11:01:13 +01:00
parent 85b5af2e76
commit ae4d5f0fc6
5 changed files with 78 additions and 87 deletions

17
Cargo.lock generated
View File

@@ -986,16 +986,6 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" 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]] [[package]]
name = "errno" name = "errno"
version = "0.3.9" version = "0.3.9"
@@ -2516,7 +2506,6 @@ dependencies = [
"actix-web", "actix-web",
"async-trait", "async-trait",
"derive_more 1.0.0", "derive_more 1.0.0",
"erased-serde",
"futures-util", "futures-util",
"itertools", "itertools",
"log", "log",
@@ -3426,12 +3415,6 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
[[package]]
name = "typeid"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e13db2e0ccd5e14a544e8a246ba2312cd25223f616442d7f2cb0e3db614236e"
[[package]] [[package]]
name = "typenum" name = "typenum"
version = "1.17.0" version = "1.17.0"

View File

@@ -20,4 +20,3 @@ log = { workspace = true }
derive_more = { workspace = true } derive_more = { workspace = true }
tracing = { workspace = true } tracing = { workspace = true }
tracing-actix-web = { workspace = true } tracing-actix-web = { workspace = true }
erased-serde = "0.4.5"

View File

@@ -0,0 +1,9 @@
pub trait InvalidProperty {
fn invalid_property(&self) -> bool;
}
impl<T: Default + PartialEq> InvalidProperty for T {
fn invalid_property(&self) -> bool {
self == &T::default()
}
}

View File

@@ -1,4 +1,3 @@
use crate::methods::{route_delete, route_propfind, route_proppatch};
use crate::privileges::UserPrivilegeSet; use crate::privileges::UserPrivilegeSet;
use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper}; use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper};
use crate::xml::{multistatus::ResponseElement, TagList}; use crate::xml::{multistatus::ResponseElement, TagList};
@@ -6,17 +5,19 @@ use crate::xml::{HrefElement, Resourcetype};
use crate::Error; use crate::Error;
use actix_web::dev::ResourceMap; use actix_web::dev::ResourceMap;
use actix_web::error::UrlGenerationError; use actix_web::error::UrlGenerationError;
use actix_web::http::Method;
use actix_web::test::TestRequest; use actix_web::test::TestRequest;
use actix_web::web; use actix_web::{http::StatusCode, ResponseError};
use actix_web::{http::StatusCode, HttpRequest, ResponseError}; pub use invalid_property::InvalidProperty;
use async_trait::async_trait;
use itertools::Itertools; use itertools::Itertools;
pub use resource_service::ResourceService;
use rustical_store::auth::User; use rustical_store::auth::User;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::str::FromStr; use std::str::FromStr;
use strum::{EnumString, VariantNames}; use strum::{EnumString, VariantNames};
mod invalid_property;
mod resource_service;
pub trait ResourceProp: InvalidProperty + Serialize + for<'de> Deserialize<'de> {} pub trait ResourceProp: InvalidProperty + Serialize + for<'de> Deserialize<'de> {}
impl<T: InvalidProperty + Serialize + for<'de> Deserialize<'de>> ResourceProp for T {} impl<T: InvalidProperty + Serialize + for<'de> Deserialize<'de>> ResourceProp for T {}
@@ -234,67 +235,3 @@ pub trait Resource: Clone + 'static {
}) })
} }
} }
pub trait InvalidProperty {
fn invalid_property(&self) -> bool;
}
impl<T: Default + PartialEq> InvalidProperty for T {
fn invalid_property(&self) -> bool {
self == &T::default()
}
}
#[async_trait(?Send)]
pub trait ResourceService: Sized + 'static {
type MemberType: Resource<Error = Self::Error>;
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<Error = Self::Error>;
type Error: ResponseError + From<crate::Error>;
async fn new(
req: &HttpRequest,
path_components: Self::PathComponents,
) -> Result<Self, Self::Error>;
async fn get_members(
&self,
_rmap: &ResourceMap,
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
Ok(vec![])
}
async fn get_resource(&self) -> Result<Self::Resource, Self::Error>;
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::<Self>),
)
.route(
web::method(Method::from_str("PROPPATCH").unwrap()).to(route_proppatch::<Self>),
)
.delete(route_delete::<Self>),
)
}
/// 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
}
}

View File

@@ -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<Error = Self::Error>;
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<Error = Self::Error>;
type Error: ResponseError + From<crate::Error>;
async fn new(
req: &HttpRequest,
path_components: Self::PathComponents,
) -> Result<Self, Self::Error>;
async fn get_members(
&self,
_rmap: &ResourceMap,
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
Ok(vec![])
}
async fn get_resource(&self) -> Result<Self::Resource, Self::Error>;
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::<Self>),
)
.route(
web::method(Method::from_str("PROPPATCH").unwrap()).to(route_proppatch::<Self>),
)
.delete(route_delete::<Self>),
)
}
/// 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
}
}