diff --git a/crates/caldav/src/calendar/resource.rs b/crates/caldav/src/calendar/resource.rs index baa3edf..9478e77 100644 --- a/crates/caldav/src/calendar/resource.rs +++ b/crates/caldav/src/calendar/resource.rs @@ -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 { + fn get_prop( + &self, + prefix: &str, + prop: Self::PropType, + ) -> Result { match prop { CalendarProp::Resourcetype => { Ok(CalendarPropResponse::Resourcetype(Resourcetype::default())) @@ -220,8 +225,9 @@ impl ResourceService for CalendarResource { type MemberType = CalendarFile; type PathComponents = (String, String); // principal, calendar_id type File = CalendarFile; + type Error = Error; - async fn get_file(&self) -> Result { + async fn get_file(&self) -> Result { let calendar = self .cal_store .read() @@ -236,7 +242,10 @@ impl ResourceService for CalendarResource { }) } - async fn get_members(&self, _auth_info: AuthInfo) -> Result> { + async fn get_members( + &self, + _auth_info: AuthInfo, + ) -> Result, Self::Error> { // As of now the calendar resource has no members since events are shown with REPORT Ok(vec![]) } @@ -245,7 +254,7 @@ impl ResourceService for CalendarResource { req: HttpRequest, auth_info: AuthInfo, path_components: Self::PathComponents, - ) -> Result { + ) -> Result { let cal_store = req .app_data::>>() .ok_or(anyhow!("no calendar store in app_data!"))? diff --git a/crates/caldav/src/error.rs b/crates/caldav/src/error.rs index 5afaefe..93c24eb 100644 --- a/crates/caldav/src/error.rs +++ b/crates/caldav/src/error.rs @@ -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 { diff --git a/crates/caldav/src/event/resource.rs b/crates/caldav/src/event/resource.rs index 20124ac..4a66733 100644 --- a/crates/caldav/src/event/resource.rs +++ b/crates/caldav/src/event/resource.rs @@ -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 { + fn get_prop( + &self, + _prefix: &str, + prop: Self::PropType, + ) -> Result { match prop { EventProp::Getetag => Ok(PrincipalPropResponse::Getetag(TextNode(Some( self.event.get_etag(), @@ -68,8 +73,12 @@ impl ResourceService for EventResource { 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> { + async fn get_members( + &self, + _auth_info: AuthInfo, + ) -> Result, Self::Error> { Ok(vec![]) } @@ -77,7 +86,7 @@ impl ResourceService for EventResource { req: HttpRequest, _auth_info: AuthInfo, path_components: Self::PathComponents, - ) -> Result { + ) -> Result { let (_principal, cid, uid) = path_components; let cal_store = req @@ -94,7 +103,7 @@ impl ResourceService for EventResource { }) } - async fn get_file(&self) -> Result { + async fn get_file(&self) -> Result { let event = self .cal_store .read() diff --git a/crates/caldav/src/principal/mod.rs b/crates/caldav/src/principal/mod.rs index dae4280..ceeec4b 100644 --- a/crates/caldav/src/principal/mod.rs +++ b/crates/caldav/src/principal/mod.rs @@ -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 { + fn get_prop( + &self, + prefix: &str, + prop: Self::PropType, + ) -> Result { match prop { PrincipalProp::Resourcetype => { Ok(PrincipalPropResponse::Resourcetype(Resourcetype::default())) @@ -93,14 +98,15 @@ impl ResourceService for PrincipalResource { type PathComponents = (String,); type MemberType = CalendarFile; type File = PrincipalFile; + type Error = Error; async fn new( req: HttpRequest, auth_info: AuthInfo, (principal,): Self::PathComponents, - ) -> Result { + ) -> Result { if auth_info.user_id != principal { - return Err(rustical_dav::error::Error::Unauthorized); + return Err(Error::Unauthorized); } let cal_store = req .app_data::>>() @@ -115,14 +121,17 @@ impl ResourceService for PrincipalResource { }) } - async fn get_file(&self) -> Result { + async fn get_file(&self) -> Result { Ok(PrincipalFile { principal: self.principal.to_owned(), path: self.path.to_owned(), }) } - async fn get_members(&self, _auth_info: AuthInfo) -> Result> { + async fn get_members( + &self, + _auth_info: AuthInfo, + ) -> Result, Self::Error> { let calendars = self .cal_store .read() diff --git a/crates/caldav/src/root/mod.rs b/crates/caldav/src/root/mod.rs index be5b786..96ce499 100644 --- a/crates/caldav/src/root/mod.rs +++ b/crates/caldav/src/root/mod.rs @@ -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 { + fn get_prop( + &self, + prefix: &str, + prop: Self::PropType, + ) -> Result { 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> { + async fn get_members( + &self, + _auth_info: AuthInfo, + ) -> Result, Self::Error> { Ok(vec![]) } @@ -70,14 +79,14 @@ impl ResourceService for RootResource { req: HttpRequest, auth_info: AuthInfo, _path_components: Self::PathComponents, - ) -> Result { + ) -> Result { Ok(Self { principal: auth_info.user_id, path: req.path().to_string(), }) } - async fn get_file(&self) -> Result { + async fn get_file(&self) -> Result { Ok(RootFile { path: self.path.to_owned(), principal: self.principal.to_owned(), diff --git a/crates/dav/src/error.rs b/crates/dav/src/error.rs index a738edf..fb5e571 100644 --- a/crates/dav/src/error.rs +++ b/crates/dav/src/error.rs @@ -13,6 +13,8 @@ pub enum Error { Unauthorized, #[error("Internal server error :(")] InternalError, + #[error(transparent)] + XmlDecodeError(#[from] quick_xml::DeError), #[error("Internal server error")] Other(#[from] anyhow::Error), } @@ -25,6 +27,7 @@ impl actix_web::error::ResponseError for Error { Self::NotFound => StatusCode::NOT_FOUND, Self::BadRequest => StatusCode::BAD_REQUEST, Self::Unauthorized => StatusCode::UNAUTHORIZED, + Self::XmlDecodeError(_) => StatusCode::BAD_REQUEST, } } diff --git a/crates/dav/src/propfind.rs b/crates/dav/src/propfind.rs index 34845ec..641caf3 100644 --- a/crates/dav/src/propfind.rs +++ b/crates/dav/src/propfind.rs @@ -3,6 +3,7 @@ use crate::namespace::Namespace; use crate::resource::HandlePropfind; use crate::resource::ResourceService; use crate::xml::tag_list::TagList; +use crate::Error; use actix_web::http::header::ContentType; use actix_web::web::{Data, Path}; use actix_web::{HttpRequest, HttpResponse}; @@ -58,21 +59,22 @@ pub async fn route_propfind prefix: Data, auth: AuthInfoExtractor, depth: Depth, -) -> Result { +) -> Result { let auth_info = auth.inner; let prefix = prefix.0.to_owned(); let path_components = path.into_inner(); let resource_service = R::new(req, auth_info.clone(), path_components.clone()).await?; - let propfind: PropfindElement = quick_xml::de::from_str(&body).unwrap(); + let propfind: PropfindElement = + quick_xml::de::from_str(&body).map_err(Error::XmlDecodeError)?; let props = match propfind.prop { PropfindType::Allprop => { vec!["allprop".to_owned()] } PropfindType::Propname => { // TODO: Implement - return Err(crate::error::Error::InternalError); + return Err(Error::InternalError.into()); } PropfindType::Prop(PropElement { prop: prop_tags }) => prop_tags.into(), }; diff --git a/crates/dav/src/resource.rs b/crates/dav/src/resource.rs index d0d353d..b9d4bdf 100644 --- a/crates/dav/src/resource.rs +++ b/crates/dav/src/resource.rs @@ -1,6 +1,6 @@ -use crate::{error::Error, xml::tag_list::TagList}; -use actix_web::{http::StatusCode, HttpRequest}; -use anyhow::{anyhow, Result}; +use crate::xml::tag_list::TagList; +use actix_web::{http::StatusCode, HttpRequest, ResponseError}; +use anyhow::anyhow; use async_trait::async_trait; use itertools::Itertools; use rustical_auth::AuthInfo; @@ -12,12 +12,17 @@ use strum::VariantNames; pub trait Resource { type PropType: FromStr + VariantNames + Clone; type PropResponse: Serialize; + type Error: ResponseError + From + From; fn list_dead_props() -> &'static [&'static str] { Self::PropType::VARIANTS } - fn get_prop(&self, prefix: &str, prop: Self::PropType) -> Result; + fn get_prop( + &self, + prefix: &str, + prop: Self::PropType, + ) -> Result; fn get_path(&self) -> &str; } @@ -28,19 +33,20 @@ pub trait Resource { // A resource exists #[async_trait(?Send)] pub trait ResourceService: Sized { - type MemberType: Resource; + type MemberType: Resource; type PathComponents: Sized + Clone; // defines how the resource URI maps to parameters, i.e. /{principal}/{calendar} -> (String, String) - type File: Resource; + type File: Resource; + type Error: ResponseError + From + From; async fn new( req: HttpRequest, auth_info: AuthInfo, path_components: Self::PathComponents, - ) -> Result; + ) -> Result; - async fn get_file(&self) -> Result; + async fn get_file(&self) -> Result; - async fn get_members(&self, auth_info: AuthInfo) -> Result>; + async fn get_members(&self, auth_info: AuthInfo) -> Result, Self::Error>; } #[derive(Serialize)] @@ -72,21 +78,26 @@ enum PropstatType { #[async_trait(?Send)] pub trait HandlePropfind { - async fn propfind(&self, prefix: &str, props: Vec<&str>) -> Result; + type Error: ResponseError + From + From; + + async fn propfind(&self, prefix: &str, props: Vec<&str>) + -> Result; } #[async_trait(?Send)] impl HandlePropfind for R { + type Error = R::Error; + async fn propfind( &self, prefix: &str, props: Vec<&str>, - ) -> Result>, TagList>> { + ) -> Result>, TagList>, R::Error> { let mut props = props; if props.contains(&"propname") { if props.len() != 1 { // propname MUST be the only queried prop per spec - return Err(anyhow!("propname MUST be the only queried prop")); + return Err(anyhow!("propname MUST be the only queried prop").into()); } // TODO: implement propname props = R::list_dead_props().into(); @@ -94,7 +105,7 @@ impl HandlePropfind for R { if props.contains(&"allprop") { if props.len() != 1 { // allprop MUST be the only queried prop per spec - return Err(anyhow!("allprop MUST be the only queried prop")); + return Err(anyhow!("allprop MUST be the only queried prop").into()); } props = R::list_dead_props().into(); }