From 50307fb74caaf8e279b9ea3f50b6683a67260333 Mon Sep 17 00:00:00 2001 From: gtors Date: Sun, 17 Dec 2017 15:30:36 +0300 Subject: Move extraction of message and code in single function --- validator_derive/src/validation.rs | 112 ++++++++++++++----------------------- 1 file changed, 43 insertions(+), 69 deletions(-) diff --git a/validator_derive/src/validation.rs b/validator_derive/src/validation.rs index 7598d79..bbec9d6 100644 --- a/validator_derive/src/validation.rs +++ b/validator_derive/src/validation.rs @@ -36,8 +36,7 @@ pub fn extract_length_validation(field: String, meta_items: &Vec ! { panic!("Invalid attribute #[validate] on field `{}`: {}", field, msg); @@ -47,6 +46,7 @@ pub fn extract_length_validation(field: String, meta_items: &Vec continue, "min" => { min = match lit_to_int(val) { Some(s) => Some(s), @@ -65,18 +65,6 @@ pub fn extract_length_validation(field: String, meta_items: &Vec error("invalid argument type for `equal` of `length` validator: only integers are allowed"), }; }, - "code" => { - code = match lit_to_string(val) { - Some(s) => Some(s), - None => error("invalid argument type for `code` of `length` validator: only a string is allowed"), - }; - }, - "message" => { - message = match lit_to_string(val) { - Some(s) => Some(s), - None => error("invalid argument type for `message` of `length` validator: only a string is allowed"), - }; - }, _ => error(&format!( "unknown argument `{}` for validator `length` (it only has `min`, `max`, `equal`)", name.to_string() @@ -107,9 +95,8 @@ pub fn extract_range_validation(field: String, meta_items: &Vec ! { panic!("Invalid attribute #[validate] on field `{}`: {}", field, msg); }; @@ -123,6 +110,7 @@ pub fn extract_range_validation(field: String, meta_items: &Vec match *item { syn::MetaItem::NameValue(ref name, ref val) => { match name.to_string().as_ref() { + "message" | "code" => continue, "min" => { min = match lit_to_float(val) { Some(s) => s, @@ -137,18 +125,6 @@ pub fn extract_range_validation(field: String, meta_items: &Vec { - code = match lit_to_string(val) { - Some(s) => Some(s), - None => error("invalid argument type for `code` of `length` validator: only a string is allowed"), - }; - }, - "message" => { - message = match lit_to_string(val) { - Some(s) => Some(s), - None => error("invalid argument type for `message` of `length` validator: only a string is allowed"), - }; - }, _ => error(&format!( "unknown argument `{}` for validator `range` (it only has `min`, `max`)", name.to_string() @@ -175,32 +151,14 @@ pub fn extract_range_validation(field: String, meta_items: &Vec) -> FieldValidation { - let mut code = None; - let mut message = None; + let (message, code) = extract_message_and_code(&validator_name, &field, meta_items); for meta_item in meta_items { match *meta_item { syn::NestedMetaItem::MetaItem(ref item) => match *item { syn::MetaItem::NameValue(ref name, ref val) => { match name.to_string().as_ref() { - "code" => { - code = match lit_to_string(val) { - 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(val) { - Some(s) => Some(s), - None => panic!( - "Invalid argument type for `message` for validator `{}` on field `{}`: only a string is allowed", - validator_name, field - ), - }; - }, + "message" | "code" => continue, _ => panic!( "Unknown argument `{}` for validator `{}` on field `{}`", name.to_string(), validator_name, field @@ -230,15 +188,15 @@ pub fn extract_argless_validation(validator_name: String, field: String, meta_it /// For custom, contains, regex, must_match pub fn extract_one_arg_validation(val_name: &str, validator_name: String, field: String, meta_items: &Vec) -> FieldValidation { - let mut code = None; - let mut message = None; let mut value = None; + let (message, code) = extract_message_and_code(&validator_name, &field, meta_items); for meta_item in meta_items { match *meta_item { syn::NestedMetaItem::MetaItem(ref item) => match *item { syn::MetaItem::NameValue(ref name, ref val) => { match name.to_string().as_ref() { + "message" | "code" => continue, v if v == val_name => { value = match lit_to_string(val) { Some(s) => Some(s), @@ -248,24 +206,6 @@ pub fn extract_one_arg_validation(val_name: &str, validator_name: String, field: ), }; }, - "code" => { - code = match lit_to_string(val) { - 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(val) { - Some(s) => Some(s), - None => panic!( - "Invalid argument type for `message` for validator `{}` on field `{}`: only a string is allowed", - validator_name, field - ), - }; - }, _ => panic!( "Unknown argument `{}` for validator `{}` on field `{}`", name.to_string(), validator_name, field @@ -289,9 +229,43 @@ pub fn extract_one_arg_validation(val_name: &str, validator_name: String, field: "regex" => Validator::Regex(value.unwrap()), _ => unreachable!(), }; + FieldValidation { message, code: code.unwrap_or_else(|| validator.code().to_string()), validator, } } + +fn extract_message_and_code(validator_name: &str, field: &str, meta_items: &Vec) -> (Option, Option) { + let mut message = None; + let mut code = None; + + for meta_item in meta_items { + if let syn::NestedMetaItem::MetaItem(syn::MetaItem::NameValue(ref name, ref val)) = *meta_item { + match name.to_string().as_ref() { + "code" => { + code = match lit_to_string(val) { + 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(val) { + Some(s) => Some(s), + None => panic!( + "Invalid argument type for `message` for validator `{}` on field `{}`: only a string is allowed", + validator_name, field + ), + }; + }, + _ => continue + } + } + } + + (message, code) +} -- cgit v1.2.3