This commit is contained in:
2020-01-21 00:36:53 +08:00
parent d862d3c173
commit bacd995f8e
3 changed files with 27 additions and 59 deletions

View File

@@ -10,39 +10,13 @@ use syn::parse_macro_input;
use parse::Input;
// #[proc_macro_attribute]
// pub fn dsample(attr: TokenStream, item: TokenStream) -> TokenStream {
// println!("attr: \"{}\"", attr.to_string());
// println!("item: \"{}\"", item.to_string());
// item
// }
#[proc_macro_derive(Serialize_repr, attributes(dsample))]
#[proc_macro_derive(TestDerive, attributes(dsample))]
pub fn derive_serialize(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as Input);
println!("");
println!("::::: {:?}", &input);
let ident = input.ident;
let repr = input.repr;
let match_variants = input.variants.iter().map(|variant| {
let variant = &variant.ident;
quote! {
#ident::#variant => #ident::#variant as #repr,
}
});
let _ident = input.ident;
TokenStream::from(quote! {
// println!("{}", #ident);
// impl serde::Serialize for #ident {
// fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
// where S: serde::Serializer
// {
// let value: #repr = match *self {
// #(#match_variants)*
// };
// serde::Serialize::serialize(&value, serializer)
// }
// }
})
}

View File

@@ -1,13 +1,11 @@
use proc_macro2::Span;
use syn::parse::{Error, Parse, ParseStream, Parser, Result};
use syn::{parenthesized, Data, DeriveInput, Fields, Ident, Meta, NestedMeta};
use syn::parse::{Error, Parse, ParseStream, Result};
use syn::{Lit, Data, DeriveInput, Fields, Ident, Meta, NestedMeta};
#[derive(Debug)]
pub struct Input {
pub ident: Ident, // enum ident
pub repr: Ident,
pub variants: Vec<Variant>,
pub default_variant: Option<Variant>,
}
#[derive(Clone, Debug)]
@@ -43,7 +41,8 @@ fn parse_attrs(variant: &syn::Variant) -> Result<VariantAttrs> {
let meta = attr.parse_meta()?;
if let Meta::List(value) = meta {
for meta in &value.nested {
if let NestedMeta::Meta(Meta::Path(path)) = meta {
if let NestedMeta::Meta(Meta::Path(_path)) = meta {
// TODO ...
// if path.is_ident("other") {
// attrs.is_default = true;
// }
@@ -99,36 +98,34 @@ impl Parse for Input {
println!(">>>>>>>>>>>>>>>>>>");
let mut repr = None;
// get dsmple here
for attr in derive_input.attrs {
println!(">>>>>>> {:?}", &attr.tokens);
if attr.path.is_ident("repr") {
fn repr_arg(input: ParseStream) -> Result<Ident> {
let content;
parenthesized!(content in input);
content.parse()
if attr.path.is_ident("dsample") {
match attr.parse_meta() {
Ok(Meta::List(meta)) => {
let list: Vec<NestedMeta> = meta.nested.into_iter().collect();
for nested_meta in list.iter() {
match nested_meta {
NestedMeta::Meta(Meta::NameValue(nv)) => {
if let Lit::Str(lit) = &nv.lit {
if nv.path.is_ident("a") {
println!("------------------------------ a = {}", &lit.value());
}
}
},
_ => (),
}
}
},
_ => (),
}
let ty = repr_arg.parse2(attr.tokens)?;
repr = Some(ty);
break;
}
}
let repr = repr.ok_or_else(|| Error::new(call_site, "missing #[repr(...)] attribute"))?;
let mut default_variants = variants.iter().filter(|x| x.attrs.is_default);
let default_variant = default_variants.next().cloned();
if default_variants.next().is_some() {
return Err(Error::new(
call_site,
"only one variant can be #[serde(other)]",
));
}
Ok(Input {
ident: derive_input.ident,
repr,
variants,
default_variant,
})
}
}

View File

@@ -1,12 +1,9 @@
use derive_sample::{Serialize_repr};
// use derive_sample::dsample;
use derive_sample::{TestDerive};
mod small_prime {
use super::*;
// #[dsample(name = "X")]
#[derive(Serialize_repr)]
#[repr(u8)]
#[derive(TestDerive)]
#[dsample(a= "b")]
enum SmallPrime {
Two = 2,