rustical_dav/resource/
axum_methods.rs

1use 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    #[must_use]
13    fn report() -> Option<MethodFunction<Self>> {
14        None
15    }
16
17    #[inline]
18    #[must_use]
19    fn get() -> Option<MethodFunction<Self>> {
20        None
21    }
22
23    #[inline]
24    #[must_use]
25    fn post() -> Option<MethodFunction<Self>> {
26        None
27    }
28
29    #[inline]
30    #[must_use]
31    fn mkcol() -> Option<MethodFunction<Self>> {
32        None
33    }
34
35    #[inline]
36    #[must_use]
37    fn mkcalendar() -> Option<MethodFunction<Self>> {
38        None
39    }
40
41    #[inline]
42    #[must_use]
43    fn put() -> Option<MethodFunction<Self>> {
44        None
45    }
46
47    #[inline]
48    #[must_use]
49    fn import() -> Option<MethodFunction<Self>> {
50        None
51    }
52
53    #[inline]
54    #[must_use]
55    fn allow_header() -> Allow {
56        let mut allow = vec![
57            Method::from_str("PROPFIND").unwrap(),
58            Method::from_str("PROPPATCH").unwrap(),
59            Method::from_str("COPY").unwrap(),
60            Method::from_str("MOVE").unwrap(),
61            Method::DELETE,
62            Method::OPTIONS,
63        ];
64        if Self::report().is_some() {
65            allow.push(Method::from_str("REPORT").unwrap());
66        }
67        if Self::get().is_some() {
68            allow.push(Method::GET);
69            allow.push(Method::HEAD);
70        }
71        if Self::post().is_some() {
72            allow.push(Method::POST);
73        }
74        if Self::mkcol().is_some() {
75            allow.push(Method::from_str("MKCOL").unwrap());
76        }
77        if Self::mkcalendar().is_some() {
78            allow.push(Method::from_str("MKCALENDAR").unwrap());
79        }
80        if Self::put().is_some() {
81            allow.push(Method::PUT);
82        }
83        if Self::import().is_some() {
84            allow.push(Method::from_str("IMPORT").unwrap());
85        }
86
87        allow.into_iter().collect()
88    }
89}