xml: namespace serialization

This commit is contained in:
Lennart
2024-12-31 18:20:02 +01:00
parent 61e2dab37f
commit 098e374e4d
20 changed files with 215 additions and 133 deletions

View File

@@ -1,9 +1,12 @@
use crate::{namespace::Namespace, xml::TagList};
use std::collections::HashMap;
use crate::xml::TagList;
use actix_web::{
body::BoxBody,
http::{header::ContentType, StatusCode},
HttpRequest, HttpResponse, Responder, ResponseError,
};
use quick_xml::name::Namespace;
use rustical_xml::{XmlRootTag, XmlSerialize, XmlSerializeRoot};
// Intermediate struct because of a serde limitation, see following article:
@@ -25,11 +28,12 @@ pub struct PropstatElement<PropType: XmlSerialize> {
fn xml_serialize_status<W: ::std::io::Write>(
status: &StatusCode,
ns: Option<&[u8]>,
ns: Option<Namespace>,
tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> {
XmlSerialize::serialize(&format!("HTTP/1.1 {}", status), ns, tag, writer)
XmlSerialize::serialize(&format!("HTTP/1.1 {}", status), ns, tag, namespaces, writer)
}
#[derive(XmlSerialize)]
@@ -53,14 +57,16 @@ pub struct ResponseElement<PropstatType: XmlSerialize> {
fn xml_serialize_optional_status<W: ::std::io::Write>(
val: &Option<StatusCode>,
ns: Option<&[u8]>,
ns: Option<Namespace>,
tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> {
XmlSerialize::serialize(
&val.map(|status| format!("HTTP/1.1 {}", status)),
ns,
tag,
namespaces,
writer,
)
}
@@ -86,12 +92,12 @@ pub struct MultistatusElement<PropType: XmlSerialize, MemberPropType: XmlSeriali
#[xml(rename = b"response", flatten)]
pub member_responses: Vec<ResponseElement<MemberPropType>>,
// TODO: napespaces
pub ns_dav: &'static str,
pub ns_davpush: &'static str,
pub ns_caldav: &'static str,
pub ns_ical: &'static str,
pub ns_calendarserver: &'static str,
pub ns_carddav: &'static str,
// pub ns_dav: &'static str,
// pub ns_davpush: &'static str,
// pub ns_caldav: &'static str,
// pub ns_ical: &'static str,
// pub ns_calendarserver: &'static str,
// pub ns_carddav: &'static str,
pub sync_token: Option<String>,
}
@@ -100,12 +106,12 @@ impl<T1: XmlSerialize, T2: XmlSerialize> Default for MultistatusElement<T1, T2>
Self {
responses: vec![],
member_responses: vec![],
ns_dav: Namespace::Dav.as_str(),
ns_davpush: Namespace::DavPush.as_str(),
ns_caldav: Namespace::CalDAV.as_str(),
ns_ical: Namespace::ICal.as_str(),
ns_calendarserver: Namespace::CServer.as_str(),
ns_carddav: Namespace::CardDAV.as_str(),
// ns_dav: Namespace::Dav.as_str(),
// ns_davpush: Namespace::DavPush.as_str(),
// ns_caldav: Namespace::CalDAV.as_str(),
// ns_ical: Namespace::ICal.as_str(),
// ns_calendarserver: Namespace::CServer.as_str(),
// ns_carddav: Namespace::CardDAV.as_str(),
sync_token: None,
}
}

View File

@@ -2,7 +2,7 @@ use rustical_xml::XmlDeserialize;
use rustical_xml::XmlRootTag;
#[derive(Debug, Clone, XmlDeserialize, XmlRootTag, PartialEq)]
#[xml(root = b"propfind", ns = b"DAV:")]
#[xml(root = b"propfind", ns = "crate::namespace::NS_DAV")]
pub struct PropfindElement {
#[xml(ty = "untagged")]
pub prop: PropfindType,

View File

@@ -1,5 +1,8 @@
use std::collections::HashMap;
use quick_xml::events::attributes::Attribute;
use quick_xml::events::{BytesEnd, BytesStart, Event};
use quick_xml::name::Namespace;
use rustical_xml::XmlSerialize;
#[derive(Debug, Clone, PartialEq)]
@@ -8,8 +11,9 @@ pub struct Resourcetype(pub &'static [&'static str]);
impl XmlSerialize for Resourcetype {
fn serialize<W: std::io::Write>(
&self,
ns: Option<&[u8]>,
ns: Option<Namespace>,
tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> {
let tag_str = tag.map(String::from_utf8_lossy);

View File

@@ -1,5 +1,7 @@
use derive_more::derive::From;
use quick_xml::name::Namespace;
use rustical_xml::XmlSerialize;
use std::collections::HashMap;
#[derive(Clone, Debug, PartialEq, From)]
pub struct TagList(Vec<String>);
@@ -7,8 +9,9 @@ pub struct TagList(Vec<String>);
impl XmlSerialize for TagList {
fn serialize<W: std::io::Write>(
&self,
ns: Option<&[u8]>,
ns: Option<Namespace>,
tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> {
#[derive(Debug, XmlSerialize, PartialEq)]
@@ -25,7 +28,7 @@ impl XmlSerialize for TagList {
Inner {
tags: self.0.iter().map(|t| Tag { name: t.to_owned() }).collect(),
}
.serialize(ns, tag, writer)
.serialize(ns, tag, namespaces, writer)
}
#[allow(refining_impl_trait)]