Skip to content

Commit

Permalink
caldav, carddav: Generate Propname enums with strum
Browse files Browse the repository at this point in the history
  • Loading branch information
lennart-k committed Nov 12, 2024
1 parent 6747fde commit 7dfa0c9
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 82 deletions.
35 changes: 11 additions & 24 deletions crates/caldav/src/calendar/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use actix_web::http::Method;
use actix_web::web;
use actix_web::{web::Data, HttpRequest};
use async_trait::async_trait;
use base64::prelude::{BASE64_STANDARD, BASE64_URL_SAFE};
use base64::Engine;
use derive_more::derive::{From, Into};
use rustical_dav::privileges::UserPrivilegeSet;
use rustical_dav::resource::{Resource, ResourceService};
Expand All @@ -25,35 +23,20 @@ use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::str::FromStr;
use std::sync::Arc;
use strum::{EnumString, VariantNames};
use strum::{EnumDiscriminants, EnumString, VariantNames};

pub struct CalendarResourceService<C: CalendarStore + ?Sized> {
cal_store: Arc<C>,
principal: String,
calendar_id: String,
}

#[derive(EnumString, VariantNames, Clone)]
#[strum(serialize_all = "kebab-case")]
pub enum CalendarPropName {
Displayname,
Getcontenttype,
Transports,
Topic,
CalendarColor,
CalendarDescription,
CalendarTimezone,
CalendarOrder,
SupportedCalendarComponentSet,
SupportedCalendarData,
MaxResourceSize,
SupportedReportSet,
SyncToken,
Getctag,
Source,
}

#[derive(Default, Deserialize, Serialize, PartialEq)]
#[derive(Default, Deserialize, Serialize, PartialEq, EnumDiscriminants)]
#[strum_discriminants(
name(CalendarPropName),
derive(EnumString, VariantNames),
strum(serialize_all = "kebab-case")
)]
#[serde(rename_all = "kebab-case")]
pub enum CalendarProp {
// WebDAV (RFC 2518)
Expand Down Expand Up @@ -178,6 +161,9 @@ impl Resource for CalendarResource {
CalendarPropName::Source => {
CalendarProp::Source(self.0.subscription_url.to_owned().map(HrefElement::from))
}
CalendarPropName::Invalid => {
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
}
})
}

Expand Down Expand Up @@ -255,6 +241,7 @@ impl Resource for CalendarResource {
CalendarPropName::Getctag => Err(rustical_dav::Error::PropReadOnly),
// Converting a calendar subscription calendar into a normal one would be weird
CalendarPropName::Source => Err(rustical_dav::Error::PropReadOnly),
CalendarPropName::Invalid => Err(rustical_dav::Error::PropReadOnly),
}
}

Expand Down
20 changes: 10 additions & 10 deletions crates/caldav/src/calendar_object/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustical_dav::{
use rustical_store::{auth::User, CalendarObject, CalendarStore};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use strum::{EnumString, VariantNames};
use strum::{EnumDiscriminants, EnumString, VariantNames};

pub struct CalendarObjectResourceService<C: CalendarStore + ?Sized> {
cal_store: Arc<C>,
Expand All @@ -19,15 +19,12 @@ pub struct CalendarObjectResourceService<C: CalendarStore + ?Sized> {
object_id: String,
}

#[derive(EnumString, VariantNames, Clone)]
#[strum(serialize_all = "kebab-case")]
pub enum CalendarObjectPropName {
Getetag,
CalendarData,
Getcontenttype,
}

#[derive(Default, Deserialize, Serialize, PartialEq)]
#[derive(Default, Deserialize, Serialize, PartialEq, EnumDiscriminants)]
#[strum_discriminants(
name(CalendarObjectPropName),
derive(EnumString, VariantNames),
strum(serialize_all = "kebab-case")
)]
#[serde(rename_all = "kebab-case")]
pub enum CalendarObjectProp {
// WebDAV (RFC 2518)
Expand Down Expand Up @@ -73,6 +70,9 @@ impl Resource for CalendarObjectResource {
CalendarObjectPropName::Getcontenttype => {
CalendarObjectProp::Getcontenttype("text/calendar;charset=utf-8".to_owned())
}
CalendarObjectPropName::Invalid => {
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
}
})
}

Expand Down
22 changes: 11 additions & 11 deletions crates/caldav/src/principal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustical_store::auth::User;
use rustical_store::CalendarStore;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use strum::{EnumString, VariantNames};
use strum::{EnumDiscriminants, EnumString, VariantNames};

pub struct PrincipalResourceService<C: CalendarStore + ?Sized> {
principal: String,
Expand All @@ -23,11 +23,17 @@ pub struct PrincipalResource {
principal: String,
}

#[derive(Default, Deserialize, Serialize, PartialEq)]
#[derive(Default, Deserialize, Serialize, PartialEq, EnumDiscriminants)]
#[strum_discriminants(
name(PrincipalPropName),
derive(EnumString, VariantNames),
strum(serialize_all = "kebab-case")
)]
#[serde(rename_all = "kebab-case")]
pub enum PrincipalProp {
// WebDAV Access Control (RFC 3744)
#[serde(rename = "principal-URL")]
#[strum_discriminants(strum(serialize = "principal-URL"))]
PrincipalUrl(HrefElement),

// CalDAV (RFC 4791)
Expand All @@ -41,15 +47,6 @@ pub enum PrincipalProp {
Invalid,
}

#[derive(EnumString, VariantNames, Clone)]
#[strum(serialize_all = "kebab-case")]
pub enum PrincipalPropName {
#[strum(serialize = "principal-URL")]
PrincipalUrl,
CalendarHomeSet,
CalendarUserAddressSet,
}

impl PrincipalResource {
pub fn get_principal_url(rmap: &ResourceMap, principal: &str) -> String {
Self::get_url(rmap, vec![principal]).unwrap()
Expand Down Expand Up @@ -80,6 +77,9 @@ impl Resource for PrincipalResource {
PrincipalPropName::CalendarUserAddressSet => {
PrincipalProp::CalendarUserAddressSet(principal_href)
}
PrincipalPropName::Invalid => {
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
}
})
}

Expand Down
20 changes: 10 additions & 10 deletions crates/carddav/src/address_object/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustical_dav::{
use rustical_store::{auth::User, AddressObject, AddressbookStore};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use strum::{EnumString, VariantNames};
use strum::{EnumDiscriminants, EnumString, VariantNames};

use super::methods::{get_object, put_object};

Expand All @@ -20,15 +20,12 @@ pub struct AddressObjectResourceService<AS: AddressbookStore + ?Sized> {
object_id: String,
}

#[derive(EnumString, VariantNames, Clone)]
#[strum(serialize_all = "kebab-case")]
pub enum AddressObjectPropName {
Getetag,
AddressData,
Getcontenttype,
}

#[derive(Default, Deserialize, Serialize, PartialEq)]
#[derive(Default, Deserialize, Serialize, PartialEq, EnumDiscriminants)]
#[strum_discriminants(
name(AddressObjectPropName),
derive(EnumString, VariantNames),
strum(serialize_all = "kebab-case")
)]
#[serde(rename_all = "kebab-case")]
pub enum AddressObjectProp {
// WebDAV (RFC 2518)
Expand Down Expand Up @@ -74,6 +71,9 @@ impl Resource for AddressObjectResource {
AddressObjectPropName::Getcontenttype => {
AddressObjectProp::Getcontenttype("text/vcard;charset=utf-8".to_owned())
}
AddressObjectPropName::Invalid => {
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
}
})
}

Expand Down
26 changes: 11 additions & 15 deletions crates/carddav/src/addressbook/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,21 @@ use rustical_store::{Addressbook, AddressbookStore};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use std::sync::Arc;
use strum::{EnumString, VariantNames};
use strum::{EnumDiscriminants, EnumString, VariantNames};

pub struct AddressbookResourceService<AS: AddressbookStore + ?Sized> {
addr_store: Arc<AS>,
principal: String,
addressbook_id: String,
}

#[derive(EnumString, VariantNames, Clone)]
#[strum(serialize_all = "kebab-case")]
pub enum AddressbookPropName {
Displayname,
Getcontenttype,
AddressbookDescription,
SupportedAddressData,
SupportedReportSet,
MaxResourceSize,
SyncToken,
Getctag,
}

#[derive(Default, Deserialize, Serialize, PartialEq)]
#[derive(Default, Deserialize, Serialize, PartialEq, EnumDiscriminants)]
#[serde(rename_all = "kebab-case")]
#[strum_discriminants(
name(AddressbookPropName),
derive(EnumString, VariantNames),
strum(serialize_all = "kebab-case")
)]
pub enum AddressbookProp {
// WebDAV (RFC 2518)
Displayname(Option<String>),
Expand Down Expand Up @@ -110,6 +102,9 @@ impl Resource for AddressbookResource {
}
AddressbookPropName::SyncToken => AddressbookProp::SyncToken(self.0.format_synctoken()),
AddressbookPropName::Getctag => AddressbookProp::Getctag(self.0.format_synctoken()),
AddressbookPropName::Invalid => {
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
}
})
}

Expand Down Expand Up @@ -149,6 +144,7 @@ impl Resource for AddressbookResource {
AddressbookPropName::SupportedAddressData => Err(rustical_dav::Error::PropReadOnly),
AddressbookPropName::SyncToken => Err(rustical_dav::Error::PropReadOnly),
AddressbookPropName::Getctag => Err(rustical_dav::Error::PropReadOnly),
AddressbookPropName::Invalid => Err(rustical_dav::Error::PropReadOnly),
}
}

Expand Down
22 changes: 11 additions & 11 deletions crates/carddav/src/principal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustical_store::auth::User;
use rustical_store::AddressbookStore;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use strum::{EnumString, VariantNames};
use strum::{EnumDiscriminants, EnumString, VariantNames};

pub struct PrincipalResourceService<A: AddressbookStore + ?Sized> {
principal: String,
Expand All @@ -23,11 +23,17 @@ pub struct PrincipalResource {
principal: String,
}

#[derive(Default, Deserialize, Serialize, PartialEq)]
#[derive(Default, Deserialize, Serialize, PartialEq, EnumDiscriminants)]
#[strum_discriminants(
name(PrincipalPropName),
derive(EnumString, VariantNames),
strum(serialize_all = "kebab-case")
)]
#[serde(rename_all = "kebab-case")]
pub enum PrincipalProp {
// WebDAV Access Control (RFC 3744)
#[serde(rename = "principal-URL")]
#[strum_discriminants(strum(serialize = "principal-URL"))]
PrincipalUrl(HrefElement),

// CardDAV (RFC 6352)
Expand All @@ -41,15 +47,6 @@ pub enum PrincipalProp {
Invalid,
}

#[derive(EnumString, VariantNames, Clone)]
#[strum(serialize_all = "kebab-case")]
pub enum PrincipalPropName {
#[strum(serialize = "principal-URL")]
PrincipalUrl,
AddressbookHomeSet,
PrincipalAddress,
}

impl PrincipalResource {
pub fn get_principal_url(rmap: &ResourceMap, principal: &str) -> String {
Self::get_url(rmap, vec![principal]).unwrap()
Expand Down Expand Up @@ -80,6 +77,9 @@ impl Resource for PrincipalResource {
PrincipalProp::AddressbookHomeSet(principal_href)
}
PrincipalPropName::PrincipalAddress => PrincipalProp::PrincipalAddress(None),
PrincipalPropName::Invalid => {
return Err(rustical_dav::Error::BadRequest("invalid prop name".to_owned()).into())
}
})
}

Expand Down
2 changes: 1 addition & 1 deletion crates/dav/src/resources/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<PR: Resource> Resource for RootResource<PR> {
_user: &User,
_prop: &Self::PropName,
) -> Result<Self::Prop, Self::Error> {
panic!("we shouldn't end up here")
unreachable!("we shouldn't end up here")
}

#[inline]
Expand Down

0 comments on commit 7dfa0c9

Please sign in to comment.