mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 20:32:48 +00:00
xml: Fix dumb bug where default value would be evaluated and panic even when not needed
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "xml_derive"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
edition = "2024"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
@@ -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<LitByteStr>,
|
||||
pub ns: Option<syn::Path>,
|
||||
@@ -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)]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user