Skip to content

Commit

Permalink
refactor: update hint.
Browse files Browse the repository at this point in the history
  • Loading branch information
yassun7010 committed Jan 6, 2024
1 parent 1aea7b9 commit 9ef25ea
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 32 deletions.
1 change: 0 additions & 1 deletion serde_valid_derive/src/validate/meta/meta_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ pub fn extract_validator_from_meta_list(
}
syn::Meta::NameValue(name_value) => extract_validator_from_nested_meta_name_value(
field,
attribute,
name_value,
custom_message,
rename_map,
Expand Down
48 changes: 31 additions & 17 deletions serde_valid_derive/src/validate/meta/nested_meta_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,48 @@ use crate::validate::common::{CustomMessageToken, MetaListValidation};
use crate::validate::generic::{
extract_generic_custom_validator, extract_generic_enumerate_validator,
};
use crate::validate::Validator;
use crate::validate::{MetaNameValueValidation, MetaPathValidation, Validator};
use std::str::FromStr;

pub fn extract_validator_from_nested_meta_list(
field: &impl Field,
validation_list: &syn::MetaList,
validation: &syn::MetaList,
custom_message: CustomMessageToken,
rename_map: &RenameMap,
) -> Result<Validator, crate::Errors> {
let syn::MetaList {
path: validation_name,
..
} = validation_list;
let validation_ident = SingleIdentPath::new(validation_name).ident();
let mut errors = vec![];

match MetaListValidation::from_str(&validation_ident.to_string()) {
let validation_ident = SingleIdentPath::new(&validation.path).ident();
let validation_name = validation_ident.to_string();

match MetaListValidation::from_str(&validation_name) {
Ok(MetaListValidation::Enumerate) => {
extract_generic_enumerate_validator(field, validation_list, custom_message, rename_map)
extract_generic_enumerate_validator(field, validation, custom_message, rename_map)
}
Ok(MetaListValidation::Custom) => {
extract_generic_custom_validator(field, validation_list, rename_map)
extract_generic_custom_validator(field, validation, rename_map)
}
Err(unknown) => {
let error = if MetaNameValueValidation::from_str(&validation_name).is_ok() {
crate::Error::validate_meta_name_value_need_value(
&validation.path,
&validation_name,
)
} else if MetaPathValidation::from_str(&validation_name).is_ok() {
crate::Error::validate_meta_path_need_value(&validation.path, &validation_name)
} else {
crate::Error::validate_unknown_type(
&validation.path,
&unknown,
&(MetaPathValidation::iter().map(|x| x.name()))
.chain(MetaNameValueValidation::iter().map(|x| x.name()))
.chain(MetaListValidation::iter().map(|x| x.name()))
.collect::<Vec<_>>(),
)
};
errors.push(error);

Err(errors)
}
Err(unknown) => Err(vec![crate::Error::validate_unknown_type(
validation_name,
&unknown,
&MetaListValidation::iter()
.map(|x| x.name())
.collect::<Vec<_>>(),
)]),
}
}
44 changes: 30 additions & 14 deletions serde_valid_derive/src/validate/meta/nested_meta_name_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ use crate::validate::string::{
extract_string_max_length_validator, extract_string_min_length_validator,
extract_string_pattern_validator,
};
use crate::validate::Validator;
use crate::validate::{MetaListValidation, MetaPathValidation, Validator};
use std::str::FromStr;

pub fn extract_validator_from_nested_meta_name_value(
field: &impl Field,
_attribute: &syn::Attribute,
name_value: &syn::MetaNameValue,
validation: &syn::MetaNameValue,
custom_message: CustomMessageToken,
rename_map: &RenameMap,
) -> Result<Validator, crate::Errors> {
let validation_name = &name_value.path;
let validation_name_ident = SingleIdentPath::new(validation_name).ident();
let validation_value = get_lit(&name_value.value)?;
let mut errors = vec![];

match MetaNameValueValidation::from_str(&validation_name_ident.to_string()) {
let validation_ident = SingleIdentPath::new(&validation.path).ident();
let validation_name = validation_ident.to_string();
let validation_value = get_lit(&validation.value)?;

match MetaNameValueValidation::from_str(&validation_name) {
Ok(MetaNameValueValidation::Minimum) => {
extract_numeric_minimum_validator(field, validation_value, custom_message, rename_map)
}
Expand Down Expand Up @@ -86,12 +87,27 @@ pub fn extract_validator_from_nested_meta_name_value(
Ok(MetaNameValueValidation::Pattern) => {
extract_string_pattern_validator(field, validation_value, custom_message, rename_map)
}
Err(unknown) => Err(vec![crate::Error::validate_unknown_type(
validation_name,
&unknown,
&MetaNameValueValidation::iter()
.map(|x| x.name())
.collect::<Vec<_>>(),
)]),
Err(unknown) => {
let error = if MetaListValidation::from_str(&validation_name).is_ok() {
crate::Error::validate_meta_name_value_need_value(
&validation.path,
&validation_name,
)
} else if MetaPathValidation::from_str(&validation_name).is_ok() {
crate::Error::validate_meta_path_need_value(&validation.path, &validation_name)
} else {
crate::Error::validate_unknown_type(
&validation.path,
&unknown,
&(MetaPathValidation::iter().map(|x| x.name()))
.chain(MetaNameValueValidation::iter().map(|x| x.name()))
.chain(MetaListValidation::iter().map(|x| x.name()))
.collect::<Vec<_>>(),
)
};
errors.push(error);

Err(errors)
}
}
}
1 change: 1 addition & 0 deletions serde_valid_derive/src/validate/meta/nested_meta_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub fn extract_validator_from_nested_meta_path(
rename_map: &RenameMap,
) -> Result<Validator, crate::Errors> {
let mut errors = vec![];

let validation_ident = SingleIdentPath::new(validation).ident();
let validation_name = validation_ident.to_string();

Expand Down

0 comments on commit 9ef25ea

Please sign in to comment.