xml: Fix dumb bug where default value would be evaluated and panic even when not needed

This commit is contained in:
Lennart
2025-05-02 20:38:27 +02:00
parent 6dc948ee78
commit 630a4600c2
4 changed files with 28 additions and 10 deletions

View File

@@ -1,7 +1,7 @@
[package]
name = "xml_derive"
version = "0.1.0"
edition = "2021"
edition = "2024"
[lib]
proc-macro = true

View File

@@ -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)]

View File

@@ -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