rustical_dav/extensions/
common.rs

1use crate::{
2    Principal,
3    privileges::UserPrivilegeSet,
4    resource::{PrincipalUri, Resource},
5    xml::{HrefElement, Resourcetype},
6};
7use rustical_xml::{EnumVariants, PropName, XmlDeserialize, XmlSerialize};
8
9#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, PropName, EnumVariants)]
10#[xml(unit_variants_ident = "CommonPropertiesPropName")]
11pub enum CommonPropertiesProp {
12    // WebDAV (RFC 2518)
13    #[xml(skip_deserializing)]
14    #[xml(ns = "crate::namespace::NS_DAV")]
15    Resourcetype(Resourcetype),
16    #[xml(ns = "crate::namespace::NS_DAV")]
17    Displayname(Option<String>),
18
19    // WebDAV Current Principal Extension (RFC 5397)
20    #[xml(ns = "crate::namespace::NS_DAV")]
21    CurrentUserPrincipal(HrefElement),
22
23    // WebDAV Access Control Protocol (RFC 3477)
24    #[xml(skip_deserializing)]
25    #[xml(ns = "crate::namespace::NS_DAV")]
26    CurrentUserPrivilegeSet(UserPrivilegeSet),
27    #[xml(ns = "crate::namespace::NS_DAV")]
28    Owner(Option<HrefElement>),
29}
30
31pub trait CommonPropertiesExtension: Resource {
32    fn get_prop(
33        &self,
34        principal_uri: &impl PrincipalUri,
35        principal: &Self::Principal,
36        prop: &CommonPropertiesPropName,
37    ) -> Result<CommonPropertiesProp, <Self as Resource>::Error> {
38        Ok(match prop {
39            CommonPropertiesPropName::Resourcetype => {
40                CommonPropertiesProp::Resourcetype(self.get_resourcetype())
41            }
42            CommonPropertiesPropName::Displayname => {
43                CommonPropertiesProp::Displayname(self.get_displayname().map(|s| s.to_string()))
44            }
45            CommonPropertiesPropName::CurrentUserPrincipal => {
46                CommonPropertiesProp::CurrentUserPrincipal(
47                    principal_uri.principal_uri(principal.get_id()).into(),
48                )
49            }
50            CommonPropertiesPropName::CurrentUserPrivilegeSet => {
51                CommonPropertiesProp::CurrentUserPrivilegeSet(self.get_user_privileges(principal)?)
52            }
53            CommonPropertiesPropName::Owner => CommonPropertiesProp::Owner(
54                self.get_owner()
55                    .map(|owner| principal_uri.principal_uri(owner).into()),
56            ),
57        })
58    }
59
60    fn set_prop(&mut self, prop: CommonPropertiesProp) -> Result<(), crate::Error> {
61        match prop {
62            CommonPropertiesProp::Displayname(name) => self.set_displayname(name),
63            _ => Err(crate::Error::PropReadOnly),
64        }
65    }
66
67    fn remove_prop(&mut self, prop: &CommonPropertiesPropName) -> Result<(), crate::Error> {
68        match prop {
69            CommonPropertiesPropName::Displayname => self.set_displayname(None),
70            _ => Err(crate::Error::PropReadOnly),
71        }
72    }
73}
74
75impl<R: Resource> CommonPropertiesExtension for R {}