1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
//! This crate contains macros for deriving useful traits on C-like enums

#![cfg_attr(feature="clippy", feature(plugin))]
#![cfg_attr(feature="clippy", plugin(clippy))]

extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;

use proc_macro::TokenStream;

fn filter_primitive_type_attr(attr: &syn::Attribute) -> Option<&str> {
    use syn::MetaItem::NameValue;

    match attr.value {
        NameValue(ref ident, syn::Lit::Str(ref value, _)) => {
            if ident == "TryFromPrimitiveType" {
                Some(value)
            } else {
                None
            }
        },
        _ => None,
    }
}

fn impl_single_type<'a, I: Iterator<Item=&'a syn::Variant>>
    (name: &syn::Ident, ty: &syn::Ident, variants: I) -> quote::Tokens {
    let blocks = variants.map(|var| {
        let ident = &var.ident;
        if var.data != syn::VariantData::Unit {
            panic!("Enum variant may not store data!")
        }
        quote!{
            if v == #name::#ident as #ty {
                Ok(#name::#ident)
            }
        }
    });
    let mut tokens = quote::Tokens::new();
    tokens.append_separated(blocks, "else");

    quote! {
        impl TryFrom<#ty> for #name {
            type Error = enum_tryfrom::InvalidEnumValue;

            fn try_from(v: #ty) -> Result<Self, Self::Error> {
                #tokens else {
                    Err(Self::Error::new())
                }
            }
        }
    }
}

fn impl_from_primitive(ast: &syn::DeriveInput) -> quote::Tokens {
    let name = &ast.ident;
    let types = ast.attrs.iter().filter_map(filter_primitive_type_attr)
        .map(syn::Ident::new);
    let variants =
        if let syn::Body::Enum(ref variants) = ast.body {
            variants
        } else {
            panic!("`TryFromPrimitive` is only supported on Enums")
        };
    let impls = types.map(|ty| impl_single_type(name, &ty, variants.iter()));

    let mut tokens = quote::Tokens::new();
    tokens.append_all(impls);
    tokens
}

/// Generate `TryFrom` for each primitive type mentioned as a
/// `TryFromPrimitiveType` attribute.
///
/// When combined with unwrap, this is as fast as a bounds check + transmute, at
/// least for small enums. See the `benches` folder for benchmarks.
///
/// # Examples
///
/// ```
/// #![feature(try_from)]
/// use std::convert::TryFrom;
///
/// extern crate enum_tryfrom;
///
/// #[macro_use] extern crate enum_tryfrom_derive;
/// #[derive(PartialEq,Debug,TryFromPrimitive)]
/// #[TryFromPrimitiveType="u32"]
/// enum Foo {
///     FirstFoo = 1,
///     SecondFoo,
///     ThirdFoo,
/// }
///
/// # fn main() {
/// let v : u32 = 2;
/// assert_eq!(Foo::try_from(v).unwrap(), Foo::SecondFoo);
/// # }
/// ```
#[proc_macro_derive(TryFromPrimitive, attributes(TryFromPrimitiveType))]
pub fn from_primitive(input: TokenStream) -> TokenStream {
    let s = input.to_string();
    let ast = syn::parse_derive_input(&s).unwrap();
    let gen = impl_from_primitive(&ast);

    gen.parse().unwrap()
}