mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 19:32:29 +00:00
Add explicit error type to propfind resources
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use crate::Error;
|
||||
use actix_web::{web::Data, HttpRequest};
|
||||
use anyhow::{anyhow, Result};
|
||||
use anyhow::anyhow;
|
||||
use async_trait::async_trait;
|
||||
use rustical_auth::AuthInfo;
|
||||
use rustical_dav::error::Error;
|
||||
use rustical_dav::resource::{Resource, ResourceService};
|
||||
use rustical_dav::xml_snippets::{HrefElement, TextNode};
|
||||
use rustical_store::calendar::Calendar;
|
||||
@@ -166,8 +166,13 @@ pub struct CalendarFile {
|
||||
impl Resource for CalendarFile {
|
||||
type PropType = CalendarProp;
|
||||
type PropResponse = CalendarPropResponse;
|
||||
type Error = Error;
|
||||
|
||||
fn get_prop(&self, prefix: &str, prop: Self::PropType) -> Result<Self::PropResponse> {
|
||||
fn get_prop(
|
||||
&self,
|
||||
prefix: &str,
|
||||
prop: Self::PropType,
|
||||
) -> Result<Self::PropResponse, Self::Error> {
|
||||
match prop {
|
||||
CalendarProp::Resourcetype => {
|
||||
Ok(CalendarPropResponse::Resourcetype(Resourcetype::default()))
|
||||
@@ -220,8 +225,9 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
|
||||
type MemberType = CalendarFile;
|
||||
type PathComponents = (String, String); // principal, calendar_id
|
||||
type File = CalendarFile;
|
||||
type Error = Error;
|
||||
|
||||
async fn get_file(&self) -> Result<Self::File> {
|
||||
async fn get_file(&self) -> Result<Self::File, Error> {
|
||||
let calendar = self
|
||||
.cal_store
|
||||
.read()
|
||||
@@ -236,7 +242,10 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_members(&self, _auth_info: AuthInfo) -> Result<Vec<Self::MemberType>> {
|
||||
async fn get_members(
|
||||
&self,
|
||||
_auth_info: AuthInfo,
|
||||
) -> Result<Vec<Self::MemberType>, Self::Error> {
|
||||
// As of now the calendar resource has no members since events are shown with REPORT
|
||||
Ok(vec![])
|
||||
}
|
||||
@@ -245,7 +254,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
|
||||
req: HttpRequest,
|
||||
auth_info: AuthInfo,
|
||||
path_components: Self::PathComponents,
|
||||
) -> Result<Self, rustical_dav::error::Error> {
|
||||
) -> Result<Self, Self::Error> {
|
||||
let cal_store = req
|
||||
.app_data::<Data<RwLock<C>>>()
|
||||
.ok_or(anyhow!("no calendar store in app_data!"))?
|
||||
|
||||
@@ -5,6 +5,9 @@ pub enum Error {
|
||||
#[error("Unauthorized")]
|
||||
Unauthorized,
|
||||
|
||||
#[error("Not Found")]
|
||||
NotFound,
|
||||
|
||||
#[error("Not implemented")]
|
||||
NotImplemented,
|
||||
|
||||
@@ -33,6 +36,7 @@ impl actix_web::ResponseError for Error {
|
||||
Error::XmlDecodeError(_) => StatusCode::BAD_REQUEST,
|
||||
Error::NotImplemented => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Error::Other(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Error::NotFound => StatusCode::NOT_FOUND,
|
||||
}
|
||||
}
|
||||
fn error_response(&self) -> actix_web::HttpResponse<actix_web::body::BoxBody> {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::Error;
|
||||
use actix_web::{web::Data, HttpRequest};
|
||||
use anyhow::{anyhow, Result};
|
||||
use anyhow::anyhow;
|
||||
use async_trait::async_trait;
|
||||
use rustical_auth::AuthInfo;
|
||||
use rustical_dav::error::Error;
|
||||
use rustical_dav::resource::{Resource, ResourceService};
|
||||
use rustical_dav::xml_snippets::TextNode;
|
||||
use rustical_store::event::Event;
|
||||
@@ -43,12 +43,17 @@ pub struct EventFile {
|
||||
impl Resource for EventFile {
|
||||
type PropType = EventProp;
|
||||
type PropResponse = PrincipalPropResponse;
|
||||
type Error = Error;
|
||||
|
||||
fn get_path(&self) -> &str {
|
||||
&self.path
|
||||
}
|
||||
|
||||
fn get_prop(&self, _prefix: &str, prop: Self::PropType) -> Result<Self::PropResponse> {
|
||||
fn get_prop(
|
||||
&self,
|
||||
_prefix: &str,
|
||||
prop: Self::PropType,
|
||||
) -> Result<Self::PropResponse, Self::Error> {
|
||||
match prop {
|
||||
EventProp::Getetag => Ok(PrincipalPropResponse::Getetag(TextNode(Some(
|
||||
self.event.get_etag(),
|
||||
@@ -68,8 +73,12 @@ impl<C: CalendarStore + ?Sized> ResourceService for EventResource<C> {
|
||||
type PathComponents = (String, String, String); // principal, calendar, event
|
||||
type File = EventFile;
|
||||
type MemberType = EventFile;
|
||||
type Error = Error;
|
||||
|
||||
async fn get_members(&self, _auth_info: AuthInfo) -> Result<Vec<Self::MemberType>> {
|
||||
async fn get_members(
|
||||
&self,
|
||||
_auth_info: AuthInfo,
|
||||
) -> Result<Vec<Self::MemberType>, Self::Error> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
@@ -77,7 +86,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for EventResource<C> {
|
||||
req: HttpRequest,
|
||||
_auth_info: AuthInfo,
|
||||
path_components: Self::PathComponents,
|
||||
) -> Result<Self, Error> {
|
||||
) -> Result<Self, Self::Error> {
|
||||
let (_principal, cid, uid) = path_components;
|
||||
|
||||
let cal_store = req
|
||||
@@ -94,7 +103,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for EventResource<C> {
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_file(&self) -> Result<Self::File> {
|
||||
async fn get_file(&self) -> Result<Self::File, Self::Error> {
|
||||
let event = self
|
||||
.cal_store
|
||||
.read()
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::Error;
|
||||
use actix_web::web::Data;
|
||||
use actix_web::HttpRequest;
|
||||
use anyhow::{anyhow, Result};
|
||||
use anyhow::anyhow;
|
||||
use async_trait::async_trait;
|
||||
use rustical_auth::AuthInfo;
|
||||
use rustical_dav::resource::{Resource, ResourceService};
|
||||
use rustical_dav::xml_snippets::HrefElement;
|
||||
use rustical_store::CalendarStore;
|
||||
use serde::Serialize;
|
||||
use std::sync::Arc;
|
||||
use strum::{EnumString, IntoStaticStr, VariantNames};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
@@ -60,8 +60,13 @@ pub enum PrincipalProp {
|
||||
impl Resource for PrincipalFile {
|
||||
type PropType = PrincipalProp;
|
||||
type PropResponse = PrincipalPropResponse;
|
||||
type Error = Error;
|
||||
|
||||
fn get_prop(&self, prefix: &str, prop: Self::PropType) -> Result<Self::PropResponse> {
|
||||
fn get_prop(
|
||||
&self,
|
||||
prefix: &str,
|
||||
prop: Self::PropType,
|
||||
) -> Result<Self::PropResponse, Self::Error> {
|
||||
match prop {
|
||||
PrincipalProp::Resourcetype => {
|
||||
Ok(PrincipalPropResponse::Resourcetype(Resourcetype::default()))
|
||||
@@ -93,14 +98,15 @@ impl<C: CalendarStore + ?Sized> ResourceService for PrincipalResource<C> {
|
||||
type PathComponents = (String,);
|
||||
type MemberType = CalendarFile;
|
||||
type File = PrincipalFile;
|
||||
type Error = Error;
|
||||
|
||||
async fn new(
|
||||
req: HttpRequest,
|
||||
auth_info: AuthInfo,
|
||||
(principal,): Self::PathComponents,
|
||||
) -> Result<Self, rustical_dav::error::Error> {
|
||||
) -> Result<Self, Self::Error> {
|
||||
if auth_info.user_id != principal {
|
||||
return Err(rustical_dav::error::Error::Unauthorized);
|
||||
return Err(Error::Unauthorized);
|
||||
}
|
||||
let cal_store = req
|
||||
.app_data::<Data<RwLock<C>>>()
|
||||
@@ -115,14 +121,17 @@ impl<C: CalendarStore + ?Sized> ResourceService for PrincipalResource<C> {
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_file(&self) -> Result<Self::File> {
|
||||
async fn get_file(&self) -> Result<Self::File, Self::Error> {
|
||||
Ok(PrincipalFile {
|
||||
principal: self.principal.to_owned(),
|
||||
path: self.path.to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_members(&self, _auth_info: AuthInfo) -> Result<Vec<Self::MemberType>> {
|
||||
async fn get_members(
|
||||
&self,
|
||||
_auth_info: AuthInfo,
|
||||
) -> Result<Vec<Self::MemberType>, Self::Error> {
|
||||
let calendars = self
|
||||
.cal_store
|
||||
.read()
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::Error;
|
||||
use actix_web::HttpRequest;
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use rustical_auth::AuthInfo;
|
||||
use rustical_dav::error::Error;
|
||||
use rustical_dav::resource::{Resource, ResourceService};
|
||||
use rustical_dav::xml_snippets::HrefElement;
|
||||
use serde::Serialize;
|
||||
@@ -41,8 +41,13 @@ pub struct RootFile {
|
||||
impl Resource for RootFile {
|
||||
type PropType = RootProp;
|
||||
type PropResponse = RootPropResponse;
|
||||
type Error = Error;
|
||||
|
||||
fn get_prop(&self, prefix: &str, prop: Self::PropType) -> Result<Self::PropResponse> {
|
||||
fn get_prop(
|
||||
&self,
|
||||
prefix: &str,
|
||||
prop: Self::PropType,
|
||||
) -> Result<Self::PropResponse, Self::Error> {
|
||||
match prop {
|
||||
RootProp::Resourcetype => Ok(RootPropResponse::Resourcetype(Resourcetype::default())),
|
||||
RootProp::CurrentUserPrincipal => Ok(RootPropResponse::CurrentUserPrincipal(
|
||||
@@ -61,8 +66,12 @@ impl ResourceService for RootResource {
|
||||
type PathComponents = ();
|
||||
type MemberType = RootFile;
|
||||
type File = RootFile;
|
||||
type Error = Error;
|
||||
|
||||
async fn get_members(&self, _auth_info: AuthInfo) -> Result<Vec<Self::MemberType>> {
|
||||
async fn get_members(
|
||||
&self,
|
||||
_auth_info: AuthInfo,
|
||||
) -> Result<Vec<Self::MemberType>, Self::Error> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
@@ -70,14 +79,14 @@ impl ResourceService for RootResource {
|
||||
req: HttpRequest,
|
||||
auth_info: AuthInfo,
|
||||
_path_components: Self::PathComponents,
|
||||
) -> Result<Self, Error> {
|
||||
) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
principal: auth_info.user_id,
|
||||
path: req.path().to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn get_file(&self) -> Result<Self::File> {
|
||||
async fn get_file(&self) -> Result<Self::File, Self::Error> {
|
||||
Ok(RootFile {
|
||||
path: self.path.to_owned(),
|
||||
principal: self.principal.to_owned(),
|
||||
|
||||
Reference in New Issue
Block a user