diff --git a/crates/serde_valid/tests/deprecated_rule_test.rs b/crates/serde_valid/tests/deprecated_rule_test.rs deleted file mode 100644 index 9d490bf..0000000 --- a/crates/serde_valid/tests/deprecated_rule_test.rs +++ /dev/null @@ -1,311 +0,0 @@ -#[allow(deprecated)] -mod test { - use serde_json::json; - use serde_valid::Validate; - - fn sample_ok_rule(_val: &i32) -> Result<(), serde_valid::validation::Error> { - Ok(()) - } - - fn sample_ok_rule2(_val1: &i32, _val2: &str) -> Result<(), serde_valid::validation::Error> { - Ok(()) - } - - fn sample_ok_rule3( - _val1: &i32, - _val2: &str, - _val3: bool, - ) -> Result<(), serde_valid::validation::Error> { - Ok(()) - } - - fn sample_err_rule(_val1: &i32, _val2: &str) -> Result<(), serde_valid::validation::Error> { - Err(serde_valid::validation::Error::Custom( - "Rule error.".to_owned(), - )) - } - - #[test] - fn rule_struct_named_field_is_ok() { - #[derive(Validate)] - #[rule(sample_ok_rule(val))] - struct TestStruct { - val: i32, - } - - let s = TestStruct { val: 5 }; - assert!(s.validate().is_ok()); - } - - #[test] - fn rule_struct_named_fields_is_ok() { - #[derive(Validate)] - #[rule(sample_ok_rule2(val1, val2))] - struct TestStruct { - val1: i32, - val2: String, - } - - let s = TestStruct { - val1: 5, - val2: "val2".to_owned(), - }; - assert!(s.validate().is_ok()); - } - - #[test] - fn rule_struct_named_fields_is_err() { - #[derive(Validate)] - #[rule(sample_err_rule(val2, val1))] - struct TestStruct { - val1: String, - val2: i32, - } - - let s = TestStruct { - val1: "val1".to_owned(), - val2: 1, - }; - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": ["Rule error."], - "properties": {} - }) - .to_string() - ); - } - - #[test] - fn rule_struct_unnamed_fields_is_ok() { - #[derive(Validate)] - #[rule(sample_ok_rule(0))] - #[rule(sample_ok_rule2(0, 1))] - struct TestStruct(i32, String); - - let s = TestStruct(0, "1".to_owned()); - assert!(s.validate().is_ok()); - } - - #[test] - fn rule_new_type_is_ok() { - #[derive(Validate)] - #[rule(sample_ok_rule(0))] - struct TestNewType(i32); - - let s = TestNewType(5); - assert!(s.validate().is_ok()); - } - - #[test] - fn rule_enum_is_ok() { - #[derive(Validate)] - enum TestEnum { - #[rule(sample_ok_rule(val))] - NamedFields { - val: i32, - }, - #[rule(sample_ok_rule(0))] - #[rule(sample_ok_rule2(0, 1))] - UnnamedFields(i32, String), - #[rule(sample_ok_rule(0))] - NewType(i32), - NoField, - } - - let s1 = TestEnum::NamedFields { val: 5 }; - assert!(s1.validate().is_ok()); - let s2 = TestEnum::UnnamedFields(0, "1".to_owned()); - assert!(s2.validate().is_ok()); - let s3 = TestEnum::NewType(5); - assert!(s3.validate().is_ok()); - let s4 = TestEnum::NoField; - assert!(s4.validate().is_ok()); - } - - #[test] - fn rule_closure_struct_named_fields_is_ok() { - #[derive(Validate)] - #[rule(|val| sample_ok_rule2(val, "abcd"))] - struct TestStruct { - val: i32, - } - - let s = TestStruct { val: 5 }; - assert!(s.validate().is_ok()); - } - - #[test] - fn rule_clousure2_struct_named_fields_is_ok() { - #[derive(Validate)] - #[rule(|val1, val2| sample_ok_rule3(val1, val2, true))] - struct TestStruct { - val1: i32, - val2: String, - } - - let s = TestStruct { - val1: 5, - val2: "val2".to_owned(), - }; - assert!(s.validate().is_ok()); - } - - #[test] - fn rule_closure_struct_unnamed_fields_is_ok() { - #[derive(Validate)] - #[rule(|_0| sample_ok_rule2(_0, "abcd"))] - struct TestStruct(i32); - - let s = TestStruct(5); - assert!(s.validate().is_ok()); - } - - #[test] - fn rule_clousure2_struct_unnamed_fields_is_ok() { - #[derive(Validate)] - #[rule(|_0, _1| sample_ok_rule3(_0, _1, true))] - struct TestStruct(i32, String); - - let s = TestStruct(5, "val2".to_owned()); - assert!(s.validate().is_ok()); - } - - #[test] - fn enum_named_variant_validation_is_err() { - fn err_rule( - _a: &TestStruct, - _b: &TestStruct, - ) -> Result<(), serde_valid::validation::Error> { - Err(serde_valid::validation::Error::Custom( - "Rule error.".to_owned(), - )) - } - - #[derive(Validate)] - enum TestEnum { - #[rule(err_rule(a, b))] - Named { - #[validate] - a: TestStruct, - #[validate] - b: TestStruct, - }, - } - - #[derive(Validate)] - struct TestStruct { - #[validate(minimum = 0)] - #[validate(maximum = 10)] - val: i32, - } - - let s = TestEnum::Named { - a: TestStruct { val: 12 }, - b: TestStruct { val: 12 }, - }; - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": ["Rule error."], - "properties": { - "a": { - "errors": [], - "properties": { - "val": { - "errors": ["The number must be `<= 10`."] - } - } - }, - "b": { - "errors": [], - "properties": { - "val": { - "errors": ["The number must be `<= 10`."] - } - } - } - } - }) - .to_string() - ); - } - - #[test] - fn enum_unnamed_variant_validation_is_err() { - fn err_rule( - _a: &TestStruct, - _b: &TestStruct, - ) -> Result<(), serde_valid::validation::Error> { - Err(serde_valid::validation::Error::Custom( - "Rule error.".to_owned(), - )) - } - - #[derive(Validate)] - enum TestEnum { - #[rule(err_rule(0, 1))] - Named(#[validate] TestStruct, #[validate] TestStruct), - } - - #[derive(Validate)] - struct TestStruct { - #[validate(minimum = 0)] - #[validate(maximum = 10)] - val: i32, - } - - let s = TestEnum::Named(TestStruct { val: 12 }, TestStruct { val: 12 }); - - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": ["Rule error."], - "items": { - "0": { - "errors": [], - "properties": { - "val": { - "errors": ["The number must be `<= 10`."] - } - } - }, - "1": { - "errors": [], - "properties": { - "val": { - "errors": ["The number must be `<= 10`."] - } - } - } - } - }) - .to_string() - ); - } - - #[test] - fn enum_newtype_variant_validation_is_err() { - fn err_rule(_a: &u32) -> Result<(), serde_valid::validation::Error> { - Err(serde_valid::validation::Error::Custom( - "Rule error.".to_owned(), - )) - } - - #[derive(Validate)] - enum TestEnum { - #[rule(err_rule(0))] - NewType(#[validate(minimum = 5)] u32), - } - - let s = TestEnum::NewType(4); - - assert_eq!( - s.validate().unwrap_err().to_string(), - json!({ - "errors": ["Rule error.", "The number must be `>= 5`."] - }) - .to_string() - ); - } -} diff --git a/crates/serde_valid_derive/src/attribute.rs b/crates/serde_valid_derive/src/attribute.rs index c48fbe1..09fb51b 100644 --- a/crates/serde_valid_derive/src/attribute.rs +++ b/crates/serde_valid_derive/src/attribute.rs @@ -2,7 +2,6 @@ use proc_macro2::TokenStream; pub mod common; pub mod field_validate; -pub mod rule; pub mod struct_validate; pub mod variant_validate; @@ -82,7 +81,6 @@ enum_str! { enum_str! { pub enum MetaListStructValidation { - Custom = "custom", } } diff --git a/crates/serde_valid_derive/src/attribute/rule.rs b/crates/serde_valid_derive/src/attribute/rule.rs deleted file mode 100644 index 3783fb7..0000000 --- a/crates/serde_valid_derive/src/attribute/rule.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod named_struct_rule; -mod unnamed_struct_rule; - -pub use named_struct_rule::collect_rules_from_named_struct; -pub use unnamed_struct_rule::collect_rules_from_unnamed_struct; diff --git a/crates/serde_valid_derive/src/attribute/rule/named_struct_rule.rs b/crates/serde_valid_derive/src/attribute/rule/named_struct_rule.rs deleted file mode 100644 index 9eb1ce6..0000000 --- a/crates/serde_valid_derive/src/attribute/rule/named_struct_rule.rs +++ /dev/null @@ -1,187 +0,0 @@ -use std::collections::HashSet; - -use proc_macro2::TokenStream; -use quote::{quote, ToTokens}; -use syn::spanned::Spanned; - -use crate::{ - types::{CommaSeparatedNestedMetas, CommaSeparatedTokenStreams}, - warning::Warning, - warning::WithWarnings, -}; - -pub fn collect_rules_from_named_struct( - ident: &syn::Ident, - attributes: &[syn::Attribute], -) -> Result<(HashSet, WithWarnings), crate::Errors> { - let mut errors = vec![]; - - let mut rule_fields = HashSet::new(); - let mut warnings = vec![]; - let rules = attributes - .iter() - .filter(|attribute| attribute.path().is_ident("rule")) - .inspect(|attribute| { - warnings.push(Warning::new_rule_deprecated( - ident, - attribute.bracket_token.span.span(), - )); - }) - .filter_map(|attribute| match &attribute.meta { - syn::Meta::List(list) => match collect_rule(list) { - Ok((field_ident, stream)) => { - rule_fields.extend(field_ident); - Some(stream) - } - Err(rule_errors) => { - errors.extend(rule_errors); - None - } - }, - _ => { - errors.push(crate::Error::rule_allow_function_call_or_closure( - attribute.meta.path(), - )); - None - } - }) - .collect::>(); - - if errors.is_empty() { - Ok(( - rule_fields, - WithWarnings { - data: TokenStream::from_iter(rules), - warnings, - }, - )) - } else { - Err(errors) - } -} - -fn collect_rule( - meta_list: &syn::MetaList, -) -> Result<(HashSet, TokenStream), crate::Errors> { - 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::rule_allow_function_call_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::List(list)) => extract_rule_from_meta_list(list), - crate::types::NestedMeta::Closure(closure) => extract_rule_from_closure(closure), - _ => Err(vec![crate::Error::rule_allow_function_call_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_rule_from_meta_list( - meta_list: &syn::MetaList, -) -> Result<(HashSet, TokenStream), crate::Errors> { - let mut errors = vec![]; - - let rule_fn_name = &meta_list.path; - let nested = meta_list - .parse_args_with(crate::types::CommaSeparatedNestedMetas::parse_terminated) - .map_err(|error| vec![crate::Error::rule_args_parse_error(meta_list, &error)])?; - - if nested.is_empty() { - errors.push(crate::Error::rule_need_arguments(rule_fn_name)); - } - - let mut arg_idents = HashSet::new(); - let rule_fn_args = nested - .iter() - .filter_map(|nested| match nested { - crate::types::NestedMeta::Meta(syn::Meta::Path(path)) => { - let ident = path.to_token_stream().to_string(); - if ident == "r#type" { - arg_idents.insert(syn::Ident::new_raw("type", path.span())); - Some(quote!(r#type)) - } else { - arg_idents.insert(syn::Ident::new(&ident, path.span())); - Some(quote!(#path)) - } - } - _ => { - errors.push(crate::Error::rule_args_allow_field_name( - rule_fn_name, - nested, - )); - None - } - }) - .collect::(); - - if !errors.is_empty() { - return Err(errors); - } - - Ok(( - arg_idents, - quote!( - if let Err(__error) = #rule_fn_name(#rule_fn_args) { - __rule_vec_errors.push(__error); - }; - ), - )) -} - -fn extract_rule_from_closure( - closure: &syn::ExprClosure, -) -> Result<(HashSet, TokenStream), crate::Errors> { - let mut errors = vec![]; - - let mut arg_idents = HashSet::new(); - let rule_fn_args = closure - .inputs - .iter() - .filter_map(|input| { - if let syn::Pat::Ident(ident) = input { - arg_idents.insert(ident.ident.clone()); - Some(quote!(#ident)) - } else { - errors.push(crate::Error::rule_named_clousure_input(input)); - None - } - }) - .collect::(); - - if !errors.is_empty() { - return Err(errors); - } - - Ok(( - arg_idents, - quote!( - if let Err(__error) = (#closure)(#rule_fn_args) { - __rule_vec_errors.push(__error); - }; - ), - )) -} diff --git a/crates/serde_valid_derive/src/attribute/rule/unnamed_struct_rule.rs b/crates/serde_valid_derive/src/attribute/rule/unnamed_struct_rule.rs deleted file mode 100644 index d514bf7..0000000 --- a/crates/serde_valid_derive/src/attribute/rule/unnamed_struct_rule.rs +++ /dev/null @@ -1,187 +0,0 @@ -use std::collections::HashSet; - -use proc_macro2::TokenStream; -use quote::quote; -use syn::spanned::Spanned; - -use crate::{ - types::{CommaSeparatedNestedMetas, CommaSeparatedTokenStreams, NestedMeta}, - warning::Warning, - warning::WithWarnings, -}; - -pub fn collect_rules_from_unnamed_struct( - ident: &syn::Ident, - attributes: &[syn::Attribute], -) -> Result<(HashSet, WithWarnings), crate::Errors> { - let mut errors = vec![]; - - let mut rule_fields = HashSet::new(); - let mut warnings = vec![]; - let rules = attributes - .iter() - .filter(|attribute| attribute.path().is_ident("rule")) - .inspect(|attribute| { - warnings.push(Warning::new_rule_deprecated( - ident, - attribute.bracket_token.span.span(), - )); - }) - .filter_map(|attribute| match &attribute.meta { - syn::Meta::List(list) => match collect_rule(list) { - Ok((field_ident, stream)) => { - rule_fields.extend(field_ident); - Some(stream) - } - Err(rule_errors) => { - errors.extend(rule_errors); - None - } - }, - _ => { - errors.push(crate::Error::rule_allow_function_call_or_closure( - attribute.meta.path(), - )); - None - } - }) - .collect::>(); - - if errors.is_empty() { - Ok(( - rule_fields, - WithWarnings { - data: TokenStream::from_iter(rules), - warnings, - }, - )) - } else { - Err(errors) - } -} - -fn collect_rule( - meta_list: &syn::MetaList, -) -> Result<(HashSet, TokenStream), crate::Errors> { - 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::rule_allow_function_call_or_closure( - meta_list.path.span(), - )])?, - 2.. => nested.iter().skip(1).for_each(|nested_meta| { - errors.push(crate::Error::rule_allow_single_function(nested_meta)) - }), - _ => {} - } - - let rule = match &nested[0] { - crate::types::NestedMeta::Meta(syn::Meta::List(list)) => extract_rule_from_meta_list(list), - - crate::types::NestedMeta::Closure(closure) => extract_rule_from_closure(closure), - _ => Err(vec![crate::Error::rule_allow_function_call_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_rule_from_meta_list( - meta_list: &syn::MetaList, -) -> Result<(HashSet, TokenStream), crate::Errors> { - let mut errors = vec![]; - - let rule_fn_name = &meta_list.path; - let nested = meta_list - .parse_args_with(CommaSeparatedNestedMetas::parse_terminated) - .map_err(|error| vec![crate::Error::rule_args_parse_error(meta_list, &error)])?; - if nested.is_empty() { - errors.push(crate::Error::rule_need_arguments(rule_fn_name)); - } - - let mut arg_idents = HashSet::new(); - let rule_fn_args = nested - .iter() - .filter_map(|nested_meta| match nested_meta { - NestedMeta::Lit(syn::Lit::Int(int)) => { - let index = syn::Ident::new(&format!("__{}", int), int.span()); - arg_idents.insert(index.clone()); - Some(quote!(#index)) - } - _ => { - errors.push(crate::Error::rule_args_allow_field_index( - rule_fn_name, - nested_meta, - )); - - None - } - }) - .collect::(); - - if !errors.is_empty() { - return Err(errors); - } - - Ok(( - arg_idents, - quote!( - if let Err(__error) = #rule_fn_name(#rule_fn_args) { - __rule_vec_errors - .push(__error); - }; - ), - )) -} - -fn extract_rule_from_closure( - closure: &syn::ExprClosure, -) -> Result<(HashSet, TokenStream), crate::Errors> { - let mut errors = vec![]; - - let mut arg_idents = HashSet::new(); - let rule_fn_args = closure - .inputs - .iter() - .filter_map(|input| match input { - syn::Pat::Ident(syn::PatIdent { ident, .. }) => { - let index = syn::Ident::new(&format!("_{}", ident), ident.span()); - arg_idents.insert(index.clone()); - - Some(quote!(#index)) - } - _ => { - errors.push(crate::Error::rule_unnamed_clousure_input(input)); - - None - } - }) - .collect::(); - - if !errors.is_empty() { - return Err(errors); - } - - Ok(( - arg_idents, - quote!( - if let Err(__error) = (#closure)(#rule_fn_args) { - __rule_vec_errors.push(__error); - }; - ), - )) -} diff --git a/crates/serde_valid_derive/src/attribute/struct_validate/generic.rs b/crates/serde_valid_derive/src/attribute/struct_validate/generic.rs index b437e6a..2bbd3bd 100644 --- a/crates/serde_valid_derive/src/attribute/struct_validate/generic.rs +++ b/crates/serde_valid_derive/src/attribute/struct_validate/generic.rs @@ -1,6 +1,3 @@ mod custom; -pub use custom::{ - extract_generic_struct_custom_validator_from_meta_list, - extract_generic_struct_custom_validator_from_meta_name_value, -}; +pub use custom::extract_generic_struct_custom_validator_from_meta_name_value; diff --git a/crates/serde_valid_derive/src/attribute/struct_validate/generic/custom.rs b/crates/serde_valid_derive/src/attribute/struct_validate/generic/custom.rs index d119df3..4cd4e9f 100644 --- a/crates/serde_valid_derive/src/attribute/struct_validate/generic/custom.rs +++ b/crates/serde_valid_derive/src/attribute/struct_validate/generic/custom.rs @@ -1,54 +1,7 @@ use crate::attribute::common::message_format::MessageFormat; use crate::attribute::Validator; -use crate::types::CommaSeparatedNestedMetas; use quote::quote; -pub fn extract_generic_struct_custom_validator_from_meta_list( - 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_struct_custom_from_meta_path(path) - } - crate::types::NestedMeta::Meta(syn::Meta::List(list)) => { - extract_struct_custom_from_meta_list(list) - } - crate::types::NestedMeta::Closure(closure) => extract_struct_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()), - } -} - pub fn extract_generic_struct_custom_validator_from_meta_name_value( meta_name_value: &syn::MetaNameValue, _message_format: MessageFormat, @@ -73,16 +26,6 @@ fn extract_struct_custom_from_meta_path(meta_path: &syn::Path) -> Result Result { - Ok(quote!( - if let Err(__errors) = serde_valid::validation::custom::wrap_closure_validation(self, #meta_list) { - __rule_vec_errors.extend(__errors); - }; - )) -} - 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) { diff --git a/crates/serde_valid_derive/src/attribute/struct_validate/meta/meta_list.rs b/crates/serde_valid_derive/src/attribute/struct_validate/meta/meta_list.rs index 883debc..2eb9122 100644 --- a/crates/serde_valid_derive/src/attribute/struct_validate/meta/meta_list.rs +++ b/crates/serde_valid_derive/src/attribute/struct_validate/meta/meta_list.rs @@ -1,32 +1,12 @@ -use syn::spanned::Spanned; - use crate::{ - attribute::{ - common::message_format::MessageFormat, - struct_validate::generic::extract_generic_struct_custom_validator_from_meta_list, - MetaListStructValidation, Validator, - }, - warning::{Warning, WithWarnings}, + attribute::{common::message_format::MessageFormat, MetaListStructValidation, Validator}, + warning::WithWarnings, }; pub fn extract_struct_validator_from_meta_list( validation_type: MetaListStructValidation, - validation: &syn::MetaList, - message_format: MessageFormat, + _validation: &syn::MetaList, + _message_format: MessageFormat, ) -> Result, crate::Errors> { - match validation_type { - MetaListStructValidation::Custom => { - extract_generic_struct_custom_validator_from_meta_list(validation, message_format).map( - |validator| { - WithWarnings::new_with_warnings( - validator, - vec![Warning::new_custom_meta_list_deprecated( - validation.path.get_ident().unwrap(), - validation.span(), - )], - ) - }, - ) - } - } + match validation_type {} } diff --git a/crates/serde_valid_derive/src/attribute/variant_validate/meta/meta_list.rs b/crates/serde_valid_derive/src/attribute/variant_validate/meta/meta_list.rs index de447e6..6c1109e 100644 --- a/crates/serde_valid_derive/src/attribute/variant_validate/meta/meta_list.rs +++ b/crates/serde_valid_derive/src/attribute/variant_validate/meta/meta_list.rs @@ -1,32 +1,12 @@ -use syn::spanned::Spanned; - use crate::{ - attribute::{ - common::message_format::MessageFormat, - struct_validate::generic::extract_generic_struct_custom_validator_from_meta_list, - MetaListStructValidation, Validator, - }, - warning::{Warning, WithWarnings}, + attribute::{common::message_format::MessageFormat, MetaListStructValidation, Validator}, + warning::WithWarnings, }; pub fn extract_variant_validator_from_meta_list( validation_type: MetaListStructValidation, - validation: &syn::MetaList, - message_format: MessageFormat, + _validation: &syn::MetaList, + _message_format: MessageFormat, ) -> Result, crate::Errors> { - match validation_type { - MetaListStructValidation::Custom => { - extract_generic_struct_custom_validator_from_meta_list(validation, message_format).map( - |validator| { - WithWarnings::new_with_warnings( - validator, - vec![Warning::new_custom_meta_list_deprecated( - validation.path.get_ident().unwrap(), - validation.span(), - )], - ) - }, - ) - } - } + match validation_type {} } diff --git a/crates/serde_valid_derive/src/derive/enum_derive.rs b/crates/serde_valid_derive/src/derive/enum_derive.rs index f9a6511..ba6d1ef 100644 --- a/crates/serde_valid_derive/src/derive/enum_derive.rs +++ b/crates/serde_valid_derive/src/derive/enum_derive.rs @@ -1,6 +1,5 @@ use super::named_struct_derive::collect_named_fields_validators_list; use super::unnamed_struct_derive::collect_unnamed_fields_validators_list; -use crate::attribute::rule::{collect_rules_from_named_struct, collect_rules_from_unnamed_struct}; use crate::attribute::variant_validate::collect_variant_custom_from_variant; use crate::attribute::Validator; use crate::error::{array_errors_tokens, new_type_errors_tokens, object_errors_tokens}; @@ -9,7 +8,6 @@ use crate::types::CommaSeparatedTokenStreams; use crate::warning::WithWarnings; use proc_macro2::TokenStream; use quote::quote; -use std::collections::HashSet; use std::iter::FromIterator; pub type Variants = syn::punctuated::Punctuated; @@ -92,26 +90,13 @@ fn expand_enum_variant_named_fields_validation( variant: &syn::Variant, named_fields: &syn::FieldsNamed, ) -> Result, crate::Errors> { + let mut warnings = vec![]; let mut errors = vec![]; let variant_ident = &variant.ident; let mut fields_idents = CommaSeparatedTokenStreams::new(); let rename_map = collect_serde_rename_map(named_fields); - let ( - rule_fields, - WithWarnings { - data: rules, - mut warnings, - }, - ) = match collect_rules_from_named_struct(&variant.ident, &variant.attrs) { - Ok(field_rules) => field_rules, - Err(variant_errors) => { - errors.extend(variant_errors); - (HashSet::new(), WithWarnings::new(Validator::new())) - } - }; - let enum_validates = match collect_variant_custom_from_variant(&input.attrs) { Ok(validations) => { warnings.extend(validations.warnings); @@ -132,11 +117,7 @@ fn expand_enum_variant_named_fields_validation( fields_idents.push(quote!(#field_ident)); quote!(#token) } else { - if rule_fields.contains(field_ident) { - fields_idents.push(quote!(#field_ident)); - } else { - fields_idents.push(quote!(#field_ident: _)); - } + fields_idents.push(quote!(#field_ident)); quote!() } })) @@ -158,7 +139,6 @@ fn expand_enum_variant_named_fields_validation( #enum_validates #validates - #rules if !(__rule_vec_errors.is_empty() && __property_vec_errors_map.is_empty()) { Err(#variant_errors)? @@ -178,25 +158,12 @@ fn expand_enum_variant_unnamed_fields_varidation( variant: &syn::Variant, unnamed_fields: &syn::FieldsUnnamed, ) -> Result, crate::Errors> { + let mut warnings = vec![]; let mut errors = vec![]; let variant_ident = &variant.ident; let mut fields_idents = CommaSeparatedTokenStreams::new(); - let ( - rule_fields, - WithWarnings { - data: rules, - mut warnings, - }, - ) = match collect_rules_from_unnamed_struct(&variant.ident, &variant.attrs) { - Ok(field_rules) => field_rules, - Err(variant_errors) => { - errors.extend(variant_errors); - (HashSet::new(), WithWarnings::new(Validator::new())) - } - }; - let enum_validates = match collect_variant_custom_from_variant(&input.attrs) { Ok(validations) => { warnings.extend(validations.warnings); @@ -217,11 +184,7 @@ fn expand_enum_variant_unnamed_fields_varidation( fields_idents.push(quote!(#field_ident)); quote!(#token) } else { - if rule_fields.contains(field_ident) { - fields_idents.push(quote!(#field_ident)); - } else { - fields_idents.push(quote!(_)); - } + fields_idents.push(quote!(#field_ident)); quote!() } })) @@ -247,7 +210,6 @@ fn expand_enum_variant_unnamed_fields_varidation( #enum_validates #validates - #rules if !(__rule_vec_errors.is_empty() && __item_vec_errors_map.is_empty()) { Err(#variant_errors)? diff --git a/crates/serde_valid_derive/src/derive/named_struct_derive.rs b/crates/serde_valid_derive/src/derive/named_struct_derive.rs index d886f4d..ab6a6bd 100644 --- a/crates/serde_valid_derive/src/derive/named_struct_derive.rs +++ b/crates/serde_valid_derive/src/derive/named_struct_derive.rs @@ -1,15 +1,11 @@ use crate::attribute::field_validate::{extract_field_validator, FieldValidators}; -use crate::attribute::rule::collect_rules_from_named_struct; use crate::attribute::struct_validate::collect_struct_custom_from_named_struct; -use crate::attribute::Validator; use crate::error::object_errors_tokens; use crate::serde::rename::{collect_serde_rename_map, RenameMap}; use crate::types::{Field, NamedField}; -use crate::warning::WithWarnings; use proc_macro2::TokenStream; use quote::quote; use std::borrow::Cow; -use std::collections::HashSet; use std::iter::FromIterator; pub fn expand_named_struct_derive( @@ -20,22 +16,9 @@ pub fn expand_named_struct_derive( let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl(); let rename_map = collect_serde_rename_map(fields); + let mut warnings = vec![]; let mut errors = vec![]; - let ( - rule_fields, - WithWarnings { - data: rules, - mut warnings, - }, - ) = match collect_rules_from_named_struct(&input.ident, &input.attrs) { - Ok((rule_fields, rules)) => (rule_fields, rules), - Err(rule_errors) => { - errors.extend(rule_errors); - (HashSet::new(), WithWarnings::new(Validator::new())) - } - }; - let struct_validations = match collect_struct_custom_from_named_struct(&input.attrs) { Ok(validations) => { warnings.extend(validations.warnings); @@ -50,8 +33,8 @@ pub fn expand_named_struct_derive( let field_validates = match collect_named_fields_validators_list(fields, &rename_map) { Ok(field_validators) => TokenStream::from_iter(field_validators.iter().map(|validator| { warnings.extend(validator.warnings.clone()); - if validator.is_empty() && rule_fields.contains(validator.ident()) { - validator.get_field_variable_token() + if validator.is_empty() { + quote!() } else { validator.generate_tokens() } @@ -80,7 +63,6 @@ pub fn expand_named_struct_derive( #field_validates #struct_validations - #rules if __rule_vec_errors.is_empty() && __property_vec_errors_map.is_empty() { Ok(()) diff --git a/crates/serde_valid_derive/src/derive/unnamed_struct_derive.rs b/crates/serde_valid_derive/src/derive/unnamed_struct_derive.rs index 7071049..0d0b718 100644 --- a/crates/serde_valid_derive/src/derive/unnamed_struct_derive.rs +++ b/crates/serde_valid_derive/src/derive/unnamed_struct_derive.rs @@ -1,14 +1,12 @@ use crate::attribute::field_validate::{extract_field_validator, FieldValidators}; -use crate::attribute::rule::collect_rules_from_unnamed_struct; use crate::attribute::struct_validate::collect_struct_custom_from_named_struct; use crate::attribute::Validator; use crate::error::{array_errors_tokens, new_type_errors_tokens}; use crate::types::{Field, UnnamedField}; -use crate::warning::WithWarnings; use proc_macro2::TokenStream; use quote::quote; use std::borrow::Cow; -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; use std::iter::FromIterator; pub fn expand_unnamed_struct_derive( @@ -18,22 +16,9 @@ pub fn expand_unnamed_struct_derive( let ident = &input.ident; let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl(); + let mut warnings = vec![]; let mut errors = vec![]; - let ( - rule_fields, - WithWarnings { - data: rules, - mut warnings, - }, - ) = match collect_rules_from_unnamed_struct(&input.ident, &input.attrs) { - Ok((rule_fields, rules)) => (rule_fields, rules), - Err(rule_errors) => { - errors.extend(rule_errors); - (HashSet::new(), WithWarnings::new(Validator::new())) - } - }; - let struct_validations = match collect_struct_custom_from_named_struct(&input.attrs) { Ok(validations) => { warnings.extend(validations.warnings); @@ -48,8 +33,8 @@ pub fn expand_unnamed_struct_derive( let field_validates: TokenStream = match collect_unnamed_fields_validators_list(fields) { Ok(field_validators) => TokenStream::from_iter(field_validators.iter().map(|validator| { warnings.extend(validator.warnings.clone()); - if validator.is_empty() && rule_fields.contains(validator.ident()) { - validator.get_field_variable_token() + if validator.is_empty() { + quote!() } else { validator.generate_tokens() } @@ -82,7 +67,6 @@ pub fn expand_unnamed_struct_derive( #field_validates #struct_validations - #rules if __rule_vec_errors.is_empty() && __item_vec_errors_map.is_empty() { Ok(()) diff --git a/crates/serde_valid_derive/src/error.rs b/crates/serde_valid_derive/src/error.rs index 0c5042e..28a96b2 100644 --- a/crates/serde_valid_derive/src/error.rs +++ b/crates/serde_valid_derive/src/error.rs @@ -174,58 +174,6 @@ impl Error { Self::new(input.span(), "#[derive(Validate)] does not support Union.") } - pub fn rule_allow_function_call_or_closure(span: impl Spanned) -> Self { - Self::new(span.span(), "#[rule(???)] allows function call or closure.") - } - - pub fn rule_allow_single_function(meta: &crate::types::NestedMeta) -> Self { - Self::new(meta.span(), "#[rule(???)] allows single function.") - } - - pub fn rule_need_arguments(path: &syn::Path) -> Self { - Self::new(path.span(), "`rule` function needs arguments.") - } - - pub fn rule_args_parse_error(meta_list: &syn::MetaList, error: &syn::Error) -> Self { - Self::new( - meta_list.span(), - format!("#[rule(???)] parse error: {error}"), - ) - } - - pub fn rule_args_allow_field_name( - rule_fn_name_path: &syn::Path, - meta: &crate::types::NestedMeta, - ) -> Self { - let rule_fn_name = quote!(#rule_fn_name_path).to_string(); - Self::new( - meta.span(), - format!("#[rule({rule_fn_name}(???, ...))] allows field name only."), - ) - } - - pub fn rule_args_allow_field_index( - rule_fn_name_path: &syn::Path, - meta: &crate::types::NestedMeta, - ) -> Self { - let rule_fn_name = quote!(#rule_fn_name_path).to_string(); - Self::new( - meta.span(), - format!("#[rule({rule_fn_name}(???, ...))] allows field index only."), - ) - } - - pub fn rule_named_clousure_input(meta: &syn::Pat) -> Self { - Self::new(meta.span(), "Inputs of closure allows filed name only.") - } - - pub fn rule_unnamed_clousure_input(meta: &syn::Pat) -> Self { - Self::new( - meta.span(), - "Inputs of closure allows field index (like _0, _1, etc...) only.", - ) - } - pub fn validate_meta_name_value_not_supported(name_value: &syn::MetaNameValue) -> Self { Self::new(name_value.span(), "#[validate = ???] not supported.") } diff --git a/crates/serde_valid_derive/src/warning.rs b/crates/serde_valid_derive/src/warning.rs index 85a7527..2e057c9 100644 --- a/crates/serde_valid_derive/src/warning.rs +++ b/crates/serde_valid_derive/src/warning.rs @@ -92,24 +92,6 @@ impl std::cmp::PartialEq for Warning { impl std::cmp::Eq for Warning {} impl Warning { - pub fn new_rule_deprecated(ident: &syn::Ident, span: Span) -> Self { - Self::Deprecated { - ident: ident.clone(), - note: "#[rule(...)] is deprecated, please use #[validate(custom(...)))] instead." - .to_string(), - span, - } - } - - pub fn new_custom_meta_list_deprecated(ident: &syn::Ident, span: Span) -> Self { - Self::Deprecated { - ident: ident.clone(), - note: "#[validate(custom(...))] is deprecated, please use #[validate(custom = ...)] instead." - .to_string(), - span, - } - } - pub fn add_index(&self, index: usize) -> Self { match self { Self::Deprecated { ident, note, span } => Self::Deprecated {