mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
xml: Implement XmlDocument for tagged enums and fix small bug
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use core::panic;
|
||||
|
||||
use super::attrs::EnumAttrs;
|
||||
use crate::de::attrs::VariantAttrs;
|
||||
use darling::{FromDeriveInput, FromVariant};
|
||||
@@ -219,4 +221,35 @@ impl Enum {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
let event = reader.read_event_into(&mut buf)?;
|
||||
let empty = matches!(event, Event::Empty(_));
|
||||
|
||||
match event {
|
||||
Event::Start(start) | Event::Empty(start) => {
|
||||
return Self::deserialize(&mut reader, &start, empty);
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
Err(::rustical_xml::XmlDeError::UnknownError)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,3 +39,15 @@ pub fn derive_xml_root_tag(input: proc_macro::TokenStream) -> proc_macro::TokenS
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
#[proc_macro_derive(XmlDocument, attributes(xml))]
|
||||
pub fn derive_xml_document(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
|
||||
match &input.data {
|
||||
syn::Data::Struct(_) => panic!("Struct not supported, use XmlRootTag instead"),
|
||||
syn::Data::Enum(e) => Enum::parse(&input, e).impl_xml_document(),
|
||||
syn::Data::Union(_) => panic!("Union not supported as root"),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ use quick_xml::name::Namespace;
|
||||
use quick_xml::name::ResolveResult;
|
||||
use std::io::BufRead;
|
||||
pub use xml_derive::XmlDeserialize;
|
||||
pub use xml_derive::XmlDocument;
|
||||
pub use xml_derive::XmlRootTag;
|
||||
|
||||
use quick_xml::events::{BytesStart, Event};
|
||||
@@ -74,7 +75,7 @@ impl<T: XmlRootTag + XmlDeserialize> XmlDocument for T {
|
||||
{
|
||||
let mut buf = Vec::new();
|
||||
let event = reader.read_event_into(&mut buf)?;
|
||||
let empty = event.is_empty();
|
||||
let empty = matches!(event, Event::Empty(_));
|
||||
match event {
|
||||
Event::Start(start) | Event::Empty(start) => {
|
||||
let (ns, name) = reader.resolve_element(start.name());
|
||||
|
||||
@@ -7,6 +7,7 @@ mod value;
|
||||
|
||||
pub use de::XmlDeError;
|
||||
pub use de::XmlDeserialize;
|
||||
pub use de::XmlDocument;
|
||||
pub use de::XmlRootTag;
|
||||
pub use se::XmlSerialize;
|
||||
pub use value::Value;
|
||||
|
||||
@@ -96,3 +96,20 @@ fn test_tagged_enum_complex() {
|
||||
.unwrap();
|
||||
dbg!(asd);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_enum_document() {
|
||||
#[derive(Debug, XmlDeserialize, XmlDocument, PartialEq)]
|
||||
enum Document {
|
||||
Hello,
|
||||
Goodbye,
|
||||
}
|
||||
assert_eq!(
|
||||
Document::parse_str(r"<hello></hello>").unwrap(),
|
||||
Document::Hello
|
||||
);
|
||||
assert_eq!(
|
||||
Document::parse_str(r"<goodbye/>").unwrap(),
|
||||
Document::Goodbye
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user