Refactoring around routing and getting the principal uri (less dependence on actix)

This commit is contained in:
Lennart K
2025-06-02 16:17:13 +02:00
parent 0f294cf2e1
commit ef33868151
23 changed files with 169 additions and 216 deletions

View File

@@ -14,7 +14,7 @@ use rustical_xml::XmlDocument;
use tracing::instrument;
use tracing_actix_web::RootSpan;
#[instrument(parent = root_span.id(), skip(path, req, root_span, resource_service))]
#[instrument(parent = root_span.id(), skip(path, req, root_span, resource_service, puri))]
#[allow(clippy::type_complexity)]
pub(crate) async fn route_propfind<R: ResourceService>(
path: Path<R::PathComponents>,
@@ -24,6 +24,7 @@ pub(crate) async fn route_propfind<R: ResourceService>(
depth: Depth,
root_span: RootSpan,
resource_service: Data<R>,
puri: Data<R::PrincipalUri>,
) -> Result<
MultistatusElement<<R::Resource as Resource>::Prop, <R::MemberType as Resource>::Prop>,
R::Error,
@@ -61,13 +62,13 @@ pub(crate) async fn route_propfind<R: ResourceService>(
member_responses.push(member.propfind(
&format!("{}/{}", req.path().trim_end_matches('/'), subpath),
&props,
puri.as_ref(),
&user,
req.resource_map(),
)?);
}
}
let response = resource.propfind(req.path(), &props, &user, req.resource_map())?;
let response = resource.propfind(req.path(), &props, puri.as_ref(), &user)?;
Ok(MultistatusElement {
responses: vec![response],

View File

@@ -3,7 +3,6 @@ use crate::xml::Resourcetype;
use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper};
use crate::xml::{TagList, multistatus::ResponseElement};
use crate::{Error, Principal};
use actix_web::dev::ResourceMap;
use actix_web::http::header::{EntityTag, IfMatch, IfNoneMatch};
use actix_web::{ResponseError, http::StatusCode};
use itertools::Itertools;
@@ -13,8 +12,10 @@ use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize}
use std::str::FromStr;
mod methods;
mod principal_uri;
mod resource_service;
pub use principal_uri::PrincipalUri;
pub use resource_service::*;
pub trait ResourceProp: XmlSerialize + XmlDeserialize {}
@@ -36,7 +37,7 @@ pub trait Resource: Clone + 'static {
fn get_prop(
&self,
rmap: &ResourceMap,
principal_uri: &impl PrincipalUri,
principal: &Self::Principal,
prop: &<Self::Prop as EnumUnitVariants>::UnitVariants,
) -> Result<Self::Prop, Self::Error>;
@@ -101,8 +102,8 @@ pub trait Resource: Clone + 'static {
&self,
path: &str,
props: &[&str],
principal_uri: &impl PrincipalUri,
principal: &Self::Principal,
rmap: &ResourceMap,
) -> Result<ResponseElement<Self::Prop>, Self::Error> {
let mut props = props.to_vec();
@@ -154,7 +155,7 @@ pub trait Resource: Clone + 'static {
let prop_responses = valid_props
.into_iter()
.map(|prop| self.get_prop(rmap, principal, &prop))
.map(|prop| self.get_prop(principal_uri, principal, &prop))
.collect::<Result<Vec<_>, Self::Error>>()?;
let mut propstats = vec![PropstatWrapper::Normal(PropstatElement {

View File

@@ -0,0 +1,3 @@
pub trait PrincipalUri: 'static {
fn principal_uri(&self, principal: &str) -> String;
}

View File

@@ -1,17 +1,13 @@
use super::methods::{route_delete, route_propfind, route_proppatch};
use super::{PrincipalUri, Resource};
use crate::Principal;
use actix_web::dev::{AppService, HttpServiceFactory};
use actix_web::error::UrlGenerationError;
use actix_web::test::TestRequest;
use actix_web::web::Data;
use actix_web::{ResponseError, dev::ResourceMap, http::Method, web};
use actix_web::{ResponseError, http::Method, web};
use async_trait::async_trait;
use serde::Deserialize;
use std::str::FromStr;
use crate::Principal;
use super::Resource;
use super::methods::{route_delete, route_propfind, route_proppatch};
#[async_trait(?Send)]
pub trait ResourceService: Sized + 'static {
type MemberType: Resource<Error = Self::Error, Principal = Self::Principal>;
@@ -19,6 +15,7 @@ pub trait ResourceService: Sized + 'static {
type Resource: Resource<Error = Self::Error, Principal = Self::Principal>;
type Error: ResponseError + From<crate::Error>;
type Principal: Principal;
type PrincipalUri: PrincipalUri;
async fn get_members(
&self,
@@ -31,6 +28,7 @@ pub trait ResourceService: Sized + 'static {
&self,
_path: &Self::PathComponents,
) -> Result<Self::Resource, Self::Error>;
async fn save_resource(
&self,
_path: &Self::PathComponents,
@@ -38,6 +36,7 @@ pub trait ResourceService: Sized + 'static {
) -> Result<(), Self::Error> {
Err(crate::Error::Unauthorized.into())
}
async fn delete_resource(
&self,
_path: &Self::PathComponents,
@@ -68,25 +67,6 @@ pub trait ResourceService: Sized + 'static {
}
}
pub trait NamedRoute {
fn route_name() -> &'static str;
fn get_url<U, I>(rmap: &ResourceMap, elements: U) -> Result<String, UrlGenerationError>
where
U: IntoIterator<Item = I>,
I: AsRef<str>,
{
Ok(rmap
.url_for(
&TestRequest::default().to_http_request(),
Self::route_name(),
elements,
)?
.path()
.to_owned())
}
}
pub struct ResourceServiceRoute<RS: ResourceService>(pub RS);
impl<RS: ResourceService> HttpServiceFactory for ResourceServiceRoute<RS> {