Remove unnecessary code in codegen_next.

This commit is contained in:
Sergio Benitez 2018-07-27 21:03:33 -07:00
parent 706cd32053
commit c04655f290
3 changed files with 1 additions and 124 deletions

View File

@ -1,101 +0,0 @@
use syn::*;
use ext::*;
use proc_macro2::TokenStream;
use spanned::Spanned;
use FieldMember;
pub trait CodegenFieldsExt {
fn surround(&self, tokens: TokenStream) -> TokenStream;
fn ignore_tokens(&self) -> TokenStream;
fn id_match_tokens(&self) -> TokenStream;
}
pub fn field_to_ident(i: usize, field: &Field) -> Ident {
let name = match field.ident {
Some(ref id) => format!("_{}", id),
None => format!("_{}", i)
};
Ident::new(&name, field.span().into())
}
pub fn field_to_match((i, field): (usize, &Field)) -> TokenStream {
let ident = field_to_ident(i, field);
match field.ident {
Some(ref id) => quote!(#id: #ident),
None => quote!(#ident)
}
}
impl CodegenFieldsExt for Fields {
fn surround(&self, tokens: TokenStream) -> TokenStream {
match *self {
Fields::Named(..) => quote!({ #tokens }),
Fields::Unnamed(..) => quote!(( #tokens )),
Fields::Unit => quote!()
}
}
fn ignore_tokens(&self) -> TokenStream {
self.surround(quote!(..))
}
fn id_match_tokens(&self) -> TokenStream {
let idents = self.iter()
.enumerate()
.map(field_to_match);
self.surround(quote!(#(#idents),*))
}
}
pub trait TokenStreamExt {
fn tokens(&self) -> TokenStream;
}
impl<'f> TokenStreamExt for FieldMember<'f> {
fn tokens(&self) -> TokenStream {
let index = self.member.unnamed().map(|i| i.index).unwrap_or(0);
let ident = field_to_ident(index as usize, &self.field);
quote!(#ident)
}
}
// use rocket::http::{ContentType, MediaType, Status};
// impl TokenStreamExt for ContentType {
// fn tokens(&self) -> TokenStream {
// let mt_tokens = self.0.tokens();
// quote!(rocket::http::ContentType(#mt_tokens))
// }
// }
// impl TokenStreamExt for MediaType {
// fn tokens(&self) -> TokenStream {
// let (top, sub) = (self.top().as_str(), self.sub().as_str());
// let (keys, values) = (self.params().map(|(k, _)| k), self.params().map(|(_, v)| v));
// quote!(rocket::http::MediaType {
// source: rocket::http::Source::None,
// top: rocket::http::IndexedStr::Concrete(
// std::borrow::Cow::Borrowed(#top)
// ),
// sub: rocket::http::IndexedStr::Concrete(
// std::borrow::Cow::Borrowed(#sub)
// ),
// params: rocket::http::MediaParams::Static(&[
// #((
// rocket::http::IndexedStr::Concrete(std::borrow::Cow::Borrowed(#keys)),
// rocket::http::IndexedStr::Concrete(std::borrow::Cow::Borrowed(#values))
// )),*
// ])
// })
// }
// }
// impl TokenStreamExt for Status {
// fn tokens(&self) -> TokenStream {
// let (code, reason) = (self.code, self.reason);
// quote!(rocket::http::Status { code: #code, reason: #reason })
// }
// }

View File

@ -1,6 +1,4 @@
use syn::*;
use FieldMember;
use spanned::Spanned;
pub trait MemberExt {
fn named(&self) -> Option<&Ident>;
@ -33,7 +31,6 @@ pub(crate) trait FieldsExt {
fn is_unit(&self) -> bool;
fn nth(&self, i: usize) -> Option<&Field>;
fn find_member(&self, member: &Member) -> Option<&Field>;
fn to_field_members<'f>(&'f self) -> Box<Iterator<Item = FieldMember<'f>> + 'f>;
}
impl FieldsExt for Fields {
@ -78,18 +75,6 @@ impl FieldsExt for Fields {
}
}
fn to_field_members<'f>(&'f self) -> Box<Iterator<Item = FieldMember<'f>> + 'f> {
Box::new(self.iter().enumerate().map(|(index, field)| {
if let Some(ref ident) = field.ident {
FieldMember { field, member: Member::Named(ident.clone()) }
} else {
let index = Index { index: index as u32, span: field.span().into() };
let member = Member::Unnamed(index);
FieldMember { field, member }
}
}))
}
fn nth(&self, i: usize) -> Option<&Field> {
match *self {
Fields::Named(ref fields) => fields.named.iter().nth(i),

View File

@ -10,7 +10,6 @@ extern crate proc_macro2;
mod parser;
mod spanned;
mod ext;
mod codegen_ext;
use parser::Result as PResult;
use proc_macro::{Span, TokenStream};
@ -24,12 +23,6 @@ const NO_GENERICS: &str = "enums with generics cannot derive `FromFormValue`";
const ONLY_ENUMS: &str = "`FromFormValue` can only be derived for enums";
const EMPTY_ENUM_WARN: &str = "deriving `FromFormValue` for empty enum";
#[derive(Debug, Clone)]
pub(crate) struct FieldMember<'f> {
field: &'f Field,
member: Member
}
fn validate_input(input: DeriveInput) -> PResult<DataEnum> {
// This derive doesn't support generics. Error out if there are generics.
if !input.generics.params.is_empty() {
@ -67,7 +60,7 @@ fn real_derive_from_form_value(input: TokenStream) -> PResult<TokenStream> {
// Create iterators over the identifers as idents and as strings.
let variant_strs = enum_data.variants.iter().map(|v| v.ident.to_string());
let variant_idents = enum_data.variants.iter().map(|v| v.ident.clone());
let variant_idents = enum_data.variants.iter().map(|v| &v.ident);
let names = ::std::iter::repeat(&name);
// Generate the implementation.