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    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 import() -> Option<MethodFunction<Self>> {
43        None
44    }
45
46    #[inline]
47    fn allow_header() -> Allow {
48        let mut allow = vec![
49            Method::from_str("PROPFIND").unwrap(),
50            Method::from_str("PROPPATCH").unwrap(),
51            Method::from_str("COPY").unwrap(),
52            Method::from_str("MOVE").unwrap(),
53            Method::DELETE,
54            Method::OPTIONS,
55        ];
56        if Self::report().is_some() {
57            allow.push(Method::from_str("REPORT").unwrap());
58        }
59        if Self::get().is_some() {
60            allow.push(Method::GET);
61            allow.push(Method::HEAD);
62        }
63        if Self::post().is_some() {
64            allow.push(Method::POST);
65        }
66        if Self::mkcol().is_some() {
67            allow.push(Method::from_str("MKCOL").unwrap());
68        }
69        if Self::mkcalendar().is_some() {
70            allow.push(Method::from_str("MKCALENDAR").unwrap());
71        }
72        if Self::put().is_some() {
73            allow.push(Method::PUT);
74        }
75        if Self::import().is_some() {
76            allow.push(Method::from_str("IMPORT").unwrap());
77        }
78
79        allow.into_iter().collect()
80    }
81}