xml use tuple structs

This commit is contained in:
Lennart
2025-01-05 17:06:15 +01:00
parent 2eb6b1934d
commit 9ea9beb143
17 changed files with 57 additions and 141 deletions

View File

@@ -9,13 +9,8 @@ use actix_web::{
use quick_xml::name::Namespace;
use rustical_xml::{XmlRootTag, XmlSerialize, XmlSerializeRoot};
// Intermediate struct because of a serde limitation, see following article:
// https://stackoverflow.com/questions/78444158/unsupportedcannot-serialize-enum-newtype-variant-exampledata
#[derive(XmlSerialize)]
pub struct PropTagWrapper<T: XmlSerialize> {
#[xml(flatten, ty = "untagged")]
pub prop: Vec<T>,
}
pub struct PropTagWrapper<T: XmlSerialize>(#[xml(flatten, ty = "untagged")] pub Vec<T>);
// RFC 2518
// <!ELEMENT propstat (prop, status, responsedescription?) >

View File

@@ -4,21 +4,15 @@ use rustical_xml::XmlRootTag;
#[derive(Debug, Clone, XmlDeserialize, XmlRootTag, PartialEq)]
#[xml(root = b"propfind", ns = "crate::namespace::NS_DAV")]
pub struct PropfindElement {
#[xml(ty = "untagged")]
#[xml(ns = "crate::namespace::NS_DAV")]
pub prop: PropfindType,
}
#[derive(Debug, Clone, XmlDeserialize, PartialEq)]
pub struct PropElement {
#[xml(ty = "untagged", flatten)]
pub prop: Vec<Propname>,
}
pub struct PropElement(#[xml(ty = "untagged", flatten)] pub Vec<Propname>);
#[derive(Debug, Clone, XmlDeserialize, PartialEq)]
pub struct Propname {
#[xml(ty = "tag_name")]
pub name: String,
}
pub struct Propname(#[xml(ty = "tag_name")] pub String);
#[derive(Debug, Clone, XmlDeserialize, PartialEq)]
pub enum PropfindType {

View File

@@ -4,12 +4,10 @@ use rustical_xml::XmlSerialize;
pub struct Resourcetype(#[xml(flatten, ty = "untagged")] pub &'static [ResourcetypeInner]);
#[derive(Debug, Clone, PartialEq, XmlSerialize)]
pub struct ResourcetypeInner {
#[xml(ty = "namespace")]
pub ns: quick_xml::name::Namespace<'static>,
#[xml(ty = "tag_name")]
pub name: &'static str,
}
pub struct ResourcetypeInner(
#[xml(ty = "namespace")] pub quick_xml::name::Namespace<'static>,
#[xml(ty = "tag_name")] pub &'static str,
);
#[cfg(test)]
mod tests {
@@ -29,14 +27,8 @@ mod tests {
let mut writer = quick_xml::Writer::new(&mut buf);
Document {
resourcetype: Resourcetype(&[
ResourcetypeInner {
ns: crate::namespace::NS_DAV,
name: "displayname",
},
ResourcetypeInner {
ns: crate::namespace::NS_CALENDARSERVER,
name: "calendar-color",
},
ResourcetypeInner(crate::namespace::NS_DAV, "displayname"),
ResourcetypeInner(crate::namespace::NS_CALENDARSERVER, "calendar-color"),
]),
}
.serialize_root(&mut writer)

View File

@@ -15,20 +15,12 @@ impl XmlSerialize for TagList {
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> {
#[derive(Debug, XmlSerialize, PartialEq)]
struct Inner {
#[xml(ty = "untagged", flatten)]
tags: Vec<Tag>,
}
struct Inner(#[xml(ty = "untagged", flatten)] Vec<Tag>);
#[derive(Debug, XmlSerialize, PartialEq)]
struct Tag {
#[xml(ty = "tag_name")]
name: String,
}
Inner {
tags: self.0.iter().map(|t| Tag { name: t.to_owned() }).collect(),
}
.serialize(ns, tag, namespaces, writer)
struct Tag(#[xml(ty = "tag_name")] String);
Inner(self.0.iter().map(|t| Tag(t.to_owned())).collect())
.serialize(ns, tag, namespaces, writer)
}
#[allow(refining_impl_trait)]
@@ -36,12 +28,3 @@ impl XmlSerialize for TagList {
None
}
}
impl TagList {
pub fn inner(&self) -> &Vec<String> {
&self.0
}
pub fn into_inner(self) -> Vec<String> {
self.0
}
}