From bd49d9bc2b76d638469b1974bc859ac628dc00bd Mon Sep 17 00:00:00 2001 From: yassun7010 Date: Fri, 21 Jun 2024 23:30:13 +0900 Subject: [PATCH] feat: support struct custsom. --- serde_valid/tests/custom_test.rs | 18 ++-- serde_valid_derive/src/attribute.rs | 1 + .../src/attribute/struct_validate.rs | 2 +- .../src/attribute/struct_validate/generic.rs | 5 +- .../struct_validate/generic/custom.rs | 24 +++++- .../struct_validate/meta/meta_list.rs | 6 +- .../struct_validate/meta/meta_name_value.rs | 14 +++- .../src/attribute/variant_validate.rs | 1 - .../src/attribute/variant_validate/generic.rs | 3 - .../variant_validate/generic/custom.rs | 82 ------------------- .../variant_validate/meta/meta_list.rs | 6 +- .../variant_validate/meta/meta_name_value.rs | 14 +++- 12 files changed, 64 insertions(+), 112 deletions(-) delete mode 100644 serde_valid_derive/src/attribute/variant_validate/generic.rs delete mode 100644 serde_valid_derive/src/attribute/variant_validate/generic/custom.rs diff --git a/serde_valid/tests/custom_test.rs b/serde_valid/tests/custom_test.rs index 690759e..23cece6 100644 --- a/serde_valid/tests/custom_test.rs +++ b/serde_valid/tests/custom_test.rs @@ -119,7 +119,7 @@ fn named_struct_custom_is_ok() { } #[derive(Validate)] - #[validate(custom(sample_struct_validation))] + #[validate(custom = sample_struct_validation)] struct TestStruct { val: i32, } @@ -136,7 +136,7 @@ fn named_struct_custom_closure_is_ok() { } #[derive(Validate)] - #[validate(custom(|s| sample_struct_validation(s.val)))] + #[validate(custom = |s| sample_struct_validation(s.val))] struct TestStruct { val: i32, } @@ -153,7 +153,7 @@ fn unnamed_struct_custom_is_ok() { } #[derive(Validate)] - #[validate(custom(sample_struct_validation))] + #[validate(custom = sample_struct_validation)] struct TestStruct(i32); let s = TestStruct(5); @@ -168,7 +168,7 @@ fn unnamed_struct_custom_closure_is_ok() { } #[derive(Validate)] - #[validate(custom(|s| sample_struct_validation(s.0)))] + #[validate(custom = |s| sample_struct_validation(s.0))] struct TestStruct(i32); let s = TestStruct(5); @@ -185,7 +185,7 @@ fn unnamed_struct_custom_closure_is_err() { } #[derive(Validate)] - #[validate(custom(|s| sample_struct_validation(s.0)))] + #[validate(custom = |s| sample_struct_validation(s.0))] struct TestStruct(i32); let s = TestStruct(5); @@ -200,7 +200,7 @@ fn named_struct_custom_vec_errors_is_ok() { } #[derive(Validate)] - #[validate(custom(validation))] + #[validate(custom = validation)] struct TestStruct { val: i32, } @@ -220,7 +220,7 @@ fn named_struct_custom_vec_errors_is_err() { } #[derive(Validate)] - #[validate(custom(validation))] + #[validate(custom = validation)] struct TestStruct { val: i32, } @@ -245,7 +245,7 @@ fn named_struct_custom_closure_vec_errors_is_ok() { } #[derive(Validate)] - #[validate(custom(|s| sample_struct_validation(s.val)))] + #[validate(custom = |s| sample_struct_validation(s.val))] struct TestStruct { val: i32, } @@ -265,7 +265,7 @@ fn named_struct_custom_closure_vec_errors_is_err() { } #[derive(Validate)] - #[validate(custom(|s| sample_struct_validation(s.val)))] + #[validate(custom = |s| sample_struct_validation(s.val))] struct TestStruct { val: i32, } diff --git a/serde_valid_derive/src/attribute.rs b/serde_valid_derive/src/attribute.rs index 64a54b1..4858522 100644 --- a/serde_valid_derive/src/attribute.rs +++ b/serde_valid_derive/src/attribute.rs @@ -88,6 +88,7 @@ enum_str! { enum_str! { pub enum MetaNameValueStructValidation { + Custom = "custom", } } diff --git a/serde_valid_derive/src/attribute/struct_validate.rs b/serde_valid_derive/src/attribute/struct_validate.rs index b471bd5..eda8468 100644 --- a/serde_valid_derive/src/attribute/struct_validate.rs +++ b/serde_valid_derive/src/attribute/struct_validate.rs @@ -1,4 +1,4 @@ -mod generic; +pub mod generic; mod meta; use crate::{attribute::Validator, warning::WithWarnings}; diff --git a/serde_valid_derive/src/attribute/struct_validate/generic.rs b/serde_valid_derive/src/attribute/struct_validate/generic.rs index 20dc85a..b437e6a 100644 --- a/serde_valid_derive/src/attribute/struct_validate/generic.rs +++ b/serde_valid_derive/src/attribute/struct_validate/generic.rs @@ -1,3 +1,6 @@ mod custom; -pub use custom::extract_generic_struct_custom_validator; +pub use custom::{ + extract_generic_struct_custom_validator_from_meta_list, + extract_generic_struct_custom_validator_from_meta_name_value, +}; diff --git a/serde_valid_derive/src/attribute/struct_validate/generic/custom.rs b/serde_valid_derive/src/attribute/struct_validate/generic/custom.rs index 1498861..d119df3 100644 --- a/serde_valid_derive/src/attribute/struct_validate/generic/custom.rs +++ b/serde_valid_derive/src/attribute/struct_validate/generic/custom.rs @@ -3,7 +3,7 @@ use crate::attribute::Validator; use crate::types::CommaSeparatedNestedMetas; use quote::quote; -pub fn extract_generic_struct_custom_validator( +pub fn extract_generic_struct_custom_validator_from_meta_list( meta_list: &syn::MetaList, _message_format: MessageFormat, ) -> Result { @@ -49,6 +49,20 @@ pub fn extract_generic_struct_custom_validator( } } +pub fn extract_generic_struct_custom_validator_from_meta_name_value( + meta_name_value: &syn::MetaNameValue, + _message_format: MessageFormat, +) -> Result { + match &meta_name_value.value { + syn::Expr::Path(syn::ExprPath { path, .. }) => extract_struct_custom_from_meta_path(path), + syn::Expr::Call(call) => extract_struct_custom_from_call(call), + syn::Expr::Closure(closure) => extract_struct_custom_from_closure(closure), + _ => Err(vec![ + crate::Error::validate_custom_meta_name_value_need_function_or_closure(meta_name_value), + ]), + } +} + fn extract_struct_custom_from_meta_path(meta_path: &syn::Path) -> Result { let rule_fn_name = &meta_path; @@ -69,6 +83,14 @@ fn extract_struct_custom_from_meta_list( )) } +fn extract_struct_custom_from_call(call: &syn::ExprCall) -> Result { + Ok(quote!( + if let Err(__errors) = serde_valid::validation::custom::wrap_call_validation(self, #call) { + __rule_vec_errors.extend(__errors); + }; + )) +} + fn extract_struct_custom_from_closure( closure: &syn::ExprClosure, ) -> Result { diff --git a/serde_valid_derive/src/attribute/struct_validate/meta/meta_list.rs b/serde_valid_derive/src/attribute/struct_validate/meta/meta_list.rs index fe65d16..71c36d4 100644 --- a/serde_valid_derive/src/attribute/struct_validate/meta/meta_list.rs +++ b/serde_valid_derive/src/attribute/struct_validate/meta/meta_list.rs @@ -1,7 +1,7 @@ use crate::attribute::{ common::message_format::MessageFormat, - struct_validate::generic::extract_generic_struct_custom_validator, MetaListStructValidation, - Validator, + struct_validate::generic::extract_generic_struct_custom_validator_from_meta_list, + MetaListStructValidation, Validator, }; pub fn extract_struct_validator_from_meta_list( @@ -11,7 +11,7 @@ pub fn extract_struct_validator_from_meta_list( ) -> Result { match validation_type { MetaListStructValidation::Custom => { - extract_generic_struct_custom_validator(validation, message_format) + extract_generic_struct_custom_validator_from_meta_list(validation, message_format) } } } diff --git a/serde_valid_derive/src/attribute/struct_validate/meta/meta_name_value.rs b/serde_valid_derive/src/attribute/struct_validate/meta/meta_name_value.rs index 2dec887..d70cd3c 100644 --- a/serde_valid_derive/src/attribute/struct_validate/meta/meta_name_value.rs +++ b/serde_valid_derive/src/attribute/struct_validate/meta/meta_name_value.rs @@ -1,12 +1,18 @@ use crate::attribute::{ - common::message_format::MessageFormat, MetaNameValueStructValidation, Validator, + common::message_format::MessageFormat, + struct_validate::generic::extract_generic_struct_custom_validator_from_meta_name_value, + MetaNameValueStructValidation, Validator, }; #[inline] pub fn extract_struct_validator_from_meta_name_value( validation_type: MetaNameValueStructValidation, - _validation: &syn::MetaNameValue, - _message_format: MessageFormat, + validation: &syn::MetaNameValue, + message_format: MessageFormat, ) -> Result { - match validation_type {} + match validation_type { + MetaNameValueStructValidation::Custom => { + extract_generic_struct_custom_validator_from_meta_name_value(validation, message_format) + } + } } diff --git a/serde_valid_derive/src/attribute/variant_validate.rs b/serde_valid_derive/src/attribute/variant_validate.rs index 1e46ec2..f623c7d 100644 --- a/serde_valid_derive/src/attribute/variant_validate.rs +++ b/serde_valid_derive/src/attribute/variant_validate.rs @@ -1,4 +1,3 @@ -mod generic; mod meta; use crate::attribute::Validator; diff --git a/serde_valid_derive/src/attribute/variant_validate/generic.rs b/serde_valid_derive/src/attribute/variant_validate/generic.rs deleted file mode 100644 index 49e6c40..0000000 --- a/serde_valid_derive/src/attribute/variant_validate/generic.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod custom; - -pub use custom::extract_generic_variant_custom_validator; diff --git a/serde_valid_derive/src/attribute/variant_validate/generic/custom.rs b/serde_valid_derive/src/attribute/variant_validate/generic/custom.rs deleted file mode 100644 index 360fc87..0000000 --- a/serde_valid_derive/src/attribute/variant_validate/generic/custom.rs +++ /dev/null @@ -1,82 +0,0 @@ -use crate::attribute::common::message_format::MessageFormat; -use crate::attribute::Validator; -use crate::types::CommaSeparatedNestedMetas; -use quote::quote; - -pub fn extract_generic_variant_custom_validator( - meta_list: &syn::MetaList, - _message_format: MessageFormat, -) -> Result { - let mut errors = vec![]; - - let nested = meta_list - .parse_args_with(CommaSeparatedNestedMetas::parse_terminated) - .map_err(|error| vec![crate::Error::rule_args_parse_error(meta_list, &error)])?; - - match nested.len() { - 0 => Err(vec![ - crate::Error::validate_custom_meta_list_need_function_or_closure(meta_list), - ])?, - 2.. => nested - .iter() - .skip(1) - .for_each(|error| errors.push(crate::Error::rule_allow_single_function(error))), - _ => {} - } - - let rule = match &nested[0] { - crate::types::NestedMeta::Meta(syn::Meta::Path(path)) => { - extract_variant_custom_from_meta_path(path) - } - crate::types::NestedMeta::Meta(syn::Meta::List(list)) => { - extract_variant_custom_from_meta_list(list) - } - crate::types::NestedMeta::Closure(closure) => extract_variant_custom_from_closure(closure), - _ => Err(vec![ - crate::Error::validate_custom_meta_list_need_function_or_closure(&nested[0]), - ]), - }; - - match rule { - Ok(_) => { - if errors.is_empty() { - rule - } else { - Err(errors) - } - } - Err(rule_errors) => Err(errors.into_iter().chain(rule_errors).collect()), - } -} - -fn extract_variant_custom_from_meta_path( - meta_path: &syn::Path, -) -> Result { - let rule_fn_name = &meta_path; - - Ok(quote!( - if let Err(__errors) = serde_valid::validation::custom::wrap_into_vec_errors(#rule_fn_name(self)) { - __rule_vec_errors.extend(__errors); - }; - )) -} - -fn extract_variant_custom_from_meta_list( - meta_list: &syn::MetaList, -) -> Result { - Ok(quote!( - if let Err(__errors) = serde_valid::validation::custom::wrap_closure_validation(self, #meta_list) { - __rule_vec_errors.extend(__errors); - }; - )) -} - -fn extract_variant_custom_from_closure( - closure: &syn::ExprClosure, -) -> Result { - Ok(quote!( - if let Err(__errors) = serde_valid::validation::custom::wrap_closure_validation(self, #closure) { - __rule_vec_errors.extend(__errors); - }; - )) -} diff --git a/serde_valid_derive/src/attribute/variant_validate/meta/meta_list.rs b/serde_valid_derive/src/attribute/variant_validate/meta/meta_list.rs index 6608771..4c80dff 100644 --- a/serde_valid_derive/src/attribute/variant_validate/meta/meta_list.rs +++ b/serde_valid_derive/src/attribute/variant_validate/meta/meta_list.rs @@ -1,7 +1,7 @@ use crate::attribute::{ common::message_format::MessageFormat, - variant_validate::generic::extract_generic_variant_custom_validator, MetaListStructValidation, - Validator, + struct_validate::generic::extract_generic_struct_custom_validator_from_meta_list, + MetaListStructValidation, Validator, }; pub fn extract_variant_validator_from_meta_list( @@ -11,7 +11,7 @@ pub fn extract_variant_validator_from_meta_list( ) -> Result { match validation_type { MetaListStructValidation::Custom => { - extract_generic_variant_custom_validator(validation, message_format) + extract_generic_struct_custom_validator_from_meta_list(validation, message_format) } } } diff --git a/serde_valid_derive/src/attribute/variant_validate/meta/meta_name_value.rs b/serde_valid_derive/src/attribute/variant_validate/meta/meta_name_value.rs index 3b52bd3..09a44d4 100644 --- a/serde_valid_derive/src/attribute/variant_validate/meta/meta_name_value.rs +++ b/serde_valid_derive/src/attribute/variant_validate/meta/meta_name_value.rs @@ -1,12 +1,18 @@ use crate::attribute::{ - common::message_format::MessageFormat, MetaNameValueStructValidation, Validator, + common::message_format::MessageFormat, + struct_validate::generic::extract_generic_struct_custom_validator_from_meta_name_value, + MetaNameValueStructValidation, Validator, }; #[inline] pub fn extract_variant_validator_from_meta_name_value( validation_type: MetaNameValueStructValidation, - _validation: &syn::MetaNameValue, - _message_format: MessageFormat, + validation: &syn::MetaNameValue, + message_format: MessageFormat, ) -> Result { - match validation_type {} + match validation_type { + MetaNameValueStructValidation::Custom => { + extract_generic_struct_custom_validator_from_meta_name_value(validation, message_format) + } + } }