diff options
| author | Vincent Prouillet | 2019-10-20 18:18:41 +0200 |
|---|---|---|
| committer | GitHub | 2019-10-20 18:18:41 +0200 |
| commit | 97a11c88f26b00e897d21709d9576ad3cea6f220 (patch) | |
| tree | 7469738d72627423c5b99df7dfae099951f4d322 /validator_derive/src | |
| parent | 87cf7cdac24dd0d4ea83a8a88640da95ae7ac93a (diff) | |
| parent | 52cd62998b8f45be56512ce1d89aef8bf1a9f670 (diff) | |
| download | validator-97a11c88f26b00e897d21709d9576ad3cea6f220.tar.bz2 | |
Merge pull request #87 from Keats/next
Use Github actions for CI
Diffstat (limited to 'validator_derive/src')
| -rw-r--r-- | validator_derive/src/asserts.rs | 2 | ||||
| -rw-r--r-- | validator_derive/src/lib.rs | 110 | ||||
| -rw-r--r-- | validator_derive/src/lit.rs | 7 | ||||
| -rw-r--r-- | validator_derive/src/quoting.rs | 31 | ||||
| -rw-r--r-- | validator_derive/src/validation.rs | 99 |
5 files changed, 147 insertions, 102 deletions
diff --git a/validator_derive/src/asserts.rs b/validator_derive/src/asserts.rs index a5892a8..023dd0d 100644 --- a/validator_derive/src/asserts.rs +++ b/validator_derive/src/asserts.rs @@ -1,5 +1,7 @@ use regex::Regex; +use lazy_static::lazy_static; + lazy_static! { pub static ref COW_TYPE: Regex = Regex::new(r"Cow<'[a-z]+,str>").unwrap(); } diff --git a/validator_derive/src/lib.rs b/validator_derive/src/lib.rs index f3796b4..6f74202 100644 --- a/validator_derive/src/lib.rs +++ b/validator_derive/src/lib.rs @@ -1,21 +1,11 @@ #![recursion_limit = "128"] - -#[macro_use] -extern crate if_chain; -#[macro_use] -extern crate lazy_static; extern crate proc_macro; -extern crate proc_macro2; -#[macro_use] -extern crate quote; -extern crate regex; -#[macro_use] -extern crate syn; -extern crate validator; - -use proc_macro::TokenStream; + +use if_chain::if_chain; +use quote::quote; use quote::ToTokens; use std::collections::HashMap; +use syn::parse_quote; use validator::Validator; mod asserts; @@ -29,7 +19,7 @@ use quoting::{quote_field_validation, quote_schema_validation, FieldQuoter}; use validation::*; #[proc_macro_derive(Validate, attributes(validate))] -pub fn derive_validation(input: TokenStream) -> TokenStream { +pub fn derive_validation(input: proc_macro::TokenStream) -> proc_macro::TokenStream { let ast = syn::parse(input).unwrap(); impl_validate(&ast).into() } @@ -110,11 +100,12 @@ fn find_struct_validation(struct_attrs: &Vec<syn::Attribute>) -> Option<SchemaVa } if_chain! { - if let Some(syn::Meta::List(syn::MetaList { ref nested, .. })) = attr.interpret_meta(); + if let Ok(syn::Meta::List(syn::MetaList { ref nested, .. })) = attr.parse_meta(); if let syn::NestedMeta::Meta(ref item) = nested[0]; - if let &syn::Meta::List(syn::MetaList { ref ident, ref nested, .. }) = item; + if let &syn::Meta::List(syn::MetaList { ref path, ref nested, .. }) = item; then { + let ident = path.get_ident().unwrap(); if ident != "schema" { error("Only `schema` is allowed as validator on a struct") } @@ -127,9 +118,10 @@ fn find_struct_validation(struct_attrs: &Vec<syn::Attribute>) -> Option<SchemaVa for arg in nested { if_chain! { if let syn::NestedMeta::Meta(ref item) = *arg; - if let syn::Meta::NameValue(syn::MetaNameValue { ref ident, ref lit, .. }) = *item; + if let syn::Meta::NameValue(syn::MetaNameValue { ref path, ref lit, .. }) = *item; then { + let ident = path.get_ident().unwrap(); match ident.to_string().as_ref() { "function" => { function = match lit_to_string(lit) { @@ -251,8 +243,8 @@ fn find_validators_for_field( has_validate = true; } - match attr.interpret_meta() { - Some(syn::Meta::List(syn::MetaList { ref nested, .. })) => { + match attr.parse_meta() { + Ok(syn::Meta::List(syn::MetaList { ref nested, .. })) => { let meta_items = nested.iter().collect(); // original name before serde rename if attr.path == parse_quote!(serde) { @@ -266,32 +258,43 @@ fn find_validators_for_field( for meta_item in meta_items { match *meta_item { syn::NestedMeta::Meta(ref item) => match *item { - // email, url, phone - syn::Meta::Word(ref name) => match name.to_string().as_ref() { - "email" => { - assert_string_type("email", field_type); - validators.push(FieldValidation::new(Validator::Email)); - } - "url" => { - assert_string_type("url", field_type); - validators.push(FieldValidation::new(Validator::Url)); - } - #[cfg(feature = "phone")] - "phone" => { - assert_string_type("phone", field_type); - validators.push(FieldValidation::new(Validator::Phone)); - } - #[cfg(feature = "card")] - "credit_card" => { - assert_string_type("credit_card", field_type); - validators.push(FieldValidation::new(Validator::CreditCard)); + // email, url, phone, credit_card, non_control_character + syn::Meta::Path(ref name) => { + match name.get_ident().unwrap().to_string().as_ref() { + "email" => { + assert_string_type("email", field_type); + validators.push(FieldValidation::new(Validator::Email)); + } + "url" => { + assert_string_type("url", field_type); + validators.push(FieldValidation::new(Validator::Url)); + } + #[cfg(feature = "phone")] + "phone" => { + assert_string_type("phone", field_type); + validators.push(FieldValidation::new(Validator::Phone)); + } + #[cfg(feature = "card")] + "credit_card" => { + assert_string_type("credit_card", field_type); + validators + .push(FieldValidation::new(Validator::CreditCard)); + } + #[cfg(feature = "unic")] + "non_control_character" => { + assert_string_type("non_control_character", field_type); + validators.push(FieldValidation::new( + Validator::NonControlCharacter, + )); + } + _ => panic!("Unexpected validator: {:?}", name.get_ident()), } - _ => panic!("Unexpected validator: {}", name), - }, + } // custom, contains, must_match, regex syn::Meta::NameValue(syn::MetaNameValue { - ref ident, ref lit, .. + ref path, ref lit, .. }) => { + let ident = path.get_ident().unwrap(); match ident.to_string().as_ref() { "custom" => { match lit_to_string(lit) { @@ -324,8 +327,9 @@ fn find_validators_for_field( }; } // Validators with several args - syn::Meta::List(syn::MetaList { ref ident, ref nested, .. }) => { + syn::Meta::List(syn::MetaList { ref path, ref nested, .. }) => { let meta_items = nested.iter().cloned().collect(); + let ident = path.get_ident().unwrap(); match ident.to_string().as_ref() { "length" => { assert_has_len(rust_ident.clone(), field_type); @@ -341,7 +345,11 @@ fn find_validators_for_field( &meta_items, )); } - "email" | "url" | "phone" | "credit_card" => { + "email" + | "url" + | "phone" + | "credit_card" + | "non_control_character" => { validators.push(extract_argless_validation( ident.to_string(), rust_ident.clone(), @@ -396,10 +404,11 @@ fn find_validators_for_field( }; } } - Some(syn::Meta::Word(_)) => validators.push(FieldValidation::new(Validator::Nested)), - _ => unreachable!( - "Got something other than a list of attributes while checking field `{}`", - field_ident + Ok(syn::Meta::Path(_)) => validators.push(FieldValidation::new(Validator::Nested)), + Ok(syn::Meta::NameValue(_)) => panic!("Unexpected name=value argument"), + Err(e) => unreachable!( + "Got something other than a list of attributes while checking field `{}`: {:?}", + field_ident, e ), } } @@ -422,8 +431,9 @@ fn find_original_field_name(meta_items: &Vec<&syn::NestedMeta>) -> Option<String for meta_item in meta_items { match **meta_item { syn::NestedMeta::Meta(ref item) => match *item { - syn::Meta::Word(_) => continue, - syn::Meta::NameValue(syn::MetaNameValue { ref ident, ref lit, .. }) => { + syn::Meta::Path(_) => continue, + syn::Meta::NameValue(syn::MetaNameValue { ref path, ref lit, .. }) => { + let ident = path.get_ident().unwrap(); if ident == "rename" { original_name = Some(lit_to_string(lit).unwrap()); } diff --git a/validator_derive/src/lit.rs b/validator_derive/src/lit.rs index 41deffc..f11cdd6 100644 --- a/validator_derive/src/lit.rs +++ b/validator_derive/src/lit.rs @@ -1,4 +1,5 @@ use proc_macro2; +use quote::quote; use syn; pub fn lit_to_string(lit: &syn::Lit) -> Option<String> { @@ -10,15 +11,15 @@ pub fn lit_to_string(lit: &syn::Lit) -> Option<String> { pub fn lit_to_int(lit: &syn::Lit) -> Option<u64> { match *lit { - syn::Lit::Int(ref s) => Some(s.value()), + syn::Lit::Int(ref s) => Some(s.base10_parse().unwrap()), _ => None, } } pub fn lit_to_float(lit: &syn::Lit) -> Option<f64> { match *lit { - syn::Lit::Float(ref s) => Some(s.value()), - syn::Lit::Int(ref s) => Some(s.value() as f64), + syn::Lit::Float(ref s) => Some(s.base10_parse::<f64>().unwrap()), + syn::Lit::Int(ref s) => Some(s.base10_parse::<f64>().unwrap()), _ => None, } } diff --git a/validator_derive/src/quoting.rs b/validator_derive/src/quoting.rs index b94962c..dc466ac 100644 --- a/validator_derive/src/quoting.rs +++ b/validator_derive/src/quoting.rs @@ -1,10 +1,11 @@ use proc_macro2::{self, Span}; +use quote::quote; use syn; use validator::Validator; -use asserts::{COW_TYPE, NUMBER_TYPES}; -use lit::{option_f64_to_tokens, option_u64_to_tokens}; -use validation::{FieldValidation, SchemaValidation}; +use crate::asserts::{COW_TYPE, NUMBER_TYPES}; +use crate::lit::{option_f64_to_tokens, option_u64_to_tokens}; +use crate::validation::{FieldValidation, SchemaValidation}; /// Pass around all the information needed for creating a validation #[derive(Debug)] @@ -258,6 +259,26 @@ pub fn quote_phone_validation( field_quoter.wrap_if_option(quoted) } +#[cfg(feature = "unic")] +pub fn quote_non_control_character_validation( + field_quoter: &FieldQuoter, + validation: &FieldValidation, +) -> proc_macro2::TokenStream { + let field_name = &field_quoter.name; + let validator_param = field_quoter.quote_validator_param(); + + let quoted_error = quote_error(&validation); + let quoted = quote!( + if !::validator::validate_non_control_character(#validator_param) { + #quoted_error + err.add_param(::std::borrow::Cow::from("value"), &#validator_param); + errors.add(#field_name, err); + } + ); + + field_quoter.wrap_if_option(quoted) +} + pub fn quote_url_validation( field_quoter: &FieldQuoter, validation: &FieldValidation, @@ -440,6 +461,10 @@ pub fn quote_field_validation( #[cfg(feature = "phone")] Validator::Phone => validations.push(quote_phone_validation(&field_quoter, validation)), Validator::Nested => nested_validations.push(quote_nested_validation(&field_quoter)), + #[cfg(feature = "unic")] + Validator::NonControlCharacter => { + validations.push(quote_non_control_character_validation(&field_quoter, validation)) + } } } diff --git a/validator_derive/src/validation.rs b/validator_derive/src/validation.rs index 5f42d7b..f0657ef 100644 --- a/validator_derive/src/validation.rs +++ b/validator_derive/src/validation.rs @@ -2,7 +2,7 @@ use syn; use validator::Validator; -use lit::*; +use crate::lit::*; #[derive(Debug)] pub struct SchemaValidation { @@ -41,32 +41,33 @@ pub fn extract_length_validation( for meta_item in meta_items { if let syn::NestedMeta::Meta(ref item) = *meta_item { - if let syn::Meta::NameValue(syn::MetaNameValue { ref ident, ref lit, .. }) = *item { + if let syn::Meta::NameValue(syn::MetaNameValue { ref path, ref lit, .. }) = *item { + let ident = path.get_ident().unwrap(); match ident.to_string().as_ref() { - "message" | "code" => continue, - "min" => { - min = match lit_to_int(lit) { - Some(s) => Some(s), - None => error("invalid argument type for `min` of `length` validator: only integers are allowed"), - }; - }, - "max" => { - max = match lit_to_int(lit) { - Some(s) => Some(s), - None => error("invalid argument type for `max` of `length` validator: only integers are allowed"), - }; - }, - "equal" => { - equal = match lit_to_int(lit) { - Some(s) => Some(s), - None => error("invalid argument type for `equal` of `length` validator: only integers are allowed"), - }; - }, - v => error(&format!( - "unknown argument `{}` for validator `length` (it only has `min`, `max`, `equal`)", - v - )) - } + "message" | "code" => continue, + "min" => { + min = match lit_to_int(lit) { + Some(s) => Some(s), + None => error("invalid argument type for `min` of `length` validator: only integers are allowed"), + }; + }, + "max" => { + max = match lit_to_int(lit) { + Some(s) => Some(s), + None => error("invalid argument type for `max` of `length` validator: only integers are allowed"), + }; + }, + "equal" => { + equal = match lit_to_int(lit) { + Some(s) => Some(s), + None => error("invalid argument type for `equal` of `length` validator: only integers are allowed"), + }; + }, + v => error(&format!( + "unknown argument `{}` for validator `length` (it only has `min`, `max`, `equal`)", + v + )) + } } else { panic!( "unexpected item {:?} while parsing `length` validator of field {}", @@ -107,7 +108,8 @@ pub fn extract_range_validation( for meta_item in meta_items { match *meta_item { syn::NestedMeta::Meta(ref item) => match *item { - syn::Meta::NameValue(syn::MetaNameValue { ref ident, ref lit, .. }) => { + syn::Meta::NameValue(syn::MetaNameValue { ref path, ref lit, .. }) => { + let ident = path.get_ident().unwrap(); match ident.to_string().as_ref() { "message" | "code" => continue, "min" => { @@ -123,9 +125,9 @@ pub fn extract_range_validation( }; } v => error(&format!( - "unknown argument `{}` for validator `range` (it only has `min`, `max`)", - v - )), + "unknown argument `{}` for validator `range` (it only has `min`, `max`)", + v + )), } } _ => panic!("unexpected item {:?} while parsing `range` validator", item), @@ -146,7 +148,7 @@ pub fn extract_range_validation( } } -/// Extract url/email/phone field validation with a code or a message +/// Extract url/email/phone/non_control_character field validation with a code or a message pub fn extract_argless_validation( validator_name: String, field: String, @@ -157,7 +159,8 @@ pub fn extract_argless_validation( for meta_item in meta_items { match *meta_item { syn::NestedMeta::Meta(ref item) => match *item { - syn::Meta::NameValue(syn::MetaNameValue { ref ident, .. }) => { + syn::Meta::NameValue(syn::MetaNameValue { ref path, .. }) => { + let ident = path.get_ident().unwrap(); match ident.to_string().as_ref() { "message" | "code" => continue, v => panic!( @@ -178,6 +181,8 @@ pub fn extract_argless_validation( "credit_card" => Validator::CreditCard, #[cfg(feature = "phone")] "phone" => Validator::Phone, + #[cfg(feature = "unic")] + "non_control_character" => Validator::NonControlCharacter, _ => Validator::Url, }; @@ -201,7 +206,8 @@ pub fn extract_one_arg_validation( for meta_item in meta_items { match *meta_item { syn::NestedMeta::Meta(ref item) => match *item { - syn::Meta::NameValue(syn::MetaNameValue { ref ident, ref lit, .. }) => { + syn::Meta::NameValue(syn::MetaNameValue { ref path, ref lit, .. }) => { + let ident = path.get_ident().unwrap(); match ident.to_string().as_ref() { "message" | "code" => continue, v if v == val_name => { @@ -257,29 +263,30 @@ fn extract_message_and_code( for meta_item in meta_items { if let syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { - ref ident, + ref path, ref lit, .. })) = *meta_item { + let ident = path.get_ident().unwrap(); match ident.to_string().as_ref() { "code" => { code = match lit_to_string(lit) { - Some(s) => Some(s), - None => panic!( - "Invalid argument type for `code` for validator `{}` on field `{}`: only a string is allowed", - validator_name, field - ), - }; + Some(s) => Some(s), + None => panic!( + "Invalid argument type for `code` for validator `{}` on field `{}`: only a string is allowed", + validator_name, field + ), + }; } "message" => { message = match lit_to_string(lit) { - Some(s) => Some(s), - None => panic!( - "Invalid argument type for `message` for validator `{}` on field `{}`: only a string is allowed", - validator_name, field - ), - }; + Some(s) => Some(s), + None => panic!( + "Invalid argument type for `message` for validator `{}` on field `{}`: only a string is allowed", + validator_name, field + ), + }; } _ => continue, } |
