Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
scottlamb committed Oct 23, 2022
1 parent acce396 commit 40764cb
Show file tree
Hide file tree
Showing 5 changed files with 419 additions and 577 deletions.
52 changes: 6 additions & 46 deletions static-xml-derive/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{cell::RefCell, collections::BTreeMap};
use proc_macro2::TokenStream;
use quote::{quote, quote_spanned, ToTokens};
use static_xml::{de::WhiteSpace, ExpandedNameRef};
use syn::{spanned::Spanned, DeriveInput, Fields, Lit, LitStr, Meta, MetaNameValue, NestedMeta};
use syn::{DeriveInput, Fields, Lit, LitStr, Meta, MetaNameValue, NestedMeta};

// See serde/serde_derive/src/internals/attr.rs and yaserde_derive/src/common/field.rs

Expand Down Expand Up @@ -249,7 +249,6 @@ pub(crate) enum TextVariantMode {
pub(crate) struct ElementAttr<'a> {
pub(crate) name: Name<'a>,
pub(crate) namespaces: Namespaces,
pub(crate) direct: bool,
// TODO: rename_all.
}

Expand Down Expand Up @@ -382,7 +381,6 @@ impl<'a> ElementAttr<'a> {
let mut name = Name::from_ident(&input.ident);
let mut namespaces = Namespaces::default();
let mut prefix_nv = None;
let mut direct = false;
for item in get_meta_items(errors, &input.attrs) {
match item {
NestedMeta::Meta(Meta::NameValue(nv)) => {
Expand All @@ -403,28 +401,13 @@ impl<'a> ElementAttr<'a> {
errors.push(syn::Error::new_spanned(nv, "item not understood"));
}
}
NestedMeta::Meta(Meta::Path(p)) => {
if let Some(id) = p.get_ident() {
if id == "direct" {
direct = true;
} else {
errors.push(syn::Error::new_spanned(p, "item not understood"));
}
} else {
errors.push(syn::Error::new_spanned(p, "item not understood"));
}
}
i => errors.push(syn::Error::new_spanned(i, "item not understood")),
}
}
if let Some(ref nv) = prefix_nv {
namespaces.process_prefix(errors, nv, &mut name);
}
ElementAttr {
name,
namespaces,
direct,
}
ElementAttr { name, namespaces }
}
}

Expand Down Expand Up @@ -506,20 +489,6 @@ impl<'a> ElementStruct<'a> {
text_field_pos,
})
}

pub(crate) fn quote_flatten_fields(&self) -> Vec<TokenStream> {
self.fields
.iter()
.filter_map(|f| {
if matches!(f.mode, ElementFieldMode::Flatten) {
let ident = &f.inner.ident;
Some(quote_spanned! { f.inner.ident.span() => &mut self.#ident })
} else {
None
}
})
.collect()
}
}

const STATIC_XML: &str = "static_xml";
Expand Down Expand Up @@ -553,20 +522,10 @@ pub(crate) enum ElementFieldMode {
Flatten,
}

impl ElementFieldMode {
pub(crate) fn quote_deserialize_trait(self) -> TokenStream {
match self {
ElementFieldMode::Element { .. } => quote! { ::static_xml::de::DeserializeField },
ElementFieldMode::Attribute { .. } => quote! { ::static_xml::de::DeserializeAttr },
ElementFieldMode::Text => panic!("text is different"),
ElementFieldMode::Flatten => quote! { ::static_xml::de::DeserializeFlatten },
}
}
}

/// Field within an `ElementStruct`.
pub(crate) struct ElementField<'a> {
pub(crate) inner: &'a syn::Field,
pub(crate) ident: &'a syn::Ident,
pub(crate) mode: ElementFieldMode,
pub(crate) default: bool,
pub(crate) name: Name<'a>,
Expand All @@ -579,8 +538,8 @@ impl<'a> ElementField<'a> {
let mut default = false;
let mut flatten = false;
let mut text = false;
let mut name =
Name::from_ident(inner.ident.as_ref().expect("struct fields should be named"));
let ident = inner.ident.as_ref().expect("struct fields should be named");
let mut name = Name::from_ident(ident);
for item in get_meta_items(errors, &inner.attrs) {
match &item {
NestedMeta::Meta(Meta::Path(p)) if p.is_ident("attribute") => attribute = true,
Expand Down Expand Up @@ -631,6 +590,7 @@ impl<'a> ElementField<'a> {
};
Ok(ElementField {
inner,
ident,
default,
mode,
name,
Expand Down
Loading

0 comments on commit 40764cb

Please sign in to comment.