rustical_dav/resource/
mod.rs1use crate::Principal;
2use crate::privileges::UserPrivilegeSet;
3use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper};
4use crate::xml::{PropElement, PropfindType, Resourcetype};
5use crate::xml::{TagList, multistatus::ResponseElement};
6use headers::{ETag, IfMatch, IfNoneMatch};
7use http::StatusCode;
8use itertools::Itertools;
9use quick_xml::name::Namespace;
10pub use resource_service::ResourceService;
11use rustical_xml::{EnumVariants, NamespaceOwned, PropName, XmlDeserialize, XmlSerialize};
12use std::collections::HashSet;
13use std::str::FromStr;
14
15mod axum_methods;
16mod axum_service;
17mod methods;
18mod principal_uri;
19mod resource_service;
20
21pub use axum_methods::{AxumMethods, MethodFunction};
22pub use axum_service::AxumService;
23pub use principal_uri::PrincipalUri;
24
25pub trait ResourceProp: XmlSerialize + XmlDeserialize {}
26impl<T: XmlSerialize + XmlDeserialize> ResourceProp for T {}
27
28pub trait ResourcePropName: FromStr {}
29impl<T: FromStr> ResourcePropName for T {}
30
31pub trait ResourceName {
32 fn get_name(&self) -> String;
33}
34
35pub trait Resource: Clone + Send + 'static {
36 type Prop: ResourceProp + PartialEq + Clone + EnumVariants + PropName + Send;
37 type Error: From<crate::Error>;
38 type Principal: Principal;
39
40 fn is_collection(&self) -> bool;
41
42 fn get_resourcetype(&self) -> Resourcetype;
43
44 fn list_props() -> Vec<(Option<Namespace<'static>>, &'static str)> {
45 Self::Prop::variant_names()
46 }
47
48 fn get_prop(
49 &self,
50 principal_uri: &impl PrincipalUri,
51 principal: &Self::Principal,
52 prop: &<Self::Prop as PropName>::Names,
53 ) -> Result<Self::Prop, Self::Error>;
54
55 fn set_prop(&mut self, _prop: Self::Prop) -> Result<(), crate::Error> {
56 Err(crate::Error::PropReadOnly)
57 }
58
59 fn remove_prop(&mut self, _prop: &<Self::Prop as PropName>::Names) -> Result<(), crate::Error> {
60 Err(crate::Error::PropReadOnly)
61 }
62
63 fn get_displayname(&self) -> Option<&str>;
64 fn set_displayname(&mut self, _name: Option<String>) -> Result<(), crate::Error> {
65 Err(crate::Error::PropReadOnly)
66 }
67
68 fn get_owner(&self) -> Option<&str> {
69 None
70 }
71
72 fn get_etag(&self) -> Option<String> {
73 None
74 }
75
76 fn satisfies_if_match(&self, if_match: &IfMatch) -> bool {
77 if let Some(etag) = self.get_etag() {
78 if let Ok(etag) = ETag::from_str(&etag) {
79 if_match.precondition_passes(&etag)
80 } else {
81 if_match.is_any()
82 }
83 } else {
84 if_match.is_any()
85 }
86 }
87
88 fn satisfies_if_none_match(&self, if_none_match: &IfNoneMatch) -> bool {
89 if let Some(etag) = self.get_etag() {
90 if let Ok(etag) = ETag::from_str(&etag) {
91 if_none_match.precondition_passes(&etag)
92 } else {
93 if_none_match != &IfNoneMatch::any()
94 }
95 } else {
96 if_none_match != &IfNoneMatch::any()
97 }
98 }
99
100 fn get_user_privileges(
101 &self,
102 principal: &Self::Principal,
103 ) -> Result<UserPrivilegeSet, Self::Error>;
104
105 fn propfind(
106 &self,
107 path: &str,
108 prop: &PropfindType<<Self::Prop as PropName>::Names>,
109 principal_uri: &impl PrincipalUri,
110 principal: &Self::Principal,
111 ) -> Result<ResponseElement<Self::Prop>, Self::Error> {
112 let mut path = path.to_string();
114 if self.is_collection() && !path.ends_with('/') {
115 path.push('/');
116 }
117
118 let (props, invalid_props): (HashSet<<Self::Prop as PropName>::Names>, Vec<_>) = match prop
120 {
121 PropfindType::Propname => {
122 let props = Self::list_props()
123 .into_iter()
124 .map(|(ns, tag)| (ns.map(NamespaceOwned::from), tag.to_string()))
125 .collect_vec();
126
127 return Ok(ResponseElement {
128 href: path.to_owned(),
129 propstat: vec![PropstatWrapper::TagList(PropstatElement {
130 prop: TagList::from(props),
131 status: StatusCode::OK,
132 })],
133 ..Default::default()
134 });
135 }
136 PropfindType::Allprop => (
137 Self::list_props()
138 .iter()
139 .map(|(_ns, name)| <Self::Prop as PropName>::Names::from_str(name).unwrap())
140 .collect(),
141 vec![],
142 ),
143 PropfindType::Prop(PropElement(valid_tags, invalid_tags)) => (
144 valid_tags.iter().cloned().collect(),
145 invalid_tags.to_owned(),
146 ),
147 };
148
149 let prop_responses = props
150 .into_iter()
151 .map(|prop| self.get_prop(principal_uri, principal, &prop))
152 .collect::<Result<Vec<_>, Self::Error>>()?;
153
154 let mut propstats = vec![PropstatWrapper::Normal(PropstatElement {
155 status: StatusCode::OK,
156 prop: PropTagWrapper(prop_responses),
157 })];
158 if !invalid_props.is_empty() {
159 propstats.push(PropstatWrapper::TagList(PropstatElement {
160 status: StatusCode::NOT_FOUND,
161 prop: invalid_props.into(),
162 }));
163 }
164 Ok(ResponseElement {
165 href: path.to_owned(),
166 propstat: propstats,
167 ..Default::default()
168 })
169 }
170}