mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 08:12:24 +00:00
Add basic framework for PROPPATCH implementation
This commit is contained in:
@@ -2,29 +2,32 @@ use crate::xml::tag_list::TagList;
|
||||
use crate::Error;
|
||||
use actix_web::{http::StatusCode, HttpRequest, ResponseError};
|
||||
use async_trait::async_trait;
|
||||
use core::fmt;
|
||||
use itertools::Itertools;
|
||||
use rustical_auth::AuthInfo;
|
||||
use serde::Serialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::str::FromStr;
|
||||
use strum::VariantNames;
|
||||
|
||||
#[async_trait(?Send)]
|
||||
pub trait Resource {
|
||||
type PropType: FromStr + VariantNames + Clone;
|
||||
type PropResponse: Serialize;
|
||||
pub trait Resource: Clone {
|
||||
type PropName: FromStr + VariantNames + Clone;
|
||||
type Prop: Serialize + for<'de> Deserialize<'de> + fmt::Debug + InvalidProperty;
|
||||
type Error: ResponseError + From<crate::Error> + From<anyhow::Error>;
|
||||
|
||||
fn list_dead_props() -> &'static [&'static str] {
|
||||
Self::PropType::VARIANTS
|
||||
Self::PropName::VARIANTS
|
||||
}
|
||||
|
||||
fn get_prop(
|
||||
&self,
|
||||
prefix: &str,
|
||||
prop: Self::PropType,
|
||||
) -> Result<Self::PropResponse, Self::Error>;
|
||||
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>;
|
||||
}
|
||||
|
||||
pub trait InvalidProperty {
|
||||
fn invalid_property(&self) -> bool;
|
||||
}
|
||||
|
||||
// A resource is identified by a URI and has properties
|
||||
@@ -47,6 +50,8 @@ pub trait ResourceService: Sized {
|
||||
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 save_file(&self, file: Self::File) -> Result<(), Self::Error>;
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@@ -57,23 +62,24 @@ pub struct PropWrapper<T: Serialize> {
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
struct PropstatElement<T: Serialize> {
|
||||
prop: T,
|
||||
status: String,
|
||||
pub struct PropstatElement<T: Serialize> {
|
||||
pub prop: T,
|
||||
pub status: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub struct PropstatResponseElement<T1: Serialize, T2: Serialize> {
|
||||
href: String,
|
||||
propstat: Vec<PropstatType<T1, T2>>,
|
||||
pub href: String,
|
||||
pub propstat: Vec<PropstatType<T1, T2>>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(untagged)]
|
||||
enum PropstatType<T1: Serialize, T2: Serialize> {
|
||||
pub enum PropstatType<T1: Serialize, T2: Serialize> {
|
||||
Normal(PropstatElement<T1>),
|
||||
NotFound(PropstatElement<T2>),
|
||||
Conflict(PropstatElement<T2>),
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
@@ -92,7 +98,7 @@ impl<R: Resource> HandlePropfind for R {
|
||||
&self,
|
||||
prefix: &str,
|
||||
props: Vec<&str>,
|
||||
) -> Result<PropstatResponseElement<PropWrapper<Vec<R::PropResponse>>, TagList>, R::Error> {
|
||||
) -> Result<PropstatResponseElement<PropWrapper<Vec<R::Prop>>, TagList>, R::Error> {
|
||||
let mut props = props;
|
||||
if props.contains(&"propname") {
|
||||
if props.len() != 1 {
|
||||
@@ -117,7 +123,7 @@ impl<R: Resource> HandlePropfind for R {
|
||||
let mut invalid_props = Vec::new();
|
||||
let mut prop_responses = Vec::new();
|
||||
for prop in props {
|
||||
if let Ok(valid_prop) = R::PropType::from_str(prop) {
|
||||
if let Ok(valid_prop) = R::PropName::from_str(prop) {
|
||||
let response = self.get_prop(prefix, valid_prop.clone())?;
|
||||
prop_responses.push(response);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user