Parcourir la source

Update to syn 0.6.0

Łukasz Jan Niemier il y a 8 ans
Parent
commit
ada17a1793
2 fichiers modifiés avec 11 ajouts et 2 suppressions
  1. 1 1
      macros/Cargo.toml
  2. 10 1
      macros/src/lib.rs

+ 1 - 1
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]
 

+ 10 - 1
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