xml: Remove generics from XmlSerialize

This commit is contained in:
Lennart
2025-07-02 19:02:25 +02:00
parent a18ff2b400
commit d8e4bd1cc4
9 changed files with 26 additions and 32 deletions

View File

@@ -17,7 +17,7 @@ pub enum Precondition {
impl IntoResponse for Precondition { impl IntoResponse for Precondition {
fn into_response(self) -> axum::response::Response { fn into_response(self) -> axum::response::Response {
let mut output: Vec<_> = b"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".into(); 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); let mut writer = quick_xml::Writer::new_with_indent(output.as_mut_slice(), b' ', 4);
let error = rustical_dav::xml::ErrorElement(&self); let error = rustical_dav::xml::ErrorElement(&self);
if let Err(err) = error.serialize_root(&mut writer) { if let Err(err) = error.serialize_root(&mut writer) {

View File

@@ -16,12 +16,12 @@ pub enum UserPrivilege {
} }
impl XmlSerialize for UserPrivilegeSet { impl XmlSerialize for UserPrivilegeSet {
fn serialize<W: std::io::Write>( fn serialize(
&self, &self,
ns: Option<Namespace>, ns: Option<Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>, namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>, writer: &mut quick_xml::Writer<&mut [u8]>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
#[derive(XmlSerialize)] #[derive(XmlSerialize)]
pub struct FakeUserPrivilegeSet { pub struct FakeUserPrivilegeSet {

View File

@@ -19,12 +19,12 @@ pub struct PropstatElement<PropType: XmlSerialize> {
pub status: StatusCode, pub status: StatusCode,
} }
fn xml_serialize_status<W: ::std::io::Write>( fn xml_serialize_status(
status: &StatusCode, status: &StatusCode,
ns: Option<Namespace>, ns: Option<Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>, namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>, writer: &mut quick_xml::Writer<&mut [u8]>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
XmlSerialize::serialize(&format!("HTTP/1.1 {}", status), ns, tag, namespaces, writer) XmlSerialize::serialize(&format!("HTTP/1.1 {}", status), ns, tag, namespaces, writer)
} }
@@ -49,12 +49,12 @@ pub struct ResponseElement<PropstatType: XmlSerialize> {
pub propstat: Vec<PropstatWrapper<PropstatType>>, pub propstat: Vec<PropstatWrapper<PropstatType>>,
} }
fn xml_serialize_optional_status<W: ::std::io::Write>( fn xml_serialize_optional_status(
val: &Option<StatusCode>, val: &Option<StatusCode>,
ns: Option<Namespace>, ns: Option<Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>, namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>, writer: &mut quick_xml::Writer<&mut [u8]>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
XmlSerialize::serialize( XmlSerialize::serialize(
&val.map(|status| format!("HTTP/1.1 {}", status)), &val.map(|status| format!("HTTP/1.1 {}", status)),

View File

@@ -10,12 +10,12 @@ use std::collections::HashMap;
pub struct TagList(Vec<(Option<NamespaceOwned>, String)>); pub struct TagList(Vec<(Option<NamespaceOwned>, String)>);
impl XmlSerialize for TagList { impl XmlSerialize for TagList {
fn serialize<W: std::io::Write>( fn serialize(
&self, &self,
ns: Option<Namespace>, ns: Option<Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>, namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>, writer: &mut quick_xml::Writer<&mut [u8]>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
let prefix = ns let prefix = ns
.map(|ns| namespaces.get(&ns)) .map(|ns| namespaces.get(&ns))

View File

@@ -13,12 +13,12 @@ impl Enum {
quote! { quote! {
impl #impl_generics ::rustical_xml::XmlSerialize for #ident #type_generics #where_clause { impl #impl_generics ::rustical_xml::XmlSerialize for #ident #type_generics #where_clause {
fn serialize<W: ::std::io::Write>( fn serialize(
&self, &self,
ns: Option<::quick_xml::name::Namespace>, ns: Option<::quick_xml::name::Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &::std::collections::HashMap<::quick_xml::name::Namespace, &[u8]>, namespaces: &::std::collections::HashMap<::quick_xml::name::Namespace, &[u8]>,
writer: &mut ::quick_xml::Writer<W> writer: &mut ::quick_xml::Writer<&mut [u8]>
) -> ::std::io::Result<()> { ) -> ::std::io::Result<()> {
use ::quick_xml::events::{BytesEnd, BytesStart, BytesText, Event}; use ::quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};

View File

@@ -88,12 +88,12 @@ impl NamedStruct {
quote! { quote! {
impl #impl_generics ::rustical_xml::XmlSerialize for #ident #type_generics #where_clause { impl #impl_generics ::rustical_xml::XmlSerialize for #ident #type_generics #where_clause {
fn serialize<W: ::std::io::Write>( fn serialize(
&self, &self,
ns: Option<::quick_xml::name::Namespace>, ns: Option<::quick_xml::name::Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &::std::collections::HashMap<::quick_xml::name::Namespace, &[u8]>, namespaces: &::std::collections::HashMap<::quick_xml::name::Namespace, &[u8]>,
writer: &mut ::quick_xml::Writer<W> writer: &mut ::quick_xml::Writer<&mut [u8]>
) -> ::std::io::Result<()> { ) -> ::std::io::Result<()> {
use ::quick_xml::events::{BytesEnd, BytesStart, BytesText, Event}; use ::quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};

View File

@@ -7,24 +7,24 @@ use std::collections::HashMap;
pub use xml_derive::XmlSerialize; pub use xml_derive::XmlSerialize;
pub trait XmlSerialize { pub trait XmlSerialize {
fn serialize<W: std::io::Write>( fn serialize(
&self, &self,
ns: Option<Namespace>, ns: Option<Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>, namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>, writer: &mut quick_xml::Writer<&mut [u8]>,
) -> std::io::Result<()>; ) -> std::io::Result<()>;
fn attributes<'a>(&self) -> Option<Vec<Attribute<'a>>>; fn attributes<'a>(&self) -> Option<Vec<Attribute<'a>>>;
} }
impl<T: XmlSerialize> XmlSerialize for Option<T> { impl<T: XmlSerialize> XmlSerialize for Option<T> {
fn serialize<W: std::io::Write>( fn serialize(
&self, &self,
ns: Option<Namespace>, ns: Option<Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>, namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>, writer: &mut quick_xml::Writer<&mut [u8]>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
if let Some(some) = self { if let Some(some) = self {
some.serialize(ns, tag, namespaces, writer) some.serialize(ns, tag, namespaces, writer)
@@ -39,36 +39,30 @@ impl<T: XmlSerialize> XmlSerialize for Option<T> {
} }
pub trait XmlSerializeRoot { pub trait XmlSerializeRoot {
fn serialize_root<W: std::io::Write>( fn serialize_root(&self, writer: &mut quick_xml::Writer<&mut [u8]>) -> std::io::Result<()>;
&self,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()>;
fn serialize_to_string(&self) -> std::io::Result<String> { 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 buf: Vec<_> = b"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".into();
let mut writer = quick_xml::Writer::new(&mut buf); let mut writer = quick_xml::Writer::new(buf.as_mut_slice());
self.serialize_root(&mut writer)?; self.serialize_root(&mut writer)?;
Ok(String::from_utf8_lossy(&buf).to_string()) Ok(String::from_utf8_lossy(&buf).to_string())
} }
} }
impl<T: XmlSerialize + XmlRootTag> XmlSerializeRoot for T { impl<T: XmlSerialize + XmlRootTag> XmlSerializeRoot for T {
fn serialize_root<W: std::io::Write>( fn serialize_root(&self, writer: &mut quick_xml::Writer<&mut [u8]>) -> std::io::Result<()> {
&self,
writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> {
let namespaces = Self::root_ns_prefixes(); let namespaces = Self::root_ns_prefixes();
self.serialize(Self::root_ns(), Some(Self::root_tag()), &namespaces, writer) self.serialize(Self::root_ns(), Some(Self::root_tag()), &namespaces, writer)
} }
} }
impl XmlSerialize for () { impl XmlSerialize for () {
fn serialize<W: std::io::Write>( fn serialize(
&self, &self,
ns: Option<Namespace>, ns: Option<Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>, namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>, writer: &mut quick_xml::Writer<&mut [u8]>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
let prefix = ns let prefix = ns
.map(|ns| namespaces.get(&ns)) .map(|ns| namespaces.get(&ns))

View File

@@ -104,12 +104,12 @@ impl<T: ValueDeserialize> XmlDeserialize for T {
} }
impl<T: ValueSerialize> XmlSerialize for T { impl<T: ValueSerialize> XmlSerialize for T {
fn serialize<W: std::io::Write>( fn serialize(
&self, &self,
ns: Option<Namespace>, ns: Option<Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>, namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>, writer: &mut quick_xml::Writer<&mut [u8]>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
let prefix = ns let prefix = ns
.map(|ns| namespaces.get(&ns)) .map(|ns| namespaces.get(&ns))

View File

@@ -124,12 +124,12 @@ fn test_struct_serialize_with() {
href: String, href: String,
} }
fn serialize_href<W: ::std::io::Write>( fn serialize_href(
val: &str, val: &str,
ns: Option<Namespace>, ns: Option<Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>, namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut Writer<W>, writer: &mut Writer<&mut [u8]>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
val.to_uppercase().serialize(ns, tag, namespaces, writer) val.to_uppercase().serialize(ns, tag, namespaces, writer)
} }