Skip to main content

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