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 quick_xml::{events::BytesText, Writer};
use rustical_auth::AuthInfo; use rustical_auth::AuthInfo;
use rustical_store::calendar::{Calendar, CalendarStore}; use rustical_store::calendar::{Calendar, CalendarStore};
use strum::{EnumString, IntoStaticStr, VariantNames}; use strum::{EnumProperty, EnumString, IntoStaticStr, VariantNames};
use tokio::sync::RwLock; 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}; use rustical_dav::{resource::Resource, xml_snippets::write_resourcetype};
pub struct CalendarResource<C: CalendarStore + ?Sized> { pub struct CalendarResource<C: CalendarStore + ?Sized> {
@@ -20,18 +23,22 @@ pub struct CalendarResource<C: CalendarStore + ?Sized> {
pub principal: String, pub principal: String,
} }
#[derive(EnumString, Debug, VariantNames, IntoStaticStr)] #[derive(EnumString, Debug, VariantNames, IntoStaticStr, EnumProperty)]
#[strum(serialize_all = "kebab-case")] #[strum(serialize_all = "kebab-case")]
pub enum CalendarProp { pub enum CalendarProp {
Resourcetype, Resourcetype,
CurrentUserPrincipal, CurrentUserPrincipal,
Owner,
Displayname, Displayname,
#[strum(props(tagname = "IC:calendar-color"))]
CalendarColor,
#[strum(props(tagname = "C:calendar-description"))]
CalendarDescription,
#[strum(props(tagname = "C:supported-calendar-component-set"))]
SupportedCalendarComponentSet, SupportedCalendarComponentSet,
#[strum(props(tagname = "C:supported-calendar-data"))]
SupportedCalendarData, SupportedCalendarData,
Getcontenttype, Getcontenttype,
CalendarDescription,
Owner,
CalendarColor,
CurrentUserPrivilegeSet, CurrentUserPrivilegeSet,
MaxResourceSize, MaxResourceSize,
} }
@@ -82,12 +89,12 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
CalendarProp::CurrentUserPrincipal | CalendarProp::Owner => { CalendarProp::CurrentUserPrincipal | CalendarProp::Owner => {
write_href_prop( write_href_prop(
writer, writer,
prop.into(), prop.tagname(),
&format!("{}/{}/", self.prefix, self.principal), &format!("{}/{}/", self.prefix, self.principal),
)?; )?;
} }
CalendarProp::Displayname => { CalendarProp::Displayname => {
let el = writer.create_element("displayname"); let el = writer.create_element(prop.tagname());
if let Some(name) = self.calendar.clone().name { if let Some(name) = self.calendar.clone().name {
el.write_text_content(BytesText::new(&name))?; el.write_text_content(BytesText::new(&name))?;
} else { } else {
@@ -95,7 +102,7 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
} }
} }
CalendarProp::CalendarColor => { 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 { if let Some(color) = self.calendar.clone().color {
el.write_text_content(BytesText::new(&color))?; el.write_text_content(BytesText::new(&color))?;
} else { } else {
@@ -103,7 +110,7 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
} }
} }
CalendarProp::CalendarDescription => { 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 { if let Some(description) = self.calendar.clone().description {
el.write_text_content(BytesText::new(&description))?; el.write_text_content(BytesText::new(&description))?;
} else { } else {
@@ -112,7 +119,7 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
} }
CalendarProp::SupportedCalendarComponentSet => { CalendarProp::SupportedCalendarComponentSet => {
writer writer
.create_element("C:supported-calendar-component-set") .create_element(prop.tagname())
.write_inner_content(|writer| { .write_inner_content(|writer| {
writer writer
.create_element("C:comp") .create_element("C:comp")
@@ -123,7 +130,7 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
} }
CalendarProp::SupportedCalendarData => { CalendarProp::SupportedCalendarData => {
writer writer
.create_element("C:supported-calendar-data") .create_element(prop.tagname())
.write_inner_content(|writer| { .write_inner_content(|writer| {
// <cal:calendar-data content-type="text/calendar" version="2.0" /> // <cal:calendar-data content-type="text/calendar" version="2.0" />
writer writer
@@ -137,14 +144,14 @@ impl<C: CalendarStore + ?Sized> Resource for CalendarResource<C> {
})?; })?;
} }
CalendarProp::Getcontenttype => { CalendarProp::Getcontenttype => {
write_string_prop(writer, "getcontenttype", "text/calendar")?; write_string_prop(writer, prop.tagname(), "text/calendar")?;
} }
CalendarProp::MaxResourceSize => { CalendarProp::MaxResourceSize => {
write_string_prop(writer, "max-resource-size", "10000000")?; write_string_prop(writer, prop.tagname(), "10000000")?;
} }
CalendarProp::CurrentUserPrivilegeSet => { CalendarProp::CurrentUserPrivilegeSet => {
writer 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 // These are just hard-coded for now and will possibly change in the future
.write_inner_content(|writer| { .write_inner_content(|writer| {
for privilege in [ 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 actix_web::{web::Data, HttpRequest};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use async_trait::async_trait; use async_trait::async_trait;
@@ -7,7 +7,7 @@ use rustical_dav::resource::Resource;
use rustical_store::calendar::CalendarStore; use rustical_store::calendar::CalendarStore;
use rustical_store::event::Event; use rustical_store::event::Event;
use std::sync::Arc; use std::sync::Arc;
use strum::{EnumString, VariantNames}; use strum::{EnumProperty, EnumString, IntoStaticStr, VariantNames};
use tokio::sync::RwLock; use tokio::sync::RwLock;
pub struct EventResource<C: CalendarStore + ?Sized> { pub struct EventResource<C: CalendarStore + ?Sized> {
@@ -16,10 +16,11 @@ pub struct EventResource<C: CalendarStore + ?Sized> {
pub event: Event, pub event: Event,
} }
#[derive(EnumString, Debug, VariantNames)] #[derive(EnumString, Debug, VariantNames, IntoStaticStr, EnumProperty)]
#[strum(serialize_all = "kebab-case")] #[strum(serialize_all = "kebab-case")]
pub enum EventProp { pub enum EventProp {
Getetag, Getetag,
#[strum(props(tagname = "C:calendar-data"))]
CalendarData, CalendarData,
Getcontenttype, Getcontenttype,
} }
@@ -68,13 +69,13 @@ impl<C: CalendarStore + ?Sized> Resource for EventResource<C> {
) -> Result<()> { ) -> Result<()> {
match prop { match prop {
EventProp::Getetag => { EventProp::Getetag => {
write_string_prop(writer, "getetag", &self.event.get_etag())?; write_string_prop(writer, prop.tagname(), &self.event.get_etag())?;
} }
EventProp::CalendarData => { 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 => { EventProp::Getcontenttype => {
write_string_prop(writer, "getcontenttype", "text/calendar;charset=utf-8")?; write_string_prop(writer, prop.tagname(), "text/calendar;charset=utf-8")?;
} }
}; };
Ok(()) Ok(())

View File

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

View File

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