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