caldav: user enums for props

This commit is contained in:
Lennart
2024-03-14 13:57:59 +01:00
parent bed64796b3
commit ade96aa559
7 changed files with 104 additions and 80 deletions

View File

@@ -6,6 +6,7 @@ use async_trait::async_trait;
use quick_xml::{events::BytesText, Writer};
use rustical_auth::AuthInfo;
use rustical_store::calendar::{Calendar, CalendarStore};
use strum::{EnumString, IntoStaticStr, VariantNames};
use tokio::sync::RwLock;
use crate::proptypes::{write_href_prop, write_string_prop};
@@ -19,10 +20,27 @@ pub struct CalendarResource<C: CalendarStore + ?Sized> {
pub principal: String,
}
#[derive(EnumString, Debug, VariantNames, IntoStaticStr)]
#[strum(serialize_all = "kebab-case")]
pub enum CalendarProp {
Resourcetype,
CurrentUserPrincipal,
Displayname,
SupportedCalendarComponentSet,
SupportedCalendarData,
Getcontenttype,
CalendarDescription,
Owner,
CalendarColor,
CurrentUserPrivilegeSet,
MaxResourceSize,
}
#[async_trait(?Send)]
impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
type MemberType = Self;
type UriComponents = (String, String); // principal, calendar_id
type PropType = CalendarProp;
async fn acquire_from_request(
req: HttpRequest,
@@ -56,33 +74,19 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
Ok(vec![])
}
#[inline]
fn list_dead_props() -> Vec<&'static str> {
vec![
"resourcetype",
"current-user-principal",
"displayname",
"supported-calendar-component-set",
"supported-calendar-data",
"getcontenttype",
"calendar-description",
"owner",
"calendar-color",
"current-user-privilege-set",
"max-resource-size",
]
}
fn write_prop<W: Write>(&self, writer: &mut Writer<W>, prop: &str) -> Result<()> {
fn write_prop<W: Write>(&self, writer: &mut Writer<W>, prop: Self::PropType) -> Result<()> {
match prop {
"resourcetype" => write_resourcetype(writer, vec!["C:calendar", "collection"])?,
"current-user-principal" | "owner" => {
CalendarProp::Resourcetype => {
write_resourcetype(writer, vec!["C:calendar", "collection"])?
}
CalendarProp::CurrentUserPrincipal | CalendarProp::Owner => {
write_href_prop(
writer,
prop,
prop.into(),
&format!("{}/{}/", self.prefix, self.principal),
)?;
}
"displayname" => {
CalendarProp::Displayname => {
let el = writer.create_element("displayname");
if let Some(name) = self.calendar.clone().name {
el.write_text_content(BytesText::new(&name))?;
@@ -90,7 +94,7 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
el.write_empty()?;
}
}
"calendar-color" => {
CalendarProp::CalendarColor => {
let el = writer.create_element("IC:calendar-color");
if let Some(color) = self.calendar.clone().color {
el.write_text_content(BytesText::new(&color))?;
@@ -98,7 +102,7 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
el.write_empty()?;
}
}
"calendar-description" => {
CalendarProp::CalendarDescription => {
let el = writer.create_element("C:calendar-description");
if let Some(description) = self.calendar.clone().description {
el.write_text_content(BytesText::new(&description))?;
@@ -106,7 +110,7 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
el.write_empty()?;
}
}
"supported-calendar-component-set" => {
CalendarProp::SupportedCalendarComponentSet => {
writer
.create_element("C:supported-calendar-component-set")
.write_inner_content(|writer| {
@@ -117,7 +121,7 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
Ok::<(), quick_xml::Error>(())
})?;
}
"supported-calendar-data" => {
CalendarProp::SupportedCalendarData => {
writer
.create_element("C:supported-calendar-data")
.write_inner_content(|writer| {
@@ -132,13 +136,13 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
Ok::<(), quick_xml::Error>(())
})?;
}
"getcontenttype" => {
CalendarProp::Getcontenttype => {
write_string_prop(writer, "getcontenttype", "text/calendar")?;
}
"max-resource-size" => {
CalendarProp::MaxResourceSize => {
write_string_prop(writer, "max-resource-size", "10000000")?;
}
"current-user-privilege-set" => {
CalendarProp::CurrentUserPrivilegeSet => {
writer
.create_element("current-user-privilege-set")
// These are just hard-coded for now and will possibly change in the future
@@ -163,9 +167,6 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
Ok::<(), quick_xml::Error>(())
})?;
}
_ => {
return Err(anyhow!("invalid prop"));
}
};
Ok(())
}

View File

@@ -7,6 +7,7 @@ use rustical_dav::resource::Resource;
use rustical_store::calendar::CalendarStore;
use rustical_store::event::Event;
use std::sync::Arc;
use strum::{EnumString, VariantNames};
use tokio::sync::RwLock;
pub struct EventResource<C: CalendarStore + ?Sized> {
@@ -15,10 +16,19 @@ pub struct EventResource<C: CalendarStore + ?Sized> {
pub event: Event,
}
#[derive(EnumString, Debug, VariantNames)]
#[strum(serialize_all = "kebab-case")]
pub enum EventProp {
Getetag,
CalendarData,
Getcontenttype,
}
#[async_trait(?Send)]
impl<C: CalendarStore + ?Sized> Resource for EventResource<C> {
type UriComponents = (String, String, String); // principal, calendar, event
type MemberType = Self;
type PropType = EventProp;
fn get_path(&self) -> &str {
&self.path
@@ -54,24 +64,19 @@ impl<C: CalendarStore + ?Sized> Resource for EventResource<C> {
fn write_prop<W: std::io::Write>(
&self,
writer: &mut quick_xml::Writer<W>,
prop: &str,
prop: Self::PropType,
) -> Result<()> {
match prop {
"getetag" => {
EventProp::Getetag => {
write_string_prop(writer, "getetag", &self.event.get_etag())?;
}
"calendar-data" => {
EventProp::CalendarData => {
write_string_prop(writer, "C:calendar-data", &self.event.get_ics())?;
}
"getcontenttype" => {
EventProp::Getcontenttype => {
write_string_prop(writer, "getcontenttype", "text/calendar;charset=utf-8")?;
}
_ => return Err(anyhow!("invalid prop!")),
};
Ok(())
}
fn list_dead_props() -> Vec<&'static str> {
vec!["getetag", "calendar-data", "getcontenttype"]
}
}

View File

@@ -8,6 +8,7 @@ use quick_xml::events::BytesText;
use rustical_auth::AuthInfo;
use rustical_dav::{resource::Resource, xml_snippets::write_resourcetype};
use rustical_store::calendar::CalendarStore;
use strum::{EnumString, IntoStaticStr, VariantNames};
use tokio::sync::RwLock;
use super::calendar::CalendarResource;
@@ -19,10 +20,22 @@ pub struct PrincipalCalendarsResource<C: CalendarStore + ?Sized> {
cal_store: Arc<RwLock<C>>,
}
#[derive(EnumString, Debug, VariantNames, IntoStaticStr)]
#[strum(serialize_all = "kebab-case")]
pub enum PrincipalProp {
Resourcetype,
CurrentUserPrincipal,
#[strum(serialize = "principal-URL")]
PrincipalUrl,
CalendarHomeSet,
CalendarUserAddressSet,
}
#[async_trait(?Send)]
impl<C: CalendarStore + ?Sized> Resource for PrincipalCalendarsResource<C> {
type UriComponents = ();
type MemberType = CalendarResource<C>;
type PropType = PrincipalProp;
fn get_path(&self) -> &str {
&self.path
@@ -71,20 +84,23 @@ impl<C: CalendarStore + ?Sized> Resource for PrincipalCalendarsResource<C> {
fn write_prop<W: std::io::Write>(
&self,
writer: &mut quick_xml::Writer<W>,
prop: &str,
prop: Self::PropType,
) -> Result<()> {
match prop {
"resourcetype" => write_resourcetype(writer, vec!["principal", "collection"])?,
"current-user-principal" | "principal-URL" => {
PrincipalProp::Resourcetype => {
write_resourcetype(writer, vec!["principal", "collection"])?
}
PrincipalProp::CurrentUserPrincipal | PrincipalProp::PrincipalUrl => {
write_href_prop(
writer,
prop,
prop.into(),
&format!("{}/{}/", self.prefix, self.principal),
)?;
}
"calendar-home-set" | "calendar-user-address-set" => {
PrincipalProp::CalendarHomeSet | PrincipalProp::CalendarUserAddressSet => {
let propname: &'static str = prop.into();
writer
.create_element(&format!("C:{prop}"))
.create_element(&format!("C:{propname}"))
.write_inner_content(|writer| {
writer
.create_element("href")
@@ -95,21 +111,7 @@ impl<C: CalendarStore + ?Sized> Resource for PrincipalCalendarsResource<C> {
Ok::<(), quick_xml::Error>(())
})?;
}
"allprops" => {}
_ => {
return Err(anyhow!("invalid prop"));
}
};
Ok(())
}
fn list_dead_props() -> Vec<&'static str> {
vec![
"resourcetype",
"current-user-principal",
"principal-URL",
"calendar-home-set",
"calendar-user-address-set",
]
}
}

View File

@@ -4,6 +4,7 @@ use async_trait::async_trait;
use quick_xml::events::BytesText;
use rustical_auth::AuthInfo;
use rustical_dav::{resource::Resource, xml_snippets::write_resourcetype};
use strum::{EnumString, VariantNames};
pub struct RootResource {
prefix: String,
@@ -11,10 +12,18 @@ pub struct RootResource {
path: String,
}
#[derive(EnumString, Debug, VariantNames)]
#[strum(serialize_all = "kebab-case")]
pub enum RootProp {
Resourcetype,
CurrentUserPrincipal,
}
#[async_trait(?Send)]
impl Resource for RootResource {
type UriComponents = ();
type MemberType = Self;
type PropType = RootProp;
fn get_path(&self) -> &str {
&self.path
@@ -40,11 +49,11 @@ impl Resource for RootResource {
fn write_prop<W: std::io::Write>(
&self,
writer: &mut quick_xml::Writer<W>,
prop: &str,
prop: Self::PropType,
) -> Result<()> {
match prop {
"resourcetype" => write_resourcetype(writer, vec!["collection"])?,
"current-user-principal" => {
RootProp::Resourcetype => write_resourcetype(writer, vec!["collection"])?,
RootProp::CurrentUserPrincipal => {
writer
.create_element("current-user-principal")
.write_inner_content(|writer| {
@@ -57,12 +66,7 @@ impl Resource for RootResource {
Ok::<(), quick_xml::Error>(())
})?;
}
_ => return Err(anyhow!("invalid prop!")),
};
Ok(())
}
fn list_dead_props() -> Vec<&'static str> {
vec!["resourcetype", "current-user-principal"]
}
}