Update to syn 0.6.0

This commit is contained in:
Łukasz Jan Niemier 2016-09-26 20:32:46 +02:00
parent e9768a0a76
commit ada17a1793
No known key found for this signature in database
GPG Key ID: C775391C950A6AEE
2 changed files with 11 additions and 2 deletions

View File

@ -11,7 +11,7 @@ version = "0.1.33"
[dependencies] [dependencies]
quote = "0.1.3" quote = "0.1.3"
syn = "0.5.2" syn = "0.6.0"
[dev-dependencies] [dev-dependencies]

View File

@ -24,12 +24,21 @@ use syn::Body::Enum;
pub fn from_primitive(input: TokenStream) -> TokenStream { pub fn from_primitive(input: TokenStream) -> TokenStream {
let source = input.to_string(); let source = input.to_string();
let ast = syn::parse_item(&source).unwrap(); let ast = syn::parse_macro_input(&source).unwrap();
let name = &ast.ident;
let variants = match ast.body {
Enum(ref variants) => variants,
_ => panic!("`FromPrimitive` can be applied only to the enums, {} is not an enum", name),
};
let mut idx = 0; let mut idx = 0;
let variants: Vec<_> = variants.iter() let variants: Vec<_> = variants.iter()
.map(|variant| { .map(|variant| {
let ident = &variant.ident; let ident = &variant.ident;
if let Some(val) = variant.discriminant {
idx = val.value;
}
let tt = quote!(#idx => Some(#name::#ident)); let tt = quote!(#idx => Some(#name::#ident));
idx += 1; idx += 1;
tt tt