rustical_dav/resource/
resource_service.rs

1use super::{PrincipalUri, Resource};
2use crate::Principal;
3use crate::resource::{AxumMethods, AxumService};
4use async_trait::async_trait;
5use axum::Router;
6use axum::extract::FromRequestParts;
7use axum::response::IntoResponse;
8use serde::Deserialize;
9
10#[async_trait]
11pub trait ResourceService: Clone + Sized + Send + Sync + AxumMethods + 'static {
12    type PathComponents: std::fmt::Debug
13        + for<'de> Deserialize<'de>
14        + Sized
15        + Send
16        + Sync
17        + Clone
18        + 'static; // defines how the resource URI maps to parameters, i.e. /{principal}/{calendar} -> (String, String)
19    type MemberType: Resource<Error = Self::Error, Principal = Self::Principal>
20        + super::ResourceName;
21    type Resource: Resource<Error = Self::Error, Principal = Self::Principal>;
22    type Error: From<crate::Error> + Send + Sync + IntoResponse + 'static;
23    type Principal: Principal + FromRequestParts<Self>;
24    type PrincipalUri: PrincipalUri;
25
26    const DAV_HEADER: &'static str;
27
28    async fn get_members(
29        &self,
30        _path: &Self::PathComponents,
31    ) -> Result<Vec<Self::MemberType>, Self::Error> {
32        Ok(vec![])
33    }
34
35    async fn get_resource(
36        &self,
37        path: &Self::PathComponents,
38        show_deleted: bool,
39    ) -> Result<Self::Resource, Self::Error>;
40
41    async fn save_resource(
42        &self,
43        _path: &Self::PathComponents,
44        _file: Self::Resource,
45    ) -> Result<(), Self::Error> {
46        Err(crate::Error::Unauthorized.into())
47    }
48
49    async fn delete_resource(
50        &self,
51        _path: &Self::PathComponents,
52        _use_trashbin: bool,
53    ) -> Result<(), Self::Error> {
54        Err(crate::Error::Unauthorized.into())
55    }
56
57    // Returns whether an existing resource was overwritten
58    async fn copy_resource(
59        &self,
60        _path: &Self::PathComponents,
61        _destination: &Self::PathComponents,
62        _user: &Self::Principal,
63        _overwrite: bool,
64    ) -> Result<bool, Self::Error> {
65        Err(crate::Error::Forbidden.into())
66    }
67
68    // Returns whether an existing resource was overwritten
69    async fn move_resource(
70        &self,
71        _path: &Self::PathComponents,
72        _destination: &Self::PathComponents,
73        _user: &Self::Principal,
74        _overwrite: bool,
75    ) -> Result<bool, Self::Error> {
76        Err(crate::Error::Forbidden.into())
77    }
78
79    fn axum_service(self) -> AxumService<Self>
80    where
81        Self: AxumMethods,
82    {
83        AxumService::new(self)
84    }
85
86    fn axum_router<S: Send + Sync + Clone + 'static>(self) -> Router<S> {
87        Router::new().route_service("/", self.axum_service())
88    }
89}