/
enum_variant_names.rs
36 lines (30 loc) · 1020 Bytes
/
enum_variant_names.rs
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
use proc_macro2::TokenStream;
use syn;
use case_style::CaseStyle;
use helpers::{convert_case, extract_meta, unique_attr};
pub fn enum_variant_names_inner(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;
let variants = match ast.data {
syn::Data::Enum(ref v) => &v.variants,
_ => panic!("EnumVariantNames only works on Enums"),
};
// Derives for the generated enum
let type_meta = extract_meta(&ast.attrs);
let case_style = unique_attr(&type_meta, "strum", "serialize_all")
.map(|style| CaseStyle::from(style.as_ref()));
let names = variants
.iter()
.map(|v| convert_case(&v.ident, case_style))
.collect::<Vec<_>>();
quote! {
impl #name {
/// Return a slice containing the names of the variants of this enum
#[allow(dead_code)]
pub fn variants() -> &'static [&'static str] {
&[
#(#names),*
]
}
}
}
}