From ada17a179309e72a14dcac0f36266b0bd1a36eff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Jan=20Niemier?= Date: Mon, 26 Sep 2016 20:32:46 +0200 Subject: [PATCH] Update to syn 0.6.0 --- macros/Cargo.toml | 2 +- macros/src/lib.rs | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 91e24c2..2fa2563 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -11,7 +11,7 @@ version = "0.1.33" [dependencies] quote = "0.1.3" -syn = "0.5.2" +syn = "0.6.0" [dev-dependencies] diff --git a/macros/src/lib.rs b/macros/src/lib.rs index d00f1e8..d85218c 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -24,12 +24,21 @@ use syn::Body::Enum; pub fn from_primitive(input: TokenStream) -> TokenStream { 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 variants: Vec<_> = variants.iter() .map(|variant| { let ident = &variant.ident; + if let Some(val) = variant.discriminant { + idx = val.value; + } let tt = quote!(#idx => Some(#name::#ident)); idx += 1; tt