Explorar o código

Update to syn 0.6.0

Łukasz Jan Niemier %!s(int64=8) %!d(string=hai) anos
pai
achega
ada17a1793
Modificáronse 2 ficheiros con 11 adicións e 2 borrados
  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