mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 15:52:27 +00:00
rustical_xml: Use darling for proc-macro parsing
This commit is contained in:
@@ -12,3 +12,4 @@ quote = "1.0"
|
||||
proc-macro2 = "1.0"
|
||||
heck = "0.5.0"
|
||||
strum.workspace = true
|
||||
darling = "0.20"
|
||||
|
||||
@@ -1,231 +1,48 @@
|
||||
use core::panic;
|
||||
use darling::{util::Flag, FromDeriveInput, FromField, FromMeta, FromVariant};
|
||||
use syn::LitByteStr;
|
||||
|
||||
use heck::{ToKebabCase, ToPascalCase};
|
||||
use quote::ToTokens;
|
||||
use strum::EnumString;
|
||||
use syn::{
|
||||
punctuated::Punctuated, token::Comma, Attribute, Expr, ExprLit, Lit, LitByteStr, LitStr, Meta,
|
||||
};
|
||||
#[derive(Default, FromMeta)]
|
||||
pub struct ContainerAttrs {
|
||||
pub ns_strict: Flag,
|
||||
}
|
||||
|
||||
const ATTR_SCOPE: &str = "xml";
|
||||
#[derive(Default, FromMeta)]
|
||||
pub struct TagAttrs {
|
||||
pub rename: Option<LitByteStr>,
|
||||
pub ns_strict: Flag,
|
||||
pub ns: Option<LitByteStr>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, FromVariant)]
|
||||
#[darling(attributes(xml))]
|
||||
pub struct VariantAttrs {
|
||||
pub rename: Option<String>,
|
||||
pub ns: Option<String>,
|
||||
#[darling(flatten)]
|
||||
pub common: TagAttrs,
|
||||
}
|
||||
|
||||
pub fn get_scoped_attrs(attrs: &[Attribute]) -> Option<Punctuated<Meta, Comma>> {
|
||||
attrs
|
||||
.iter()
|
||||
.find(|attr| attr.path().is_ident(ATTR_SCOPE))
|
||||
.map(|attr| {
|
||||
attr.parse_args_with(Punctuated::<Meta, Comma>::parse_terminated)
|
||||
.unwrap()
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parse_variant_attrs(attrs: &[Attribute]) -> VariantAttrs {
|
||||
let mut variant_attrs = VariantAttrs::default();
|
||||
|
||||
let attrs = get_scoped_attrs(attrs);
|
||||
|
||||
let attrs = if let Some(attrs) = attrs {
|
||||
attrs
|
||||
} else {
|
||||
return variant_attrs;
|
||||
};
|
||||
|
||||
for meta in attrs {
|
||||
match meta {
|
||||
// single flag
|
||||
Meta::Path(path) => {
|
||||
panic!("unrecognized variant flag: {}", path.to_token_stream());
|
||||
}
|
||||
Meta::List(list) => {
|
||||
panic!("list-type attrs not supported: {}", list.to_token_stream());
|
||||
}
|
||||
Meta::NameValue(name_value) => {
|
||||
if name_value.path.is_ident("ns") {
|
||||
if let Expr::Lit(ExprLit {
|
||||
lit: Lit::Str(lit_str),
|
||||
..
|
||||
}) = name_value.value
|
||||
{
|
||||
variant_attrs.ns = Some(lit_str.value());
|
||||
}
|
||||
} else if name_value.path.is_ident("rename") {
|
||||
if let Expr::Lit(ExprLit {
|
||||
lit: Lit::Str(lit_str),
|
||||
..
|
||||
}) = name_value.value
|
||||
{
|
||||
variant_attrs.rename = Some(lit_str.value());
|
||||
}
|
||||
} else {
|
||||
panic!(
|
||||
"unrecognized variant attribute: {}",
|
||||
name_value.to_token_stream()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
variant_attrs
|
||||
}
|
||||
|
||||
#[derive(EnumString)]
|
||||
pub enum CaseStyle {
|
||||
#[strum(serialize = "kebab-case")]
|
||||
KebabCase,
|
||||
#[strum(serialize = "PascalCase")]
|
||||
PascalCase,
|
||||
}
|
||||
|
||||
impl CaseStyle {
|
||||
fn transform_text(&self, input: &str) -> String {
|
||||
match self {
|
||||
Self::KebabCase => input.to_kebab_case(),
|
||||
Self::PascalCase => input.to_pascal_case(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, FromDeriveInput)]
|
||||
#[darling(attributes(xml))]
|
||||
pub struct EnumAttrs {
|
||||
pub case_style: Option<CaseStyle>,
|
||||
pub ns_strict: bool,
|
||||
#[darling(flatten)]
|
||||
container: ContainerAttrs,
|
||||
}
|
||||
|
||||
fn parse_enum_attrs(attrs: &[Attribute]) -> EnumAttrs {
|
||||
let enum_attrs = EnumAttrs::default();
|
||||
|
||||
enum_attrs
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, FromDeriveInput)]
|
||||
#[darling(attributes(xml))]
|
||||
pub struct StructAttrs {
|
||||
#[darling(flatten)]
|
||||
pub container: ContainerAttrs,
|
||||
|
||||
pub root: Option<LitByteStr>,
|
||||
}
|
||||
|
||||
pub fn parse_struct_attrs(attrs: &[Attribute]) -> StructAttrs {
|
||||
let mut struct_attrs = StructAttrs::default();
|
||||
|
||||
let attrs = get_scoped_attrs(attrs);
|
||||
let attrs = if let Some(attrs) = attrs {
|
||||
attrs
|
||||
} else {
|
||||
return struct_attrs;
|
||||
};
|
||||
|
||||
for meta in attrs {
|
||||
match meta {
|
||||
// single flag
|
||||
Meta::Path(_path) => {
|
||||
panic!("invalid path attribute")
|
||||
}
|
||||
Meta::List(list) => {
|
||||
panic!("list-type attrs not supported: {}", list.to_token_stream());
|
||||
}
|
||||
Meta::NameValue(name_value) => {
|
||||
if name_value.path.is_ident("root") {
|
||||
if let Expr::Lit(ExprLit {
|
||||
lit: Lit::ByteStr(lit_str),
|
||||
..
|
||||
}) = name_value.value
|
||||
{
|
||||
struct_attrs.root = Some(lit_str);
|
||||
}
|
||||
} else {
|
||||
panic!(
|
||||
"unrecognized field attribute: {}",
|
||||
name_value.to_token_stream()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct_attrs
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, FromField)]
|
||||
#[darling(attributes(xml))]
|
||||
pub struct FieldAttrs {
|
||||
pub rename: Option<String>,
|
||||
pub ns: Option<String>,
|
||||
pub text: bool,
|
||||
pub untagged: bool,
|
||||
pub flatten: bool,
|
||||
#[darling(flatten)]
|
||||
pub common: TagAttrs,
|
||||
pub text: Flag,
|
||||
pub untagged: Flag,
|
||||
pub flatten: Flag,
|
||||
pub default: Option<syn::ExprPath>,
|
||||
}
|
||||
|
||||
pub fn parse_field_attrs(attrs: &[Attribute]) -> FieldAttrs {
|
||||
let mut field_attrs = FieldAttrs::default();
|
||||
|
||||
let attrs = get_scoped_attrs(attrs);
|
||||
let attrs = if let Some(attrs) = attrs {
|
||||
attrs
|
||||
} else {
|
||||
return field_attrs;
|
||||
};
|
||||
|
||||
for meta in attrs {
|
||||
match meta {
|
||||
// single flag
|
||||
Meta::Path(path) => {
|
||||
if path.is_ident("text") {
|
||||
field_attrs.text = true;
|
||||
}
|
||||
if path.is_ident("untagged") {
|
||||
field_attrs.untagged = true;
|
||||
}
|
||||
if path.is_ident("flatten") {
|
||||
field_attrs.flatten = true;
|
||||
}
|
||||
}
|
||||
Meta::List(list) => {
|
||||
panic!("list-type attrs not supported: {}", list.to_token_stream());
|
||||
}
|
||||
Meta::NameValue(name_value) => {
|
||||
if name_value.path.is_ident("ns") {
|
||||
if let Expr::Lit(ExprLit {
|
||||
lit: Lit::Str(lit_str),
|
||||
..
|
||||
}) = name_value.value
|
||||
{
|
||||
field_attrs.ns = Some(lit_str.value());
|
||||
}
|
||||
} else if name_value.path.is_ident("rename") {
|
||||
if let Expr::Lit(ExprLit {
|
||||
lit: Lit::Str(lit_str),
|
||||
..
|
||||
}) = name_value.value
|
||||
{
|
||||
field_attrs.rename = Some(lit_str.value());
|
||||
} else {
|
||||
panic!("invalid rename attribute");
|
||||
}
|
||||
} else if name_value.path.is_ident("default") {
|
||||
if let Expr::Lit(ExprLit {
|
||||
lit: Lit::Str(lit_str),
|
||||
..
|
||||
}) = name_value.value
|
||||
{
|
||||
let a: syn::ExprPath = syn::parse_str(&lit_str.value())
|
||||
.expect("could not parse default attribute as expression");
|
||||
field_attrs.default = Some(a);
|
||||
} else {
|
||||
panic!("invalid default attribute");
|
||||
}
|
||||
} else {
|
||||
panic!(
|
||||
"unrecognized field attribute: {}",
|
||||
name_value.to_token_stream()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
field_attrs
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
use crate::de::attrs::parse_variant_attrs;
|
||||
use proc_macro2::Span;
|
||||
use darling::FromVariant;
|
||||
use heck::ToKebabCase;
|
||||
use quote::quote;
|
||||
use syn::{DataEnum, DeriveInput, Fields, FieldsUnnamed, Variant};
|
||||
|
||||
use crate::de::attrs::VariantAttrs;
|
||||
|
||||
pub fn enum_variant_branch(variant: &Variant) -> proc_macro2::TokenStream {
|
||||
let ident = &variant.ident;
|
||||
|
||||
@@ -35,12 +37,13 @@ pub fn impl_de_enum(input: &DeriveInput, data: &DataEnum) -> proc_macro2::TokenS
|
||||
let name = &input.ident;
|
||||
|
||||
let variants = data.variants.iter().map(|variant| {
|
||||
let attrs = parse_variant_attrs(&variant.attrs);
|
||||
|
||||
let variant_name = attrs.rename.unwrap_or(variant.ident.to_string());
|
||||
let variant_name = syn::LitByteStr::new(variant_name.as_bytes(), Span::call_site());
|
||||
let attrs = VariantAttrs::from_variant(variant).unwrap();
|
||||
let variant_name = attrs.common.rename.unwrap_or(syn::LitByteStr::new(
|
||||
variant.ident.to_string().to_kebab_case().as_bytes(),
|
||||
variant.ident.span(),
|
||||
));
|
||||
let branch = enum_variant_branch(variant);
|
||||
// dbg!(variant.fields.to_token_stream());
|
||||
dbg!(&variant_name);
|
||||
|
||||
quote! {
|
||||
#variant_name => {
|
||||
@@ -55,17 +58,16 @@ pub fn impl_de_enum(input: &DeriveInput, data: &DataEnum) -> proc_macro2::TokenS
|
||||
reader: &mut quick_xml::NsReader<R>,
|
||||
start: &quick_xml::events::BytesStart,
|
||||
empty: bool
|
||||
) -> Result<Self, rustical_xml::XmlError> {
|
||||
) -> Result<Self, rustical_xml::XmlDeError> {
|
||||
use quick_xml::events::Event;
|
||||
|
||||
let (_ns, name) = reader.resolve_element(start.name());
|
||||
|
||||
|
||||
match name.as_ref() {
|
||||
#(#variants)*
|
||||
name => {
|
||||
// Handle invalid variant name
|
||||
Err(rustical_xml::XmlError::InvalidVariant(String::from_utf8_lossy(name).to_string()))
|
||||
Err(rustical_xml::XmlDeError::InvalidVariant(String::from_utf8_lossy(name).to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use super::attrs::{parse_field_attrs, parse_struct_attrs, FieldAttrs};
|
||||
use crate::de::attrs::StructAttrs;
|
||||
|
||||
use super::attrs::FieldAttrs;
|
||||
use core::panic;
|
||||
use darling::{FromDeriveInput, FromField};
|
||||
use heck::ToKebabCase;
|
||||
use proc_macro2::Span;
|
||||
use quote::quote;
|
||||
use syn::{AngleBracketedGenericArguments, DataStruct, DeriveInput, TypePath};
|
||||
use syn::{AngleBracketedGenericArguments, DataStruct, DeriveInput, LitByteStr, TypePath};
|
||||
|
||||
fn get_generic_type(ty: &syn::Type) -> Option<&syn::Type> {
|
||||
if let syn::Type::Path(TypePath { path, .. }) = ty {
|
||||
@@ -29,15 +31,19 @@ pub struct Field {
|
||||
impl Field {
|
||||
fn from_syn_field(field: syn::Field) -> Self {
|
||||
Self {
|
||||
attrs: parse_field_attrs(&field.attrs),
|
||||
attrs: FieldAttrs::from_field(&field).unwrap(),
|
||||
field,
|
||||
}
|
||||
}
|
||||
fn de_name(&self) -> String {
|
||||
fn de_name(&self) -> LitByteStr {
|
||||
self.attrs
|
||||
.common
|
||||
.rename
|
||||
.to_owned()
|
||||
.unwrap_or(self.field_ident().to_string().to_kebab_case())
|
||||
.unwrap_or(LitByteStr::new(
|
||||
self.field_ident().to_string().to_kebab_case().as_bytes(),
|
||||
self.field_ident().span(),
|
||||
))
|
||||
}
|
||||
|
||||
fn field_ident(&self) -> &syn::Ident {
|
||||
@@ -58,7 +64,7 @@ impl Field {
|
||||
quote! {
|
||||
#field_ident: #ty,
|
||||
}
|
||||
} else if self.attrs.flatten {
|
||||
} else if self.attrs.flatten.is_present() {
|
||||
let generic_type = get_generic_type(ty).expect("flatten attribute only implemented for explicit generics (rustical_xml will assume the first generic as the inner type)");
|
||||
quote! {
|
||||
#field_ident: Vec<#generic_type>,
|
||||
@@ -76,7 +82,7 @@ impl Field {
|
||||
quote! {
|
||||
#field_ident: #default(),
|
||||
}
|
||||
} else if self.attrs.flatten {
|
||||
} else if self.attrs.flatten.is_present() {
|
||||
quote! {
|
||||
#field_ident: vec![],
|
||||
}
|
||||
@@ -89,7 +95,7 @@ impl Field {
|
||||
|
||||
fn builder_field_build(&self) -> proc_macro2::TokenStream {
|
||||
let field_ident = self.field_ident();
|
||||
if self.attrs.flatten {
|
||||
if self.attrs.flatten.is_present() {
|
||||
quote! {
|
||||
#field_ident: FromIterator::from_iter(builder.#field_ident.into_iter())
|
||||
}
|
||||
@@ -105,13 +111,13 @@ impl Field {
|
||||
}
|
||||
|
||||
fn named_branch(&self) -> Option<proc_macro2::TokenStream> {
|
||||
if self.attrs.text {
|
||||
if self.attrs.text.is_present() {
|
||||
return None;
|
||||
}
|
||||
if self.attrs.untagged {
|
||||
if self.attrs.untagged.is_present() {
|
||||
return None;
|
||||
}
|
||||
let field_name = syn::LitByteStr::new(self.de_name().as_bytes(), Span::call_site());
|
||||
let field_name = self.de_name();
|
||||
let field_ident = self.field_ident();
|
||||
let deserializer = self.ty();
|
||||
Some(if self.attrs.default.is_some() {
|
||||
@@ -120,7 +126,7 @@ impl Field {
|
||||
builder.#field_ident = <#deserializer as rustical_xml::XmlDeserialize>::deserialize(reader, &start, empty)?;
|
||||
},
|
||||
}
|
||||
} else if self.attrs.flatten {
|
||||
} else if self.attrs.flatten.is_present() {
|
||||
let deserializer = get_generic_type(self.ty()).expect("flatten attribute only implemented for explicit generics (rustical_xml will assume the first generic as the inner type)");
|
||||
quote! {
|
||||
#field_name => {
|
||||
@@ -137,20 +143,29 @@ impl Field {
|
||||
}
|
||||
|
||||
fn untagged_branch(&self) -> Option<proc_macro2::TokenStream> {
|
||||
if !self.attrs.untagged {
|
||||
if !self.attrs.untagged.is_present() {
|
||||
return None;
|
||||
}
|
||||
let field_ident = self.field_ident();
|
||||
let deserializer = self.ty();
|
||||
Some(quote! {
|
||||
_ => {
|
||||
builder.#field_ident = Some(<#deserializer as rustical_xml::XmlDeserialize>::deserialize(reader, &start, empty)?);
|
||||
},
|
||||
Some(if self.attrs.flatten.is_present() {
|
||||
let deserializer = get_generic_type(self.ty()).expect("flatten attribute only implemented for explicit generics (rustical_xml will assume the first generic as the inner type)");
|
||||
quote! {
|
||||
_ => {
|
||||
builder.#field_ident.push(<#deserializer as rustical_xml::XmlDeserialize>::deserialize(reader, &start, empty)?);
|
||||
},
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
_ => {
|
||||
builder.#field_ident = Some(<#deserializer as rustical_xml::XmlDeserialize>::deserialize(reader, &start, empty)?);
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn text_branch(&self) -> Option<proc_macro2::TokenStream> {
|
||||
if !self.attrs.text {
|
||||
if !self.attrs.text.is_present() {
|
||||
return None;
|
||||
}
|
||||
let field_ident = self.field_ident();
|
||||
@@ -164,7 +179,7 @@ pub fn impl_de_struct(input: &DeriveInput, data: &DataStruct) -> proc_macro2::To
|
||||
let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl();
|
||||
let name = &input.ident;
|
||||
|
||||
let struct_attrs = parse_struct_attrs(&input.attrs);
|
||||
let struct_attrs = StructAttrs::from_derive_input(input).unwrap();
|
||||
|
||||
let fields: Vec<_> = data
|
||||
.fields
|
||||
@@ -203,9 +218,9 @@ pub fn impl_de_struct(input: &DeriveInput, data: &DataStruct) -> proc_macro2::To
|
||||
reader: &mut quick_xml::NsReader<R>,
|
||||
start: &quick_xml::events::BytesStart,
|
||||
empty: bool
|
||||
) -> Result<Self, rustical_xml::XmlError> {
|
||||
) -> Result<Self, rustical_xml::XmlDeError> {
|
||||
use quick_xml::events::Event;
|
||||
use rustical_xml::XmlError;
|
||||
use rustical_xml::XmlDeError;
|
||||
|
||||
let mut buf = Vec::new();
|
||||
|
||||
@@ -225,7 +240,7 @@ pub fn impl_de_struct(input: &DeriveInput, data: &DataStruct) -> proc_macro2::To
|
||||
Event::End(e) if e.name() == start.name() => {
|
||||
break;
|
||||
}
|
||||
Event::Eof => return Err(XmlError::Eof),
|
||||
Event::Eof => return Err(XmlDeError::Eof),
|
||||
// start of a child element
|
||||
Event::Start(start) | Event::Empty(start) => {
|
||||
let empty = matches!(event, Event::Empty(_));
|
||||
@@ -235,7 +250,7 @@ pub fn impl_de_struct(input: &DeriveInput, data: &DataStruct) -> proc_macro2::To
|
||||
#(#untagged_field_branches)*
|
||||
_ => {
|
||||
// invalid field name
|
||||
return Err(XmlError::UnknownError)
|
||||
return Err(XmlDeError::InvalidFieldName)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -244,26 +259,26 @@ pub fn impl_de_struct(input: &DeriveInput, data: &DataStruct) -> proc_macro2::To
|
||||
#(#text_field_branches)*
|
||||
}
|
||||
Event::CData(cdata) => {
|
||||
return Err(XmlError::UnsupportedEvent("CDATA"));
|
||||
return Err(XmlDeError::UnsupportedEvent("CDATA"));
|
||||
}
|
||||
Event::Comment(_) => {
|
||||
// ignore
|
||||
}
|
||||
Event::Decl(_) => {
|
||||
// Error: not supported
|
||||
return Err(XmlError::UnsupportedEvent("Declaration"));
|
||||
return Err(XmlDeError::UnsupportedEvent("Declaration"));
|
||||
}
|
||||
Event::PI(_) => {
|
||||
// Error: not supported
|
||||
return Err(XmlError::UnsupportedEvent("Processing instruction"));
|
||||
return Err(XmlDeError::UnsupportedEvent("Processing instruction"));
|
||||
}
|
||||
Event::DocType(doctype) => {
|
||||
// Error: start of new document
|
||||
return Err(XmlError::UnsupportedEvent("Doctype in the middle of the document"));
|
||||
return Err(XmlDeError::UnsupportedEvent("Doctype in the middle of the document"));
|
||||
}
|
||||
Event::End(end) => {
|
||||
// Error: premature end
|
||||
return Err(XmlError::Other("Unexpected closing tag for wrong element".to_owned()));
|
||||
return Err(XmlDeError::Other("Unexpected closing tag for wrong element".to_owned()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user