Remove Resource::PropName

This commit is contained in:
Lennart
2025-01-18 23:18:59 +01:00
parent 020e836c42
commit 8359e4ee1c
11 changed files with 29 additions and 30 deletions

View File

@@ -93,7 +93,6 @@ impl DavPushExtension for CalendarResource {
} }
impl Resource for CalendarResource { impl Resource for CalendarResource {
type PropName = CalendarPropWrapperName;
type Prop = CalendarPropWrapper; type Prop = CalendarPropWrapper;
type Error = Error; type Error = Error;
type PrincipalResource = PrincipalResource; type PrincipalResource = PrincipalResource;
@@ -119,7 +118,7 @@ impl Resource for CalendarResource {
&self, &self,
rmap: &ResourceMap, rmap: &ResourceMap,
user: &User, user: &User,
prop: &Self::PropName, prop: &CalendarPropWrapperName,
) -> Result<Self::Prop, Self::Error> { ) -> Result<Self::Prop, Self::Error> {
Ok(match prop { Ok(match prop {
CalendarPropWrapperName::Calendar(prop) => CalendarPropWrapper::Calendar(match prop { CalendarPropWrapperName::Calendar(prop) => CalendarPropWrapper::Calendar(match prop {
@@ -230,7 +229,7 @@ impl Resource for CalendarResource {
} }
} }
fn remove_prop(&mut self, prop: &Self::PropName) -> Result<(), rustical_dav::Error> { fn remove_prop(&mut self, prop: &CalendarPropWrapperName) -> Result<(), rustical_dav::Error> {
if self.read_only { if self.read_only {
return Err(rustical_dav::Error::PropReadOnly); return Err(rustical_dav::Error::PropReadOnly);
} }

View File

@@ -52,7 +52,6 @@ pub struct CalendarObjectResource {
} }
impl Resource for CalendarObjectResource { impl Resource for CalendarObjectResource {
type PropName = CalendarObjectPropWrapperName;
type Prop = CalendarObjectPropWrapper; type Prop = CalendarObjectPropWrapper;
type Error = Error; type Error = Error;
type PrincipalResource = PrincipalResource; type PrincipalResource = PrincipalResource;
@@ -65,7 +64,7 @@ impl Resource for CalendarObjectResource {
&self, &self,
rmap: &ResourceMap, rmap: &ResourceMap,
user: &User, user: &User,
prop: &Self::PropName, prop: &CalendarObjectPropWrapperName,
) -> Result<Self::Prop, Self::Error> { ) -> Result<Self::Prop, Self::Error> {
Ok(match prop { Ok(match prop {
CalendarObjectPropWrapperName::CalendarObject(prop) => { CalendarObjectPropWrapperName::CalendarObject(prop) => {

View File

@@ -24,7 +24,6 @@ pub enum PrincipalPropWrapper {
} }
impl Resource for CalendarSetResource { impl Resource for CalendarSetResource {
type PropName = PrincipalPropWrapperName;
type Prop = PrincipalPropWrapper; type Prop = PrincipalPropWrapper;
type Error = Error; type Error = Error;
type PrincipalResource = PrincipalResource; type PrincipalResource = PrincipalResource;
@@ -40,7 +39,7 @@ impl Resource for CalendarSetResource {
&self, &self,
rmap: &ResourceMap, rmap: &ResourceMap,
user: &User, user: &User,
prop: &Self::PropName, prop: &PrincipalPropWrapperName,
) -> Result<Self::Prop, Self::Error> { ) -> Result<Self::Prop, Self::Error> {
Ok(match prop { Ok(match prop {
PrincipalPropWrapperName::Common(prop) => PrincipalPropWrapper::Common( PrincipalPropWrapperName::Common(prop) => PrincipalPropWrapper::Common(

View File

@@ -59,7 +59,6 @@ impl NamedRoute for PrincipalResource {
} }
impl Resource for PrincipalResource { impl Resource for PrincipalResource {
type PropName = PrincipalPropWrapperName;
type Prop = PrincipalPropWrapper; type Prop = PrincipalPropWrapper;
type Error = Error; type Error = Error;
type PrincipalResource = PrincipalResource; type PrincipalResource = PrincipalResource;
@@ -75,7 +74,7 @@ impl Resource for PrincipalResource {
&self, &self,
rmap: &ResourceMap, rmap: &ResourceMap,
user: &User, user: &User,
prop: &Self::PropName, prop: &PrincipalPropWrapperName,
) -> Result<Self::Prop, Self::Error> { ) -> Result<Self::Prop, Self::Error> {
let principal_url = Self::get_url(rmap, vec![&self.principal]).unwrap(); let principal_url = Self::get_url(rmap, vec![&self.principal]).unwrap();
let home_set = CalendarHomeSet( let home_set = CalendarHomeSet(

View File

@@ -48,7 +48,6 @@ pub struct AddressObjectResource {
} }
impl Resource for AddressObjectResource { impl Resource for AddressObjectResource {
type PropName = AddressObjectPropWrapperName;
type Prop = AddressObjectPropWrapper; type Prop = AddressObjectPropWrapper;
type Error = Error; type Error = Error;
type PrincipalResource = PrincipalResource; type PrincipalResource = PrincipalResource;
@@ -61,7 +60,7 @@ impl Resource for AddressObjectResource {
&self, &self,
rmap: &ResourceMap, rmap: &ResourceMap,
user: &User, user: &User,
prop: &Self::PropName, prop: &AddressObjectPropWrapperName,
) -> Result<Self::Prop, Self::Error> { ) -> Result<Self::Prop, Self::Error> {
Ok(match prop { Ok(match prop {
AddressObjectPropWrapperName::AddressObject(prop) => { AddressObjectPropWrapperName::AddressObject(prop) => {

View File

@@ -81,7 +81,6 @@ impl DavPushExtension for AddressbookResource {
} }
impl Resource for AddressbookResource { impl Resource for AddressbookResource {
type PropName = AddressbookPropWrapperName;
type Prop = AddressbookPropWrapper; type Prop = AddressbookPropWrapper;
type Error = Error; type Error = Error;
type PrincipalResource = PrincipalResource; type PrincipalResource = PrincipalResource;
@@ -97,7 +96,7 @@ impl Resource for AddressbookResource {
&self, &self,
rmap: &ResourceMap, rmap: &ResourceMap,
user: &User, user: &User,
prop: &Self::PropName, prop: &AddressbookPropWrapperName,
) -> Result<Self::Prop, Self::Error> { ) -> Result<Self::Prop, Self::Error> {
Ok(match prop { Ok(match prop {
AddressbookPropWrapperName::Addressbook(prop) => { AddressbookPropWrapperName::Addressbook(prop) => {
@@ -153,7 +152,10 @@ impl Resource for AddressbookResource {
} }
} }
fn remove_prop(&mut self, prop: &Self::PropName) -> Result<(), rustical_dav::Error> { fn remove_prop(
&mut self,
prop: &AddressbookPropWrapperName,
) -> Result<(), rustical_dav::Error> {
match prop { match prop {
AddressbookPropWrapperName::Addressbook(prop) => match prop { AddressbookPropWrapperName::Addressbook(prop) => match prop {
AddressbookPropName::Displayname => { AddressbookPropName::Displayname => {

View File

@@ -64,7 +64,6 @@ impl NamedRoute for PrincipalResource {
} }
impl Resource for PrincipalResource { impl Resource for PrincipalResource {
type PropName = PrincipalPropWrapperName;
type Prop = PrincipalPropWrapper; type Prop = PrincipalPropWrapper;
type Error = Error; type Error = Error;
type PrincipalResource = PrincipalResource; type PrincipalResource = PrincipalResource;
@@ -80,7 +79,7 @@ impl Resource for PrincipalResource {
&self, &self,
rmap: &ResourceMap, rmap: &ResourceMap,
user: &User, user: &User,
prop: &Self::PropName, prop: &PrincipalPropWrapperName,
) -> Result<Self::Prop, Self::Error> { ) -> Result<Self::Prop, Self::Error> {
let principal_href = HrefElement::new(Self::get_principal_url(rmap, &self.principal)); let principal_href = HrefElement::new(Self::get_principal_url(rmap, &self.principal));

View File

@@ -11,6 +11,7 @@ use actix_web::{web::Path, HttpRequest};
use itertools::Itertools; use itertools::Itertools;
use quick_xml::name::Namespace; use quick_xml::name::Namespace;
use rustical_store::auth::User; use rustical_store::auth::User;
use rustical_xml::EnumUnitVariants;
use rustical_xml::Unparsed; use rustical_xml::Unparsed;
use rustical_xml::XmlDeserialize; use rustical_xml::XmlDeserialize;
use rustical_xml::XmlDocument; use rustical_xml::XmlDocument;
@@ -97,7 +98,7 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
}) => { }) => {
match property { match property {
SetPropertyPropWrapper::Valid(prop) => { SetPropertyPropWrapper::Valid(prop) => {
let propname: <R::Resource as Resource>::PropName = prop.clone().into(); let propname: <<R::Resource as Resource>::Prop as EnumUnitVariants>::UnitVariants = prop.clone().into();
let (ns, propname): (Option<Namespace>, &str) = propname.into(); let (ns, propname): (Option<Namespace>, &str) = propname.into();
match resource.set_prop(prop) { match resource.set_prop(prop) {
Ok(()) => props_ok.push((ns, propname.to_owned())), Ok(()) => props_ok.push((ns, propname.to_owned())),
@@ -127,7 +128,9 @@ pub(crate) async fn route_proppatch<R: ResourceService>(
} }
Operation::Remove(remove_el) => { Operation::Remove(remove_el) => {
let propname = remove_el.prop.0 .0; let propname = remove_el.prop.0 .0;
match <<R::Resource as Resource>::PropName as FromStr>::from_str(&propname) { match <<R::Resource as Resource>::Prop as EnumUnitVariants>::UnitVariants::from_str(
&propname,
) {
Ok(prop) => match resource.remove_prop(&prop) { Ok(prop) => match resource.remove_prop(&prop) {
Ok(()) => props_ok.push((None, propname)), Ok(()) => props_ok.push((None, propname)),
Err(Error::PropReadOnly) => props_conflict.push((None, propname)), Err(Error::PropReadOnly) => props_conflict.push((None, propname)),

View File

@@ -9,7 +9,7 @@ use itertools::Itertools;
use quick_xml::name::Namespace; use quick_xml::name::Namespace;
pub use resource_service::ResourceService; pub use resource_service::ResourceService;
use rustical_store::auth::User; use rustical_store::auth::User;
use rustical_xml::{EnumVariants, XmlDeserialize, XmlSerialize}; use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
use std::str::FromStr; use std::str::FromStr;
mod methods; mod methods;
@@ -24,10 +24,7 @@ pub trait ResourcePropName: FromStr {}
impl<T: FromStr> ResourcePropName for T {} impl<T: FromStr> ResourcePropName for T {}
pub trait Resource: Clone + 'static { pub trait Resource: Clone + 'static {
type PropName: ResourcePropName type Prop: ResourceProp + PartialEq + Clone + EnumVariants + EnumUnitVariants;
+ From<Self::Prop>
+ Into<(Option<Namespace<'static>>, &'static str)>;
type Prop: ResourceProp + PartialEq + Clone + EnumVariants;
type Error: ResponseError + From<crate::Error>; type Error: ResponseError + From<crate::Error>;
type PrincipalResource: Resource + NamedRoute; type PrincipalResource: Resource + NamedRoute;
@@ -41,14 +38,17 @@ pub trait Resource: Clone + 'static {
&self, &self,
rmap: &ResourceMap, rmap: &ResourceMap,
user: &User, user: &User,
prop: &Self::PropName, prop: &<Self::Prop as EnumUnitVariants>::UnitVariants,
) -> Result<Self::Prop, Self::Error>; ) -> Result<Self::Prop, Self::Error>;
fn set_prop(&mut self, _prop: Self::Prop) -> Result<(), crate::Error> { fn set_prop(&mut self, _prop: Self::Prop) -> Result<(), crate::Error> {
Err(crate::Error::PropReadOnly) Err(crate::Error::PropReadOnly)
} }
fn remove_prop(&mut self, _prop: &Self::PropName) -> Result<(), crate::Error> { fn remove_prop(
&mut self,
_prop: &<Self::Prop as EnumUnitVariants>::UnitVariants,
) -> Result<(), crate::Error> {
Err(crate::Error::PropReadOnly) Err(crate::Error::PropReadOnly)
} }
@@ -106,7 +106,7 @@ pub trait Resource: Clone + 'static {
let mut valid_props = vec![]; let mut valid_props = vec![];
let mut invalid_props = vec![]; let mut invalid_props = vec![];
for prop in props { for prop in props {
if let Ok(valid_prop) = Self::PropName::from_str(prop) { if let Ok(valid_prop) = <Self::Prop as EnumUnitVariants>::UnitVariants::from_str(prop) {
valid_props.push(valid_prop); valid_props.push(valid_prop);
} else { } else {
invalid_props.push(prop.to_string()) invalid_props.push(prop.to_string())

View File

@@ -19,7 +19,6 @@ impl<PR: Resource> Default for RootResource<PR> {
} }
impl<PR: Resource + NamedRoute> Resource for RootResource<PR> { impl<PR: Resource + NamedRoute> Resource for RootResource<PR> {
type PropName = CommonPropertiesPropName;
type Prop = CommonPropertiesProp; type Prop = CommonPropertiesProp;
type Error = PR::Error; type Error = PR::Error;
type PrincipalResource = PR; type PrincipalResource = PR;
@@ -35,7 +34,7 @@ impl<PR: Resource + NamedRoute> Resource for RootResource<PR> {
&self, &self,
rmap: &ResourceMap, rmap: &ResourceMap,
user: &User, user: &User,
prop: &Self::PropName, prop: &CommonPropertiesPropName,
) -> Result<Self::Prop, Self::Error> { ) -> Result<Self::Prop, Self::Error> {
CommonPropertiesExtension::get_prop(self, rmap, user, prop) CommonPropertiesExtension::get_prop(self, rmap, user, prop)
} }

View File

@@ -1,5 +1,6 @@
use quick_xml::name::Namespace; use quick_xml::name::Namespace;
use std::collections::HashMap; use std::collections::HashMap;
use std::str::FromStr;
pub mod de; pub mod de;
mod error; mod error;
@@ -34,6 +35,6 @@ pub trait EnumVariants {
fn variant_names() -> Vec<(Option<Namespace<'static>>, &'static str)>; fn variant_names() -> Vec<(Option<Namespace<'static>>, &'static str)>;
} }
pub trait EnumUnitVariants { pub trait EnumUnitVariants: Sized {
type UnitVariants; type UnitVariants: Into<(Option<Namespace<'static>>, &'static str)> + From<Self> + FromStr;
} }