use quote::Tokens;
use common::{build_hcons_constr, to_ast};
use syn::{Ident, Body, VariantData, Field};
use proc_macro::TokenStream;
const ALPHA_CHARS: &'static [char] = &['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
const UNDERSCORE_CHARS: &'static [char] = &['_', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
pub fn impl_labelled_generic(input: TokenStream) -> Tokens {
let ast = to_ast(&input);
let name = &ast.ident;
let generics = &ast.generics;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let fields: &Vec<Field> = match ast.body {
Body::Struct(VariantData::Struct(ref fields)) => fields,
_ => panic!("Only Structs are supported. Tuple structs cannot be turned into Labelled Generics.")
};
let repr_type = build_labelled_repr(fields);
let fnames: Vec<Ident> = fields
.iter()
.map(|f| f.ident.clone().unwrap())
.collect();
let hcons_constr = build_labelled_hcons_constr(&fields);
let hcons_pat = build_hcons_constr(&fnames);
let new_struct_constr = build_new_labelled_struct_constr(name, &fnames);
let struct_deconstr = quote! { #name { #(#fnames, )* } };
quote! {
#[allow(non_snake_case, non_camel_case_types)]
impl #impl_generics ::frunk_core::labelled::LabelledGeneric for #name #ty_generics #where_clause {
type Repr = #repr_type;
fn into(self) -> Self::Repr {
let #struct_deconstr = self;
#hcons_constr
}
fn from(r: Self::Repr) -> Self {
let #hcons_pat = r;
#new_struct_constr
}
}
}
}
fn build_labelled_repr(fields: &Vec<Field>) -> Tokens {
match fields.len() {
0 => quote! { ::frunk_core::hlist::HNil },
1 => {
let field = fields[0].clone();
let labelled_type = build_labelled_type_for(&field);
quote! { ::frunk_core::hlist::HCons<#labelled_type, ::frunk_core::hlist::HNil> }
},
_ => {
let field = fields[0].clone();
let labelled_type = build_labelled_type_for(&field);
let tail = fields[1..].to_vec();
let tail_type = build_labelled_repr(&tail);
quote! { ::frunk_core::hlist::HCons<#labelled_type, #tail_type> }
}
}
}
fn build_labelled_type_for(field: &Field) -> Tokens {
let ident = field.clone().ident.unwrap(); let name_as_type = build_type_level_name_for(&ident);
let ref field_type = field.ty;
quote! { ::frunk_core::labelled::Field<#name_as_type, #field_type> }
}
fn build_type_level_name_for(ident: &Ident) -> Tokens {
let name = ident.as_ref();
let name_as_idents: Vec<Ident> = name.chars().flat_map(|c| encode_as_ident(&c)).collect();
let name_as_tokens: Vec<Tokens> = name_as_idents.iter().map(|ident| {
quote! { ::frunk_core::labelled::#ident }
}).collect();
quote! { (#(#name_as_tokens),*) }
}
fn encode_as_ident(c: &char) -> Vec<Ident> {
if ALPHA_CHARS.contains(c) {
vec![Ident::new(c.to_string())]
} else if UNDERSCORE_CHARS.contains(c) {
vec![Ident::new(format!("_{}", c))]
} else {
let as_unicode = c.escape_unicode();
let delimited_hex = as_unicode.filter(|c| c.is_alphanumeric());
let mut hex_idents: Vec<Ident> = delimited_hex.flat_map(|c| encode_as_ident(&c)).collect();
let mut book_ended: Vec<Ident> = vec![Ident::new("_uc")];
book_ended.append(&mut hex_idents);
book_ended.push(Ident::new("uc_"));
book_ended
}
}
fn build_labelled_hcons_constr(fields: &Vec<Field>) -> Tokens {
match fields.len() {
0 => quote! { ::frunk_core::hlist::HNil },
1 => {
let field = fields[0].clone();
let labelled_constructor = build_field_constr_for(&field);
quote! { ::frunk_core::hlist::HCons{ head: #labelled_constructor, tail: ::frunk_core::hlist::HNil } }
},
_ => {
let field = fields[0].clone();
let labelled_constructor = build_field_constr_for(&field);
let tail = fields[1..].to_vec();
let hlist_tail = build_labelled_hcons_constr(&tail);
quote! { ::frunk_core::hlist::HCons{ head: #labelled_constructor, tail: #hlist_tail }}
}
}
}
fn build_field_constr_for(field: &Field) -> Tokens {
let name_as_type = build_type_level_name_for(&field.clone().ident.unwrap());
let field_type = field.ty.clone();
let field_name = field.ident.clone();
let field_name_str = field.ident.clone().unwrap().as_ref().to_string();
quote! { ::frunk_core::labelled::field_with_name::<#name_as_type, #field_type>(#field_name_str, #field_name) }
}
fn build_new_labelled_struct_constr(struct_name: &Ident, bindnames: &Vec<Ident>) -> Tokens {
let cloned_bind1 = bindnames.clone();
let cloned_bind2 = bindnames.clone();
quote! { #struct_name { #(#cloned_bind1: #cloned_bind2.value),* } }
}