rustical_dav/extensions/
common.rs1use crate::{
2 Principal,
3 privileges::UserPrivilegeSet,
4 resource::{NamedRoute, Resource},
5 xml::{HrefElement, Resourcetype},
6};
7use actix_web::dev::ResourceMap;
8use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
9
10#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumUnitVariants, EnumVariants)]
11#[xml(unit_variants_ident = "CommonPropertiesPropName")]
12pub enum CommonPropertiesProp {
13 #[xml(skip_deserializing)]
15 #[xml(ns = "crate::namespace::NS_DAV")]
16 Resourcetype(Resourcetype),
17
18 #[xml(ns = "crate::namespace::NS_DAV")]
20 CurrentUserPrincipal(HrefElement),
21
22 #[xml(skip_deserializing)]
24 #[xml(ns = "crate::namespace::NS_DAV")]
25 CurrentUserPrivilegeSet(UserPrivilegeSet),
26 #[xml(ns = "crate::namespace::NS_DAV")]
27 Owner(Option<HrefElement>),
28}
29
30pub trait CommonPropertiesExtension: Resource {
31 type PrincipalResource: NamedRoute;
32
33 fn get_prop(
34 &self,
35 rmap: &ResourceMap,
36 principal: &Self::Principal,
37 prop: &CommonPropertiesPropName,
38 ) -> Result<CommonPropertiesProp, <Self as Resource>::Error> {
39 Ok(match prop {
40 CommonPropertiesPropName::Resourcetype => {
41 CommonPropertiesProp::Resourcetype(self.get_resourcetype())
42 }
43 CommonPropertiesPropName::CurrentUserPrincipal => {
44 CommonPropertiesProp::CurrentUserPrincipal(
45 Self::PrincipalResource::get_url(rmap, [&principal.get_id()])
46 .unwrap()
47 .into(),
48 )
49 }
50 CommonPropertiesPropName::CurrentUserPrivilegeSet => {
51 CommonPropertiesProp::CurrentUserPrivilegeSet(self.get_user_privileges(principal)?)
52 }
53 CommonPropertiesPropName::Owner => {
54 CommonPropertiesProp::Owner(self.get_owner().map(|owner| {
55 Self::PrincipalResource::get_url(rmap, [owner])
56 .unwrap()
57 .into()
58 }))
59 }
60 })
61 }
62
63 fn set_prop(&self, _prop: CommonPropertiesProp) -> Result<(), crate::Error> {
64 Err(crate::Error::PropReadOnly)
65 }
66
67 fn remove_prop(&self, _prop: &CommonPropertiesPropName) -> Result<(), crate::Error> {
68 Err(crate::Error::PropReadOnly)
69 }
70}