mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 07:02:24 +00:00
Some refactoring
This commit is contained in:
@@ -38,7 +38,7 @@ struct PropfindElement {
|
||||
}
|
||||
|
||||
pub async fn route_propfind<A: CheckAuthentication, R: ResourceService + ?Sized>(
|
||||
path: Path<R::PathComponents>,
|
||||
path_components: Path<R::PathComponents>,
|
||||
body: String,
|
||||
req: HttpRequest,
|
||||
prefix: Data<ServicePrefix>,
|
||||
@@ -48,9 +48,10 @@ pub async fn route_propfind<A: CheckAuthentication, R: ResourceService + ?Sized>
|
||||
debug!("{body}");
|
||||
let auth_info = auth.inner;
|
||||
let prefix = prefix.0.to_owned();
|
||||
let path_components = path.into_inner();
|
||||
let path_components = path_components.into_inner();
|
||||
let path = req.path().to_owned();
|
||||
|
||||
let resource_service = R::new(req, auth_info.clone(), path_components.clone()).await?;
|
||||
let resource_service = R::new(&req, &auth_info, path_components.clone()).await?;
|
||||
|
||||
// A request body is optional. If empty we MUST return all props
|
||||
let propfind: PropfindElement = if !body.is_empty() {
|
||||
@@ -75,13 +76,13 @@ pub async fn route_propfind<A: CheckAuthentication, R: ResourceService + ?Sized>
|
||||
|
||||
let mut member_responses = Vec::new();
|
||||
if depth != Depth::Zero {
|
||||
for member in resource_service.get_members(auth_info).await? {
|
||||
member_responses.push(member.propfind(&prefix, props.clone()).await?);
|
||||
for (path, member) in resource_service.get_members(auth_info).await? {
|
||||
member_responses.push(member.propfind(&prefix, path, props.clone()).await?);
|
||||
}
|
||||
}
|
||||
|
||||
let resource = resource_service.get_file().await?;
|
||||
let response = resource.propfind(&prefix, props).await?;
|
||||
let response = resource.propfind(&prefix, path, props).await?;
|
||||
|
||||
Ok(MultistatusElement {
|
||||
responses: vec![response],
|
||||
|
||||
@@ -59,7 +59,7 @@ pub async fn route_proppatch<A: CheckAuthentication, R: ResourceService + ?Sized
|
||||
let auth_info = auth.inner;
|
||||
let path_components = path.into_inner();
|
||||
let href = req.path().to_owned();
|
||||
let resource_service = R::new(req, auth_info.clone(), path_components.clone()).await?;
|
||||
let resource_service = R::new(&req, &auth_info, path_components.clone()).await?;
|
||||
|
||||
debug!("{body}");
|
||||
|
||||
|
||||
@@ -21,8 +21,6 @@ pub trait Resource: Clone {
|
||||
|
||||
fn get_prop(&self, prefix: &str, prop: Self::PropName) -> Result<Self::Prop, Self::Error>;
|
||||
|
||||
fn get_path(&self) -> &str;
|
||||
|
||||
fn set_prop(&mut self, _prop: Self::Prop) -> Result<(), crate::Error> {
|
||||
Err(crate::Error::PropReadOnly)
|
||||
}
|
||||
@@ -48,14 +46,19 @@ pub trait ResourceService: Sized {
|
||||
type Error: ResponseError + From<crate::Error> + From<anyhow::Error>;
|
||||
|
||||
async fn new(
|
||||
req: HttpRequest,
|
||||
auth_info: AuthInfo,
|
||||
req: &HttpRequest,
|
||||
auth_info: &AuthInfo,
|
||||
path_components: Self::PathComponents,
|
||||
) -> Result<Self, Self::Error>;
|
||||
|
||||
async fn get_file(&self) -> Result<Self::File, Self::Error>;
|
||||
|
||||
async fn get_members(&self, auth_info: AuthInfo) -> Result<Vec<Self::MemberType>, Self::Error>;
|
||||
async fn get_members(
|
||||
&self,
|
||||
_auth_info: AuthInfo,
|
||||
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
async fn save_file(&self, file: Self::File) -> Result<(), Self::Error>;
|
||||
}
|
||||
@@ -99,8 +102,12 @@ pub enum PropstatType<T1: Serialize, T2: Serialize> {
|
||||
pub trait HandlePropfind {
|
||||
type Error: ResponseError + From<crate::Error> + From<anyhow::Error>;
|
||||
|
||||
async fn propfind(&self, prefix: &str, props: Vec<&str>)
|
||||
-> Result<impl Serialize, Self::Error>;
|
||||
async fn propfind(
|
||||
&self,
|
||||
prefix: &str,
|
||||
path: String,
|
||||
props: Vec<&str>,
|
||||
) -> Result<impl Serialize, Self::Error>;
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
@@ -110,6 +117,7 @@ impl<R: Resource> HandlePropfind for R {
|
||||
async fn propfind(
|
||||
&self,
|
||||
prefix: &str,
|
||||
path: String,
|
||||
props: Vec<&str>,
|
||||
) -> Result<PropstatResponseElement<PropWrapper<Vec<R::Prop>>, TagList>, R::Error> {
|
||||
let mut props = props;
|
||||
@@ -125,7 +133,7 @@ impl<R: Resource> HandlePropfind for R {
|
||||
.map(|&prop| prop.to_string())
|
||||
.collect();
|
||||
return Ok(PropstatResponseElement {
|
||||
href: self.get_path().to_owned(),
|
||||
href: path,
|
||||
propstat: vec![PropstatType::Normal(PropstatElement {
|
||||
prop: PropWrapper::TagList(TagList::from(props)),
|
||||
status: format!("HTTP/1.1 {}", StatusCode::OK),
|
||||
@@ -177,7 +185,7 @@ impl<R: Resource> HandlePropfind for R {
|
||||
}));
|
||||
}
|
||||
Ok(PropstatResponseElement {
|
||||
href: self.get_path().to_owned(),
|
||||
href: path,
|
||||
propstat: propstats,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user