mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 04:42:15 +00:00
xml: Comprehensive refactoring from byte strings to strings
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use darling::{FromDeriveInput, FromField, FromMeta, FromVariant, util::Flag};
|
||||
use syn::{Ident, LitByteStr, LitStr};
|
||||
use syn::{Ident, LitStr};
|
||||
|
||||
#[derive(Debug, Default, FromMeta, Clone)]
|
||||
pub struct TagAttrs {
|
||||
pub rename: Option<LitByteStr>,
|
||||
pub rename: Option<LitStr>,
|
||||
pub ns: Option<syn::Path>,
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ pub struct EnumAttrs {
|
||||
#[derive(Default, FromDeriveInput, Clone)]
|
||||
#[darling(attributes(xml))]
|
||||
pub struct StructAttrs {
|
||||
pub root: Option<LitByteStr>,
|
||||
pub root: Option<LitStr>,
|
||||
pub ns: Option<syn::Path>,
|
||||
#[darling(default)]
|
||||
pub ns_prefix: HashMap<syn::Path, LitStr>,
|
||||
|
||||
@@ -35,7 +35,7 @@ impl Field {
|
||||
}
|
||||
|
||||
/// Field name in XML
|
||||
pub fn xml_name(&self) -> syn::LitByteStr {
|
||||
pub fn xml_name(&self) -> syn::LitStr {
|
||||
if let Some(rename) = self.attrs.common.rename.to_owned() {
|
||||
rename
|
||||
} else {
|
||||
@@ -43,7 +43,7 @@ impl Field {
|
||||
.field_ident()
|
||||
.as_ref()
|
||||
.expect("unnamed tag fields need a rename attribute");
|
||||
syn::LitByteStr::new(ident.to_string().to_kebab_case().as_bytes(), ident.span())
|
||||
syn::LitStr::new(ident.to_string().to_kebab_case().as_str(), ident.span())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,6 +174,8 @@ impl Field {
|
||||
.map(|ns| quote! { if ns == #ns });
|
||||
|
||||
let field_name = self.xml_name();
|
||||
let b_field_name =
|
||||
syn::LitByteStr::new(self.xml_name().value().as_bytes(), field_name.span());
|
||||
let builder_field_ident = self.builder_field_ident();
|
||||
let deserializer = self.deserializer_type();
|
||||
let value = quote! { <#deserializer as rustical_xml::XmlDeserialize>::deserialize(reader, &start, empty)? };
|
||||
@@ -186,7 +188,7 @@ impl Field {
|
||||
};
|
||||
|
||||
Some(quote! {
|
||||
(#namespace_match, #field_name) #namespace_condition => { #assignment; }
|
||||
(#namespace_match, #b_field_name) #namespace_condition => { #assignment; }
|
||||
})
|
||||
}
|
||||
|
||||
@@ -231,6 +233,8 @@ impl Field {
|
||||
}
|
||||
let builder_field_ident = self.builder_field_ident();
|
||||
let field_name = self.xml_name();
|
||||
let b_field_name =
|
||||
syn::LitByteStr::new(self.xml_name().value().as_bytes(), field_name.span());
|
||||
|
||||
let value = wrap_option_if_no_default(
|
||||
quote! {
|
||||
@@ -240,7 +244,7 @@ impl Field {
|
||||
);
|
||||
|
||||
Some(quote! {
|
||||
#field_name => {
|
||||
#b_field_name => {
|
||||
builder.#builder_field_ident = #value;
|
||||
}
|
||||
})
|
||||
@@ -255,7 +259,6 @@ impl Field {
|
||||
let value = quote! {
|
||||
if let ::quick_xml::name::ResolveResult::Bound(ns) = &ns {
|
||||
Some(ns.into())
|
||||
// Some(rustical_xml::ValueDeserialize::deserialize(&String::from_utf8_lossy(ns.0.as_ref()))?)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
pub(crate) fn get_generic_type(ty: &syn::Type) -> Option<&syn::Type> {
|
||||
if let syn::Type::Path(syn::TypePath { path, .. }) = ty {
|
||||
if let Some(seg) = path.segments.last() {
|
||||
if let syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
|
||||
args,
|
||||
..
|
||||
}) = &seg.arguments
|
||||
{
|
||||
if let Some(syn::GenericArgument::Type(t)) = &args.first() {
|
||||
return Some(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
if let syn::Type::Path(syn::TypePath { path, .. }) = ty
|
||||
&& let Some(seg) = path.segments.last()
|
||||
&& let syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
|
||||
args, ..
|
||||
}) = &seg.arguments
|
||||
&& let Some(syn::GenericArgument::Type(t)) = &args.first()
|
||||
{
|
||||
return Some(t);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
@@ -14,13 +14,13 @@ impl Variant {
|
||||
&self.variant.ident
|
||||
}
|
||||
|
||||
pub fn xml_name(&self) -> syn::LitByteStr {
|
||||
pub fn xml_name(&self) -> syn::LitStr {
|
||||
self.attrs
|
||||
.common
|
||||
.rename
|
||||
.to_owned()
|
||||
.unwrap_or(syn::LitByteStr::new(
|
||||
self.ident().to_string().to_kebab_case().as_bytes(),
|
||||
.unwrap_or(syn::LitStr::new(
|
||||
self.ident().to_string().to_kebab_case().as_str(),
|
||||
self.ident().span(),
|
||||
))
|
||||
}
|
||||
@@ -75,6 +75,8 @@ impl Variant {
|
||||
}
|
||||
let ident = self.ident();
|
||||
let variant_name = self.xml_name();
|
||||
let b_variant_name =
|
||||
syn::LitByteStr::new(self.xml_name().value().as_bytes(), variant_name.span());
|
||||
let deserializer_type = self.deserializer_type();
|
||||
|
||||
Some(
|
||||
@@ -93,7 +95,7 @@ impl Variant {
|
||||
panic!("tuple variants should contain exactly one element");
|
||||
}
|
||||
quote! {
|
||||
#variant_name => {
|
||||
#b_variant_name => {
|
||||
let val = Some(<#deserializer_type as ::rustical_xml::XmlDeserialize>::deserialize(reader, start, empty)?);
|
||||
Ok(Self::#ident(val))
|
||||
}
|
||||
@@ -104,7 +106,7 @@ impl Variant {
|
||||
panic!("tuple variants should contain exactly one element");
|
||||
}
|
||||
quote! {
|
||||
#variant_name => {
|
||||
#b_variant_name => {
|
||||
let val = <#deserializer_type as ::rustical_xml::XmlDeserialize>::deserialize(reader, start, empty)?;
|
||||
Ok(Self::#ident(val))
|
||||
}
|
||||
@@ -112,7 +114,7 @@ impl Variant {
|
||||
}
|
||||
(false, Fields::Unit, _) => {
|
||||
quote! {
|
||||
#variant_name => {
|
||||
#b_variant_name => {
|
||||
// Make sure that content is still consumed
|
||||
<() as ::rustical_xml::XmlDeserialize>::deserialize(reader, start, empty)?;
|
||||
Ok(Self::#ident)
|
||||
|
||||
@@ -111,8 +111,7 @@ impl Enum {
|
||||
Some(ns) => quote! { Some(#ns) },
|
||||
None => quote! { None },
|
||||
};
|
||||
let b_xml_name = variant.xml_name().value();
|
||||
let xml_name = String::from_utf8_lossy(&b_xml_name);
|
||||
let xml_name = variant.xml_name().value();
|
||||
let out = quote! {(#ns, #xml_name)};
|
||||
|
||||
let ident = &variant.variant.ident;
|
||||
@@ -134,8 +133,7 @@ impl Enum {
|
||||
|
||||
let str_to_unit_branches = tagged_variants.iter().map(|variant| {
|
||||
let ident = &variant.variant.ident;
|
||||
let b_xml_name = variant.xml_name().value();
|
||||
let xml_name = String::from_utf8_lossy(&b_xml_name);
|
||||
let xml_name = variant.xml_name().value();
|
||||
if variant.attrs.prop.is_some() {
|
||||
quote! { #xml_name => Ok(Self::#ident (Default::default())) }
|
||||
} else {
|
||||
|
||||
@@ -16,8 +16,8 @@ impl Enum {
|
||||
fn serialize(
|
||||
&self,
|
||||
ns: Option<::quick_xml::name::Namespace>,
|
||||
tag: Option<&[u8]>,
|
||||
namespaces: &::std::collections::HashMap<::quick_xml::name::Namespace, &[u8]>,
|
||||
tag: Option<&str>,
|
||||
namespaces: &::std::collections::HashMap<::quick_xml::name::Namespace, &str>,
|
||||
writer: &mut ::quick_xml::Writer<&mut Vec<u8>>
|
||||
) -> ::std::io::Result<()> {
|
||||
use ::quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
|
||||
@@ -25,14 +25,16 @@ impl Enum {
|
||||
let prefix = ns
|
||||
.map(|ns| namespaces.get(&ns))
|
||||
.unwrap_or(None)
|
||||
.map(|prefix| if !prefix.is_empty() {
|
||||
[*prefix, b":"].concat()
|
||||
} else {
|
||||
vec![]
|
||||
});
|
||||
.map(|prefix| {
|
||||
if !prefix.is_empty() {
|
||||
format!("{prefix}:")
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
});
|
||||
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));
|
||||
let qname = tagname.as_ref().map(|tagname| ::quick_xml::name::QName(tagname.as_bytes()));
|
||||
|
||||
const enum_untagged: bool = #enum_untagged;
|
||||
|
||||
|
||||
@@ -108,8 +108,7 @@ impl Enum {
|
||||
Some(ns) => quote! { Some(#ns) },
|
||||
None => quote! { None },
|
||||
};
|
||||
let b_xml_name = variant.xml_name().value();
|
||||
let xml_name = String::from_utf8_lossy(&b_xml_name);
|
||||
let xml_name = variant.xml_name().value();
|
||||
quote! {(#ns, #xml_name)}
|
||||
});
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ impl NamedStruct {
|
||||
let field_index = field.target_field_index();
|
||||
quote! {
|
||||
::quick_xml::events::attributes::Attribute {
|
||||
key: ::quick_xml::name::QName(#field_name),
|
||||
key: ::quick_xml::name::QName(#field_name.as_bytes()),
|
||||
value: ::std::borrow::Cow::from(::rustical_xml::ValueSerialize::serialize(&self.#field_index).into_bytes())
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,7 @@ impl NamedStruct {
|
||||
let field_index = field.target_field_index();
|
||||
quote! {
|
||||
let tag_str = self.#field_index.to_string();
|
||||
let tag = Some(tag.unwrap_or(tag_str.as_bytes()));
|
||||
let tag = Some(tag.unwrap_or(tag_str.as_str()));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -90,8 +90,8 @@ impl NamedStruct {
|
||||
fn serialize(
|
||||
&self,
|
||||
ns: Option<::quick_xml::name::Namespace>,
|
||||
tag: Option<&[u8]>,
|
||||
namespaces: &::std::collections::HashMap<::quick_xml::name::Namespace, &[u8]>,
|
||||
tag: Option<&str>,
|
||||
namespaces: &::std::collections::HashMap<::quick_xml::name::Namespace, &str>,
|
||||
writer: &mut ::quick_xml::Writer<&mut Vec<u8>>
|
||||
) -> ::std::io::Result<()> {
|
||||
use ::quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
|
||||
@@ -104,15 +104,15 @@ impl NamedStruct {
|
||||
.unwrap_or(None)
|
||||
.map(|prefix| {
|
||||
if !prefix.is_empty() {
|
||||
[*prefix, b":"].concat()
|
||||
format!("{prefix}:")
|
||||
} else {
|
||||
Vec::new()
|
||||
String::new()
|
||||
}
|
||||
});
|
||||
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));
|
||||
//
|
||||
let qname = tagname.as_ref().map(|tagname| ::quick_xml::name::QName(tagname.as_bytes()));
|
||||
|
||||
if let Some(qname) = &qname {
|
||||
let mut bytes_start = BytesStart::from(qname.to_owned());
|
||||
if !has_prefix {
|
||||
|
||||
@@ -68,7 +68,7 @@ impl NamedStruct {
|
||||
.ns_prefix
|
||||
.iter()
|
||||
.map(|(ns, prefix)| {
|
||||
quote! { (#ns, #prefix.as_ref()) }
|
||||
quote! { (#ns, #prefix) }
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
@@ -77,9 +77,9 @@ impl NamedStruct {
|
||||
|
||||
quote! {
|
||||
impl #impl_generics ::rustical_xml::XmlRootTag for #ident #type_generics #where_clause {
|
||||
fn root_tag() -> &'static [u8] { #root }
|
||||
fn root_tag() -> &'static str { #root }
|
||||
fn root_ns() -> Option<::quick_xml::name::Namespace<'static>> { #ns }
|
||||
fn root_ns_prefixes() -> ::std::collections::HashMap<::quick_xml::name::Namespace<'static>, &'static [u8]> {
|
||||
fn root_ns_prefixes() -> ::std::collections::HashMap<::quick_xml::name::Namespace<'static>, &'static str> {
|
||||
::std::collections::HashMap::from_iter(vec![
|
||||
#(#prefixes),*
|
||||
])
|
||||
|
||||
Reference in New Issue
Block a user