mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 20:32:48 +00:00
xml: Make serialization more ergonomic and clippy appeasement
This commit is contained in:
@@ -111,11 +111,10 @@ impl<T1: XmlSerialize, T2: XmlSerialize> axum::response::IntoResponse
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
use axum::body::Body;
|
||||
|
||||
let mut output: Vec<_> = b"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".into();
|
||||
let mut writer = quick_xml::Writer::new_with_indent(&mut output, b' ', 4);
|
||||
if let Err(err) = self.serialize_root(&mut writer) {
|
||||
return crate::Error::from(err).into_response();
|
||||
}
|
||||
let output = match self.serialize_to_string() {
|
||||
Ok(out) => out,
|
||||
Err(err) => return crate::Error::from(err).into_response(),
|
||||
};
|
||||
|
||||
let mut resp = axum::response::Response::builder().status(StatusCode::MULTI_STATUS);
|
||||
let hdrs = resp.headers_mut().unwrap();
|
||||
|
||||
@@ -23,20 +23,17 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_serialize_resourcetype() {
|
||||
let mut buf = Vec::new();
|
||||
let mut writer = quick_xml::Writer::new(&mut buf);
|
||||
Document {
|
||||
let out = Document {
|
||||
resourcetype: Resourcetype(&[
|
||||
ResourcetypeInner(Some(crate::namespace::NS_DAV), "displayname"),
|
||||
ResourcetypeInner(Some(crate::namespace::NS_CALENDARSERVER), "calendar-color"),
|
||||
]),
|
||||
}
|
||||
.serialize_root(&mut writer)
|
||||
.serialize_to_string()
|
||||
.unwrap();
|
||||
let out = String::from_utf8(buf).unwrap();
|
||||
assert_eq!(
|
||||
out,
|
||||
"<document><resourcetype><displayname xmlns=\"DAV:\"/><calendar-color xmlns=\"http://calendarserver.org/ns/\"/></resourcetype></document>"
|
||||
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<document><resourcetype><displayname xmlns=\"DAV:\"/><calendar-color xmlns=\"http://calendarserver.org/ns/\"/></resourcetype></document>"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,13 +99,13 @@ impl<S: SubscriptionStore> DavPushController<S> {
|
||||
content_update,
|
||||
};
|
||||
|
||||
let mut output: Vec<_> = b"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".into();
|
||||
let mut writer = quick_xml::Writer::new_with_indent(&mut output, b' ', 4);
|
||||
if let Err(err) = push_message.serialize_root(&mut writer) {
|
||||
error!("Could not serialize push message: {}", err);
|
||||
return;
|
||||
}
|
||||
let payload = String::from_utf8(output).unwrap();
|
||||
let payload = match push_message.serialize_to_string() {
|
||||
Ok(payload) => payload,
|
||||
Err(err) => {
|
||||
error!("Could not serialize push message: {}", err);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
for subsciption in subscriptions {
|
||||
if let Some(allowed_push_servers) = &self.allowed_push_servers {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::XmlRootTag;
|
||||
use quick_xml::{
|
||||
events::{attributes::Attribute, BytesStart, Event},
|
||||
events::{BytesStart, Event, attributes::Attribute},
|
||||
name::{Namespace, QName},
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
@@ -44,6 +44,13 @@ pub trait XmlSerializeRoot {
|
||||
&self,
|
||||
writer: &mut quick_xml::Writer<W>,
|
||||
) -> std::io::Result<()>;
|
||||
|
||||
fn serialize_to_string(&self) -> std::io::Result<String> {
|
||||
let mut buf: Vec<_> = b"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".into();
|
||||
let mut writer = quick_xml::Writer::new(&mut buf);
|
||||
self.serialize_root(&mut writer)?;
|
||||
Ok(String::from_utf8_lossy(&buf).to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: XmlSerialize + XmlRootTag> XmlSerializeRoot for T {
|
||||
|
||||
@@ -198,6 +198,7 @@ fn test_struct_generics() {
|
||||
#[derive(XmlDeserialize, XmlRootTag)]
|
||||
#[xml(root = b"document")]
|
||||
struct Document<T: XmlDeserialize> {
|
||||
#[allow(dead_code)]
|
||||
child: T,
|
||||
}
|
||||
|
||||
@@ -218,6 +219,7 @@ fn test_struct_unparsed() {
|
||||
#[derive(XmlDeserialize, XmlRootTag)]
|
||||
#[xml(root = b"document")]
|
||||
struct Document {
|
||||
#[allow(dead_code)]
|
||||
child: Unparsed,
|
||||
}
|
||||
|
||||
|
||||
@@ -23,12 +23,16 @@ enum ExtensionProp {
|
||||
enum CalendarProp {
|
||||
// WebDAV (RFC 2518)
|
||||
#[xml(ns = "NS_DAV")]
|
||||
#[allow(dead_code)]
|
||||
Displayname(Option<String>),
|
||||
#[xml(ns = "NS_DAV")]
|
||||
#[allow(dead_code)]
|
||||
Getcontenttype(&'static str),
|
||||
|
||||
#[xml(ns = "NS_DAV", rename = b"principal-URL")]
|
||||
#[allow(dead_code)]
|
||||
PrincipalUrl,
|
||||
#[allow(dead_code)]
|
||||
Topic,
|
||||
}
|
||||
|
||||
|
||||
@@ -4,29 +4,34 @@ use rustical_xml::{Unparsed, XmlDeserialize, XmlDocument, XmlRootTag};
|
||||
fn test_propertyupdate() {
|
||||
#[derive(XmlDeserialize)]
|
||||
struct SetPropertyElement<T: XmlDeserialize> {
|
||||
#[allow(dead_code)]
|
||||
prop: T,
|
||||
}
|
||||
|
||||
#[derive(XmlDeserialize)]
|
||||
struct TagName {
|
||||
#[xml(ty = "tag_name")]
|
||||
#[allow(dead_code)]
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[derive(XmlDeserialize)]
|
||||
struct PropertyElement {
|
||||
#[xml(ty = "untagged")]
|
||||
#[allow(dead_code)]
|
||||
property: TagName,
|
||||
}
|
||||
|
||||
#[derive(XmlDeserialize)]
|
||||
struct RemovePropertyElement {
|
||||
#[allow(dead_code)]
|
||||
prop: PropertyElement,
|
||||
}
|
||||
|
||||
#[derive(XmlDeserialize)]
|
||||
enum Operation<T: XmlDeserialize> {
|
||||
Set(SetPropertyElement<T>),
|
||||
#[allow(dead_code)]
|
||||
Remove(RemovePropertyElement),
|
||||
}
|
||||
|
||||
@@ -34,10 +39,11 @@ fn test_propertyupdate() {
|
||||
#[xml(root = b"propertyupdate")]
|
||||
struct PropertyupdateElement<T: XmlDeserialize> {
|
||||
#[xml(ty = "untagged", flatten)]
|
||||
#[allow(dead_code)]
|
||||
operations: Vec<Operation<T>>,
|
||||
}
|
||||
|
||||
let doc = PropertyupdateElement::<Unparsed>::parse_str(
|
||||
PropertyupdateElement::<Unparsed>::parse_str(
|
||||
r#"
|
||||
<propertyupdate>
|
||||
<set>
|
||||
|
||||
@@ -11,17 +11,17 @@ fn test_struct_value_tagged() {
|
||||
#[derive(Debug, XmlSerialize, PartialEq)]
|
||||
enum Prop {
|
||||
Test(String),
|
||||
Hello(usize),
|
||||
Unit,
|
||||
// Hello(usize),
|
||||
// Unit,
|
||||
}
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut writer = quick_xml::Writer::new(&mut buf);
|
||||
Document {
|
||||
let out = Document {
|
||||
prop: Prop::Test("asd".to_owned()),
|
||||
}
|
||||
.serialize_root(&mut writer)
|
||||
.serialize_to_string()
|
||||
.unwrap();
|
||||
let out = String::from_utf8(buf).unwrap();
|
||||
assert_eq!(out, "<propfind><prop><test>asd</test></prop></propfind>");
|
||||
assert_eq!(
|
||||
out,
|
||||
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<propfind><prop><test>asd</test></prop></propfind>"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
use std::{
|
||||
borrow::{Borrow, Cow},
|
||||
collections::HashMap,
|
||||
};
|
||||
|
||||
use quick_xml::Writer;
|
||||
use quick_xml::name::Namespace;
|
||||
use rustical_xml::{XmlDocument, XmlRootTag, XmlSerialize, XmlSerializeRoot};
|
||||
use rustical_xml::{XmlRootTag, XmlSerialize, XmlSerializeRoot};
|
||||
use std::collections::HashMap;
|
||||
use xml_derive::XmlDeserialize;
|
||||
|
||||
#[test]
|
||||
@@ -22,16 +18,13 @@ fn test_struct_document() {
|
||||
text: String,
|
||||
}
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut writer = quick_xml::Writer::new(&mut buf);
|
||||
Document {
|
||||
child: Child {
|
||||
text: "asd".to_owned(),
|
||||
},
|
||||
}
|
||||
.serialize_root(&mut writer)
|
||||
.serialize_to_string()
|
||||
.unwrap();
|
||||
let out = String::from_utf8(buf).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -51,17 +44,14 @@ fn test_struct_untagged_attr() {
|
||||
text: String,
|
||||
}
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut writer = quick_xml::Writer::new(&mut buf);
|
||||
Document {
|
||||
name: "okay".to_owned(),
|
||||
child: Child {
|
||||
text: "asd".to_owned(),
|
||||
},
|
||||
}
|
||||
.serialize_root(&mut writer)
|
||||
.serialize_to_string()
|
||||
.unwrap();
|
||||
let out = String::from_utf8(buf).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -73,16 +63,16 @@ fn test_struct_value_tagged() {
|
||||
num: usize,
|
||||
}
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut writer = quick_xml::Writer::new(&mut buf);
|
||||
Document {
|
||||
let out = Document {
|
||||
href: "okay".to_owned(),
|
||||
num: 123,
|
||||
}
|
||||
.serialize_root(&mut writer)
|
||||
.serialize_to_string()
|
||||
.unwrap();
|
||||
let out = String::from_utf8(buf).unwrap();
|
||||
assert_eq!(out, "<document><href>okay</href><num>123</num></document>");
|
||||
assert_eq!(
|
||||
out,
|
||||
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<document><href>okay</href><num>123</num></document>"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -94,15 +84,15 @@ fn test_struct_value_untagged() {
|
||||
href: String,
|
||||
}
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut writer = quick_xml::Writer::new(&mut buf);
|
||||
Document {
|
||||
let out = Document {
|
||||
href: "okays".to_owned(),
|
||||
}
|
||||
.serialize_root(&mut writer)
|
||||
.serialize_to_string()
|
||||
.unwrap();
|
||||
let out = String::from_utf8(buf).unwrap();
|
||||
assert_eq!(out, "<document>okays</document>");
|
||||
assert_eq!(
|
||||
out,
|
||||
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<document>okays</document>"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -114,17 +104,14 @@ fn test_struct_vec() {
|
||||
href: Vec<String>,
|
||||
}
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut writer = quick_xml::Writer::new(&mut buf);
|
||||
Document {
|
||||
let out = Document {
|
||||
href: vec!["okay".to_owned(), "wow".to_owned()],
|
||||
}
|
||||
.serialize_root(&mut writer)
|
||||
.serialize_to_string()
|
||||
.unwrap();
|
||||
let out = String::from_utf8(buf).unwrap();
|
||||
assert_eq!(
|
||||
out,
|
||||
"<document><href>okay</href><href>wow</href></document>"
|
||||
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<document><href>okay</href><href>wow</href></document>"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -147,15 +134,15 @@ fn test_struct_serialize_with() {
|
||||
val.to_uppercase().serialize(ns, tag, namespaces, writer)
|
||||
}
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut writer = quick_xml::Writer::new(&mut buf);
|
||||
Document {
|
||||
let out = Document {
|
||||
href: "okay".to_owned(),
|
||||
}
|
||||
.serialize_root(&mut writer)
|
||||
.serialize_to_string()
|
||||
.unwrap();
|
||||
let out = String::from_utf8(buf).unwrap();
|
||||
assert_eq!(out, "<document><href>OKAY</href></document>");
|
||||
assert_eq!(
|
||||
out,
|
||||
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<document><href>OKAY</href></document>"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -173,8 +160,6 @@ fn test_struct_tag_list() {
|
||||
name: String,
|
||||
}
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut writer = quick_xml::Writer::new(&mut buf);
|
||||
Document {
|
||||
tags: vec![
|
||||
Tag {
|
||||
@@ -188,10 +173,8 @@ fn test_struct_tag_list() {
|
||||
},
|
||||
],
|
||||
}
|
||||
.serialize_root(&mut writer)
|
||||
.serialize_to_string()
|
||||
.unwrap();
|
||||
let out = String::from_utf8(buf).unwrap();
|
||||
dbg!(out);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -205,15 +188,11 @@ fn test_struct_ns() {
|
||||
child: String,
|
||||
}
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut writer = quick_xml::Writer::new(&mut buf);
|
||||
Document {
|
||||
child: "hello!".to_string(),
|
||||
}
|
||||
.serialize_root(&mut writer)
|
||||
.serialize_to_string()
|
||||
.unwrap();
|
||||
let out = String::from_utf8(buf).unwrap();
|
||||
dbg!(out);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -227,16 +206,11 @@ fn test_struct_tuple() {
|
||||
#[derive(Debug, XmlSerialize, PartialEq, Default)]
|
||||
struct Child(#[xml(ty = "tag_name")] String, #[xml(ty = "text")] String);
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut writer = quick_xml::Writer::new(&mut buf);
|
||||
|
||||
Document {
|
||||
child: Child("child".to_owned(), "Hello!".to_owned()),
|
||||
}
|
||||
.serialize_root(&mut writer)
|
||||
.serialize_to_string()
|
||||
.unwrap();
|
||||
let out = String::from_utf8(buf).unwrap();
|
||||
dbg!(out);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -247,11 +221,7 @@ fn test_tuple_struct() {
|
||||
#[xml(root = b"document")]
|
||||
struct Document(#[xml(ns = "NS", rename = b"okay")] String);
|
||||
|
||||
let mut buf = Vec::new();
|
||||
let mut writer = quick_xml::Writer::new(&mut buf);
|
||||
Document("hello!".to_string())
|
||||
.serialize_root(&mut writer)
|
||||
.serialize_to_string()
|
||||
.unwrap();
|
||||
let out = String::from_utf8(buf).unwrap();
|
||||
dbg!(out);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user