remove boxed extensions

This commit is contained in:
Lennart
2024-11-04 19:49:09 +01:00
parent 0d2e07fb65
commit 782d886302
4 changed files with 22 additions and 99 deletions

View File

@@ -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;

View File

@@ -1,16 +1,15 @@
use crate::resource::{Resource, ResourceProp, ResourcePropName};
use actix_web::dev::ResourceMap;
use derive_more::derive::Deref;
use rustical_store::auth::User;
use std::str::FromStr;
use strum::VariantNames;
pub trait ResourceExtension<R: Resource>: Clone {
type PropName: ResourcePropName;
type Prop: ResourceProp;
type Prop: ResourceProp + Into<R::Prop>;
type Error: Into<R::Error> + From<crate::Error>;
fn list_props() -> &'static [&'static str] {
fn list_props(&self) -> &'static [&'static str] {
Self::PropName::VARIANTS
}
@@ -22,55 +21,6 @@ pub trait ResourceExtension<R: Resource>: Clone {
prop: Self::PropName,
) -> Result<Self::Prop, Self::Error>;
fn remove_prop(&mut self, _prop: &Self::PropName) -> Result<(), Self::Error> {
Err(crate::Error::PropReadOnly.into())
}
}
pub struct ResourceExtensionWrapper;
pub trait BoxableExtension<R: Resource> {
fn get_prop(
&self,
resource: &R,
rmap: &ResourceMap,
user: &User,
prop: &str,
) -> Result<Option<R::Prop>, R::Error>;
fn propfind<'a>(
&self,
resource: &R,
props: Vec<&'a str>,
user: &User,
rmap: &ResourceMap,
) -> Result<(Vec<&'a str>, Vec<R::Prop>), R::Error>;
fn list_props(&self) -> &'static [&'static str];
}
impl<R: Resource, RE: ResourceExtension<R, Prop: Into<R::Prop>, Error: Into<R::Error>>>
BoxableExtension<R> for RE
{
fn get_prop(
&self,
resource: &R,
rmap: &ResourceMap,
user: &User,
prop: &str,
// prop: <R as Resource>::PropName,
) -> Result<Option<R::Prop>, R::Error> {
let prop: RE::PropName = if let Ok(prop) = prop.parse() {
prop
} else {
return Ok(None);
};
let prop = ResourceExtension::<R>::get_prop(self, resource, rmap, user, prop)
.map_err(RE::Error::into)?;
Ok(Some(prop.into()))
}
fn propfind<'a>(
&self,
resource: &R,
@@ -78,42 +28,28 @@ impl<R: Resource, RE: ResourceExtension<R, Prop: Into<R::Prop>, Error: Into<R::E
user: &User,
rmap: &ResourceMap,
) -> Result<(Vec<&'a str>, Vec<R::Prop>), R::Error> {
let (valid_props, invalid_props): (Vec<Option<RE::PropName>>, Vec<Option<&str>>) = props
let (valid_props, invalid_props): (Vec<Option<Self::PropName>>, Vec<Option<&str>>) = props
.into_iter()
.map(|prop| {
if let Ok(valid_prop) = RE::PropName::from_str(prop) {
if let Ok(valid_prop) = Self::PropName::from_str(prop) {
(Some(valid_prop), None)
} else {
(None, Some(prop))
}
})
.unzip();
let valid_props: Vec<RE::PropName> = valid_props.into_iter().flatten().collect();
let valid_props: Vec<Self::PropName> = valid_props.into_iter().flatten().collect();
let invalid_props: Vec<&str> = invalid_props.into_iter().flatten().collect();
let prop_responses = valid_props
.into_iter()
.map(|prop| self.get_prop(resource, rmap, user, prop))
.collect::<Result<Vec<_>, RE::Error>>()
.map_err(RE::Error::into)?
.map(|prop| <Self as ResourceExtension<_>>::get_prop(self, resource, rmap, user, prop))
.collect::<Result<Vec<_>, Self::Error>>()
.map_err(Self::Error::into)?
.into_iter()
.map(|prop| prop.into())
.collect::<Vec<_>>();
Ok((invalid_props, prop_responses))
}
fn list_props(&self) -> &'static [&'static str] {
Self::list_props()
}
}
#[derive(Deref)]
pub struct BoxedExtension<R>(Box<dyn BoxableExtension<R>>);
impl<R: Resource> BoxedExtension<R> {
pub fn from_ext<RE: ResourceExtension<R, Prop: Into<R::Prop>> + 'static>(ext: RE) -> Self {
let boxed_ext: Box<dyn BoxableExtension<R>> = Box::new(ext);
BoxedExtension(boxed_ext)
}
}

View File

@@ -1,4 +1,4 @@
use crate::extension::{BoxableExtension, BoxedExtension};
use crate::extension::ResourceExtension;
use crate::extensions::{CommonPropertiesExtension, CommonPropertiesProp};
use crate::methods::{route_delete, route_propfind, route_proppatch};
use crate::privileges::UserPrivilegeSet;
@@ -37,19 +37,12 @@ pub trait Resource: Clone + 'static {
type PrincipalResource: Resource;
type ResourceType: Default + Serialize + for<'de> Deserialize<'de>;
fn list_extensions() -> Vec<BoxedExtension<Self>> {
vec![BoxedExtension::from_ext(
CommonPropertiesExtension::<Self>::default(),
)]
fn list_extensions() -> Vec<impl ResourceExtension<Self>> {
vec![CommonPropertiesExtension::default()]
}
fn list_props() -> Vec<&'static str> {
Self::PropName::VARIANTS
.iter()
.map(|&prop| prop)
// Bodge, since VariantNames somehow includes Ext... props despite the strum(disabled) flag
.filter(|prop| !prop.starts_with("ext-"))
.collect()
Self::PropName::VARIANTS.iter().map(|&prop| prop).collect()
}
fn get_prop(

View File

@@ -1,10 +1,4 @@
use std::any::type_name;
use std::marker::PhantomData;
use crate::extension::BoxedExtension;
use crate::extensions::{
CommonPropertiesExtension, CommonPropertiesProp, CommonPropertiesPropName,
};
use crate::extensions::CommonPropertiesProp;
use crate::privileges::UserPrivilegeSet;
use crate::resource::{Resource, ResourceService};
use actix_web::dev::ResourceMap;
@@ -12,6 +6,9 @@ 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)]
#[serde(rename_all = "kebab-case")]
@@ -28,19 +25,17 @@ impl<PR: Resource> Default for RootResource<PR> {
}
}
#[derive(EnumString, VariantNames, Clone)]
#[strum(serialize_all = "kebab-case")]
pub enum RootResourcePropName {}
impl<PR: Resource> Resource for RootResource<PR> {
type PropName = CommonPropertiesPropName;
type PropName = RootResourcePropName;
type Prop = CommonPropertiesProp<Self::ResourceType>;
type Error = PR::Error;
type ResourceType = Resourcetype;
type PrincipalResource = PR;
fn list_extensions() -> Vec<BoxedExtension<Self>> {
vec![BoxedExtension::from_ext(
CommonPropertiesExtension::<Self>::default(),
)]
}
fn get_prop(
&self,
_rmap: &ResourceMap,