From b8b78899c6465b91835636c40f80f0064c155a66 Mon Sep 17 00:00:00 2001 From: yassun7010 Date: Mon, 8 Jan 2024 15:49:38 +0900 Subject: [PATCH] refactor: meta filename. --- .../src/struct_validate/meta.rs | 110 ++++++++++++++++-- .../src/struct_validate/meta/meta_list.rs | 101 ++-------------- ..._meta_name_value.rs => meta_name_value.rs} | 0 .../{nested_meta_path.rs => meta_path.rs} | 0 .../struct_validate/meta/nested_meta_list.rs | 11 -- 5 files changed, 110 insertions(+), 112 deletions(-) rename serde_valid_derive/src/struct_validate/meta/{nested_meta_name_value.rs => meta_name_value.rs} (100%) rename serde_valid_derive/src/struct_validate/meta/{nested_meta_path.rs => meta_path.rs} (100%) delete mode 100644 serde_valid_derive/src/struct_validate/meta/nested_meta_list.rs diff --git a/serde_valid_derive/src/struct_validate/meta.rs b/serde_valid_derive/src/struct_validate/meta.rs index e15660c..e92200c 100644 --- a/serde_valid_derive/src/struct_validate/meta.rs +++ b/serde_valid_derive/src/struct_validate/meta.rs @@ -1,18 +1,27 @@ mod meta_list; -mod nested_meta_list; -mod nested_meta_name_value; -mod nested_meta_path; +mod meta_name_value; +mod meta_path; +use crate::{ + field_validate::{ + MetaListStructValidation, MetaNameValueStructValidation, MetaPathStructValidation, + Validator, + }, + types::SingleIdentPath, +}; use quote::quote; +use std::str::FromStr; -use crate::field_validate::Validator; +use self::{ + meta_list::extract_struct_validator_from_nested_meta_list, + meta_name_value::extract_struct_validator_from_nested_meta_name_value, + meta_path::extract_struct_validator_from_nested_meta_path, +}; pub fn extract_struct_validator(attribute: &syn::Attribute) -> Result { match &attribute.meta { syn::Meta::Path(_) => Ok(quote!()), - syn::Meta::List(list) => { - meta_list::extract_struct_validator_from_meta_list(attribute, list) - } + syn::Meta::List(list) => inner_extract_struct_validator(attribute, list), syn::Meta::NameValue(name_value) => { Err(vec![crate::Error::validate_meta_name_value_not_support( name_value, @@ -20,3 +29,90 @@ pub fn extract_struct_validator(attribute: &syn::Attribute) -> Result Result { + let mut errors = vec![]; + let nested = meta_list + .parse_args_with(crate::types::CommaSeparatedMetas::parse_terminated) + .map_err(|error| { + vec![crate::Error::validate_attribute_parse_error( + attribute, &error, + )] + })?; + + match nested.len() { + 0 => Err(vec![crate::Error::struct_validation_type_required( + attribute, + )])?, + 1 => {} + _ => { + for meta in nested.iter().skip(2) { + errors.push(crate::Error::too_many_list_items(meta)); + } + } + }; + + let meta = &nested[0]; + let validation_path = match meta { + syn::Meta::Path(path) => path, + syn::Meta::List(list) => &list.path, + syn::Meta::NameValue(name_value) => &name_value.path, + }; + + let validation_name = SingleIdentPath::new(validation_path).ident().to_string(); + let validator = match ( + MetaPathStructValidation::from_str(&validation_name), + MetaListStructValidation::from_str(&validation_name), + MetaNameValueStructValidation::from_str(&validation_name), + meta, + ) { + (Ok(validation_type), _, _, syn::Meta::Path(validation)) => { + extract_struct_validator_from_nested_meta_path(validation_type, validation) + } + + (_, Ok(validation_type), _, syn::Meta::List(validation)) => { + extract_struct_validator_from_nested_meta_list(validation_type, validation) + } + + (_, _, Ok(validation_type), syn::Meta::NameValue(validation)) => { + extract_struct_validator_from_nested_meta_name_value(validation_type, validation) + } + + (Ok(_), _, _, _) => Err(vec![crate::Error::meta_path_validation_need_value( + validation_path, + &validation_name, + )]), + + (_, Ok(_), _, _) => Err(vec![crate::Error::meta_list_validation_need_value( + validation_path, + &validation_name, + )]), + + (_, _, Ok(_), _) => Err(vec![crate::Error::meta_name_value_validation_need_value( + validation_path, + &validation_name, + )]), + + _ => Err(vec![crate::Error::struct_validation_type_unknown( + validation_path, + &validation_name, + )]), + }; + + match validator { + Ok(validator) => { + if errors.is_empty() { + Ok(validator) + } else { + Err(errors) + } + } + Err(validator_errors) => { + errors.extend(validator_errors); + Err(errors) + } + } +} diff --git a/serde_valid_derive/src/struct_validate/meta/meta_list.rs b/serde_valid_derive/src/struct_validate/meta/meta_list.rs index cf07234..5b411b0 100644 --- a/serde_valid_derive/src/struct_validate/meta/meta_list.rs +++ b/serde_valid_derive/src/struct_validate/meta/meta_list.rs @@ -1,98 +1,11 @@ -use crate::field_validate::Validator; +use crate::field_validate::{MetaListStructValidation, Validator}; +use crate::struct_validate::generic::extract_generic_struct_custom_validator; -use crate::field_validate::{ - MetaListStructValidation, MetaNameValueStructValidation, MetaPathStructValidation, -}; -use crate::types::SingleIdentPath; -use std::str::FromStr; - -use super::nested_meta_list::extract_struct_validator_from_nested_meta_list; -use super::nested_meta_name_value::extract_struct_validator_from_nested_meta_name_value; -use super::nested_meta_path::extract_struct_validator_from_nested_meta_path; - -pub fn extract_struct_validator_from_meta_list( - attribute: &syn::Attribute, - meta_list: &syn::MetaList, +pub fn extract_struct_validator_from_nested_meta_list( + validation_type: MetaListStructValidation, + validation: &syn::MetaList, ) -> Result { - let mut errors = vec![]; - let nested = meta_list - .parse_args_with(crate::types::CommaSeparatedMetas::parse_terminated) - .map_err(|error| { - vec![crate::Error::validate_attribute_parse_error( - attribute, &error, - )] - })?; - - match nested.len() { - 0 => Err(vec![crate::Error::struct_validation_type_required( - attribute, - )])?, - 1 => {} - _ => { - for meta in nested.iter().skip(2) { - errors.push(crate::Error::too_many_list_items(meta)); - } - } - }; - - let meta = &nested[0]; - let validation_path = match meta { - syn::Meta::Path(path) => path, - syn::Meta::List(list) => &list.path, - syn::Meta::NameValue(name_value) => &name_value.path, - }; - - let validation_name = SingleIdentPath::new(validation_path).ident().to_string(); - let validator = match ( - MetaPathStructValidation::from_str(&validation_name), - MetaListStructValidation::from_str(&validation_name), - MetaNameValueStructValidation::from_str(&validation_name), - meta, - ) { - (Ok(validation_type), _, _, syn::Meta::Path(validation)) => { - extract_struct_validator_from_nested_meta_path(validation_type, validation) - } - - (_, Ok(validation_type), _, syn::Meta::List(validation)) => { - extract_struct_validator_from_nested_meta_list(validation_type, validation) - } - - (_, _, Ok(validation_type), syn::Meta::NameValue(validation)) => { - extract_struct_validator_from_nested_meta_name_value(validation_type, validation) - } - - (Ok(_), _, _, _) => Err(vec![crate::Error::meta_path_validation_need_value( - validation_path, - &validation_name, - )]), - - (_, Ok(_), _, _) => Err(vec![crate::Error::meta_list_validation_need_value( - validation_path, - &validation_name, - )]), - - (_, _, Ok(_), _) => Err(vec![crate::Error::meta_name_value_validation_need_value( - validation_path, - &validation_name, - )]), - - _ => Err(vec![crate::Error::struct_validation_type_unknown( - validation_path, - &validation_name, - )]), - }; - - match validator { - Ok(validator) => { - if errors.is_empty() { - Ok(validator) - } else { - Err(errors) - } - } - Err(validator_errors) => { - errors.extend(validator_errors); - Err(errors) - } + match validation_type { + MetaListStructValidation::Custom => extract_generic_struct_custom_validator(validation), } } diff --git a/serde_valid_derive/src/struct_validate/meta/nested_meta_name_value.rs b/serde_valid_derive/src/struct_validate/meta/meta_name_value.rs similarity index 100% rename from serde_valid_derive/src/struct_validate/meta/nested_meta_name_value.rs rename to serde_valid_derive/src/struct_validate/meta/meta_name_value.rs diff --git a/serde_valid_derive/src/struct_validate/meta/nested_meta_path.rs b/serde_valid_derive/src/struct_validate/meta/meta_path.rs similarity index 100% rename from serde_valid_derive/src/struct_validate/meta/nested_meta_path.rs rename to serde_valid_derive/src/struct_validate/meta/meta_path.rs diff --git a/serde_valid_derive/src/struct_validate/meta/nested_meta_list.rs b/serde_valid_derive/src/struct_validate/meta/nested_meta_list.rs deleted file mode 100644 index 5b411b0..0000000 --- a/serde_valid_derive/src/struct_validate/meta/nested_meta_list.rs +++ /dev/null @@ -1,11 +0,0 @@ -use crate::field_validate::{MetaListStructValidation, Validator}; -use crate::struct_validate::generic::extract_generic_struct_custom_validator; - -pub fn extract_struct_validator_from_nested_meta_list( - validation_type: MetaListStructValidation, - validation: &syn::MetaList, -) -> Result { - match validation_type { - MetaListStructValidation::Custom => extract_generic_struct_custom_validator(validation), - } -}