can run
This commit is contained in:
30
src/lib.rs
30
src/lib.rs
@@ -10,39 +10,13 @@ use syn::parse_macro_input;
|
|||||||
|
|
||||||
use parse::Input;
|
use parse::Input;
|
||||||
|
|
||||||
// #[proc_macro_attribute]
|
#[proc_macro_derive(TestDerive, attributes(dsample))]
|
||||||
// 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))]
|
|
||||||
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
pub fn derive_serialize(input: TokenStream) -> TokenStream {
|
||||||
let input = parse_macro_input!(input as Input);
|
let input = parse_macro_input!(input as Input);
|
||||||
println!("");
|
|
||||||
println!("::::: {:?}", &input);
|
println!("::::: {:?}", &input);
|
||||||
let ident = input.ident;
|
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,
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
TokenStream::from(quote! {
|
TokenStream::from(quote! {
|
||||||
// println!("{}", #ident);
|
// 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)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
49
src/parse.rs
49
src/parse.rs
@@ -1,13 +1,11 @@
|
|||||||
use proc_macro2::Span;
|
use proc_macro2::Span;
|
||||||
use syn::parse::{Error, Parse, ParseStream, Parser, Result};
|
use syn::parse::{Error, Parse, ParseStream, Result};
|
||||||
use syn::{parenthesized, Data, DeriveInput, Fields, Ident, Meta, NestedMeta};
|
use syn::{Lit, Data, DeriveInput, Fields, Ident, Meta, NestedMeta};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Input {
|
pub struct Input {
|
||||||
pub ident: Ident, // enum ident
|
pub ident: Ident, // enum ident
|
||||||
pub repr: Ident,
|
|
||||||
pub variants: Vec<Variant>,
|
pub variants: Vec<Variant>,
|
||||||
pub default_variant: Option<Variant>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
@@ -43,7 +41,8 @@ fn parse_attrs(variant: &syn::Variant) -> Result<VariantAttrs> {
|
|||||||
let meta = attr.parse_meta()?;
|
let meta = attr.parse_meta()?;
|
||||||
if let Meta::List(value) = meta {
|
if let Meta::List(value) = meta {
|
||||||
for meta in &value.nested {
|
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") {
|
// if path.is_ident("other") {
|
||||||
// attrs.is_default = true;
|
// attrs.is_default = true;
|
||||||
// }
|
// }
|
||||||
@@ -99,36 +98,34 @@ impl Parse for Input {
|
|||||||
|
|
||||||
println!(">>>>>>>>>>>>>>>>>>");
|
println!(">>>>>>>>>>>>>>>>>>");
|
||||||
|
|
||||||
let mut repr = None;
|
// get dsmple here
|
||||||
for attr in derive_input.attrs {
|
for attr in derive_input.attrs {
|
||||||
println!(">>>>>>> {:?}", &attr.tokens);
|
println!(">>>>>>> {:?}", &attr.tokens);
|
||||||
if attr.path.is_ident("repr") {
|
if attr.path.is_ident("dsample") {
|
||||||
fn repr_arg(input: ParseStream) -> Result<Ident> {
|
match attr.parse_meta() {
|
||||||
let content;
|
Ok(Meta::List(meta)) => {
|
||||||
parenthesized!(content in input);
|
let list: Vec<NestedMeta> = meta.nested.into_iter().collect();
|
||||||
content.parse()
|
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 {
|
Ok(Input {
|
||||||
ident: derive_input.ident,
|
ident: derive_input.ident,
|
||||||
repr,
|
|
||||||
variants,
|
variants,
|
||||||
default_variant,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,9 @@
|
|||||||
use derive_sample::{Serialize_repr};
|
use derive_sample::{TestDerive};
|
||||||
// use derive_sample::dsample;
|
|
||||||
|
|
||||||
mod small_prime {
|
mod small_prime {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
// #[dsample(name = "X")]
|
#[derive(TestDerive)]
|
||||||
#[derive(Serialize_repr)]
|
|
||||||
#[repr(u8)]
|
|
||||||
#[dsample(a= "b")]
|
#[dsample(a= "b")]
|
||||||
enum SmallPrime {
|
enum SmallPrime {
|
||||||
Two = 2,
|
Two = 2,
|
||||||
|
|||||||
Reference in New Issue
Block a user