replace plaintext props with enums

This commit is contained in:
Lennart
2024-03-14 16:15:50 +01:00
parent de0053166a
commit b9ee8506e1
4 changed files with 41 additions and 32 deletions

View File

@@ -6,10 +6,13 @@ 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 strum::{EnumProperty, EnumString, IntoStaticStr, VariantNames};
use tokio::sync::RwLock;
use crate::proptypes::{write_href_prop, write_string_prop};
use crate::{
proptypes::{write_href_prop, write_string_prop},
tagname::TagName,
};
use rustical_dav::{resource::Resource, xml_snippets::write_resourcetype};
pub struct CalendarResource<C: CalendarStore + ?Sized> {
@@ -20,18 +23,22 @@ pub struct CalendarResource<C: CalendarStore + ?Sized> {
pub principal: String,
}
#[derive(EnumString, Debug, VariantNames, IntoStaticStr)]
#[derive(EnumString, Debug, VariantNames, IntoStaticStr, EnumProperty)]
#[strum(serialize_all = "kebab-case")]
pub enum CalendarProp {
Resourcetype,
CurrentUserPrincipal,
Owner,
Displayname,
#[strum(props(tagname = "IC:calendar-color"))]
CalendarColor,
#[strum(props(tagname = "C:calendar-description"))]
CalendarDescription,
#[strum(props(tagname = "C:supported-calendar-component-set"))]
SupportedCalendarComponentSet,
#[strum(props(tagname = "C:supported-calendar-data"))]
SupportedCalendarData,
Getcontenttype,
CalendarDescription,
Owner,
CalendarColor,
CurrentUserPrivilegeSet,
MaxResourceSize,
}
@@ -82,12 +89,12 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
CalendarProp::CurrentUserPrincipal | CalendarProp::Owner => {
write_href_prop(
writer,
prop.into(),
prop.tagname(),
&format!("{}/{}/", self.prefix, self.principal),
)?;
}
CalendarProp::Displayname => {
let el = writer.create_element("displayname");
let el = writer.create_element(prop.tagname());
if let Some(name) = self.calendar.clone().name {
el.write_text_content(BytesText::new(&name))?;
} else {
@@ -95,7 +102,7 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
}
}
CalendarProp::CalendarColor => {
let el = writer.create_element("IC:calendar-color");
let el = writer.create_element(prop.tagname());
if let Some(color) = self.calendar.clone().color {
el.write_text_content(BytesText::new(&color))?;
} else {
@@ -103,7 +110,7 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
}
}
CalendarProp::CalendarDescription => {
let el = writer.create_element("C:calendar-description");
let el = writer.create_element(prop.tagname());
if let Some(description) = self.calendar.clone().description {
el.write_text_content(BytesText::new(&description))?;
} else {
@@ -112,7 +119,7 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
}
CalendarProp::SupportedCalendarComponentSet => {
writer
.create_element("C:supported-calendar-component-set")
.create_element(prop.tagname())
.write_inner_content(|writer| {
writer
.create_element("C:comp")
@@ -123,7 +130,7 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
}
CalendarProp::SupportedCalendarData => {
writer
.create_element("C:supported-calendar-data")
.create_element(prop.tagname())
.write_inner_content(|writer| {
// <cal:calendar-data content-type="text/calendar" version="2.0" />
writer
@@ -137,14 +144,14 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
})?;
}
CalendarProp::Getcontenttype => {
write_string_prop(writer, "getcontenttype", "text/calendar")?;
write_string_prop(writer, prop.tagname(), "text/calendar")?;
}
CalendarProp::MaxResourceSize => {
write_string_prop(writer, "max-resource-size", "10000000")?;
write_string_prop(writer, prop.tagname(), "10000000")?;
}
CalendarProp::CurrentUserPrivilegeSet => {
writer
.create_element("current-user-privilege-set")
.create_element(prop.tagname())
// These are just hard-coded for now and will possibly change in the future
.write_inner_content(|writer| {
for privilege in [

View File

@@ -1,4 +1,4 @@
use crate::proptypes::write_string_prop;
use crate::{proptypes::write_string_prop, tagname::TagName};
use actix_web::{web::Data, HttpRequest};
use anyhow::{anyhow, Result};
use async_trait::async_trait;
@@ -7,7 +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 strum::{EnumProperty, EnumString, IntoStaticStr, VariantNames};
use tokio::sync::RwLock;
pub struct EventResource<C: CalendarStore + ?Sized> {
@@ -16,10 +16,11 @@ pub struct EventResource<C: CalendarStore + ?Sized> {
pub event: Event,
}
#[derive(EnumString, Debug, VariantNames)]
#[derive(EnumString, Debug, VariantNames, IntoStaticStr, EnumProperty)]
#[strum(serialize_all = "kebab-case")]
pub enum EventProp {
Getetag,
#[strum(props(tagname = "C:calendar-data"))]
CalendarData,
Getcontenttype,
}
@@ -68,13 +69,13 @@ impl<C: CalendarStore + ?Sized> Resource for EventResource<C> {
) -> Result<()> {
match prop {
EventProp::Getetag => {
write_string_prop(writer, "getetag", &self.event.get_etag())?;
write_string_prop(writer, prop.tagname(), &self.event.get_etag())?;
}
EventProp::CalendarData => {
write_string_prop(writer, "C:calendar-data", &self.event.get_ics())?;
write_string_prop(writer, prop.tagname(), &self.event.get_ics())?;
}
EventProp::Getcontenttype => {
write_string_prop(writer, "getcontenttype", "text/calendar;charset=utf-8")?;
write_string_prop(writer, prop.tagname(), "text/calendar;charset=utf-8")?;
}
};
Ok(())

View File

@@ -1,6 +1,4 @@
use std::sync::Arc;
use crate::proptypes::write_href_prop;
use crate::{proptypes::write_href_prop, tagname::TagName};
use actix_web::{web::Data, HttpRequest};
use anyhow::{anyhow, Result};
use async_trait::async_trait;
@@ -8,7 +6,8 @@ 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 std::sync::Arc;
use strum::{EnumProperty, EnumString, IntoStaticStr, VariantNames};
use tokio::sync::RwLock;
use super::calendar::CalendarResource;
@@ -20,14 +19,16 @@ pub struct PrincipalCalendarsResource<C: CalendarStore + ?Sized> {
cal_store: Arc<RwLock<C>>,
}
#[derive(EnumString, Debug, VariantNames, IntoStaticStr)]
#[derive(EnumString, Debug, VariantNames, IntoStaticStr, EnumProperty)]
#[strum(serialize_all = "kebab-case")]
pub enum PrincipalProp {
Resourcetype,
CurrentUserPrincipal,
#[strum(serialize = "principal-URL")]
PrincipalUrl,
#[strum(props(tagname = "C:calendar-home-set"))]
CalendarHomeSet,
#[strum(props(tagname = "C:calendar-user-address-set"))]
CalendarUserAddressSet,
}
@@ -98,9 +99,8 @@ impl<C: CalendarStore + ?Sized> Resource for PrincipalCalendarsResource<C> {
)?;
}
PrincipalProp::CalendarHomeSet | PrincipalProp::CalendarUserAddressSet => {
let propname: &'static str = prop.into();
writer
.create_element(&format!("C:{propname}"))
.create_element(prop.tagname())
.write_inner_content(|writer| {
writer
.create_element("href")

View File

@@ -1,10 +1,11 @@
use crate::tagname::TagName;
use actix_web::HttpRequest;
use anyhow::{anyhow, Result};
use anyhow::Result;
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};
use strum::{EnumProperty, EnumString, IntoStaticStr, VariantNames};
pub struct RootResource {
prefix: String,
@@ -12,7 +13,7 @@ pub struct RootResource {
path: String,
}
#[derive(EnumString, Debug, VariantNames)]
#[derive(EnumString, Debug, VariantNames, EnumProperty, IntoStaticStr)]
#[strum(serialize_all = "kebab-case")]
pub enum RootProp {
Resourcetype,
@@ -55,7 +56,7 @@ impl Resource for RootResource {
RootProp::Resourcetype => write_resourcetype(writer, vec!["collection"])?,
RootProp::CurrentUserPrincipal => {
writer
.create_element("current-user-principal")
.create_element(prop.tagname())
.write_inner_content(|writer| {
writer
.create_element("href")