diff --git a/crates/xml/derive/Cargo.toml b/crates/xml/derive/Cargo.toml index a607a04..3a02950 100644 --- a/crates/xml/derive/Cargo.toml +++ b/crates/xml/derive/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "xml_derive" version = "0.1.0" -edition = "2021" +edition = "2024" [lib] proc-macro = true diff --git a/crates/xml/derive/src/attrs.rs b/crates/xml/derive/src/attrs.rs index d0d8053..f845ff9 100644 --- a/crates/xml/derive/src/attrs.rs +++ b/crates/xml/derive/src/attrs.rs @@ -1,9 +1,9 @@ use std::collections::HashMap; -use darling::{util::Flag, FromDeriveInput, FromField, FromMeta, FromVariant}; +use darling::{FromDeriveInput, FromField, FromMeta, FromVariant, util::Flag}; use syn::LitByteStr; -#[derive(Default, FromMeta, Clone)] +#[derive(Debug, Default, FromMeta, Clone)] pub struct TagAttrs { pub rename: Option, pub ns: Option, @@ -35,7 +35,7 @@ pub struct StructAttrs { pub allow_invalid: Flag, } -#[derive(Default, FromMeta, PartialEq)] +#[derive(Debug, Default, FromMeta, PartialEq)] pub enum FieldType { #[default] Tag, @@ -46,7 +46,7 @@ pub enum FieldType { Namespace, } -#[derive(Default, FromField)] +#[derive(Debug, Default, FromField)] #[darling(attributes(xml))] pub struct FieldAttrs { #[darling(flatten)] diff --git a/crates/xml/derive/src/field.rs b/crates/xml/derive/src/field.rs index c0e283c..8a6f302 100644 --- a/crates/xml/derive/src/field.rs +++ b/crates/xml/derive/src/field.rs @@ -4,8 +4,8 @@ use super::{ }; use darling::FromField; use heck::ToKebabCase; -use quote::quote; use quote::ToTokens; +use quote::quote; use syn::spanned::Spanned; fn wrap_option_if_no_default( @@ -36,13 +36,15 @@ impl Field { /// Field name in XML pub fn xml_name(&self) -> syn::LitByteStr { - self.attrs.common.rename.to_owned().unwrap_or({ + if let Some(rename) = self.attrs.common.rename.to_owned() { + rename + } else { let ident = self .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()) - }) + } } /// Field identifier diff --git a/crates/xml/tests/se_struct.rs b/crates/xml/tests/se_struct.rs index 4aaaa6e..10d8391 100644 --- a/crates/xml/tests/se_struct.rs +++ b/crates/xml/tests/se_struct.rs @@ -3,8 +3,8 @@ use std::{ collections::HashMap, }; -use quick_xml::name::Namespace; use quick_xml::Writer; +use quick_xml::name::Namespace; use rustical_xml::{XmlDocument, XmlRootTag, XmlSerialize, XmlSerializeRoot}; use xml_derive::XmlDeserialize; @@ -196,7 +196,6 @@ fn test_struct_tag_list() { #[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)] @@ -239,3 +238,20 @@ fn test_struct_tuple() { let out = String::from_utf8(buf).unwrap(); dbg!(out); } + +#[test] +fn test_tuple_struct() { + 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")] String); + + let mut buf = Vec::new(); + let mut writer = quick_xml::Writer::new(&mut buf); + Document("hello!".to_string()) + .serialize_root(&mut writer) + .unwrap(); + let out = String::from_utf8(buf).unwrap(); + dbg!(out); +}