some changes to rustical_xml

This commit is contained in:
Lennart
2024-12-21 15:11:11 +01:00
parent 57268f202d
commit 9ca941b97e
11 changed files with 441 additions and 207 deletions

1
crates/xml/README.md Normal file
View File

@@ -0,0 +1 @@
# rustical_xml

View File

@@ -1,39 +1,49 @@
use darling::{util::Flag, FromDeriveInput, FromField, FromMeta, FromVariant}; use darling::{util::Flag, FromDeriveInput, FromField, FromMeta, FromVariant};
use syn::LitByteStr; use syn::LitByteStr;
#[derive(Default, FromMeta)] #[derive(Default, FromMeta, Clone)]
pub struct ContainerAttrs { pub struct ContainerAttrs {
pub ns_strict: Flag, pub ns_strict: Flag,
} }
#[derive(Default, FromMeta)] #[derive(Default, FromMeta, Clone)]
pub struct TagAttrs { pub struct TagAttrs {
pub rename: Option<LitByteStr>, pub rename: Option<LitByteStr>,
pub ns_strict: Flag, pub ns_strict: Flag,
pub ns: Option<LitByteStr>, pub ns: Option<LitByteStr>,
} }
#[derive(Default, FromVariant)] #[derive(Default, FromVariant, Clone)]
#[darling(attributes(xml))] #[darling(attributes(xml))]
pub struct VariantAttrs { pub struct VariantAttrs {
#[darling(flatten)] #[darling(flatten)]
pub common: TagAttrs, pub common: TagAttrs,
} }
#[derive(Default, FromDeriveInput)] #[derive(Default, FromDeriveInput, Clone)]
#[darling(attributes(xml))] #[darling(attributes(xml))]
pub struct EnumAttrs { pub struct EnumAttrs {
#[darling(flatten)] #[darling(flatten)]
container: ContainerAttrs, container: ContainerAttrs,
} }
#[derive(Default, FromDeriveInput)] #[derive(Default, FromDeriveInput, Clone)]
#[darling(attributes(xml))] #[darling(attributes(xml))]
pub struct StructAttrs { pub struct StructAttrs {
#[darling(flatten)] #[darling(flatten)]
pub container: ContainerAttrs, pub container: ContainerAttrs,
pub root: Option<LitByteStr>, pub root: Option<LitByteStr>,
pub allow_invalid: Flag,
}
#[derive(Default, FromMeta, PartialEq)]
pub enum FieldType {
#[default]
Tag,
Attr,
Text,
Untagged,
} }
#[derive(Default, FromField)] #[derive(Default, FromField)]
@@ -41,8 +51,8 @@ pub struct StructAttrs {
pub struct FieldAttrs { pub struct FieldAttrs {
#[darling(flatten)] #[darling(flatten)]
pub common: TagAttrs, pub common: TagAttrs,
pub text: Flag,
pub untagged: Flag,
pub flatten: Flag, pub flatten: Flag,
pub default: Option<syn::ExprPath>, pub default: Option<syn::ExprPath>,
#[darling(default, rename = "ty")]
pub xml_ty: FieldType,
} }

View File

@@ -1,15 +1,14 @@
use crate::de::attrs::VariantAttrs;
use darling::FromVariant; use darling::FromVariant;
use heck::ToKebabCase; use heck::ToKebabCase;
use quote::quote; use quote::quote;
use syn::{DataEnum, DeriveInput, Fields, FieldsUnnamed, Variant}; use syn::{DataEnum, DeriveInput, Fields, FieldsUnnamed, Variant};
use crate::de::attrs::VariantAttrs;
pub fn enum_variant_branch(variant: &Variant) -> proc_macro2::TokenStream { pub fn enum_variant_branch(variant: &Variant) -> proc_macro2::TokenStream {
let ident = &variant.ident; let ident = &variant.ident;
match &variant.fields { match &variant.fields {
Fields::Named(named) => { Fields::Named(_) => {
panic!("struct variants are not supported, please use a tuple variant with a struct") panic!("struct variants are not supported, please use a tuple variant with a struct")
} }
Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => { Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => {

View File

@@ -1,6 +1,5 @@
use super::attrs::{FieldAttrs, FieldType};
use crate::de::attrs::StructAttrs; use crate::de::attrs::StructAttrs;
use super::attrs::FieldAttrs;
use core::panic; use core::panic;
use darling::{FromDeriveInput, FromField}; use darling::{FromDeriveInput, FromField};
use heck::ToKebabCase; use heck::ToKebabCase;
@@ -23,16 +22,46 @@ fn get_generic_type(ty: &syn::Type) -> Option<&syn::Type> {
None None
} }
fn invalid_field_branch(allow: bool) -> proc_macro2::TokenStream {
if allow {
quote! {
_ => {
// ignore because of allow_invalid flag
}
}
} else {
quote! {
_ => {
// invalid field name
return Err(XmlDeError::InvalidFieldName)
}
}
}
}
fn wrap_option_if_no_default(
value: proc_macro2::TokenStream,
has_default: bool,
) -> proc_macro2::TokenStream {
if has_default {
value
} else {
quote! {Some(#value)}
}
}
pub struct Field { pub struct Field {
pub field: syn::Field, pub field: syn::Field,
pub attrs: FieldAttrs, pub attrs: FieldAttrs,
pub struct_attrs: StructAttrs,
} }
impl Field { impl Field {
fn from_syn_field(field: syn::Field) -> Self { fn from_syn_field(field: syn::Field, struct_attrs: StructAttrs) -> Self {
Self { Self {
attrs: FieldAttrs::from_field(&field).unwrap(), attrs: FieldAttrs::from_field(&field).unwrap(),
field, field,
struct_attrs,
} }
} }
fn de_name(&self) -> LitByteStr { fn de_name(&self) -> LitByteStr {
@@ -46,6 +75,11 @@ impl Field {
)) ))
} }
fn ns_strict(&self) -> bool {
self.attrs.common.ns_strict.is_present()
|| self.struct_attrs.container.ns_strict.is_present()
}
fn field_ident(&self) -> &syn::Ident { fn field_ident(&self) -> &syn::Ident {
self.field self.field
.ident .ident
@@ -60,94 +94,90 @@ impl Field {
fn builder_field(&self) -> proc_macro2::TokenStream { fn builder_field(&self) -> proc_macro2::TokenStream {
let field_ident = self.field_ident(); let field_ident = self.field_ident();
let ty = self.ty(); let ty = self.ty();
if self.attrs.default.is_some() { match (self.attrs.flatten.is_present(), &self.attrs.default) {
quote! { (_, Some(_default)) => quote! { #field_ident: #ty, },
#field_ident: #ty, (true, None) => {
} 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)");
} else if self.attrs.flatten.is_present() { quote! { #field_ident: Vec<#generic_type>, }
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>,
}
} else {
quote! {
#field_ident: Option<#ty>,
} }
(false, None) => quote! { #field_ident: Option<#ty>, },
} }
} }
fn builder_field_init(&self) -> proc_macro2::TokenStream { fn builder_field_init(&self) -> proc_macro2::TokenStream {
let field_ident = self.field_ident(); let field_ident = self.field_ident();
if let Some(default) = &self.attrs.default { match (self.attrs.flatten.is_present(), &self.attrs.default) {
quote! { (_, Some(default)) => quote! { #field_ident: #default(), },
#field_ident: #default(), (true, None) => quote! { #field_ident: vec![], },
} (false, None) => quote! { #field_ident: None, },
} else if self.attrs.flatten.is_present() {
quote! {
#field_ident: vec![],
}
} else {
quote! {
#field_ident: None,
}
} }
} }
fn builder_field_build(&self) -> proc_macro2::TokenStream { fn builder_field_build(&self) -> proc_macro2::TokenStream {
let field_ident = self.field_ident(); let field_ident = self.field_ident();
if self.attrs.flatten.is_present() { match (
quote! { self.attrs.flatten.is_present(),
self.attrs.default.is_some(),
) {
(true, _) => quote! {
#field_ident: FromIterator::from_iter(builder.#field_ident.into_iter()) #field_ident: FromIterator::from_iter(builder.#field_ident.into_iter())
} },
} else if self.attrs.default.is_some() { (false, true) => quote! {
quote! {
#field_ident: builder.#field_ident, #field_ident: builder.#field_ident,
} },
} else { (false, false) => quote! {
quote! {
#field_ident: builder.#field_ident.expect("todo: handle missing field"), #field_ident: builder.#field_ident.expect("todo: handle missing field"),
} },
} }
} }
fn named_branch(&self) -> Option<proc_macro2::TokenStream> { fn named_branch(&self) -> Option<proc_macro2::TokenStream> {
if self.attrs.text.is_present() { if self.attrs.xml_ty != FieldType::Tag {
return None;
}
if self.attrs.untagged.is_present() {
return None; return None;
} }
let namespace_match = if self.ns_strict() {
if let Some(ns) = &self.attrs.common.ns {
quote! {quick_xml::name::ResolveResult::Bound(quick_xml::name::Namespace(#ns))}
} else {
quote! {quick_xml::name::ResolveResult::Unbound}
}
} else {
quote! {_}
};
let field_name = self.de_name(); let field_name = self.de_name();
let field_ident = self.field_ident(); let field_ident = self.field_ident();
let deserializer = self.ty(); let deserializer = self.ty();
Some(if self.attrs.default.is_some() { let value = quote! { <#deserializer as rustical_xml::XmlDeserialize>::deserialize(reader, &start, empty)? };
quote! { let assignment = match (self.attrs.flatten.is_present(), &self.attrs.default) {
#field_name => { (true, _) => {
builder.#field_ident = <#deserializer as rustical_xml::XmlDeserialize>::deserialize(reader, &start, empty)?; // TODO: Make nicer, watch out with deserializer typing
}, 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! {
} else if self.attrs.flatten.is_present() { builder.#field_ident.push(<#deserializer as rustical_xml::XmlDeserialize>::deserialize(reader, &start, empty)?);
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 => {
builder.#field_ident.push(<#deserializer as rustical_xml::XmlDeserialize>::deserialize(reader, &start, empty)?);
},
}
} else {
quote! {
#field_name => {
builder.#field_ident = Some(<#deserializer as rustical_xml::XmlDeserialize>::deserialize(reader, &start, empty)?);
},
} }
(false, Some(_default)) => quote! {
builder.#field_ident = #value;
},
(false, None) => quote! {
builder.#field_ident = Some(#value);
},
};
Some(quote! {
(#namespace_match, #field_name) => { #assignment; },
}) })
} }
fn untagged_branch(&self) -> Option<proc_macro2::TokenStream> { fn untagged_branch(&self) -> Option<proc_macro2::TokenStream> {
if !self.attrs.untagged.is_present() { if self.attrs.xml_ty != FieldType::Untagged {
return None; return None;
} }
let field_ident = self.field_ident(); let field_ident = self.field_ident();
let deserializer = self.ty(); let deserializer = self.ty();
Some(if self.attrs.flatten.is_present() { 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)"); 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! { quote! {
@@ -165,128 +195,190 @@ impl Field {
} }
fn text_branch(&self) -> Option<proc_macro2::TokenStream> { fn text_branch(&self) -> Option<proc_macro2::TokenStream> {
if !self.attrs.text.is_present() { if self.attrs.xml_ty != FieldType::Text {
return None; return None;
} }
let field_ident = self.field_ident(); let field_ident = self.field_ident();
let value = wrap_option_if_no_default(
quote! {
rustical_xml::Value::deserialize(text.as_ref())?
},
self.attrs.default.is_some(),
);
Some(quote! { Some(quote! {
builder.#field_ident = Some(text.to_owned().into()); builder.#field_ident = #value;
})
}
fn attr_branch(&self) -> Option<proc_macro2::TokenStream> {
if self.attrs.xml_ty != FieldType::Attr {
return None;
}
let field_ident = self.field_ident();
let field_name = self.de_name();
let value = wrap_option_if_no_default(
quote! {
rustical_xml::Value::deserialize(attr.unescape_value()?.as_ref())?
},
self.attrs.default.is_some(),
);
Some(quote! {
#field_name => {
builder.#field_ident = #value;
}
}) })
} }
} }
pub fn impl_de_struct(input: &DeriveInput, data: &DataStruct) -> proc_macro2::TokenStream { impl NamedStruct {
let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl(); pub fn parse(input: &DeriveInput, data: &DataStruct) -> Self {
let name = &input.ident; let attrs = StructAttrs::from_derive_input(input).unwrap();
let struct_attrs = StructAttrs::from_derive_input(input).unwrap(); match &data.fields {
syn::Fields::Named(named) => NamedStruct {
let fields: Vec<_> = data fields: named
.fields .named
.iter() .iter()
.map(|field| Field::from_syn_field(field.to_owned())) .map(|field| Field::from_syn_field(field.to_owned(), attrs.clone()))
.collect(); .collect(),
attrs,
let builder_fields = fields.iter().map(Field::builder_field); ident: input.ident.to_owned(),
let builder_field_inits = fields.iter().map(Field::builder_field_init); generics: input.generics.to_owned(),
let named_field_branches = fields.iter().filter_map(Field::named_branch); },
let untagged_field_branches: Vec<_> = syn::Fields::Unnamed(_) => panic!("not implemented for tuple struct"),
fields.iter().filter_map(Field::untagged_branch).collect(); syn::Fields::Unit => NamedStruct {
if untagged_field_branches.len() > 1 { fields: vec![],
panic!("Currently only one untagged field supported!"); attrs,
ident: input.ident.to_owned(),
generics: input.generics.to_owned(),
},
}
} }
let text_field_branches = fields.iter().filter_map(Field::text_branch); }
let builder_field_builds = fields.iter().map(Field::builder_field_build);
let xml_root_impl = if let Some(root) = struct_attrs.root { pub struct NamedStruct {
quote! { attrs: StructAttrs,
impl #impl_generics ::rustical_xml::XmlRoot for #name #type_generics #where_clause { fields: Vec<Field>,
fn root_tag() -> &'static [u8] { ident: syn::Ident,
#root generics: syn::Generics,
}
impl NamedStruct {
pub fn impl_de(&self) -> proc_macro2::TokenStream {
let (impl_generics, type_generics, where_clause) = self.generics.split_for_impl();
let ident = &self.ident;
let builder_fields = self.fields.iter().map(Field::builder_field);
let builder_field_inits = self.fields.iter().map(Field::builder_field_init);
let named_field_branches = self.fields.iter().filter_map(Field::named_branch);
let untagged_field_branches: Vec<_> = self
.fields
.iter()
.filter_map(Field::untagged_branch)
.collect();
if untagged_field_branches.len() > 1 {
panic!("Currently only one untagged field supported!");
}
let text_field_branches = self.fields.iter().filter_map(Field::text_branch);
let attr_field_branches = self.fields.iter().filter_map(Field::attr_branch);
let builder_field_builds = self.fields.iter().map(Field::builder_field_build);
let xml_root_impl = if let Some(root) = &self.attrs.root {
quote! {
impl #impl_generics ::rustical_xml::XmlRoot for #ident #type_generics #where_clause {
fn root_tag() -> &'static [u8] { #root }
} }
} }
} } else {
} else { quote! {}
quote! {} };
};
quote! { let invalid_field_branch = invalid_field_branch(self.attrs.allow_invalid.is_present());
#xml_root_impl
impl #impl_generics ::rustical_xml::XmlDeserialize for #name #type_generics #where_clause { quote! {
fn deserialize<R: std::io::BufRead>( #xml_root_impl
reader: &mut quick_xml::NsReader<R>,
start: &quick_xml::events::BytesStart,
empty: bool
) -> Result<Self, rustical_xml::XmlDeError> {
use quick_xml::events::Event;
use rustical_xml::XmlDeError;
let mut buf = Vec::new(); impl #impl_generics ::rustical_xml::XmlDeserialize for #ident #type_generics #where_clause {
fn deserialize<R: BufRead>(
reader: &mut quick_xml::NsReader<R>,
start: &quick_xml::events::BytesStart,
empty: bool
) -> Result<Self, rustical_xml::XmlDeError> {
use quick_xml::events::Event;
use rustical_xml::XmlDeError;
// initialise fields let mut buf = Vec::new();
struct StructBuilder {
#(#builder_fields)*
}
let mut builder = StructBuilder { // initialise fields
#(#builder_field_inits)* struct StructBuilder #type_generics #where_clause {
}; #(#builder_fields)*
}
if !empty { let mut builder = StructBuilder {
loop { #(#builder_field_inits)*
let event = reader.read_event_into(&mut buf)?; };
match &event {
Event::End(e) if e.name() == start.name() => { for attr in start.attributes() {
break; let attr = attr?;
} match attr.key.as_ref() {
Event::Eof => return Err(XmlDeError::Eof), #(#attr_field_branches)*
// start of a child element #invalid_field_branch
Event::Start(start) | Event::Empty(start) => { }
let empty = matches!(event, Event::Empty(_)); }
let (_ns, name) = reader.resolve_element(start.name());
match name.as_ref() { if !empty {
#(#named_field_branches)* loop {
#(#untagged_field_branches)* let event = reader.read_event_into(&mut buf)?;
_ => { match &event {
// invalid field name Event::End(e) if e.name() == start.name() => {
return Err(XmlDeError::InvalidFieldName) break;
}
Event::Eof => return Err(XmlDeError::Eof),
// start of a child element
Event::Start(start) | Event::Empty(start) => {
let empty = matches!(event, Event::Empty(_));
let (ns, name) = reader.resolve_element(start.name());
match (ns, name.as_ref()) {
#(#named_field_branches)*
#(#untagged_field_branches)*
#invalid_field_branch
} }
} }
} Event::Text(bytes_text) => {
Event::Text(bytes_text) => { let text = bytes_text.unescape()?;
let text = bytes_text.unescape()?; #(#text_field_branches)*
#(#text_field_branches)* }
} Event::CData(cdata) => {
Event::CData(cdata) => { return Err(XmlDeError::UnsupportedEvent("CDATA"));
return Err(XmlDeError::UnsupportedEvent("CDATA")); }
} Event::Comment(_) => { /* ignore */ }
Event::Comment(_) => { Event::Decl(_) => {
// ignore // Error: not supported
} return Err(XmlDeError::UnsupportedEvent("Declaration"));
Event::Decl(_) => { }
// Error: not supported Event::PI(_) => {
return Err(XmlDeError::UnsupportedEvent("Declaration")); // Error: not supported
} return Err(XmlDeError::UnsupportedEvent("Processing instruction"));
Event::PI(_) => { }
// Error: not supported Event::DocType(doctype) => {
return Err(XmlDeError::UnsupportedEvent("Processing instruction")); // Error: start of new document
} return Err(XmlDeError::UnsupportedEvent("Doctype in the middle of the document"));
Event::DocType(doctype) => { }
// Error: start of new document Event::End(end) => {
return Err(XmlDeError::UnsupportedEvent("Doctype in the middle of the document")); // Error: premature end
} return Err(XmlDeError::Other("Unexpected closing tag for wrong element".to_owned()));
Event::End(end) => { }
// Error: premature end
return Err(XmlDeError::Other("Unexpected closing tag for wrong element".to_owned()));
} }
} }
} }
}
Ok(Self { Ok(Self {
#(#builder_field_builds)* #(#builder_field_builds)*
}) })
}
} }
} }
} }

View File

@@ -3,4 +3,4 @@ mod de_enum;
mod de_struct; mod de_struct;
pub use de_enum::impl_de_enum; pub use de_enum::impl_de_enum;
pub use de_struct::impl_de_struct; pub use de_struct::NamedStruct;

View File

@@ -3,7 +3,7 @@ use syn::{parse_macro_input, DeriveInput};
mod de; mod de;
use de::{impl_de_enum, impl_de_struct}; use de::{impl_de_enum, NamedStruct};
#[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 {
@@ -11,7 +11,7 @@ pub fn derive_xml_deserialize(input: proc_macro::TokenStream) -> proc_macro::Tok
match &input.data { match &input.data {
syn::Data::Enum(e) => impl_de_enum(&input, e), syn::Data::Enum(e) => impl_de_enum(&input, e),
syn::Data::Struct(s) => impl_de_struct(&input, s), syn::Data::Struct(s) => NamedStruct::parse(&input, s).impl_de(),
syn::Data::Union(_) => panic!("Union not supported"), syn::Data::Union(_) => panic!("Union not supported"),
} }
.into() .into()

View File

@@ -10,6 +10,8 @@ pub enum XmlDeError {
QuickXmlDeError(#[from] quick_xml::de::DeError), QuickXmlDeError(#[from] quick_xml::de::DeError),
#[error(transparent)] #[error(transparent)]
QuickXmlError(#[from] quick_xml::Error), QuickXmlError(#[from] quick_xml::Error),
#[error(transparent)]
QuickXmlAttrError(#[from] quick_xml::events::attributes::AttrError),
#[error("Unknown error")] #[error("Unknown error")]
UnknownError, UnknownError,
#[error("Invalid tag {0}. Expected {1}")] #[error("Invalid tag {0}. Expected {1}")]
@@ -26,6 +28,8 @@ pub enum XmlDeError {
InvalidVariant(String), InvalidVariant(String),
#[error("Invalid field name: ")] #[error("Invalid field name: ")]
InvalidFieldName, InvalidFieldName,
#[error(transparent)]
InvalidValue(#[from] crate::value::ParseValueError),
} }
pub trait XmlDeserialize: Sized { pub trait XmlDeserialize: Sized {
@@ -36,12 +40,16 @@ pub trait XmlDeserialize: Sized {
) -> Result<Self, XmlDeError>; ) -> Result<Self, XmlDeError>;
} }
pub trait XmlRoot: XmlDeserialize { pub trait XmlRoot {
fn parse<R: BufRead>(mut reader: quick_xml::NsReader<R>) -> Result<Self, XmlDeError> { fn parse<R: BufRead>(mut reader: quick_xml::NsReader<R>) -> Result<Self, XmlDeError>
where
Self: XmlDeserialize,
{
let mut buf = Vec::new(); let mut buf = Vec::new();
let event = reader.read_event_into(&mut buf)?; let event = reader.read_event_into(&mut buf)?;
let empty = event.is_empty();
match event { match event {
Event::Start(start) => { Event::Start(start) | Event::Empty(start) => {
let (_ns, name) = reader.resolve_element(start.name()); let (_ns, name) = reader.resolve_element(start.name());
if name.as_ref() != Self::root_tag() { if name.as_ref() != Self::root_tag() {
return Err(XmlDeError::InvalidTag( return Err(XmlDeError::InvalidTag(
@@ -49,32 +57,33 @@ pub trait XmlRoot: XmlDeserialize {
String::from_utf8_lossy(Self::root_tag()).to_string(), String::from_utf8_lossy(Self::root_tag()).to_string(),
)); ));
}; };
// TODO: check namespace // TODO: check namespace
return Self::deserialize(&mut reader, &start, false); return Self::deserialize(&mut reader, &start, empty);
}
Event::Empty(start) => {
let (_ns, name) = reader.resolve_element(start.name());
if name.as_ref() != Self::root_tag() {
return Err(XmlDeError::InvalidTag(
String::from_utf8_lossy(name.as_ref()).to_string(),
String::from_utf8_lossy(Self::root_tag()).to_string(),
));
};
// TODO: check namespace
return Self::deserialize(&mut reader, &start, true);
} }
_ => {} _ => {}
}; };
Err(XmlDeError::UnknownError) Err(XmlDeError::UnknownError)
} }
fn parse_str(input: &str) -> Result<Self, XmlDeError> { fn parse_reader<R: BufRead>(input: R) -> Result<Self, XmlDeError>
let mut reader = quick_xml::NsReader::from_str(input); where
Self: XmlDeserialize,
{
let mut reader = quick_xml::NsReader::from_reader(input);
reader.config_mut().trim_text(true); reader.config_mut().trim_text(true);
Self::parse(reader) Self::parse(reader)
} }
fn root_tag() -> &'static [u8]; fn root_tag() -> &'static [u8];
} }
pub trait XmlRootParseStr<'i>: XmlRoot + XmlDeserialize {
#[inline]
fn parse_str(s: &'i str) -> Result<Self, XmlDeError> {
Self::parse_reader(s.as_bytes())
}
}
impl<'i, T: XmlRoot + XmlDeserialize> XmlRootParseStr<'i> for T {}

View File

@@ -2,10 +2,12 @@ use quick_xml::events::{BytesStart, Event};
use std::io::BufRead; use std::io::BufRead;
pub mod de; pub mod de;
mod value;
pub use de::XmlDeError; pub use de::XmlDeError;
pub use de::XmlDeserialize; pub use de::XmlDeserialize;
pub use de::XmlRoot; pub use de::XmlRoot;
pub use value::Value;
impl<T: XmlDeserialize> XmlDeserialize for Option<T> { impl<T: XmlDeserialize> XmlDeserialize for Option<T> {
fn deserialize<R: BufRead>( fn deserialize<R: BufRead>(
@@ -17,6 +19,7 @@ impl<T: XmlDeserialize> XmlDeserialize for Option<T> {
} }
} }
#[derive(Debug, Clone, PartialEq)]
pub struct Unit; pub struct Unit;
impl XmlDeserialize for Unit { impl XmlDeserialize for Unit {
@@ -65,3 +68,19 @@ impl XmlDeserialize for String {
Ok(content) Ok(content)
} }
} }
// TODO: actually implement
pub struct Unparsed(BytesStart<'static>);
impl XmlDeserialize for Unparsed {
fn deserialize<R: BufRead>(
reader: &mut quick_xml::NsReader<R>,
start: &BytesStart,
_empty: bool,
) -> Result<Self, XmlDeError> {
// let reader_cloned = NsReader::from_reader(reader.get_ref().to_owned());
let mut buf = vec![];
reader.read_to_end_into(start.name(), &mut buf)?;
Ok(Self(start.to_owned()))
}
}

33
crates/xml/src/value.rs Normal file
View File

@@ -0,0 +1,33 @@
use std::convert::Infallible;
use std::num::ParseIntError;
use std::str::FromStr;
use thiserror::Error;
use crate::XmlDeError;
#[derive(Debug, Error)]
pub enum ParseValueError {
#[error(transparent)]
Infallible(#[from] Infallible),
#[error(transparent)]
ParseIntError(#[from] ParseIntError),
}
pub trait Value: Sized {
fn serialize(&self) -> String;
fn deserialize(val: &str) -> Result<Self, XmlDeError>;
}
impl<E, T: FromStr<Err = E> + ToString> Value for T
where
ParseValueError: From<E>,
{
fn serialize(&self) -> String {
self.to_string()
}
fn deserialize(val: &str) -> Result<Self, XmlDeError> {
val.parse()
.map_err(ParseValueError::from)
.map_err(XmlDeError::from)
}
}

View File

@@ -1,4 +1,5 @@
use rustical_xml::XmlRoot; use rustical_xml::XmlRoot;
use std::io::BufRead;
use xml_derive::XmlDeserialize; use xml_derive::XmlDeserialize;
#[test] #[test]
@@ -11,7 +12,7 @@ fn test_struct_untagged_enum() {
#[derive(Debug, XmlDeserialize, PartialEq)] #[derive(Debug, XmlDeserialize, PartialEq)]
struct Prop { struct Prop {
#[xml(untagged)] #[xml(ty = "untagged")]
prop: PropEnum, prop: PropEnum,
} }

View File

@@ -1,24 +1,19 @@
use rustical_xml::de::XmlRootParseStr;
use rustical_xml::{Unit, Unparsed, XmlDeserialize};
use std::collections::HashSet; use std::collections::HashSet;
use std::io::BufRead;
use rustical_xml::XmlRoot;
use xml_derive::XmlDeserialize;
#[test] #[test]
fn test_struct_text_field() { fn test_struct_text_field() {
#[derive(Debug, XmlDeserialize, PartialEq)] #[derive(Debug, XmlDeserialize, PartialEq)]
#[xml(root = b"document")]
struct Document { struct Document {
#[xml(text)] #[xml(ty = "text")]
text: String, text: String,
#[xml(text)] #[xml(ty = "text")]
text2: String, text2: String,
} }
impl XmlRoot for Document {
fn root_tag() -> &'static [u8] {
b"document"
}
}
let doc = Document::parse_str(r#"<document>Hello!</document>"#).unwrap(); let doc = Document::parse_str(r#"<document>Hello!</document>"#).unwrap();
assert_eq!( assert_eq!(
doc, doc,
@@ -32,22 +27,17 @@ fn test_struct_text_field() {
#[test] #[test]
fn test_struct_document() { fn test_struct_document() {
#[derive(Debug, XmlDeserialize, PartialEq)] #[derive(Debug, XmlDeserialize, PartialEq)]
#[xml(root = b"document")]
struct Document { struct Document {
child: Child, child: Child,
} }
#[derive(Debug, XmlDeserialize, PartialEq, Default)] #[derive(Debug, XmlDeserialize, PartialEq, Default)]
struct Child { struct Child {
#[xml(text)] #[xml(ty = "text")]
text: String, text: String,
} }
impl XmlRoot for Document {
fn root_tag() -> &'static [u8] {
b"document"
}
}
let doc = Document::parse_str(r#"<document><child>Hello!</child></document>"#).unwrap(); let doc = Document::parse_str(r#"<document><child>Hello!</child></document>"#).unwrap();
assert_eq!( assert_eq!(
doc, doc,
@@ -70,7 +60,7 @@ fn test_struct_rename_field() {
#[derive(Debug, XmlDeserialize, PartialEq, Default)] #[derive(Debug, XmlDeserialize, PartialEq, Default)]
struct Child { struct Child {
#[xml(text)] #[xml(ty = "text")]
text: String, text: String,
} }
@@ -159,3 +149,83 @@ fn test_struct_set() {
} }
); );
} }
#[test]
fn test_struct_ns() {
#[derive(Debug, XmlDeserialize, PartialEq)]
#[xml(root = b"document", ns_strict)]
struct Document {
#[xml(ns = b"hello")]
child: Unit,
}
let doc = Document::parse_str(r#"<document><child xmlns="hello" /></document>"#).unwrap();
assert_eq!(doc, Document { child: Unit });
}
#[test]
fn test_struct_attr() {
#[derive(Debug, XmlDeserialize, PartialEq)]
#[xml(root = b"document", ns_strict)]
struct Document {
#[xml(ns = b"hello")]
child: Unit,
#[xml(ty = "attr", default = "Default::default")]
test: String,
#[xml(ty = "attr")]
number: usize,
}
let doc = Document::parse_str(
r#"<document test="hello!" number="2"><child xmlns="hello" /></document>"#,
)
.unwrap();
assert_eq!(
doc,
Document {
child: Unit,
test: "hello!".to_owned(),
number: 2
}
);
}
#[test]
fn test_struct_generics() {
#[derive(XmlDeserialize)]
#[xml(root = b"document", ns_strict)]
struct Document<T: XmlDeserialize> {
child: T,
}
let doc = Document::<Unparsed>::parse_str(
r#"
<document>
<child>
Hello! <h1>Nice</h1>
</child>
</document>
"#,
)
.unwrap();
}
#[test]
fn test_struct_unparsed() {
#[derive(XmlDeserialize)]
#[xml(root = b"document", ns_strict)]
struct Document {
child: Unparsed,
}
let doc = Document::parse_str(
r#"
<document>
<child>
Hello! <h1>Nice</h1>
</child>
</document>
"#,
)
.unwrap();
}