rustical_dav/resource/
axum_methods.rs1use axum::{extract::Request, response::Response};
2use futures_util::future::BoxFuture;
3use headers::Allow;
4use http::Method;
5use std::{convert::Infallible, str::FromStr};
6
7pub type MethodFunction<State> =
8 fn(State, Request) -> BoxFuture<'static, Result<Response, Infallible>>;
9
10pub trait AxumMethods: Sized + Send + Sync + 'static {
11 #[inline]
12 fn report() -> Option<MethodFunction<Self>> {
13 None
14 }
15
16 #[inline]
17 fn get() -> Option<MethodFunction<Self>> {
18 None
19 }
20
21 #[inline]
22 fn post() -> Option<MethodFunction<Self>> {
23 None
24 }
25
26 #[inline]
27 fn mkcol() -> Option<MethodFunction<Self>> {
28 None
29 }
30
31 #[inline]
32 fn mkcalendar() -> Option<MethodFunction<Self>> {
33 None
34 }
35
36 #[inline]
37 fn put() -> Option<MethodFunction<Self>> {
38 None
39 }
40
41 #[inline]
42 fn allow_header() -> Allow {
43 let mut allow = vec![
44 Method::from_str("PROPFIND").unwrap(),
45 Method::from_str("PROPPATCH").unwrap(),
46 Method::from_str("COPY").unwrap(),
47 Method::from_str("MOVE").unwrap(),
48 Method::DELETE,
49 Method::OPTIONS,
50 ];
51 if Self::report().is_some() {
52 allow.push(Method::from_str("REPORT").unwrap());
53 }
54 if Self::get().is_some() {
55 allow.push(Method::GET);
56 allow.push(Method::HEAD);
57 }
58 if Self::post().is_some() {
59 allow.push(Method::POST);
60 }
61 if Self::mkcol().is_some() {
62 allow.push(Method::from_str("MKCOL").unwrap());
63 }
64 if Self::mkcalendar().is_some() {
65 allow.push(Method::from_str("MKCALENDAR").unwrap());
66 }
67 if Self::put().is_some() {
68 allow.push(Method::PUT);
69 }
70
71 allow.into_iter().collect()
72 }
73}