diff --git a/crates/dav/src/xml/multistatus.rs b/crates/dav/src/xml/multistatus.rs index a8d04f4..82ee050 100644 --- a/crates/dav/src/xml/multistatus.rs +++ b/crates/dav/src/xml/multistatus.rs @@ -111,11 +111,10 @@ impl axum::response::IntoResponse fn into_response(self) -> axum::response::Response { use axum::body::Body; - let mut output: Vec<_> = b"\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(); diff --git a/crates/dav/src/xml/resourcetype.rs b/crates/dav/src/xml/resourcetype.rs index fe106b5..cf6b912 100644 --- a/crates/dav/src/xml/resourcetype.rs +++ b/crates/dav/src/xml/resourcetype.rs @@ -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, - "" + "\n" ) } } diff --git a/crates/dav_push/src/lib.rs b/crates/dav_push/src/lib.rs index ac385f5..0c03a0b 100644 --- a/crates/dav_push/src/lib.rs +++ b/crates/dav_push/src/lib.rs @@ -99,13 +99,13 @@ impl DavPushController { content_update, }; - let mut output: Vec<_> = b"\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 { diff --git a/crates/xml/src/se.rs b/crates/xml/src/se.rs index a790b9a..f8dd249 100644 --- a/crates/xml/src/se.rs +++ b/crates/xml/src/se.rs @@ -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, ) -> std::io::Result<()>; + + fn serialize_to_string(&self) -> std::io::Result { + let mut buf: Vec<_> = b"\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 XmlSerializeRoot for T { diff --git a/crates/xml/tests/de_struct.rs b/crates/xml/tests/de_struct.rs index 8273c04..410fd01 100644 --- a/crates/xml/tests/de_struct.rs +++ b/crates/xml/tests/de_struct.rs @@ -198,6 +198,7 @@ fn test_struct_generics() { #[derive(XmlDeserialize, XmlRootTag)] #[xml(root = b"document")] struct Document { + #[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, } diff --git a/crates/xml/tests/enum_variants.rs b/crates/xml/tests/enum_variants.rs index 1526108..41356e6 100644 --- a/crates/xml/tests/enum_variants.rs +++ b/crates/xml/tests/enum_variants.rs @@ -23,12 +23,16 @@ enum ExtensionProp { enum CalendarProp { // WebDAV (RFC 2518) #[xml(ns = "NS_DAV")] + #[allow(dead_code)] Displayname(Option), #[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, } diff --git a/crates/xml/tests/propertyupdate.rs b/crates/xml/tests/propertyupdate.rs index d6d5a75..0dac1c7 100644 --- a/crates/xml/tests/propertyupdate.rs +++ b/crates/xml/tests/propertyupdate.rs @@ -4,29 +4,34 @@ use rustical_xml::{Unparsed, XmlDeserialize, XmlDocument, XmlRootTag}; fn test_propertyupdate() { #[derive(XmlDeserialize)] struct SetPropertyElement { + #[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 { Set(SetPropertyElement), + #[allow(dead_code)] Remove(RemovePropertyElement), } @@ -34,10 +39,11 @@ fn test_propertyupdate() { #[xml(root = b"propertyupdate")] struct PropertyupdateElement { #[xml(ty = "untagged", flatten)] + #[allow(dead_code)] operations: Vec>, } - let doc = PropertyupdateElement::::parse_str( + PropertyupdateElement::::parse_str( r#" diff --git a/crates/xml/tests/se_enum.rs b/crates/xml/tests/se_enum.rs index dcbb9e5..d93e3b9 100644 --- a/crates/xml/tests/se_enum.rs +++ b/crates/xml/tests/se_enum.rs @@ -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, "asd"); + assert_eq!( + out, + "\nasd" + ); } diff --git a/crates/xml/tests/se_struct.rs b/crates/xml/tests/se_struct.rs index 10d8391..f791e04 100644 --- a/crates/xml/tests/se_struct.rs +++ b/crates/xml/tests/se_struct.rs @@ -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, "okay123"); + assert_eq!( + out, + "\nokay123" + ); } #[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, "okays"); + assert_eq!( + out, + "\nokays" + ); } #[test] @@ -114,17 +104,14 @@ fn test_struct_vec() { href: Vec, } - 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, - "okaywow" + "\nokaywow" ); } @@ -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, "OKAY"); + assert_eq!( + out, + "\nOKAY" + ); } #[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); }