mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
some changes to rustical_xml
This commit is contained in:
1
crates/xml/README.md
Normal file
1
crates/xml/README.md
Normal file
@@ -0,0 +1 @@
|
||||
# rustical_xml
|
||||
@@ -1,39 +1,49 @@
|
||||
use darling::{util::Flag, FromDeriveInput, FromField, FromMeta, FromVariant};
|
||||
use syn::LitByteStr;
|
||||
|
||||
#[derive(Default, FromMeta)]
|
||||
#[derive(Default, FromMeta, Clone)]
|
||||
pub struct ContainerAttrs {
|
||||
pub ns_strict: Flag,
|
||||
}
|
||||
|
||||
#[derive(Default, FromMeta)]
|
||||
#[derive(Default, FromMeta, Clone)]
|
||||
pub struct TagAttrs {
|
||||
pub rename: Option<LitByteStr>,
|
||||
pub ns_strict: Flag,
|
||||
pub ns: Option<LitByteStr>,
|
||||
}
|
||||
|
||||
#[derive(Default, FromVariant)]
|
||||
#[derive(Default, FromVariant, Clone)]
|
||||
#[darling(attributes(xml))]
|
||||
pub struct VariantAttrs {
|
||||
#[darling(flatten)]
|
||||
pub common: TagAttrs,
|
||||
}
|
||||
|
||||
#[derive(Default, FromDeriveInput)]
|
||||
#[derive(Default, FromDeriveInput, Clone)]
|
||||
#[darling(attributes(xml))]
|
||||
pub struct EnumAttrs {
|
||||
#[darling(flatten)]
|
||||
container: ContainerAttrs,
|
||||
}
|
||||
|
||||
#[derive(Default, FromDeriveInput)]
|
||||
#[derive(Default, FromDeriveInput, Clone)]
|
||||
#[darling(attributes(xml))]
|
||||
pub struct StructAttrs {
|
||||
#[darling(flatten)]
|
||||
pub container: ContainerAttrs,
|
||||
|
||||
pub root: Option<LitByteStr>,
|
||||
pub allow_invalid: Flag,
|
||||
}
|
||||
|
||||
#[derive(Default, FromMeta, PartialEq)]
|
||||
pub enum FieldType {
|
||||
#[default]
|
||||
Tag,
|
||||
Attr,
|
||||
Text,
|
||||
Untagged,
|
||||
}
|
||||
|
||||
#[derive(Default, FromField)]
|
||||
@@ -41,8 +51,8 @@ pub struct StructAttrs {
|
||||
pub struct FieldAttrs {
|
||||
#[darling(flatten)]
|
||||
pub common: TagAttrs,
|
||||
pub text: Flag,
|
||||
pub untagged: Flag,
|
||||
pub flatten: Flag,
|
||||
pub default: Option<syn::ExprPath>,
|
||||
#[darling(default, rename = "ty")]
|
||||
pub xml_ty: FieldType,
|
||||
}
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
use crate::de::attrs::VariantAttrs;
|
||||
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;
|
||||
|
||||
match &variant.fields {
|
||||
Fields::Named(named) => {
|
||||
Fields::Named(_) => {
|
||||
panic!("struct variants are not supported, please use a tuple variant with a struct")
|
||||
}
|
||||
Fields::Unnamed(FieldsUnnamed { unnamed, .. }) => {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use super::attrs::{FieldAttrs, FieldType};
|
||||
use crate::de::attrs::StructAttrs;
|
||||
|
||||
use super::attrs::FieldAttrs;
|
||||
use core::panic;
|
||||
use darling::{FromDeriveInput, FromField};
|
||||
use heck::ToKebabCase;
|
||||
@@ -23,16 +22,46 @@ fn get_generic_type(ty: &syn::Type) -> Option<&syn::Type> {
|
||||
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 field: syn::Field,
|
||||
pub attrs: FieldAttrs,
|
||||
pub struct_attrs: StructAttrs,
|
||||
}
|
||||
|
||||
impl Field {
|
||||
fn from_syn_field(field: syn::Field) -> Self {
|
||||
fn from_syn_field(field: syn::Field, struct_attrs: StructAttrs) -> Self {
|
||||
Self {
|
||||
attrs: FieldAttrs::from_field(&field).unwrap(),
|
||||
field,
|
||||
struct_attrs,
|
||||
}
|
||||
}
|
||||
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 {
|
||||
self.field
|
||||
.ident
|
||||
@@ -60,94 +94,90 @@ impl Field {
|
||||
fn builder_field(&self) -> proc_macro2::TokenStream {
|
||||
let field_ident = self.field_ident();
|
||||
let ty = self.ty();
|
||||
if self.attrs.default.is_some() {
|
||||
quote! {
|
||||
#field_ident: #ty,
|
||||
}
|
||||
} else if self.attrs.flatten.is_present() {
|
||||
match (self.attrs.flatten.is_present(), &self.attrs.default) {
|
||||
(_, Some(_default)) => quote! { #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)");
|
||||
quote! {
|
||||
#field_ident: Vec<#generic_type>,
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
#field_ident: Option<#ty>,
|
||||
quote! { #field_ident: Vec<#generic_type>, }
|
||||
}
|
||||
(false, None) => quote! { #field_ident: Option<#ty>, },
|
||||
}
|
||||
}
|
||||
|
||||
fn builder_field_init(&self) -> proc_macro2::TokenStream {
|
||||
let field_ident = self.field_ident();
|
||||
if let Some(default) = &self.attrs.default {
|
||||
quote! {
|
||||
#field_ident: #default(),
|
||||
}
|
||||
} else if self.attrs.flatten.is_present() {
|
||||
quote! {
|
||||
#field_ident: vec![],
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
#field_ident: None,
|
||||
}
|
||||
match (self.attrs.flatten.is_present(), &self.attrs.default) {
|
||||
(_, Some(default)) => quote! { #field_ident: #default(), },
|
||||
(true, None) => quote! { #field_ident: vec![], },
|
||||
(false, None) => quote! { #field_ident: None, },
|
||||
}
|
||||
}
|
||||
|
||||
fn builder_field_build(&self) -> proc_macro2::TokenStream {
|
||||
let field_ident = self.field_ident();
|
||||
if self.attrs.flatten.is_present() {
|
||||
quote! {
|
||||
match (
|
||||
self.attrs.flatten.is_present(),
|
||||
self.attrs.default.is_some(),
|
||||
) {
|
||||
(true, _) => quote! {
|
||||
#field_ident: FromIterator::from_iter(builder.#field_ident.into_iter())
|
||||
}
|
||||
} else if self.attrs.default.is_some() {
|
||||
quote! {
|
||||
},
|
||||
(false, true) => quote! {
|
||||
#field_ident: builder.#field_ident,
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
},
|
||||
(false, false) => quote! {
|
||||
#field_ident: builder.#field_ident.expect("todo: handle missing field"),
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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_ident = self.field_ident();
|
||||
let deserializer = self.ty();
|
||||
Some(if self.attrs.default.is_some() {
|
||||
quote! {
|
||||
#field_name => {
|
||||
builder.#field_ident = <#deserializer as rustical_xml::XmlDeserialize>::deserialize(reader, &start, empty)?;
|
||||
},
|
||||
}
|
||||
} else if self.attrs.flatten.is_present() {
|
||||
let value = quote! { <#deserializer as rustical_xml::XmlDeserialize>::deserialize(reader, &start, empty)? };
|
||||
let assignment = match (self.attrs.flatten.is_present(), &self.attrs.default) {
|
||||
(true, _) => {
|
||||
// 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! {
|
||||
#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> {
|
||||
if !self.attrs.untagged.is_present() {
|
||||
if self.attrs.xml_ty != FieldType::Untagged {
|
||||
return None;
|
||||
}
|
||||
let field_ident = self.field_ident();
|
||||
let deserializer = self.ty();
|
||||
|
||||
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! {
|
||||
@@ -165,56 +195,114 @@ impl Field {
|
||||
}
|
||||
|
||||
fn text_branch(&self) -> Option<proc_macro2::TokenStream> {
|
||||
if !self.attrs.text.is_present() {
|
||||
if self.attrs.xml_ty != FieldType::Text {
|
||||
return None;
|
||||
}
|
||||
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! {
|
||||
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 {
|
||||
let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl();
|
||||
let name = &input.ident;
|
||||
impl NamedStruct {
|
||||
pub fn parse(input: &DeriveInput, data: &DataStruct) -> Self {
|
||||
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 {
|
||||
fields: named
|
||||
.named
|
||||
.iter()
|
||||
.map(|field| Field::from_syn_field(field.to_owned(), attrs.clone()))
|
||||
.collect(),
|
||||
attrs,
|
||||
ident: input.ident.to_owned(),
|
||||
generics: input.generics.to_owned(),
|
||||
},
|
||||
syn::Fields::Unnamed(_) => panic!("not implemented for tuple struct"),
|
||||
syn::Fields::Unit => NamedStruct {
|
||||
fields: vec![],
|
||||
attrs,
|
||||
ident: input.ident.to_owned(),
|
||||
generics: input.generics.to_owned(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let fields: Vec<_> = data
|
||||
pub struct NamedStruct {
|
||||
attrs: StructAttrs,
|
||||
fields: Vec<Field>,
|
||||
ident: syn::Ident,
|
||||
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()
|
||||
.map(|field| Field::from_syn_field(field.to_owned()))
|
||||
.filter_map(Field::untagged_branch)
|
||||
.collect();
|
||||
|
||||
let builder_fields = fields.iter().map(Field::builder_field);
|
||||
let builder_field_inits = fields.iter().map(Field::builder_field_init);
|
||||
let named_field_branches = fields.iter().filter_map(Field::named_branch);
|
||||
let untagged_field_branches: Vec<_> =
|
||||
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 = fields.iter().filter_map(Field::text_branch);
|
||||
let builder_field_builds = fields.iter().map(Field::builder_field_build);
|
||||
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 xml_root_impl = if let Some(root) = struct_attrs.root {
|
||||
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 #name #type_generics #where_clause {
|
||||
fn root_tag() -> &'static [u8] {
|
||||
#root
|
||||
}
|
||||
impl #impl_generics ::rustical_xml::XmlRoot for #ident #type_generics #where_clause {
|
||||
fn root_tag() -> &'static [u8] { #root }
|
||||
}
|
||||
}
|
||||
} else {
|
||||
quote! {}
|
||||
};
|
||||
|
||||
let invalid_field_branch = invalid_field_branch(self.attrs.allow_invalid.is_present());
|
||||
|
||||
quote! {
|
||||
#xml_root_impl
|
||||
|
||||
impl #impl_generics ::rustical_xml::XmlDeserialize for #name #type_generics #where_clause {
|
||||
fn deserialize<R: std::io::BufRead>(
|
||||
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
|
||||
@@ -225,7 +313,7 @@ pub fn impl_de_struct(input: &DeriveInput, data: &DataStruct) -> proc_macro2::To
|
||||
let mut buf = Vec::new();
|
||||
|
||||
// initialise fields
|
||||
struct StructBuilder {
|
||||
struct StructBuilder #type_generics #where_clause {
|
||||
#(#builder_fields)*
|
||||
}
|
||||
|
||||
@@ -233,6 +321,14 @@ pub fn impl_de_struct(input: &DeriveInput, data: &DataStruct) -> proc_macro2::To
|
||||
#(#builder_field_inits)*
|
||||
};
|
||||
|
||||
for attr in start.attributes() {
|
||||
let attr = attr?;
|
||||
match attr.key.as_ref() {
|
||||
#(#attr_field_branches)*
|
||||
#invalid_field_branch
|
||||
}
|
||||
}
|
||||
|
||||
if !empty {
|
||||
loop {
|
||||
let event = reader.read_event_into(&mut buf)?;
|
||||
@@ -244,14 +340,11 @@ pub fn impl_de_struct(input: &DeriveInput, data: &DataStruct) -> proc_macro2::To
|
||||
// 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 name.as_ref() {
|
||||
let (ns, name) = reader.resolve_element(start.name());
|
||||
match (ns, name.as_ref()) {
|
||||
#(#named_field_branches)*
|
||||
#(#untagged_field_branches)*
|
||||
_ => {
|
||||
// invalid field name
|
||||
return Err(XmlDeError::InvalidFieldName)
|
||||
}
|
||||
#invalid_field_branch
|
||||
}
|
||||
}
|
||||
Event::Text(bytes_text) => {
|
||||
@@ -261,9 +354,7 @@ pub fn impl_de_struct(input: &DeriveInput, data: &DataStruct) -> proc_macro2::To
|
||||
Event::CData(cdata) => {
|
||||
return Err(XmlDeError::UnsupportedEvent("CDATA"));
|
||||
}
|
||||
Event::Comment(_) => {
|
||||
// ignore
|
||||
}
|
||||
Event::Comment(_) => { /* ignore */ }
|
||||
Event::Decl(_) => {
|
||||
// Error: not supported
|
||||
return Err(XmlDeError::UnsupportedEvent("Declaration"));
|
||||
@@ -290,4 +381,5 @@ pub fn impl_de_struct(input: &DeriveInput, data: &DataStruct) -> proc_macro2::To
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,4 @@ mod de_enum;
|
||||
mod de_struct;
|
||||
|
||||
pub use de_enum::impl_de_enum;
|
||||
pub use de_struct::impl_de_struct;
|
||||
pub use de_struct::NamedStruct;
|
||||
|
||||
@@ -3,7 +3,7 @@ use syn::{parse_macro_input, DeriveInput};
|
||||
|
||||
mod de;
|
||||
|
||||
use de::{impl_de_enum, impl_de_struct};
|
||||
use de::{impl_de_enum, NamedStruct};
|
||||
|
||||
#[proc_macro_derive(XmlDeserialize, attributes(xml))]
|
||||
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 {
|
||||
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"),
|
||||
}
|
||||
.into()
|
||||
|
||||
@@ -10,6 +10,8 @@ pub enum XmlDeError {
|
||||
QuickXmlDeError(#[from] quick_xml::de::DeError),
|
||||
#[error(transparent)]
|
||||
QuickXmlError(#[from] quick_xml::Error),
|
||||
#[error(transparent)]
|
||||
QuickXmlAttrError(#[from] quick_xml::events::attributes::AttrError),
|
||||
#[error("Unknown error")]
|
||||
UnknownError,
|
||||
#[error("Invalid tag {0}. Expected {1}")]
|
||||
@@ -26,6 +28,8 @@ pub enum XmlDeError {
|
||||
InvalidVariant(String),
|
||||
#[error("Invalid field name: ")]
|
||||
InvalidFieldName,
|
||||
#[error(transparent)]
|
||||
InvalidValue(#[from] crate::value::ParseValueError),
|
||||
}
|
||||
|
||||
pub trait XmlDeserialize: Sized {
|
||||
@@ -36,12 +40,16 @@ pub trait XmlDeserialize: Sized {
|
||||
) -> Result<Self, XmlDeError>;
|
||||
}
|
||||
|
||||
pub trait XmlRoot: XmlDeserialize {
|
||||
fn parse<R: BufRead>(mut reader: quick_xml::NsReader<R>) -> Result<Self, XmlDeError> {
|
||||
pub trait XmlRoot {
|
||||
fn parse<R: BufRead>(mut reader: quick_xml::NsReader<R>) -> Result<Self, XmlDeError>
|
||||
where
|
||||
Self: XmlDeserialize,
|
||||
{
|
||||
let mut buf = Vec::new();
|
||||
let event = reader.read_event_into(&mut buf)?;
|
||||
let empty = event.is_empty();
|
||||
match event {
|
||||
Event::Start(start) => {
|
||||
Event::Start(start) | Event::Empty(start) => {
|
||||
let (_ns, name) = reader.resolve_element(start.name());
|
||||
if name.as_ref() != Self::root_tag() {
|
||||
return Err(XmlDeError::InvalidTag(
|
||||
@@ -49,32 +57,33 @@ pub trait XmlRoot: XmlDeserialize {
|
||||
String::from_utf8_lossy(Self::root_tag()).to_string(),
|
||||
));
|
||||
};
|
||||
|
||||
// TODO: check namespace
|
||||
|
||||
return Self::deserialize(&mut reader, &start, false);
|
||||
}
|
||||
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);
|
||||
return Self::deserialize(&mut reader, &start, empty);
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
Err(XmlDeError::UnknownError)
|
||||
}
|
||||
|
||||
fn parse_str(input: &str) -> Result<Self, XmlDeError> {
|
||||
let mut reader = quick_xml::NsReader::from_str(input);
|
||||
fn parse_reader<R: BufRead>(input: R) -> Result<Self, XmlDeError>
|
||||
where
|
||||
Self: XmlDeserialize,
|
||||
{
|
||||
let mut reader = quick_xml::NsReader::from_reader(input);
|
||||
reader.config_mut().trim_text(true);
|
||||
Self::parse(reader)
|
||||
}
|
||||
|
||||
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 {}
|
||||
|
||||
@@ -2,10 +2,12 @@ use quick_xml::events::{BytesStart, Event};
|
||||
use std::io::BufRead;
|
||||
|
||||
pub mod de;
|
||||
mod value;
|
||||
|
||||
pub use de::XmlDeError;
|
||||
pub use de::XmlDeserialize;
|
||||
pub use de::XmlRoot;
|
||||
pub use value::Value;
|
||||
|
||||
impl<T: XmlDeserialize> XmlDeserialize for Option<T> {
|
||||
fn deserialize<R: BufRead>(
|
||||
@@ -17,6 +19,7 @@ impl<T: XmlDeserialize> XmlDeserialize for Option<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Unit;
|
||||
|
||||
impl XmlDeserialize for Unit {
|
||||
@@ -65,3 +68,19 @@ impl XmlDeserialize for String {
|
||||
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
33
crates/xml/src/value.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
use rustical_xml::XmlRoot;
|
||||
use std::io::BufRead;
|
||||
use xml_derive::XmlDeserialize;
|
||||
|
||||
#[test]
|
||||
@@ -11,7 +12,7 @@ fn test_struct_untagged_enum() {
|
||||
|
||||
#[derive(Debug, XmlDeserialize, PartialEq)]
|
||||
struct Prop {
|
||||
#[xml(untagged)]
|
||||
#[xml(ty = "untagged")]
|
||||
prop: PropEnum,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,24 +1,19 @@
|
||||
use rustical_xml::de::XmlRootParseStr;
|
||||
use rustical_xml::{Unit, Unparsed, XmlDeserialize};
|
||||
use std::collections::HashSet;
|
||||
|
||||
use rustical_xml::XmlRoot;
|
||||
use xml_derive::XmlDeserialize;
|
||||
use std::io::BufRead;
|
||||
|
||||
#[test]
|
||||
fn test_struct_text_field() {
|
||||
#[derive(Debug, XmlDeserialize, PartialEq)]
|
||||
#[xml(root = b"document")]
|
||||
struct Document {
|
||||
#[xml(text)]
|
||||
#[xml(ty = "text")]
|
||||
text: String,
|
||||
#[xml(text)]
|
||||
#[xml(ty = "text")]
|
||||
text2: String,
|
||||
}
|
||||
|
||||
impl XmlRoot for Document {
|
||||
fn root_tag() -> &'static [u8] {
|
||||
b"document"
|
||||
}
|
||||
}
|
||||
|
||||
let doc = Document::parse_str(r#"<document>Hello!</document>"#).unwrap();
|
||||
assert_eq!(
|
||||
doc,
|
||||
@@ -32,22 +27,17 @@ fn test_struct_text_field() {
|
||||
#[test]
|
||||
fn test_struct_document() {
|
||||
#[derive(Debug, XmlDeserialize, PartialEq)]
|
||||
#[xml(root = b"document")]
|
||||
struct Document {
|
||||
child: Child,
|
||||
}
|
||||
|
||||
#[derive(Debug, XmlDeserialize, PartialEq, Default)]
|
||||
struct Child {
|
||||
#[xml(text)]
|
||||
#[xml(ty = "text")]
|
||||
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();
|
||||
assert_eq!(
|
||||
doc,
|
||||
@@ -70,7 +60,7 @@ fn test_struct_rename_field() {
|
||||
|
||||
#[derive(Debug, XmlDeserialize, PartialEq, Default)]
|
||||
struct Child {
|
||||
#[xml(text)]
|
||||
#[xml(ty = "text")]
|
||||
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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user