Compare commits

...

7 Commits

20 changed files with 194 additions and 71 deletions

View File

@@ -149,7 +149,7 @@ mod tests {
use super::*;
use crate::calendar_object::{CalendarData, CalendarObjectPropName, ExpandElement};
use calendar_query::{CompFilterElement, FilterElement, TimeRangeElement};
use rustical_dav::xml::PropElement;
use rustical_dav::{extensions::CommonPropertiesPropName, xml::PropElement};
use rustical_ical::UtcDateTime;
use rustical_xml::{NamespaceOwned, ValueDeserialize};
@@ -160,7 +160,6 @@ mod tests {
<calendar-multiget xmlns="urn:ietf:params:xml:ns:caldav" xmlns:D="DAV:">
<D:prop>
<D:getetag/>
<D:displayname/>
<calendar-data>
<expand start="20250426T220000Z" end="20250503T220000Z"/>
</calendar-data>
@@ -180,7 +179,7 @@ mod tests {
end: <UtcDateTime as ValueDeserialize>::deserialize("20250503T220000Z").unwrap(),
}), limit_recurrence_set: None, limit_freebusy_set: None }
)),
], vec![(Some(NamespaceOwned(Vec::from("DAV:"))), "displayname".to_string())])),
], vec![])),
href: vec![
"/caldav/user/user/6f787542-5256-401a-8db97003260da/ae7a998fdfd1d84a20391168962c62b".to_owned()
]
@@ -253,6 +252,7 @@ mod tests {
<D:prop>
<D:getetag/>
<D:displayname/>
<D:invalid-prop/>
</D:prop>
<D:href>/caldav/user/user/6f787542-5256-401a-8db97003260da/ae7a998fdfd1d84a20391168962c62b</D:href>
</calendar-multiget>
@@ -263,7 +263,8 @@ mod tests {
ReportRequest::CalendarMultiget(CalendarMultigetRequest {
prop: rustical_dav::xml::PropfindType::Prop(PropElement(vec![
CalendarObjectPropWrapperName::CalendarObject(CalendarObjectPropName::Getetag),
], vec![(Some(NamespaceOwned(Vec::from("DAV:"))), "displayname".to_string())])),
CalendarObjectPropWrapperName::Common(CommonPropertiesPropName::Displayname),
], vec![(Some(NamespaceOwned(Vec::from("DAV:"))), "invalid-prop".to_string())])),
href: vec![
"/caldav/user/user/6f787542-5256-401a-8db97003260da/ae7a998fdfd1d84a20391168962c62b".to_owned()
]

View File

@@ -19,10 +19,6 @@ use std::str::FromStr;
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "CalendarPropName")]
pub enum CalendarProp {
// WebDAV (RFC 2518)
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
Displayname(Option<String>),
// CalDAV (RFC 4791)
#[xml(ns = "rustical_dav::namespace::NS_ICAL")]
CalendarColor(Option<String>),
@@ -127,9 +123,6 @@ impl Resource for CalendarResource {
) -> Result<Self::Prop, Self::Error> {
Ok(match prop {
CalendarPropWrapperName::Calendar(prop) => CalendarPropWrapper::Calendar(match prop {
CalendarPropName::Displayname => {
CalendarProp::Displayname(self.cal.displayname.clone())
}
CalendarPropName::CalendarColor => {
CalendarProp::CalendarColor(self.cal.color.clone())
}
@@ -187,10 +180,6 @@ impl Resource for CalendarResource {
}
match prop {
CalendarPropWrapper::Calendar(prop) => match prop {
CalendarProp::Displayname(displayname) => {
self.cal.displayname = displayname;
Ok(())
}
CalendarProp::CalendarColor(color) => {
self.cal.color = color;
Ok(())
@@ -247,10 +236,6 @@ impl Resource for CalendarResource {
}
match prop {
CalendarPropWrapperName::Calendar(prop) => match prop {
CalendarPropName::Displayname => {
self.cal.displayname = None;
Ok(())
}
CalendarPropName::CalendarColor => {
self.cal.color = None;
Ok(())
@@ -291,6 +276,14 @@ impl Resource for CalendarResource {
}
}
fn get_displayname(&self) -> Option<&str> {
self.cal.displayname.as_deref()
}
fn set_displayname(&mut self, name: Option<String>) -> Result<(), rustical_dav::Error> {
self.cal.displayname = name;
Ok(())
}
fn get_owner(&self) -> Option<&str> {
Some(&self.cal.principal)
}

View File

@@ -66,6 +66,11 @@ impl Resource for CalendarObjectResource {
})
}
fn get_displayname(&self) -> Option<&str> {
// TODO: Extract summary from object
None
}
fn get_owner(&self) -> Option<&str> {
Some(&self.principal)
}

View File

@@ -22,8 +22,11 @@ pub use error::Error;
pub struct CalDavPrincipalUri(&'static str);
impl PrincipalUri for CalDavPrincipalUri {
fn principal_collection(&self) -> String {
format!("{}/principal/", self.0)
}
fn principal_uri(&self, principal: &str) -> String {
format!("{}/principal/{}/", self.0, principal)
format!("{}{}/", self.principal_collection(), principal)
}
}

View File

@@ -44,7 +44,8 @@ impl Resource for PrincipalResource {
let principal_url = puri.principal_uri(&self.principal.id);
let home_set = CalendarHomeSet(
user.memberships()
self.principal
.memberships()
.into_iter()
.map(|principal| puri.principal_uri(principal).into())
.collect(),
@@ -56,12 +57,6 @@ impl Resource for PrincipalResource {
PrincipalPropName::CalendarUserType => {
PrincipalProp::CalendarUserType(self.principal.principal_type.to_owned())
}
PrincipalPropName::Displayname => PrincipalProp::Displayname(
self.principal
.displayname
.to_owned()
.unwrap_or(self.principal.id.to_owned()),
),
PrincipalPropName::PrincipalUrl => {
PrincipalProp::PrincipalUrl(principal_url.into())
}
@@ -69,6 +64,21 @@ impl Resource for PrincipalResource {
PrincipalPropName::CalendarUserAddressSet => {
PrincipalProp::CalendarUserAddressSet(principal_url.into())
}
PrincipalPropName::GroupMembership => {
PrincipalProp::GroupMembership(GroupMembership(
self.principal
.memberships_without_self()
.iter()
.map(|principal| puri.principal_uri(principal).into())
.collect(),
))
}
PrincipalPropName::AlternateUriSet => PrincipalProp::AlternateUriSet,
PrincipalPropName::PrincipalCollectionSet => {
PrincipalProp::PrincipalCollectionSet(PrincipalCollectionSet(
puri.principal_collection().into(),
))
}
})
}
PrincipalPropWrapperName::Common(prop) => PrincipalPropWrapper::Common(
@@ -77,6 +87,15 @@ impl Resource for PrincipalResource {
})
}
fn get_displayname(&self) -> Option<&str> {
Some(
self.principal
.displayname
.as_ref()
.unwrap_or(&self.principal.id),
)
}
fn get_owner(&self) -> Option<&str> {
Some(&self.principal.id)
}

View File

@@ -5,9 +5,6 @@ use rustical_xml::{EnumVariants, PropName, XmlDeserialize, XmlSerialize};
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "PrincipalPropName")]
pub enum PrincipalProp {
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
Displayname(String),
// Scheduling Extensions to CalDAV (RFC 6638)
#[xml(ns = "rustical_dav::namespace::NS_CALDAV", skip_deserializing)]
CalendarUserType(PrincipalType),
@@ -17,6 +14,12 @@ pub enum PrincipalProp {
// WebDAV Access Control (RFC 3744)
#[xml(ns = "rustical_dav::namespace::NS_DAV", rename = b"principal-URL")]
PrincipalUrl(HrefElement),
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
GroupMembership(GroupMembership),
#[xml(ns = "rustical_dav::namespace::NS_DAV", rename = b"alternate-URI-set")]
AlternateUriSet,
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
PrincipalCollectionSet(PrincipalCollectionSet),
// CalDAV (RFC 4791)
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]
@@ -32,3 +35,9 @@ pub enum PrincipalPropWrapper {
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone)]
pub struct CalendarHomeSet(#[xml(ty = "untagged", flatten)] pub(super) Vec<HrefElement>);
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone)]
pub struct GroupMembership(#[xml(ty = "untagged", flatten)] pub(super) Vec<HrefElement>);
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone)]
pub struct PrincipalCollectionSet(#[xml(ty = "untagged")] pub(super) HrefElement);

View File

@@ -64,6 +64,10 @@ impl Resource for AddressObjectResource {
})
}
fn get_displayname(&self) -> Option<&str> {
self.object.get_full_name()
}
fn get_owner(&self) -> Option<&str> {
Some(&self.principal)
}

View File

@@ -5,10 +5,6 @@ use rustical_xml::{EnumVariants, PropName, XmlDeserialize, XmlSerialize};
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "AddressbookPropName")]
pub enum AddressbookProp {
// WebDAV (RFC 2518)
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
Displayname(Option<String>),
// CardDAV (RFC 6352)
#[xml(ns = "rustical_dav::namespace::NS_CARDDAV")]
AddressbookDescription(Option<String>),

View File

@@ -56,9 +56,6 @@ impl Resource for AddressbookResource {
Ok(match prop {
AddressbookPropWrapperName::Addressbook(prop) => {
AddressbookPropWrapper::Addressbook(match prop {
AddressbookPropName::Displayname => {
AddressbookProp::Displayname(self.0.displayname.clone())
}
AddressbookPropName::MaxResourceSize => {
AddressbookProp::MaxResourceSize(10000000)
}
@@ -89,10 +86,6 @@ impl Resource for AddressbookResource {
fn set_prop(&mut self, prop: Self::Prop) -> Result<(), rustical_dav::Error> {
match prop {
AddressbookPropWrapper::Addressbook(prop) => match prop {
AddressbookProp::Displayname(displayname) => {
self.0.displayname = displayname;
Ok(())
}
AddressbookProp::AddressbookDescription(description) => {
self.0.description = description;
Ok(())
@@ -113,10 +106,6 @@ impl Resource for AddressbookResource {
) -> Result<(), rustical_dav::Error> {
match prop {
AddressbookPropWrapperName::Addressbook(prop) => match prop {
AddressbookPropName::Displayname => {
self.0.displayname = None;
Ok(())
}
AddressbookPropName::AddressbookDescription => {
self.0.description = None;
Ok(())
@@ -135,6 +124,14 @@ impl Resource for AddressbookResource {
}
}
fn get_displayname(&self) -> Option<&str> {
self.0.displayname.as_deref()
}
fn set_displayname(&mut self, name: Option<String>) -> Result<(), rustical_dav::Error> {
self.0.displayname = name;
Ok(())
}
fn get_owner(&self) -> Option<&str> {
Some(&self.0.principal)
}

View File

@@ -22,8 +22,11 @@ pub mod principal;
pub struct CardDavPrincipalUri(&'static str);
impl PrincipalUri for CardDavPrincipalUri {
fn principal_collection(&self) -> String {
format!("{}/principal/", self.0)
}
fn principal_uri(&self, principal: &str) -> String {
format!("{}/principal/{}/", self.0, principal)
format!("{}{}/", self.principal_collection(), principal)
}
}

View File

@@ -44,7 +44,8 @@ impl Resource for PrincipalResource {
let principal_href = HrefElement::new(puri.principal_uri(&user.id));
let home_set = AddressbookHomeSet(
user.memberships()
self.principal
.memberships()
.into_iter()
.map(|principal| puri.principal_uri(principal))
.map(HrefElement::new)
@@ -54,17 +55,26 @@ impl Resource for PrincipalResource {
Ok(match prop {
PrincipalPropWrapperName::Principal(prop) => {
PrincipalPropWrapper::Principal(match prop {
PrincipalPropName::Displayname => PrincipalProp::Displayname(
self.principal
.displayname
.to_owned()
.unwrap_or(self.principal.id.to_owned()),
),
PrincipalPropName::PrincipalUrl => PrincipalProp::PrincipalUrl(principal_href),
PrincipalPropName::AddressbookHomeSet => {
PrincipalProp::AddressbookHomeSet(home_set)
}
PrincipalPropName::PrincipalAddress => PrincipalProp::PrincipalAddress(None),
PrincipalPropName::GroupMembership => {
PrincipalProp::GroupMembership(GroupMembership(
self.principal
.memberships_without_self()
.iter()
.map(|principal| puri.principal_uri(principal).into())
.collect(),
))
}
PrincipalPropName::AlternateUriSet => PrincipalProp::AlternateUriSet,
PrincipalPropName::PrincipalCollectionSet => {
PrincipalProp::PrincipalCollectionSet(PrincipalCollectionSet(
puri.principal_collection().into(),
))
}
})
}
@@ -74,6 +84,15 @@ impl Resource for PrincipalResource {
})
}
fn get_displayname(&self) -> Option<&str> {
Some(
self.principal
.displayname
.as_ref()
.unwrap_or(&self.principal.id),
)
}
fn get_owner(&self) -> Option<&str> {
Some(&self.principal.id)
}

View File

@@ -1,19 +1,19 @@
use rustical_dav::{extensions::CommonPropertiesProp, xml::HrefElement};
use rustical_xml::{EnumVariants, PropName, XmlDeserialize, XmlSerialize};
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone)]
pub struct AddressbookHomeSet(#[xml(ty = "untagged", flatten)] pub(super) Vec<HrefElement>);
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone, EnumVariants, PropName)]
#[xml(unit_variants_ident = "PrincipalPropName")]
pub enum PrincipalProp {
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
Displayname(String),
// WebDAV Access Control (RFC 3744)
#[xml(rename = b"principal-URL")]
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
PrincipalUrl(HrefElement),
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
GroupMembership(GroupMembership),
#[xml(ns = "rustical_dav::namespace::NS_DAV", rename = b"alternate-URI-set")]
AlternateUriSet,
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
PrincipalCollectionSet(PrincipalCollectionSet),
// CardDAV (RFC 6352)
#[xml(ns = "rustical_dav::namespace::NS_CARDDAV")]
@@ -28,3 +28,12 @@ pub enum PrincipalPropWrapper {
Principal(PrincipalProp),
Common(CommonPropertiesProp),
}
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone)]
pub struct AddressbookHomeSet(#[xml(ty = "untagged", flatten)] pub(super) Vec<HrefElement>);
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone)]
pub struct GroupMembership(#[xml(ty = "untagged", flatten)] pub(super) Vec<HrefElement>);
#[derive(XmlDeserialize, XmlSerialize, PartialEq, Clone)]
pub struct PrincipalCollectionSet(#[xml(ty = "untagged")] pub(super) HrefElement);

View File

@@ -13,6 +13,8 @@ pub enum CommonPropertiesProp {
#[xml(skip_deserializing)]
#[xml(ns = "crate::namespace::NS_DAV")]
Resourcetype(Resourcetype),
#[xml(ns = "crate::namespace::NS_DAV")]
Displayname(Option<String>),
// WebDAV Current Principal Extension (RFC 5397)
#[xml(ns = "crate::namespace::NS_DAV")]
@@ -37,6 +39,9 @@ pub trait CommonPropertiesExtension: Resource {
CommonPropertiesPropName::Resourcetype => {
CommonPropertiesProp::Resourcetype(self.get_resourcetype())
}
CommonPropertiesPropName::Displayname => {
CommonPropertiesProp::Displayname(self.get_displayname().map(|s| s.to_string()))
}
CommonPropertiesPropName::CurrentUserPrincipal => {
CommonPropertiesProp::CurrentUserPrincipal(
principal_uri.principal_uri(principal.get_id()).into(),
@@ -52,12 +57,18 @@ pub trait CommonPropertiesExtension: Resource {
})
}
fn set_prop(&self, _prop: CommonPropertiesProp) -> Result<(), crate::Error> {
Err(crate::Error::PropReadOnly)
fn set_prop(&mut self, prop: CommonPropertiesProp) -> Result<(), crate::Error> {
match prop {
CommonPropertiesProp::Displayname(name) => self.set_displayname(name),
_ => Err(crate::Error::PropReadOnly),
}
}
fn remove_prop(&self, _prop: &CommonPropertiesPropName) -> Result<(), crate::Error> {
Err(crate::Error::PropReadOnly)
fn remove_prop(&mut self, prop: &CommonPropertiesPropName) -> Result<(), crate::Error> {
match prop {
CommonPropertiesPropName::Displayname => self.set_displayname(None),
_ => Err(crate::Error::PropReadOnly),
}
}
}

View File

@@ -60,6 +60,11 @@ pub trait Resource: Clone + Send + 'static {
Err(crate::Error::PropReadOnly)
}
fn get_displayname(&self) -> Option<&str>;
fn set_displayname(&mut self, _name: Option<String>) -> Result<(), crate::Error> {
Err(crate::Error::PropReadOnly)
}
fn get_owner(&self) -> Option<&str> {
None
}

View File

@@ -1,3 +1,4 @@
pub trait PrincipalUri: 'static + Clone + Send + Sync {
fn principal_collection(&self) -> String;
fn principal_uri(&self, principal: &str) -> String;
}

View File

@@ -33,6 +33,10 @@ impl<PR: Resource, P: Principal> Resource for RootResource<PR, P> {
)])
}
fn get_displayname(&self) -> Option<&str> {
Some("RustiCal DAV root")
}
fn get_prop(
&self,
principal_uri: &impl PrincipalUri,

View File

@@ -1,5 +1,8 @@
use derive_more::derive::From;
use quick_xml::name::Namespace;
use quick_xml::{
events::{BytesEnd, BytesStart, Event},
name::Namespace,
};
use rustical_xml::{NamespaceOwned, XmlSerialize};
use std::collections::HashMap;
@@ -9,11 +12,37 @@ pub struct TagList(Vec<(Option<NamespaceOwned>, String)>);
impl XmlSerialize for TagList {
fn serialize<W: std::io::Write>(
&self,
_ns: Option<Namespace>,
_tag: Option<&[u8]>,
_namespaces: &HashMap<Namespace, &[u8]>,
ns: Option<Namespace>,
tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> {
let prefix = ns
.map(|ns| namespaces.get(&ns))
.unwrap_or(None)
.map(|prefix| {
if !prefix.is_empty() {
[*prefix, b":"].concat()
} else {
Vec::new()
}
});
let has_prefix = prefix.is_some();
let tagname = tag.map(|tag| [&prefix.unwrap_or_default(), tag].concat());
let qname = tagname
.as_ref()
.map(|tagname| ::quick_xml::name::QName(tagname));
if let Some(qname) = &qname {
let mut bytes_start = BytesStart::from(qname.to_owned());
if !has_prefix {
if let Some(ns) = &ns {
bytes_start.push_attribute((b"xmlns".as_ref(), ns.as_ref()));
}
}
writer.write_event(Event::Start(bytes_start))?;
}
for (ns, tag) in &self.0 {
let mut el = writer.create_element(tag);
if let Some(ns) = ns {
@@ -21,6 +50,10 @@ impl XmlSerialize for TagList {
}
el.write_empty()?;
}
if let Some(qname) = &qname {
writer.write_event(Event::End(BytesEnd::from(qname.to_owned())))?;
}
Ok(())
}

View File

@@ -72,9 +72,9 @@ impl AddressObject {
CalDateTime::parse_prop(prop, &HashMap::default()).ok()
}
pub fn get_full_name(&self) -> Option<&String> {
pub fn get_full_name(&self) -> Option<&str> {
let prop = self.vcard.get_property("FN")?;
prop.value.as_ref()
prop.value.as_deref()
}
pub fn get_anniversary_object(&self) -> Result<Option<CalendarObject>, Error> {

View File

@@ -108,6 +108,10 @@ impl User {
memberships.push(self.id.as_str());
memberships
}
pub fn memberships_without_self(&self) -> Vec<&str> {
self.memberships.iter().map(String::as_str).collect()
}
}
impl rustical_dav::Principal for User {

View File

@@ -92,10 +92,17 @@ impl Enum {
let prop_name_variants = tagged_variants.iter().map(|variant| {
let ident = &variant.variant.ident;
let xml_name = variant.xml_name();
if let Some(proptype) = &variant.attrs.prop {
quote! {#ident(#proptype)}
quote! {
#[xml(rename = #xml_name)]
#ident(#proptype)
}
} else {
quote! {#ident}
quote! {
#[xml(rename = #xml_name)]
#ident
}
}
});