Migrate mkcol/mkcalendar to rustical_xml

This commit is contained in:
Lennart
2024-12-23 23:13:18 +01:00
parent 72844aa94e
commit 92b7be5591
6 changed files with 100 additions and 46 deletions

View File

@@ -2,48 +2,49 @@ use crate::Error;
use actix_web::web::Path;
use actix_web::{web::Data, HttpResponse};
use rustical_store::{auth::User, Addressbook, AddressbookStore};
use serde::{Deserialize, Serialize};
use rustical_xml::{Unit, XmlDeserialize, XmlDocument, XmlRootTag};
use tracing::instrument;
use tracing_actix_web::RootSpan;
#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
#[derive(XmlDeserialize, Clone, Debug, PartialEq)]
pub struct Resourcetype {
#[serde(rename = "CARD:addressbook", alias = "addressbook")]
addressbook: Option<()>,
collection: Option<()>,
addressbook: Option<Unit>,
collection: Option<Unit>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
#[derive(XmlDeserialize, Clone, Debug, PartialEq)]
pub struct MkcolAddressbookProp {
resourcetype: Option<Resourcetype>,
displayname: Option<String>,
#[xml(rename = b"addressbook-description")]
description: Option<String>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct PropElement<T: Serialize> {
#[derive(XmlDeserialize, Clone, Debug, PartialEq)]
pub struct PropElement<T: XmlDeserialize> {
prop: T,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
#[serde(rename = "mkcol")]
#[derive(XmlDeserialize, XmlRootTag, Clone, Debug, PartialEq)]
#[xml(root = b"mkcol")]
struct MkcolRequest {
set: PropElement<MkcolAddressbookProp>,
}
#[instrument(parent = root_span.id(), skip(store, root_span))]
pub async fn route_mkcol<AS: AddressbookStore + ?Sized>(
path: Path<(String, String)>,
body: String,
user: User,
store: Data<AS>,
root_span: RootSpan,
) -> Result<HttpResponse, Error> {
let (principal, addressbook_id) = path.into_inner();
if principal != user.id {
return Err(Error::Unauthorized);
}
let request: MkcolRequest = quick_xml::de::from_str(&body)?;
let request = MkcolRequest::parse_str(&body)?;
let request = request.set.prop;
let addressbook = Addressbook {
@@ -81,3 +82,42 @@ pub async fn route_mkcol<AS: AddressbookStore + ?Sized>(
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_xml_mkcol() {
let mkcol_request = MkcolRequest::parse_str(r#"
<?xml version='1.0' encoding='UTF-8' ?>
<mkcol xmlns="DAV:" xmlns:CAL="urn:ietf:params:xml:ns:caldav" xmlns:CARD="urn:ietf:params:xml:ns:carddav">
<set>
<prop>
<resourcetype>
<collection />
<CARD:addressbook />
</resourcetype>
<displayname>whoops</displayname>
<CARD:addressbook-description>okay</CARD:addressbook-description>
</prop>
</set>
</mkcol>
"#).unwrap();
assert_eq!(
mkcol_request,
MkcolRequest {
set: PropElement {
prop: MkcolAddressbookProp {
resourcetype: Some(Resourcetype {
addressbook: Some(Unit),
collection: Some(Unit)
}),
displayname: Some("whoops".to_owned()),
description: Some("okay".to_owned())
}
}
}
)
}
}