mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 11:42:25 +00:00
xml: Refactoring
This commit is contained in:
@@ -1,25 +0,0 @@
|
|||||||
pub mod attrs;
|
|
||||||
mod de_enum;
|
|
||||||
mod de_struct;
|
|
||||||
mod field;
|
|
||||||
|
|
||||||
pub use de_enum::Enum;
|
|
||||||
pub use de_struct::NamedStruct;
|
|
||||||
pub use field::Field;
|
|
||||||
|
|
||||||
pub 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,33 @@
|
|||||||
use core::panic;
|
use core::panic;
|
||||||
use syn::{parse_macro_input, DeriveInput};
|
use syn::{parse_macro_input, DeriveInput};
|
||||||
|
|
||||||
mod de;
|
pub(crate) mod attrs;
|
||||||
use de::{Enum, NamedStruct};
|
mod field;
|
||||||
|
mod variant;
|
||||||
|
mod xml_enum;
|
||||||
|
mod xml_struct;
|
||||||
|
|
||||||
|
pub(crate) use field::Field;
|
||||||
|
pub(crate) use variant::Variant;
|
||||||
|
pub(crate) use xml_enum::Enum;
|
||||||
|
pub(crate) use xml_struct::NamedStruct;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
#[proc_macro_derive(XmlDeserialize, attributes(xml))]
|
#[proc_macro_derive(XmlDeserialize, attributes(xml))]
|
||||||
pub fn derive_xml_deserialize(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
pub fn derive_xml_deserialize(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||||
|
|||||||
@@ -1,15 +1,12 @@
|
|||||||
use core::panic;
|
|
||||||
|
|
||||||
use super::{attrs::EnumAttrs, get_generic_type};
|
|
||||||
use crate::de::attrs::VariantAttrs;
|
|
||||||
use darling::{FromDeriveInput, FromVariant};
|
|
||||||
use heck::ToKebabCase;
|
use heck::ToKebabCase;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
use syn::{DataEnum, DeriveInput, Fields, FieldsUnnamed};
|
use syn::{Fields, FieldsUnnamed};
|
||||||
|
|
||||||
|
use super::{attrs::VariantAttrs, get_generic_type};
|
||||||
|
|
||||||
pub struct Variant {
|
pub struct Variant {
|
||||||
variant: syn::Variant,
|
pub variant: syn::Variant,
|
||||||
attrs: VariantAttrs,
|
pub attrs: VariantAttrs,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Variant {
|
impl Variant {
|
||||||
@@ -38,7 +35,7 @@ impl Variant {
|
|||||||
"struct variants are not supported, please use a tuple variant with a struct"
|
"struct variants are not supported, please use a tuple variant with a struct"
|
||||||
),
|
),
|
||||||
Fields::Unit => syn::Type::Path(syn::parse_str("::rustical_xml::Unit").unwrap()),
|
Fields::Unit => syn::Type::Path(syn::parse_str("::rustical_xml::Unit").unwrap()),
|
||||||
Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => {
|
Fields::Unnamed(syn::FieldsUnnamed { unnamed, .. }) => {
|
||||||
if unnamed.len() != 1 {
|
if unnamed.len() != 1 {
|
||||||
panic!("tuple variants should contain exactly one element");
|
panic!("tuple variants should contain exactly one element");
|
||||||
}
|
}
|
||||||
@@ -176,150 +173,3 @@ impl Variant {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Enum {
|
|
||||||
attrs: EnumAttrs,
|
|
||||||
variants: Vec<Variant>,
|
|
||||||
ident: syn::Ident,
|
|
||||||
generics: syn::Generics,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Enum {
|
|
||||||
fn impl_de_untagged(&self) -> proc_macro2::TokenStream {
|
|
||||||
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
|
|
||||||
let name = &self.ident;
|
|
||||||
|
|
||||||
let variant_branches = self
|
|
||||||
.variants
|
|
||||||
.iter()
|
|
||||||
.filter_map(|variant| variant.untagged_branch());
|
|
||||||
|
|
||||||
quote! {
|
|
||||||
impl #impl_generics ::rustical_xml::XmlDeserialize for #name #type_generics #where_clause {
|
|
||||||
fn deserialize<R: ::std::io::BufRead>(
|
|
||||||
reader: &mut quick_xml::NsReader<R>,
|
|
||||||
start: &quick_xml::events::BytesStart,
|
|
||||||
empty: bool
|
|
||||||
) -> Result<Self, rustical_xml::XmlDeError> {
|
|
||||||
#(#variant_branches);*
|
|
||||||
|
|
||||||
Err(rustical_xml::XmlDeError::UnknownError)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn impl_de_tagged(&self) -> proc_macro2::TokenStream {
|
|
||||||
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
|
|
||||||
let name = &self.ident;
|
|
||||||
|
|
||||||
let variant_branches = self.variants.iter().filter_map(Variant::tagged_branch);
|
|
||||||
|
|
||||||
quote! {
|
|
||||||
impl #impl_generics ::rustical_xml::XmlDeserialize for #name #type_generics #where_clause {
|
|
||||||
fn deserialize<R: std::io::BufRead>(
|
|
||||||
reader: &mut quick_xml::NsReader<R>,
|
|
||||||
start: &quick_xml::events::BytesStart,
|
|
||||||
empty: bool
|
|
||||||
) -> Result<Self, rustical_xml::XmlDeError> {
|
|
||||||
let (_ns, name) = reader.resolve_element(start.name());
|
|
||||||
|
|
||||||
match name.as_ref() {
|
|
||||||
#(#variant_branches),*
|
|
||||||
name => {
|
|
||||||
// Handle invalid variant name
|
|
||||||
Err(rustical_xml::XmlDeError::InvalidVariant(String::from_utf8_lossy(name).to_string()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn impl_de(&self) -> proc_macro2::TokenStream {
|
|
||||||
match self.attrs.untagged.is_present() {
|
|
||||||
true => self.impl_de_untagged(),
|
|
||||||
false => self.impl_de_tagged(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse(input: &DeriveInput, data: &DataEnum) -> Self {
|
|
||||||
let attrs = EnumAttrs::from_derive_input(input).unwrap();
|
|
||||||
|
|
||||||
Self {
|
|
||||||
variants: data
|
|
||||||
.variants
|
|
||||||
.iter()
|
|
||||||
.map(|variant| Variant {
|
|
||||||
attrs: VariantAttrs::from_variant(variant).unwrap(),
|
|
||||||
variant: variant.to_owned(),
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
attrs,
|
|
||||||
ident: input.ident.to_owned(),
|
|
||||||
generics: input.generics.to_owned(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn impl_se(&self) -> proc_macro2::TokenStream {
|
|
||||||
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
|
|
||||||
let ident = &self.ident;
|
|
||||||
|
|
||||||
// TODO: Implement attributes
|
|
||||||
quote! {
|
|
||||||
impl #impl_generics ::rustical_xml::XmlSerialize for #ident #type_generics #where_clause {
|
|
||||||
fn serialize<W: ::std::io::Write>(
|
|
||||||
&self,
|
|
||||||
tag: Option<&[u8]>,
|
|
||||||
writer: &mut ::quick_xml::Writer<W>
|
|
||||||
) -> ::std::io::Result<()> {
|
|
||||||
use ::quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
|
|
||||||
|
|
||||||
let tag_str = tag.map(String::from_utf8_lossy);
|
|
||||||
|
|
||||||
if let Some(tag) = &tag_str {
|
|
||||||
writer.write_event(Event::Start(BytesStart::new(tag.to_owned())))?;
|
|
||||||
}
|
|
||||||
if let Some(tag) = &tag_str {
|
|
||||||
writer.write_event(Event::End(BytesEnd::new(tag.to_owned())))?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn impl_xml_document(&self) -> proc_macro2::TokenStream {
|
|
||||||
if self.attrs.untagged.is_present() {
|
|
||||||
panic!("XmlDocument only supported for untagged enums");
|
|
||||||
}
|
|
||||||
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
|
|
||||||
let ident = &self.ident;
|
|
||||||
|
|
||||||
quote! {
|
|
||||||
impl #impl_generics ::rustical_xml::XmlDocument for #ident #type_generics #where_clause {
|
|
||||||
fn parse<R: ::std::io::BufRead>(mut reader: ::quick_xml::NsReader<R>) -> Result<Self, ::rustical_xml::XmlDeError>
|
|
||||||
where
|
|
||||||
Self: ::rustical_xml::XmlDeserialize
|
|
||||||
{
|
|
||||||
use ::quick_xml::events::Event;
|
|
||||||
|
|
||||||
let mut buf = Vec::new();
|
|
||||||
loop {
|
|
||||||
let event = reader.read_event_into(&mut buf)?;
|
|
||||||
let empty = matches!(event, Event::Empty(_));
|
|
||||||
|
|
||||||
match event {
|
|
||||||
Event::Decl(_) => { /* <?xml ... ?> ignore this */ }
|
|
||||||
Event::Comment(_) => { /* ignore this */ }
|
|
||||||
Event::Start(start) | Event::Empty(start) => {
|
|
||||||
return <Self as ::rustical_xml::XmlDeserialize>::deserialize(&mut reader, &start, empty);
|
|
||||||
}
|
|
||||||
_ => return Err(::rustical_xml::XmlDeError::UnknownError),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
153
crates/xml/derive/src/xml_enum.rs
Normal file
153
crates/xml/derive/src/xml_enum.rs
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
use super::{attrs::EnumAttrs, Variant};
|
||||||
|
use crate::attrs::VariantAttrs;
|
||||||
|
use core::panic;
|
||||||
|
use darling::{FromDeriveInput, FromVariant};
|
||||||
|
use quote::quote;
|
||||||
|
use syn::{DataEnum, DeriveInput};
|
||||||
|
|
||||||
|
pub struct Enum {
|
||||||
|
attrs: EnumAttrs,
|
||||||
|
variants: Vec<Variant>,
|
||||||
|
ident: syn::Ident,
|
||||||
|
generics: syn::Generics,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Enum {
|
||||||
|
fn impl_de_untagged(&self) -> proc_macro2::TokenStream {
|
||||||
|
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
|
||||||
|
let name = &self.ident;
|
||||||
|
|
||||||
|
let variant_branches = self
|
||||||
|
.variants
|
||||||
|
.iter()
|
||||||
|
.filter_map(|variant| variant.untagged_branch());
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
impl #impl_generics ::rustical_xml::XmlDeserialize for #name #type_generics #where_clause {
|
||||||
|
fn deserialize<R: ::std::io::BufRead>(
|
||||||
|
reader: &mut quick_xml::NsReader<R>,
|
||||||
|
start: &quick_xml::events::BytesStart,
|
||||||
|
empty: bool
|
||||||
|
) -> Result<Self, rustical_xml::XmlDeError> {
|
||||||
|
#(#variant_branches);*
|
||||||
|
|
||||||
|
Err(rustical_xml::XmlDeError::UnknownError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn impl_de_tagged(&self) -> proc_macro2::TokenStream {
|
||||||
|
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
|
||||||
|
let name = &self.ident;
|
||||||
|
|
||||||
|
let variant_branches = self.variants.iter().filter_map(Variant::tagged_branch);
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
impl #impl_generics ::rustical_xml::XmlDeserialize for #name #type_generics #where_clause {
|
||||||
|
fn deserialize<R: std::io::BufRead>(
|
||||||
|
reader: &mut quick_xml::NsReader<R>,
|
||||||
|
start: &quick_xml::events::BytesStart,
|
||||||
|
empty: bool
|
||||||
|
) -> Result<Self, rustical_xml::XmlDeError> {
|
||||||
|
let (_ns, name) = reader.resolve_element(start.name());
|
||||||
|
|
||||||
|
match name.as_ref() {
|
||||||
|
#(#variant_branches),*
|
||||||
|
name => {
|
||||||
|
// Handle invalid variant name
|
||||||
|
Err(rustical_xml::XmlDeError::InvalidVariant(String::from_utf8_lossy(name).to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn impl_de(&self) -> proc_macro2::TokenStream {
|
||||||
|
match self.attrs.untagged.is_present() {
|
||||||
|
true => self.impl_de_untagged(),
|
||||||
|
false => self.impl_de_tagged(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse(input: &DeriveInput, data: &DataEnum) -> Self {
|
||||||
|
let attrs = EnumAttrs::from_derive_input(input).unwrap();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
variants: data
|
||||||
|
.variants
|
||||||
|
.iter()
|
||||||
|
.map(|variant| Variant {
|
||||||
|
attrs: VariantAttrs::from_variant(variant).unwrap(),
|
||||||
|
variant: variant.to_owned(),
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
attrs,
|
||||||
|
ident: input.ident.to_owned(),
|
||||||
|
generics: input.generics.to_owned(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn impl_se(&self) -> proc_macro2::TokenStream {
|
||||||
|
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
|
||||||
|
let ident = &self.ident;
|
||||||
|
|
||||||
|
// TODO: Implement attributes
|
||||||
|
quote! {
|
||||||
|
impl #impl_generics ::rustical_xml::XmlSerialize for #ident #type_generics #where_clause {
|
||||||
|
fn serialize<W: ::std::io::Write>(
|
||||||
|
&self,
|
||||||
|
tag: Option<&[u8]>,
|
||||||
|
writer: &mut ::quick_xml::Writer<W>
|
||||||
|
) -> ::std::io::Result<()> {
|
||||||
|
use ::quick_xml::events::{BytesEnd, BytesStart, BytesText, Event};
|
||||||
|
|
||||||
|
let tag_str = tag.map(String::from_utf8_lossy);
|
||||||
|
|
||||||
|
if let Some(tag) = &tag_str {
|
||||||
|
writer.write_event(Event::Start(BytesStart::new(tag.to_owned())))?;
|
||||||
|
}
|
||||||
|
if let Some(tag) = &tag_str {
|
||||||
|
writer.write_event(Event::End(BytesEnd::new(tag.to_owned())))?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn impl_xml_document(&self) -> proc_macro2::TokenStream {
|
||||||
|
if self.attrs.untagged.is_present() {
|
||||||
|
panic!("XmlDocument only supported for untagged enums");
|
||||||
|
}
|
||||||
|
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
|
||||||
|
let ident = &self.ident;
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
impl #impl_generics ::rustical_xml::XmlDocument for #ident #type_generics #where_clause {
|
||||||
|
fn parse<R: ::std::io::BufRead>(mut reader: ::quick_xml::NsReader<R>) -> Result<Self, ::rustical_xml::XmlDeError>
|
||||||
|
where
|
||||||
|
Self: ::rustical_xml::XmlDeserialize
|
||||||
|
{
|
||||||
|
use ::quick_xml::events::Event;
|
||||||
|
|
||||||
|
let mut buf = Vec::new();
|
||||||
|
loop {
|
||||||
|
let event = reader.read_event_into(&mut buf)?;
|
||||||
|
let empty = matches!(event, Event::Empty(_));
|
||||||
|
|
||||||
|
match event {
|
||||||
|
Event::Decl(_) => { /* <?xml ... ?> ignore this */ }
|
||||||
|
Event::Comment(_) => { /* ignore this */ }
|
||||||
|
Event::Start(start) | Event::Empty(start) => {
|
||||||
|
return <Self as ::rustical_xml::XmlDeserialize>::deserialize(&mut reader, &start, empty);
|
||||||
|
}
|
||||||
|
_ => return Err(::rustical_xml::XmlDeError::UnknownError),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::de::attrs::StructAttrs;
|
use crate::attrs::StructAttrs;
|
||||||
use crate::de::Field;
|
use crate::Field;
|
||||||
use core::panic;
|
use core::panic;
|
||||||
use darling::FromDeriveInput;
|
use darling::FromDeriveInput;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
Reference in New Issue
Block a user