-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade Dependencies #212
Upgrade Dependencies #212
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,31 +4,31 @@ | |
|
||
#![recursion_limit = "128"] | ||
|
||
#[macro_use] | ||
extern crate quote; | ||
extern crate proc_macro; | ||
|
||
use proc_macro::TokenStream; | ||
use syn::DeriveInput; | ||
|
||
#[proc_macro_derive(Validate)] | ||
pub fn derive_validate(input: TokenStream) -> TokenStream { | ||
let source = input.to_string(); | ||
let ast = syn::parse_macro_input(&source).unwrap(); | ||
let tokens = expand(&ast); | ||
tokens.parse().unwrap() | ||
expand(&syn::parse_macro_input!(input as DeriveInput)).into() | ||
} | ||
|
||
fn expand(ast: &syn::MacroInput) -> quote::Tokens { | ||
let fields = match ast.body { | ||
syn::Body::Struct(syn::VariantData::Struct(ref fields)) => fields, | ||
fn expand(ast: &DeriveInput) -> proc_macro2::TokenStream { | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
let fields = match ast.data { | ||
syn::Data::Struct(ref data_struct) => &data_struct.fields, | ||
_ => panic!("#[derive(Validate)] only works on `struct`s"), | ||
}; | ||
let ident = &ast.ident; | ||
let minimal_validations: Vec<quote::Tokens> = fields.iter() | ||
let minimal_validations: Vec<TokenStream> = fields | ||
.iter() | ||
.map(|f| f.ident.as_ref().unwrap()) | ||
.map(|ident| { | ||
use inflections::Inflect; | ||
let field = ident.as_ref().to_camel_case(); | ||
let field = ident.to_string().to_camel_case(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using .as_ref() to get a Using .to_string() is now the only supported way to get the name of the identifier. |
||
quote!( | ||
self.#ident.validate_minimally( | ||
_root, | ||
|
@@ -38,11 +38,12 @@ fn expand(ast: &syn::MacroInput) -> quote::Tokens { | |
) | ||
}) | ||
.collect(); | ||
let complete_validations: Vec<quote::Tokens> = fields.iter() | ||
let complete_validations: Vec<TokenStream> = fields | ||
.iter() | ||
.map(|f| f.ident.as_ref().unwrap()) | ||
.map(|ident| { | ||
use inflections::Inflect; | ||
let field = ident.as_ref().to_camel_case(); | ||
let field = ident.to_string().to_camel_case(); | ||
quote!( | ||
self.#ident.validate_completely( | ||
_root, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is better.