mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 07:02:24 +00:00
rustical_xml: Use darling for proc-macro parsing
This commit is contained in:
43
Cargo.lock
generated
43
Cargo.lock
generated
@@ -892,6 +892,41 @@ dependencies = [
|
|||||||
"cipher",
|
"cipher",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "darling"
|
||||||
|
version = "0.20.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989"
|
||||||
|
dependencies = [
|
||||||
|
"darling_core",
|
||||||
|
"darling_macro",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "darling_core"
|
||||||
|
version = "0.20.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5"
|
||||||
|
dependencies = [
|
||||||
|
"fnv",
|
||||||
|
"ident_case",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"strsim",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "darling_macro"
|
||||||
|
version = "0.20.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
|
||||||
|
dependencies = [
|
||||||
|
"darling_core",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "der"
|
name = "der"
|
||||||
version = "0.7.9"
|
version = "0.7.9"
|
||||||
@@ -1482,6 +1517,12 @@ dependencies = [
|
|||||||
"thiserror",
|
"thiserror",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ident_case"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "idna"
|
name = "idna"
|
||||||
version = "0.5.0"
|
version = "0.5.0"
|
||||||
@@ -2582,7 +2623,6 @@ dependencies = [
|
|||||||
name = "rustical_xml"
|
name = "rustical_xml"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"heck",
|
|
||||||
"quick-xml",
|
"quick-xml",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"xml_derive",
|
"xml_derive",
|
||||||
@@ -3860,6 +3900,7 @@ dependencies = [
|
|||||||
name = "xml_derive"
|
name = "xml_derive"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"darling",
|
||||||
"heck",
|
"heck",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ description.workspace = true
|
|||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
heck = "0.5.0"
|
|
||||||
quick-xml.workspace = true
|
quick-xml.workspace = true
|
||||||
thiserror.workspace = true
|
thiserror.workspace = true
|
||||||
xml_derive = { path = "./derive" }
|
xml_derive = { path = "./derive" }
|
||||||
|
|||||||
@@ -12,3 +12,4 @@ quote = "1.0"
|
|||||||
proc-macro2 = "1.0"
|
proc-macro2 = "1.0"
|
||||||
heck = "0.5.0"
|
heck = "0.5.0"
|
||||||
strum.workspace = true
|
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};
|
#[derive(Default, FromMeta)]
|
||||||
use quote::ToTokens;
|
pub struct ContainerAttrs {
|
||||||
use strum::EnumString;
|
pub ns_strict: Flag,
|
||||||
use syn::{
|
}
|
||||||
punctuated::Punctuated, token::Comma, Attribute, Expr, ExprLit, Lit, LitByteStr, LitStr, Meta,
|
|
||||||
};
|
|
||||||
|
|
||||||
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 struct VariantAttrs {
|
||||||
pub rename: Option<String>,
|
#[darling(flatten)]
|
||||||
pub ns: Option<String>,
|
pub common: TagAttrs,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_scoped_attrs(attrs: &[Attribute]) -> Option<Punctuated<Meta, Comma>> {
|
#[derive(Default, FromDeriveInput)]
|
||||||
attrs
|
#[darling(attributes(xml))]
|
||||||
.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)]
|
|
||||||
pub struct EnumAttrs {
|
pub struct EnumAttrs {
|
||||||
pub case_style: Option<CaseStyle>,
|
#[darling(flatten)]
|
||||||
pub ns_strict: bool,
|
container: ContainerAttrs,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_enum_attrs(attrs: &[Attribute]) -> EnumAttrs {
|
#[derive(Default, FromDeriveInput)]
|
||||||
let enum_attrs = EnumAttrs::default();
|
#[darling(attributes(xml))]
|
||||||
|
|
||||||
enum_attrs
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct StructAttrs {
|
pub struct StructAttrs {
|
||||||
|
#[darling(flatten)]
|
||||||
|
pub container: ContainerAttrs,
|
||||||
|
|
||||||
pub root: Option<LitByteStr>,
|
pub root: Option<LitByteStr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_struct_attrs(attrs: &[Attribute]) -> StructAttrs {
|
#[derive(Default, FromField)]
|
||||||
let mut struct_attrs = StructAttrs::default();
|
#[darling(attributes(xml))]
|
||||||
|
|
||||||
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)]
|
|
||||||
pub struct FieldAttrs {
|
pub struct FieldAttrs {
|
||||||
pub rename: Option<String>,
|
#[darling(flatten)]
|
||||||
pub ns: Option<String>,
|
pub common: TagAttrs,
|
||||||
pub text: bool,
|
pub text: Flag,
|
||||||
pub untagged: bool,
|
pub untagged: Flag,
|
||||||
pub flatten: bool,
|
pub flatten: Flag,
|
||||||
pub default: Option<syn::ExprPath>,
|
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 darling::FromVariant;
|
||||||
use proc_macro2::Span;
|
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;
|
||||||
|
|
||||||
@@ -35,12 +37,13 @@ pub fn impl_de_enum(input: &DeriveInput, data: &DataEnum) -> proc_macro2::TokenS
|
|||||||
let name = &input.ident;
|
let name = &input.ident;
|
||||||
|
|
||||||
let variants = data.variants.iter().map(|variant| {
|
let variants = data.variants.iter().map(|variant| {
|
||||||
let attrs = parse_variant_attrs(&variant.attrs);
|
let attrs = VariantAttrs::from_variant(variant).unwrap();
|
||||||
|
let variant_name = attrs.common.rename.unwrap_or(syn::LitByteStr::new(
|
||||||
let variant_name = attrs.rename.unwrap_or(variant.ident.to_string());
|
variant.ident.to_string().to_kebab_case().as_bytes(),
|
||||||
let variant_name = syn::LitByteStr::new(variant_name.as_bytes(), Span::call_site());
|
variant.ident.span(),
|
||||||
|
));
|
||||||
let branch = enum_variant_branch(variant);
|
let branch = enum_variant_branch(variant);
|
||||||
// dbg!(variant.fields.to_token_stream());
|
dbg!(&variant_name);
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
#variant_name => {
|
#variant_name => {
|
||||||
@@ -55,17 +58,16 @@ pub fn impl_de_enum(input: &DeriveInput, data: &DataEnum) -> proc_macro2::TokenS
|
|||||||
reader: &mut quick_xml::NsReader<R>,
|
reader: &mut quick_xml::NsReader<R>,
|
||||||
start: &quick_xml::events::BytesStart,
|
start: &quick_xml::events::BytesStart,
|
||||||
empty: bool
|
empty: bool
|
||||||
) -> Result<Self, rustical_xml::XmlError> {
|
) -> Result<Self, rustical_xml::XmlDeError> {
|
||||||
use quick_xml::events::Event;
|
use quick_xml::events::Event;
|
||||||
|
|
||||||
let (_ns, name) = reader.resolve_element(start.name());
|
let (_ns, name) = reader.resolve_element(start.name());
|
||||||
|
|
||||||
|
|
||||||
match name.as_ref() {
|
match name.as_ref() {
|
||||||
#(#variants)*
|
#(#variants)*
|
||||||
name => {
|
name => {
|
||||||
// Handle invalid variant 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 core::panic;
|
||||||
|
use darling::{FromDeriveInput, FromField};
|
||||||
use heck::ToKebabCase;
|
use heck::ToKebabCase;
|
||||||
use proc_macro2::Span;
|
|
||||||
use quote::quote;
|
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> {
|
fn get_generic_type(ty: &syn::Type) -> Option<&syn::Type> {
|
||||||
if let syn::Type::Path(TypePath { path, .. }) = ty {
|
if let syn::Type::Path(TypePath { path, .. }) = ty {
|
||||||
@@ -29,15 +31,19 @@ pub struct Field {
|
|||||||
impl Field {
|
impl Field {
|
||||||
fn from_syn_field(field: syn::Field) -> Self {
|
fn from_syn_field(field: syn::Field) -> Self {
|
||||||
Self {
|
Self {
|
||||||
attrs: parse_field_attrs(&field.attrs),
|
attrs: FieldAttrs::from_field(&field).unwrap(),
|
||||||
field,
|
field,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn de_name(&self) -> String {
|
fn de_name(&self) -> LitByteStr {
|
||||||
self.attrs
|
self.attrs
|
||||||
|
.common
|
||||||
.rename
|
.rename
|
||||||
.to_owned()
|
.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 {
|
fn field_ident(&self) -> &syn::Ident {
|
||||||
@@ -58,7 +64,7 @@ impl Field {
|
|||||||
quote! {
|
quote! {
|
||||||
#field_ident: #ty,
|
#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)");
|
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! {
|
quote! {
|
||||||
#field_ident: Vec<#generic_type>,
|
#field_ident: Vec<#generic_type>,
|
||||||
@@ -76,7 +82,7 @@ impl Field {
|
|||||||
quote! {
|
quote! {
|
||||||
#field_ident: #default(),
|
#field_ident: #default(),
|
||||||
}
|
}
|
||||||
} else if self.attrs.flatten {
|
} else if self.attrs.flatten.is_present() {
|
||||||
quote! {
|
quote! {
|
||||||
#field_ident: vec![],
|
#field_ident: vec![],
|
||||||
}
|
}
|
||||||
@@ -89,7 +95,7 @@ impl Field {
|
|||||||
|
|
||||||
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 {
|
if self.attrs.flatten.is_present() {
|
||||||
quote! {
|
quote! {
|
||||||
#field_ident: FromIterator::from_iter(builder.#field_ident.into_iter())
|
#field_ident: FromIterator::from_iter(builder.#field_ident.into_iter())
|
||||||
}
|
}
|
||||||
@@ -105,13 +111,13 @@ impl Field {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn named_branch(&self) -> Option<proc_macro2::TokenStream> {
|
fn named_branch(&self) -> Option<proc_macro2::TokenStream> {
|
||||||
if self.attrs.text {
|
if self.attrs.text.is_present() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
if self.attrs.untagged {
|
if self.attrs.untagged.is_present() {
|
||||||
return None;
|
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 field_ident = self.field_ident();
|
||||||
let deserializer = self.ty();
|
let deserializer = self.ty();
|
||||||
Some(if self.attrs.default.is_some() {
|
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)?;
|
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)");
|
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! {
|
||||||
#field_name => {
|
#field_name => {
|
||||||
@@ -137,20 +143,29 @@ impl Field {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn untagged_branch(&self) -> Option<proc_macro2::TokenStream> {
|
fn untagged_branch(&self) -> Option<proc_macro2::TokenStream> {
|
||||||
if !self.attrs.untagged {
|
if !self.attrs.untagged.is_present() {
|
||||||
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(quote! {
|
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)");
|
||||||
builder.#field_ident = Some(<#deserializer as rustical_xml::XmlDeserialize>::deserialize(reader, &start, empty)?);
|
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> {
|
fn text_branch(&self) -> Option<proc_macro2::TokenStream> {
|
||||||
if !self.attrs.text {
|
if !self.attrs.text.is_present() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let field_ident = self.field_ident();
|
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 (impl_generics, type_generics, where_clause) = input.generics.split_for_impl();
|
||||||
let name = &input.ident;
|
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
|
let fields: Vec<_> = data
|
||||||
.fields
|
.fields
|
||||||
@@ -203,9 +218,9 @@ pub fn impl_de_struct(input: &DeriveInput, data: &DataStruct) -> proc_macro2::To
|
|||||||
reader: &mut quick_xml::NsReader<R>,
|
reader: &mut quick_xml::NsReader<R>,
|
||||||
start: &quick_xml::events::BytesStart,
|
start: &quick_xml::events::BytesStart,
|
||||||
empty: bool
|
empty: bool
|
||||||
) -> Result<Self, rustical_xml::XmlError> {
|
) -> Result<Self, rustical_xml::XmlDeError> {
|
||||||
use quick_xml::events::Event;
|
use quick_xml::events::Event;
|
||||||
use rustical_xml::XmlError;
|
use rustical_xml::XmlDeError;
|
||||||
|
|
||||||
let mut buf = Vec::new();
|
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() => {
|
Event::End(e) if e.name() == start.name() => {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Event::Eof => return Err(XmlError::Eof),
|
Event::Eof => return Err(XmlDeError::Eof),
|
||||||
// start of a child element
|
// start of a child element
|
||||||
Event::Start(start) | Event::Empty(start) => {
|
Event::Start(start) | Event::Empty(start) => {
|
||||||
let empty = matches!(event, Event::Empty(_));
|
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)*
|
#(#untagged_field_branches)*
|
||||||
_ => {
|
_ => {
|
||||||
// invalid field name
|
// 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)*
|
#(#text_field_branches)*
|
||||||
}
|
}
|
||||||
Event::CData(cdata) => {
|
Event::CData(cdata) => {
|
||||||
return Err(XmlError::UnsupportedEvent("CDATA"));
|
return Err(XmlDeError::UnsupportedEvent("CDATA"));
|
||||||
}
|
}
|
||||||
Event::Comment(_) => {
|
Event::Comment(_) => {
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
Event::Decl(_) => {
|
Event::Decl(_) => {
|
||||||
// Error: not supported
|
// Error: not supported
|
||||||
return Err(XmlError::UnsupportedEvent("Declaration"));
|
return Err(XmlDeError::UnsupportedEvent("Declaration"));
|
||||||
}
|
}
|
||||||
Event::PI(_) => {
|
Event::PI(_) => {
|
||||||
// Error: not supported
|
// Error: not supported
|
||||||
return Err(XmlError::UnsupportedEvent("Processing instruction"));
|
return Err(XmlDeError::UnsupportedEvent("Processing instruction"));
|
||||||
}
|
}
|
||||||
Event::DocType(doctype) => {
|
Event::DocType(doctype) => {
|
||||||
// Error: start of new document
|
// 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) => {
|
Event::End(end) => {
|
||||||
// Error: premature 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()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
80
crates/xml/src/de.rs
Normal file
80
crates/xml/src/de.rs
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
use std::io::BufRead;
|
||||||
|
pub use xml_derive::XmlDeserialize;
|
||||||
|
|
||||||
|
use quick_xml::events::{BytesStart, Event};
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum XmlDeError {
|
||||||
|
#[error(transparent)]
|
||||||
|
QuickXmlDeError(#[from] quick_xml::de::DeError),
|
||||||
|
#[error(transparent)]
|
||||||
|
QuickXmlError(#[from] quick_xml::Error),
|
||||||
|
#[error("Unknown error")]
|
||||||
|
UnknownError,
|
||||||
|
#[error("Invalid tag {0}. Expected {1}")]
|
||||||
|
InvalidTag(String, String),
|
||||||
|
#[error("Missing field {0}")]
|
||||||
|
MissingField(&'static str),
|
||||||
|
#[error("End of file, expected closing tags")]
|
||||||
|
Eof,
|
||||||
|
#[error("Unsupported xml event: {0}")]
|
||||||
|
UnsupportedEvent(&'static str),
|
||||||
|
#[error("{0}")]
|
||||||
|
Other(String),
|
||||||
|
#[error("Invalid variant: {0}")]
|
||||||
|
InvalidVariant(String),
|
||||||
|
#[error("Invalid field name: ")]
|
||||||
|
InvalidFieldName,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait XmlDeserialize: Sized {
|
||||||
|
fn deserialize<R: BufRead>(
|
||||||
|
reader: &mut quick_xml::NsReader<R>,
|
||||||
|
start: &BytesStart,
|
||||||
|
empty: bool,
|
||||||
|
) -> Result<Self, XmlDeError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait XmlRoot: XmlDeserialize {
|
||||||
|
fn parse<R: BufRead>(mut reader: quick_xml::NsReader<R>) -> Result<Self, XmlDeError> {
|
||||||
|
let mut buf = Vec::new();
|
||||||
|
let event = reader.read_event_into(&mut buf)?;
|
||||||
|
match event {
|
||||||
|
Event::Start(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, 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);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
Err(XmlDeError::UnknownError)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_str(input: &str) -> Result<Self, XmlDeError> {
|
||||||
|
let mut reader = quick_xml::NsReader::from_str(input);
|
||||||
|
reader.config_mut().trim_text(true);
|
||||||
|
Self::parse(reader)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn root_tag() -> &'static [u8];
|
||||||
|
}
|
||||||
@@ -1,87 +1,18 @@
|
|||||||
use quick_xml::events::{BytesStart, Event};
|
use quick_xml::events::{BytesStart, Event};
|
||||||
use std::io::BufRead;
|
use std::io::BufRead;
|
||||||
use thiserror::Error;
|
|
||||||
pub use xml_derive::XmlDeserialize;
|
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
pub mod de;
|
||||||
pub enum XmlError {
|
|
||||||
#[error(transparent)]
|
|
||||||
QuickXmlDeError(#[from] quick_xml::de::DeError),
|
|
||||||
#[error(transparent)]
|
|
||||||
QuickXmlError(#[from] quick_xml::Error),
|
|
||||||
#[error("Unknown error")]
|
|
||||||
UnknownError,
|
|
||||||
#[error("Invalid tag {0}. Expected {1}")]
|
|
||||||
InvalidTag(String, String),
|
|
||||||
#[error("Missing field {0}")]
|
|
||||||
MissingField(&'static str),
|
|
||||||
#[error("End of file, expected closing tags")]
|
|
||||||
Eof,
|
|
||||||
#[error("Unsupported xml event: {0}")]
|
|
||||||
UnsupportedEvent(&'static str),
|
|
||||||
#[error("{0}")]
|
|
||||||
Other(String),
|
|
||||||
#[error("Invalid variant: {0}")]
|
|
||||||
InvalidVariant(String),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait XmlDeserialize: Sized {
|
pub use de::XmlDeError;
|
||||||
fn deserialize<R: BufRead>(
|
pub use de::XmlDeserialize;
|
||||||
reader: &mut quick_xml::NsReader<R>,
|
pub use de::XmlRoot;
|
||||||
start: &BytesStart,
|
|
||||||
empty: bool,
|
|
||||||
) -> Result<Self, XmlError>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait XmlRoot: XmlDeserialize {
|
|
||||||
fn parse<R: BufRead>(mut reader: quick_xml::NsReader<R>) -> Result<Self, XmlError> {
|
|
||||||
let mut buf = Vec::new();
|
|
||||||
let event = reader.read_event_into(&mut buf)?;
|
|
||||||
match event {
|
|
||||||
Event::Start(start) => {
|
|
||||||
let (_ns, name) = reader.resolve_element(start.name());
|
|
||||||
if name.as_ref() != Self::root_tag() {
|
|
||||||
return Err(XmlError::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, false);
|
|
||||||
}
|
|
||||||
Event::Empty(start) => {
|
|
||||||
let (_ns, name) = reader.resolve_element(start.name());
|
|
||||||
if name.as_ref() != Self::root_tag() {
|
|
||||||
return Err(XmlError::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(XmlError::UnknownError)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_str(input: &str) -> Result<Self, XmlError> {
|
|
||||||
let mut reader = quick_xml::NsReader::from_str(input);
|
|
||||||
reader.config_mut().trim_text(true);
|
|
||||||
Self::parse(reader)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn root_tag() -> &'static [u8];
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: XmlDeserialize> XmlDeserialize for Option<T> {
|
impl<T: XmlDeserialize> XmlDeserialize for Option<T> {
|
||||||
fn deserialize<R: BufRead>(
|
fn deserialize<R: BufRead>(
|
||||||
reader: &mut quick_xml::NsReader<R>,
|
reader: &mut quick_xml::NsReader<R>,
|
||||||
start: &BytesStart,
|
start: &BytesStart,
|
||||||
empty: bool,
|
empty: bool,
|
||||||
) -> Result<Self, XmlError> {
|
) -> Result<Self, XmlDeError> {
|
||||||
Ok(Some(T::deserialize(reader, start, empty)?))
|
Ok(Some(T::deserialize(reader, start, empty)?))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -93,7 +24,7 @@ impl XmlDeserialize for Unit {
|
|||||||
reader: &mut quick_xml::NsReader<R>,
|
reader: &mut quick_xml::NsReader<R>,
|
||||||
start: &BytesStart,
|
start: &BytesStart,
|
||||||
empty: bool,
|
empty: bool,
|
||||||
) -> Result<Self, XmlError> {
|
) -> Result<Self, XmlDeError> {
|
||||||
if empty {
|
if empty {
|
||||||
return Ok(Unit);
|
return Ok(Unit);
|
||||||
}
|
}
|
||||||
@@ -101,7 +32,7 @@ impl XmlDeserialize for Unit {
|
|||||||
loop {
|
loop {
|
||||||
match reader.read_event_into(&mut buf)? {
|
match reader.read_event_into(&mut buf)? {
|
||||||
Event::End(e) if e.name() == start.name() => return Ok(Unit),
|
Event::End(e) if e.name() == start.name() => return Ok(Unit),
|
||||||
Event::Eof => return Err(XmlError::Eof),
|
Event::Eof => return Err(XmlDeError::Eof),
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -113,7 +44,7 @@ impl XmlDeserialize for String {
|
|||||||
reader: &mut quick_xml::NsReader<R>,
|
reader: &mut quick_xml::NsReader<R>,
|
||||||
start: &BytesStart,
|
start: &BytesStart,
|
||||||
empty: bool,
|
empty: bool,
|
||||||
) -> Result<Self, XmlError> {
|
) -> Result<Self, XmlDeError> {
|
||||||
if empty {
|
if empty {
|
||||||
return Ok(String::new());
|
return Ok(String::new());
|
||||||
}
|
}
|
||||||
@@ -124,11 +55,11 @@ impl XmlDeserialize for String {
|
|||||||
Event::End(e) if e.name() == start.name() => {
|
Event::End(e) if e.name() == start.name() => {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Event::Eof => return Err(XmlError::Eof),
|
Event::Eof => return Err(XmlDeError::Eof),
|
||||||
Event::Text(text) => {
|
Event::Text(text) => {
|
||||||
content.push_str(&text.unescape()?);
|
content.push_str(&text.unescape()?);
|
||||||
}
|
}
|
||||||
_a => return Err(XmlError::UnknownError),
|
_a => return Err(XmlDeError::UnknownError),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Ok(content)
|
Ok(content)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use xml_derive::XmlDeserialize;
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_struct_untagged_enum() {
|
fn test_struct_untagged_enum() {
|
||||||
#[derive(Debug, XmlDeserialize, PartialEq)]
|
#[derive(Debug, XmlDeserialize, PartialEq)]
|
||||||
|
#[xml(root = b"propfind")]
|
||||||
struct Propfind {
|
struct Propfind {
|
||||||
prop: Prop,
|
prop: Prop,
|
||||||
}
|
}
|
||||||
@@ -20,12 +21,6 @@ fn test_struct_untagged_enum() {
|
|||||||
B,
|
B,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl XmlRoot for Propfind {
|
|
||||||
fn root_tag() -> &'static [u8] {
|
|
||||||
b"propfind"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let doc = Propfind::parse_str(
|
let doc = Propfind::parse_str(
|
||||||
r#"
|
r#"
|
||||||
<propfind>
|
<propfind>
|
||||||
|
|||||||
@@ -62,8 +62,9 @@ fn test_struct_document() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_struct_rename_field() {
|
fn test_struct_rename_field() {
|
||||||
#[derive(Debug, XmlDeserialize, PartialEq)]
|
#[derive(Debug, XmlDeserialize, PartialEq)]
|
||||||
|
#[xml(root = b"document")]
|
||||||
struct Document {
|
struct Document {
|
||||||
#[xml(rename = "ok-wow")]
|
#[xml(rename = b"ok-wow")]
|
||||||
child: Child,
|
child: Child,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,12 +74,6 @@ fn test_struct_rename_field() {
|
|||||||
text: String,
|
text: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl XmlRoot for Document {
|
|
||||||
fn root_tag() -> &'static [u8] {
|
|
||||||
b"document"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let doc = Document::parse_str(r#"<document><ok-wow>Hello!</ok-wow></document>"#).unwrap();
|
let doc = Document::parse_str(r#"<document><ok-wow>Hello!</ok-wow></document>"#).unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
doc,
|
doc,
|
||||||
@@ -93,17 +88,12 @@ fn test_struct_rename_field() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_struct_optional_field() {
|
fn test_struct_optional_field() {
|
||||||
#[derive(Debug, XmlDeserialize, PartialEq)]
|
#[derive(Debug, XmlDeserialize, PartialEq)]
|
||||||
|
#[xml(root = b"document")]
|
||||||
struct Document {
|
struct Document {
|
||||||
#[xml(default = "Default::default")]
|
#[xml(default = "Default::default")]
|
||||||
child: Option<Child>,
|
child: Option<Child>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl XmlRoot for Document {
|
|
||||||
fn root_tag() -> &'static [u8] {
|
|
||||||
b"document"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, XmlDeserialize, PartialEq, Default)]
|
#[derive(Debug, XmlDeserialize, PartialEq, Default)]
|
||||||
struct Child;
|
struct Child;
|
||||||
|
|
||||||
@@ -119,7 +109,7 @@ fn test_struct_vec() {
|
|||||||
#[derive(Debug, XmlDeserialize, PartialEq)]
|
#[derive(Debug, XmlDeserialize, PartialEq)]
|
||||||
#[xml(root = b"document")]
|
#[xml(root = b"document")]
|
||||||
struct Document {
|
struct Document {
|
||||||
#[xml(rename = "child", flatten)]
|
#[xml(rename = b"child", flatten)]
|
||||||
children: Vec<Child>,
|
children: Vec<Child>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,7 +137,7 @@ fn test_struct_set() {
|
|||||||
#[derive(Debug, XmlDeserialize, PartialEq)]
|
#[derive(Debug, XmlDeserialize, PartialEq)]
|
||||||
#[xml(root = b"document")]
|
#[xml(root = b"document")]
|
||||||
struct Document {
|
struct Document {
|
||||||
#[xml(rename = "child", flatten)]
|
#[xml(rename = b"child", flatten)]
|
||||||
children: HashSet<Child>,
|
children: HashSet<Child>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,123 +0,0 @@
|
|||||||
use quick_xml::events::{BytesStart, Event};
|
|
||||||
use rustical_xml::{Unit, XmlDeserialize, XmlError, XmlRoot};
|
|
||||||
|
|
||||||
#[derive(Debug, XmlDeserialize)]
|
|
||||||
#[xml(rename_all = "kebab-case")]
|
|
||||||
pub enum Prop {
|
|
||||||
#[xml(rename = "displayname")]
|
|
||||||
Displayname(String),
|
|
||||||
#[xml(ns = "DAV:Push", rename = "transports")]
|
|
||||||
Transports,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct PropfindElement<T: XmlDeserialize> {
|
|
||||||
// child with name propfind and namespace DAV:
|
|
||||||
pub prop: Vec<T>,
|
|
||||||
pub test: Option<Prop>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: XmlDeserialize> XmlDeserialize for PropfindElement<T> {
|
|
||||||
fn deserialize<R: std::io::BufRead>(
|
|
||||||
reader: &mut quick_xml::NsReader<R>,
|
|
||||||
start: &BytesStart,
|
|
||||||
empty: bool,
|
|
||||||
) -> Result<Self, XmlError> {
|
|
||||||
// init values for the struct attributes
|
|
||||||
let mut attr_prop: Option<Vec<T>> = None;
|
|
||||||
let mut attr_test: Option<Prop> = None;
|
|
||||||
|
|
||||||
if !empty {
|
|
||||||
let mut buf = Vec::new();
|
|
||||||
loop {
|
|
||||||
match reader.read_event_into(&mut buf)? {
|
|
||||||
Event::End(e) if e.name() == start.name() => {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
Event::Eof => return Err(XmlError::Eof),
|
|
||||||
Event::Start(start) => {
|
|
||||||
let (_ns, name) = reader.resolve_element(start.name());
|
|
||||||
match name.as_ref() {
|
|
||||||
b"prop" => {
|
|
||||||
if attr_prop.is_none() {
|
|
||||||
attr_prop = Some(Vec::<T>::deserialize(reader, &start, false)?);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
b"test" => {
|
|
||||||
if attr_test.is_none() {
|
|
||||||
attr_test = Some(Prop::deserialize(reader, &start, false)?);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return Err(XmlError::InvalidTag(
|
|
||||||
String::from_utf8_lossy(name.as_ref()).to_string(),
|
|
||||||
"prop".to_string(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Event::Empty(start) => {
|
|
||||||
let (_ns, name) = reader.resolve_element(start.name());
|
|
||||||
match name.as_ref() {
|
|
||||||
b"prop" => {
|
|
||||||
if attr_prop.is_none() {
|
|
||||||
attr_prop = Some(Vec::<T>::deserialize(reader, &start, true)?);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
b"test" => {
|
|
||||||
if attr_test.is_none() {
|
|
||||||
attr_test = Some(Prop::deserialize(reader, &start, true)?);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return Err(XmlError::InvalidTag(
|
|
||||||
String::from_utf8_lossy(name.as_ref()).to_string(),
|
|
||||||
"prop".to_string(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
a => {
|
|
||||||
dbg!(a);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let attr_prop = attr_prop.ok_or(XmlError::MissingField("prop"))?;
|
|
||||||
Ok(Self {
|
|
||||||
prop: attr_prop,
|
|
||||||
test: None,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: XmlDeserialize> XmlRoot for PropfindElement<T> {
|
|
||||||
fn root_tag() -> &'static [u8] {
|
|
||||||
b"propfind"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_propfind() {
|
|
||||||
let propfind: PropfindElement<Prop> = PropfindElement::parse_str(
|
|
||||||
r#"
|
|
||||||
<propfind xmlns="DAV:" xmlns:P="DAV:Push">
|
|
||||||
<P:prop>
|
|
||||||
<displayname>Hello!</displayname>
|
|
||||||
<transports xmlns="DAV:Push" />
|
|
||||||
<transports xmlns="DAV:Push"></transports>
|
|
||||||
</P:prop>
|
|
||||||
<test>
|
|
||||||
<displayname>Okay wow!</displayname>
|
|
||||||
</test>
|
|
||||||
</propfind>
|
|
||||||
"#,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
dbg!(propfind);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn asd() {
|
|
||||||
let a: Option<String> = None;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user