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

@@ -33,7 +33,7 @@ pub enum AddressObjectProp {
Getcontenttype(String), Getcontenttype(String),
// CalDAV (RFC 4791) // CalDAV (RFC 4791)
#[xml(ns = b"urn:ietf:params:xml:ns:carddav")] #[xml(ns = "rustical_dav::namespace::NS_CARDDAV")]
AddressData(String), AddressData(String),
} }

View File

@@ -37,10 +37,10 @@ pub enum AddressbookProp {
Getcontenttype(String), Getcontenttype(String),
// CardDAV (RFC 6352) // CardDAV (RFC 6352)
#[xml(ns = b"urn:ietf:params:xml:ns:carddav")] #[xml(ns = "rustical_dav::namespace::NS_CARDDAV")]
AddressbookDescription(Option<String>), AddressbookDescription(Option<String>),
#[xml(skip_deserializing)] #[xml(skip_deserializing)]
#[xml(ns = b"urn:ietf:params:xml:ns:carddav")] #[xml(ns = "rustical_dav::namespace::NS_CARDDAV")]
SupportedAddressData(SupportedAddressData), SupportedAddressData(SupportedAddressData),
#[xml(skip_deserializing)] #[xml(skip_deserializing)]
SupportedReportSet(SupportedReportSet), SupportedReportSet(SupportedReportSet),

View File

@@ -36,9 +36,9 @@ pub enum PrincipalProp {
PrincipalUrl(HrefElement), PrincipalUrl(HrefElement),
// CardDAV (RFC 6352) // CardDAV (RFC 6352)
#[xml(ns = b"urn:ietf:params:xml:ns:carddav")] #[xml(ns = "rustical_dav::namespace::NS_CARDDAV")]
AddressbookHomeSet(HrefElement), AddressbookHomeSet(HrefElement),
#[xml(ns = b"urn:ietf:params:xml:ns:carddav")] #[xml(ns = "rustical_dav::namespace::NS_CARDDAV")]
PrincipalAddress(Option<HrefElement>), PrincipalAddress(Option<HrefElement>),
} }

View File

@@ -1,47 +1,9 @@
use quick_xml::events::attributes::Attribute; use quick_xml::name::Namespace;
// An enum keeping track of the XML namespaces used for WebDAV and its extensions pub const NS_DAV: Namespace = Namespace(b"DAV:");
// pub const NS_DAVPUSH: Namespace = Namespace(b"DAV:Push");
// Can also generate appropriate attributes for quick_xml pub const NS_CALDAV: Namespace = Namespace(b"urn:ietf:params:xml:ns:caldav");
pub enum Namespace { pub const NS_CARDDAV: Namespace = Namespace(b"urn:ietf:params:xml:ns:carddav");
Dav, pub const NS_ICAL: Namespace = Namespace(b"http://apple.com/ns/ical/");
DavPush, pub const NS_CALENDARSERVER: Namespace = Namespace(b"http://calendarserver.org/ns/");
CalDAV, pub const NS_NEXTCLOUD: Namespace = Namespace(b"http://nextcloud.com/ns");
CardDAV,
ICal,
CServer,
Nextcloud,
}
impl Namespace {
pub fn as_str(&self) -> &'static str {
match self {
Self::Dav => "DAV:",
Self::DavPush => "DAV:Push",
Self::CalDAV => "urn:ietf:params:xml:ns:caldav",
Self::CardDAV => "urn:ietf:params:xml:ns:carddav",
Self::ICal => "http://apple.com/ns/ical/",
Self::CServer => "http://calendarserver.org/ns/",
Self::Nextcloud => "http://nextcloud.com/ns",
}
}
// Returns an opinionated namespace attribute name
pub fn xml_attr(&self) -> &'static str {
match self {
Self::Dav => "xmlns",
Self::DavPush => "xmlns:P",
Self::CalDAV => "xmlns:C",
Self::CardDAV => "xmlns:CARD",
Self::ICal => "xmlns:IC",
Self::CServer => "xmlns:CS",
Self::Nextcloud => "xmlns:NEXTC",
}
}
}
impl From<Namespace> for Attribute<'static> {
fn from(value: Namespace) -> Self {
(value.xml_attr(), value.as_str()).into()
}
}

View File

@@ -1,5 +1,6 @@
use quick_xml::name::Namespace;
use rustical_xml::{XmlDeserialize, XmlSerialize}; use rustical_xml::{XmlDeserialize, XmlSerialize};
use std::collections::HashSet; use std::collections::{HashMap, HashSet};
#[derive(Debug, Clone, XmlSerialize, XmlDeserialize, Eq, Hash, PartialEq)] #[derive(Debug, Clone, XmlSerialize, XmlDeserialize, Eq, Hash, PartialEq)]
pub enum UserPrivilege { pub enum UserPrivilege {
@@ -16,8 +17,9 @@ pub enum UserPrivilege {
impl XmlSerialize for UserPrivilegeSet { impl XmlSerialize for UserPrivilegeSet {
fn serialize<W: std::io::Write>( fn serialize<W: std::io::Write>(
&self, &self,
ns: Option<&[u8]>, ns: Option<Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>, writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
#[derive(XmlSerialize)] #[derive(XmlSerialize)]
@@ -29,7 +31,7 @@ impl XmlSerialize for UserPrivilegeSet {
FakeUserPrivilegeSet { FakeUserPrivilegeSet {
privileges: self.privileges.iter().cloned().collect(), privileges: self.privileges.iter().cloned().collect(),
} }
.serialize(ns, tag, writer) .serialize(ns, tag, namespaces, writer)
} }
#[allow(refining_impl_trait)] #[allow(refining_impl_trait)]

View File

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

View File

@@ -2,7 +2,7 @@ use rustical_xml::XmlDeserialize;
use rustical_xml::XmlRootTag; use rustical_xml::XmlRootTag;
#[derive(Debug, Clone, XmlDeserialize, XmlRootTag, PartialEq)] #[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 { pub struct PropfindElement {
#[xml(ty = "untagged")] #[xml(ty = "untagged")]
pub prop: PropfindType, pub prop: PropfindType,

View File

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

View File

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

View File

@@ -10,7 +10,8 @@ pub struct ContainerAttrs {
pub struct TagAttrs { pub struct TagAttrs {
pub rename: Option<LitByteStr>, pub rename: Option<LitByteStr>,
pub ns_strict: Flag, pub ns_strict: Flag,
pub ns: Option<LitByteStr>, // pub ns: Option<LitByteStr>,
pub ns: Option<syn::ExprPath>,
} }
#[derive(Default, FromVariant)] #[derive(Default, FromVariant)]
@@ -37,7 +38,7 @@ pub struct StructAttrs {
pub container: ContainerAttrs, pub container: ContainerAttrs,
pub root: Option<LitByteStr>, pub root: Option<LitByteStr>,
pub ns: Option<LitByteStr>, pub ns: Option<syn::ExprPath>,
pub allow_invalid: Flag, pub allow_invalid: Flag,
} }

View File

@@ -148,8 +148,8 @@ impl Field {
} }
let namespace_match = if self.ns_strict() { let namespace_match = if self.ns_strict() {
if let Some(ns) = &self.attrs.common.ns { if self.attrs.common.ns.is_some() {
quote! {quick_xml::name::ResolveResult::Bound(quick_xml::name::Namespace(#ns))} quote! {quick_xml::name::ResolveResult::Bound(ns)}
} else { } else {
quote! {quick_xml::name::ResolveResult::Unbound} quote! {quick_xml::name::ResolveResult::Unbound}
} }
@@ -157,6 +157,16 @@ impl Field {
quote! {_} quote! {_}
}; };
let namespace_condition = if self.ns_strict() {
self.attrs
.common
.ns
.as_ref()
.map(|ns| quote! { if ns == #ns })
} else {
None
};
let field_name = self.xml_name(); let field_name = self.xml_name();
let field_ident = self.field_ident(); let field_ident = self.field_ident();
let deserializer = self.deserializer_type(); let deserializer = self.deserializer_type();
@@ -170,7 +180,7 @@ impl Field {
}; };
Some(quote! { Some(quote! {
(#namespace_match, #field_name) => { #assignment; } (#namespace_match, #field_name) #namespace_condition => { #assignment; }
}) })
} }
@@ -256,6 +266,10 @@ impl Field {
} else { } else {
quote! { ::rustical_xml::XmlSerialize::serialize } quote! { ::rustical_xml::XmlSerialize::serialize }
}; };
let ns = match &self.attrs.common.ns {
Some(ns) => quote! { Some(#ns) },
None => quote! { None },
};
match (&self.attrs.xml_ty, self.attrs.flatten.is_present()) { match (&self.attrs.xml_ty, self.attrs.flatten.is_present()) {
(FieldType::Attr, _) => None, (FieldType::Attr, _) => None,
@@ -269,19 +283,19 @@ impl Field {
}), }),
(FieldType::Tag, true) => Some(quote! { (FieldType::Tag, true) => Some(quote! {
for item in self.#field_ident.iter() { for item in self.#field_ident.iter() {
#serializer(item, None, Some(#field_name), writer)?; #serializer(item, #ns, Some(#field_name), namespaces, writer)?;
} }
}), }),
(FieldType::Tag, false) => Some(quote! { (FieldType::Tag, false) => Some(quote! {
#serializer(&self.#field_ident, None, Some(#field_name), writer)?; #serializer(&self.#field_ident, #ns, Some(#field_name), namespaces, writer)?;
}), }),
(FieldType::Untagged, true) => Some(quote! { (FieldType::Untagged, true) => Some(quote! {
for item in self.#field_ident.iter() { for item in self.#field_ident.iter() {
#serializer(item, None, None, writer)?; #serializer(item, None, None, namespaces, writer)?;
} }
}), }),
(FieldType::Untagged, false) => Some(quote! { (FieldType::Untagged, false) => Some(quote! {
#serializer(&self.#field_ident, None, None, writer)?; #serializer(&self.#field_ident, None, None, namespaces, writer)?;
}), }),
// TODO: Think about what to do here // TODO: Think about what to do here
(FieldType::TagName | FieldType::Namespace, _) => None, (FieldType::TagName | FieldType::Namespace, _) => None,

View File

@@ -176,6 +176,10 @@ impl Variant {
pub fn se_branch(&self) -> proc_macro2::TokenStream { pub fn se_branch(&self) -> proc_macro2::TokenStream {
let ident = self.ident(); let ident = self.ident();
let variant_name = self.xml_name(); let variant_name = self.xml_name();
let ns = match &self.attrs.common.ns {
Some(ns) => quote! { Some(#ns) },
None => quote! { None },
};
match &self.variant.fields { match &self.variant.fields {
Fields::Named(_) => { Fields::Named(_) => {
@@ -190,9 +194,9 @@ impl Variant {
quote! { quote! {
if let Self::#ident(val) = &self { if let Self::#ident(val) = &self {
if !enum_untagged { if !enum_untagged {
::rustical_xml::XmlSerialize::serialize(val, None, Some(#variant_name), writer)?; ::rustical_xml::XmlSerialize::serialize(val, #ns, Some(#variant_name), namespaces, writer)?;
} else { } else {
::rustical_xml::XmlSerialize::serialize(val, None, None, writer)?; ::rustical_xml::XmlSerialize::serialize(val, None, None, namespaces, writer)?;
}; };
} }
} }
@@ -201,9 +205,9 @@ impl Variant {
quote! { quote! {
if let Self::#ident = &self { if let Self::#ident = &self {
if !enum_untagged { if !enum_untagged {
::rustical_xml::XmlSerialize::serialize(&(), None, Some(#variant_name), writer)?; ::rustical_xml::XmlSerialize::serialize(&(), #ns, Some(#variant_name), namespaces, writer)?;
} else { } else {
::rustical_xml::XmlSerialize::serialize(&(), None, None, writer)?; ::rustical_xml::XmlSerialize::serialize(&(), None, None, namespaces, writer)?;
}; };
} }
} }

View File

@@ -99,24 +99,37 @@ impl Enum {
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<W: ::std::io::Write>(
&self, &self,
ns: Option<&[u8]>, ns: Option<::quick_xml::name::Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &::std::collections::HashMap<::quick_xml::name::Namespace, &[u8]>,
writer: &mut ::quick_xml::Writer<W> writer: &mut ::quick_xml::Writer<W>
) -> ::std::io::Result<()> { ) -> ::std::io::Result<()> {
use ::quick_xml::events::{BytesEnd, BytesStart, BytesText, Event}; use ::quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
let tag_str = tag.map(String::from_utf8_lossy); let prefix = ns
.map(|ns| namespaces.get(&ns))
.unwrap_or(None)
.map(|prefix| [*prefix, b":"].concat());
let has_prefix = prefix.is_some();
let tagname = tag.map(|tag| [&prefix.unwrap_or_default(), tag].concat());
let qname = tagname.as_ref().map(|tagname| ::quick_xml::name::QName(tagname));
const enum_untagged: bool = #enum_untagged; const enum_untagged: bool = #enum_untagged;
if let Some(tag) = &tag_str { if let Some(qname) = &qname {
let bytes_start = BytesStart::new(tag.to_owned()); let mut bytes_start = BytesStart::from(qname.to_owned());
if !has_prefix {
if let Some(ns) = &ns {
bytes_start.push_attribute((b"xmlns".as_ref(), ns.as_ref()));
}
}
writer.write_event(Event::Start(bytes_start))?; writer.write_event(Event::Start(bytes_start))?;
} }
#(#variant_serializers);* #(#variant_serializers);*
if let Some(tag) = &tag_str { if let Some(qname) = &qname {
writer.write_event(Event::End(BytesEnd::new(tag.to_owned())))?; writer.write_event(Event::End(BytesEnd::from(qname.to_owned())))?;
} }
Ok(()) Ok(())
} }

View File

@@ -60,7 +60,7 @@ impl NamedStruct {
quote! { quote! {
impl #impl_generics ::rustical_xml::XmlRootTag for #ident #type_generics #where_clause { impl #impl_generics ::rustical_xml::XmlRootTag for #ident #type_generics #where_clause {
fn root_tag() -> &'static [u8] { #root } fn root_tag() -> &'static [u8] { #root }
fn root_ns() -> Option<&'static [u8]> { #ns } fn root_ns() -> Option<::quick_xml::name::Namespace<'static>> { #ns }
} }
} }
} }
@@ -217,7 +217,8 @@ impl NamedStruct {
.map(|field| { .map(|field| {
let field_ident = field.field_ident(); let field_ident = field.field_ident();
quote! { quote! {
let tag_str = Some(tag_str.unwrap_or(self.#field_ident.to_string())); let tag_str = self.#field_ident.to_string();
let tag = Some(tag.unwrap_or(tag_str.as_bytes()));
} }
}); });
@@ -227,17 +228,30 @@ impl NamedStruct {
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<W: ::std::io::Write>(
&self, &self,
ns: Option<&[u8]>, ns: Option<::quick_xml::name::Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &::std::collections::HashMap<::quick_xml::name::Namespace, &[u8]>,
writer: &mut ::quick_xml::Writer<W> writer: &mut ::quick_xml::Writer<W>
) -> ::std::io::Result<()> { ) -> ::std::io::Result<()> {
use ::quick_xml::events::{BytesEnd, BytesStart, BytesText, Event}; use ::quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
let tag_str = tag.map(|a| String::from_utf8_lossy(a).to_string());
#tag_name_field; #tag_name_field;
if let Some(tag) = &tag_str { let prefix = ns
let mut bytes_start = BytesStart::new(tag.to_owned()); .map(|ns| namespaces.get(&ns))
.unwrap_or(None)
.map(|prefix| [*prefix, b":"].concat());
let has_prefix = prefix.is_some();
let tagname = tag.map(|tag| [&prefix.unwrap_or_default(), tag].concat());
let qname = tagname.as_ref().map(|tagname| ::quick_xml::name::QName(tagname));
if let Some(qname) = &qname {
let mut bytes_start = BytesStart::from(qname.to_owned());
if !has_prefix {
if let Some(ns) = &ns {
bytes_start.push_attribute((b"xmlns".as_ref(), ns.as_ref()));
}
}
if let Some(attrs) = self.attributes() { if let Some(attrs) = self.attributes() {
bytes_start.extend_attributes(attrs); bytes_start.extend_attributes(attrs);
} }
@@ -250,8 +264,8 @@ impl NamedStruct {
} }
if !#is_empty { if !#is_empty {
#(#tag_writers);* #(#tag_writers);*
if let Some(tag) = &tag_str { if let Some(qname) = &qname {
writer.write_event(Event::End(BytesEnd::new(tag.to_owned())))?; writer.write_event(Event::End(BytesEnd::from(qname.to_owned())))?;
} }
} }
Ok(()) Ok(())

View File

@@ -54,11 +54,7 @@ impl<T: XmlRootTag + XmlDeserialize> XmlDocument for T {
// Wrong tag // Wrong tag
(_, _, name) if name.as_ref() != Self::root_tag() => false, (_, _, name) if name.as_ref() != Self::root_tag() => false,
// Wrong namespace // Wrong namespace
(Some(root_ns), ns, _) (Some(root_ns), ns, _) if &ResolveResult::Bound(root_ns) != ns => false,
if &ResolveResult::Bound(Namespace(root_ns)) != ns =>
{
false
}
_ => true, _ => true,
}; };
if !matches { if !matches {

View File

@@ -1,5 +1,6 @@
use quick_xml::events::BytesEnd;
use quick_xml::events::{BytesStart, Event}; use quick_xml::events::{BytesStart, Event};
use quick_xml::name::{Namespace, QName};
use std::collections::HashMap;
use std::io::BufRead; use std::io::BufRead;
pub mod de; pub mod de;
@@ -38,14 +39,26 @@ impl XmlDeserialize for () {
impl XmlSerialize for () { impl XmlSerialize for () {
fn serialize<W: std::io::Write>( fn serialize<W: std::io::Write>(
&self, &self,
ns: Option<&[u8]>, ns: Option<Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>, writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
let tag_str = tag.map(String::from_utf8_lossy); let prefix = ns
.map(|ns| namespaces.get(&ns))
if let Some(tag) = &tag_str { .unwrap_or(None)
writer.write_event(Event::Empty(BytesStart::new(tag.clone())))?; .map(|prefix| [*prefix, b":"].concat());
let has_prefix = prefix.is_some();
let tagname = tag.map(|tag| [&prefix.unwrap_or_default(), tag].concat());
let qname = tagname.as_ref().map(|tagname| QName(tagname));
if let Some(qname) = &qname {
let mut bytes_start = BytesStart::from(qname.to_owned());
if !has_prefix {
if let Some(ns) = &ns {
bytes_start.push_attribute((b"xmlns".as_ref(), ns.as_ref()));
}
}
writer.write_event(Event::Empty(bytes_start))?;
} }
Ok(()) Ok(())
} }
@@ -84,5 +97,5 @@ impl XmlDeserialize for Unparsed {
pub trait XmlRootTag { pub trait XmlRootTag {
fn root_tag() -> &'static [u8]; fn root_tag() -> &'static [u8];
fn root_ns() -> Option<&'static [u8]>; fn root_ns() -> Option<Namespace<'static>>;
} }

View File

@@ -1,4 +1,6 @@
use quick_xml::events::attributes::Attribute; use std::collections::HashMap;
use quick_xml::{events::attributes::Attribute, name::Namespace};
pub use xml_derive::XmlSerialize; pub use xml_derive::XmlSerialize;
use crate::XmlRootTag; use crate::XmlRootTag;
@@ -6,8 +8,9 @@ use crate::XmlRootTag;
pub trait XmlSerialize { pub trait XmlSerialize {
fn serialize<W: std::io::Write>( fn serialize<W: std::io::Write>(
&self, &self,
ns: Option<&[u8]>, ns: Option<Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>, writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()>; ) -> std::io::Result<()>;
@@ -17,12 +20,13 @@ pub trait XmlSerialize {
impl<T: XmlSerialize> XmlSerialize for Option<T> { impl<T: XmlSerialize> XmlSerialize for Option<T> {
fn serialize<W: std::io::Write>( fn serialize<W: std::io::Write>(
&self, &self,
ns: Option<&[u8]>, ns: Option<Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>, writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
if let Some(some) = self { if let Some(some) = self {
some.serialize(ns, tag, writer) some.serialize(ns, tag, namespaces, writer)
} else { } else {
Ok(()) Ok(())
} }
@@ -46,6 +50,7 @@ impl<T: XmlSerialize + XmlRootTag> XmlSerializeRoot for T {
&self, &self,
writer: &mut quick_xml::Writer<W>, writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
self.serialize(Self::root_ns(), Some(Self::root_tag()), writer) let namespaces = HashMap::new();
self.serialize(Self::root_ns(), Some(Self::root_tag()), &namespaces, writer)
} }
} }

View File

@@ -1,4 +1,6 @@
use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event}; use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
use quick_xml::name::{Namespace, QName};
use std::collections::HashMap;
use std::num::{ParseFloatError, ParseIntError}; use std::num::{ParseFloatError, ParseIntError};
use std::{convert::Infallible, io::BufRead}; use std::{convert::Infallible, io::BufRead};
use thiserror::Error; use thiserror::Error;
@@ -93,19 +95,30 @@ impl<T: Value> XmlDeserialize for T {
impl<T: Value> XmlSerialize for T { impl<T: Value> XmlSerialize for T {
fn serialize<W: std::io::Write>( fn serialize<W: std::io::Write>(
&self, &self,
ns: Option<&[u8]>, ns: Option<Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut quick_xml::Writer<W>, writer: &mut quick_xml::Writer<W>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
let tag_str = tag.map(String::from_utf8_lossy); let prefix = ns
.map(|ns| namespaces.get(&ns))
if let Some(tag) = &tag_str { .unwrap_or(None)
writer.write_event(Event::Start(BytesStart::new(tag.clone())))?; .map(|prefix| [*prefix, b":"].concat());
let has_prefix = prefix.is_some();
let tagname = tag.map(|tag| [&prefix.unwrap_or_default(), tag].concat());
let qname = tagname.as_ref().map(|tagname| QName(tagname));
if let Some(qname) = &qname {
let mut bytes_start = BytesStart::from(qname.to_owned());
if !has_prefix {
if let Some(ns) = &ns {
bytes_start.push_attribute((b"xmlns".as_ref(), ns.as_ref()));
}
}
writer.write_event(Event::Start(bytes_start))?;
} }
writer.write_event(Event::Text(BytesText::new(&self.serialize())))?; writer.write_event(Event::Text(BytesText::new(&self.serialize())))?;
if let Some(qname) = &qname {
if let Some(tag) = &tag_str { writer.write_event(Event::End(BytesEnd::from(qname.to_owned())))?;
writer.write_event(Event::End(BytesEnd::new(tag.clone())))?;
} }
Ok(()) Ok(())
} }

View File

@@ -1,6 +1,6 @@
use quick_xml::name::Namespace;
use rustical_xml::de::XmlDocument; use rustical_xml::de::XmlDocument;
use rustical_xml::XmlRootTag; use rustical_xml::{Unparsed, XmlDeserialize, XmlRootTag};
use rustical_xml::{Unparsed, XmlDeserialize};
use std::collections::HashSet; use std::collections::HashSet;
#[test] #[test]
@@ -151,10 +151,12 @@ fn test_struct_set() {
#[test] #[test]
fn test_struct_ns() { fn test_struct_ns() {
const NS_HELLO: Namespace = Namespace(b"hello");
#[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)] #[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)]
#[xml(root = b"document", ns_strict)] #[xml(root = b"document", ns_strict)]
struct Document { struct Document {
#[xml(ns = b"hello")] #[xml(ns = "NS_HELLO")]
child: (), child: (),
} }
@@ -164,10 +166,12 @@ fn test_struct_ns() {
#[test] #[test]
fn test_struct_attr() { fn test_struct_attr() {
const NS_HELLO: Namespace = Namespace(b"hello");
#[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)] #[derive(Debug, XmlDeserialize, XmlRootTag, PartialEq)]
#[xml(root = b"document", ns_strict)] #[xml(root = b"document", ns_strict)]
struct Document { struct Document {
#[xml(ns = b"hello")] #[xml(ns = "NS_HELLO")]
child: (), child: (),
#[xml(ty = "attr", default = "Default::default")] #[xml(ty = "attr", default = "Default::default")]
test: String, test: String,

View File

@@ -1,5 +1,9 @@
use std::borrow::{Borrow, Cow}; use std::{
borrow::{Borrow, Cow},
collections::HashMap,
};
use quick_xml::name::Namespace;
use quick_xml::Writer; use quick_xml::Writer;
use rustical_xml::{XmlDocument, XmlRootTag, XmlSerialize, XmlSerializeRoot}; use rustical_xml::{XmlDocument, XmlRootTag, XmlSerialize, XmlSerializeRoot};
use xml_derive::XmlDeserialize; use xml_derive::XmlDeserialize;
@@ -135,11 +139,12 @@ fn test_struct_serialize_with() {
fn serialize_href<W: ::std::io::Write>( fn serialize_href<W: ::std::io::Write>(
val: &str, val: &str,
ns: Option<&[u8]>, ns: Option<Namespace>,
tag: Option<&[u8]>, tag: Option<&[u8]>,
namespaces: &HashMap<Namespace, &[u8]>,
writer: &mut Writer<W>, writer: &mut Writer<W>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
val.to_uppercase().serialize(ns, tag, writer) val.to_uppercase().serialize(ns, tag, namespaces, writer)
} }
let mut buf = Vec::new(); let mut buf = Vec::new();
@@ -188,3 +193,26 @@ fn test_struct_tag_list() {
let out = String::from_utf8(buf).unwrap(); let out = String::from_utf8(buf).unwrap();
dbg!(out); dbg!(out);
} }
#[test]
fn test_struct_ns() {
// const NS: Namespace = Namespace(b"TEST", quick_xml::name::Namespace(b"NS:TEST:"));
const NS: Namespace = quick_xml::name::Namespace(b"NS:TEST:");
#[derive(Debug, XmlRootTag, XmlSerialize)]
#[xml(root = b"document")]
struct Document {
#[xml(ns = "NS", rename = b"okay")]
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)
.unwrap();
let out = String::from_utf8(buf).unwrap();
dbg!(out);
}