mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 14:02:29 +00:00
Add explicit error type to propfind resources
This commit is contained in:
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<A: CheckAuthentication, R: ResourceService + ?Sized>
|
||||
prefix: Data<ServicePrefix>,
|
||||
auth: AuthInfoExtractor<A>,
|
||||
depth: Depth,
|
||||
) -> Result<HttpResponse, crate::error::Error> {
|
||||
) -> Result<HttpResponse, R::Error> {
|
||||
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(),
|
||||
};
|
||||
|
||||
@@ -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<crate::Error> + From<anyhow::Error>;
|
||||
|
||||
fn list_dead_props() -> &'static [&'static str] {
|
||||
Self::PropType::VARIANTS
|
||||
}
|
||||
|
||||
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>;
|
||||
|
||||
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<Error = Self::Error>;
|
||||
type PathComponents: Sized + Clone; // defines how the resource URI maps to parameters, i.e. /{principal}/{calendar} -> (String, String)
|
||||
type File: Resource;
|
||||
type File: Resource<Error = Self::Error>;
|
||||
type Error: ResponseError + From<crate::Error> + From<anyhow::Error>;
|
||||
|
||||
async fn new(
|
||||
req: HttpRequest,
|
||||
auth_info: AuthInfo,
|
||||
path_components: Self::PathComponents,
|
||||
) -> Result<Self, Error>;
|
||||
) -> Result<Self, Self::Error>;
|
||||
|
||||
async fn get_file(&self) -> Result<Self::File>;
|
||||
async fn get_file(&self) -> Result<Self::File, Self::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>;
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@@ -72,21 +78,26 @@ enum PropstatType<T1: Serialize, T2: Serialize> {
|
||||
|
||||
#[async_trait(?Send)]
|
||||
pub trait HandlePropfind {
|
||||
async fn propfind(&self, prefix: &str, props: Vec<&str>) -> Result<impl Serialize>;
|
||||
type Error: ResponseError + From<crate::Error> + From<anyhow::Error>;
|
||||
|
||||
async fn propfind(&self, prefix: &str, props: Vec<&str>)
|
||||
-> Result<impl Serialize, Self::Error>;
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl<R: Resource> HandlePropfind for R {
|
||||
type Error = R::Error;
|
||||
|
||||
async fn propfind(
|
||||
&self,
|
||||
prefix: &str,
|
||||
props: Vec<&str>,
|
||||
) -> Result<PropstatResponseElement<PropWrapper<Vec<R::PropResponse>>, TagList>> {
|
||||
) -> Result<PropstatResponseElement<PropWrapper<Vec<R::PropResponse>>, 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<R: Resource> 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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user