-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from yassun7010/support_warnings
Support warnings
- Loading branch information
Showing
23 changed files
with
602 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ regex = "^1.6" | |
paste = "^1.0" | ||
serde_json = "^1.0" | ||
serde = "^1.0" | ||
itertools = "0.13.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
mod generic; | ||
mod meta; | ||
|
||
use crate::attribute::Validator; | ||
|
||
use self::meta::extract_variant_validator; | ||
|
||
pub fn collect_variant_custom_from_named_variant( | ||
attributes: &[syn::Attribute], | ||
) -> Result<Validator, crate::Errors> { | ||
let mut errors = vec![]; | ||
|
||
let validations = attributes | ||
.iter() | ||
.filter_map(|attribute| { | ||
if attribute.path().is_ident("validate") { | ||
match extract_variant_validator(attribute) { | ||
Ok(validator) => Some(validator), | ||
Err(validator_error) => { | ||
errors.extend(validator_error); | ||
None | ||
} | ||
} | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
if errors.is_empty() { | ||
Ok(Validator::from_iter(validations)) | ||
} else { | ||
Err(errors) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod custom; | ||
|
||
pub use custom::extract_generic_variant_custom_validator; |
82 changes: 82 additions & 0 deletions
82
serde_valid_derive/src/attribute/variant_validate/generic/custom.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
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<Validator, 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::validate_custom_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_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<Validator, crate::Errors> { | ||
let rule_fn_name = &meta_path; | ||
|
||
Ok(quote!( | ||
if let Err(__error) = #rule_fn_name(self) { | ||
__rule_vec_errors.push(__error); | ||
}; | ||
)) | ||
} | ||
|
||
fn extract_variant_custom_from_meta_list( | ||
meta_list: &syn::MetaList, | ||
) -> Result<Validator, crate::Errors> { | ||
Ok(quote!( | ||
if let Err(__error) = serde_valid::helpers::wrap_closure_validation(self, #meta_list) { | ||
__rule_vec_errors.push(__error); | ||
}; | ||
)) | ||
} | ||
|
||
fn extract_variant_custom_from_closure( | ||
closure: &syn::ExprClosure, | ||
) -> Result<Validator, crate::Errors> { | ||
Ok(quote!( | ||
if let Err(__error) = serde_valid::helpers::wrap_closure_validation(self, #closure) { | ||
__rule_vec_errors.push(__error); | ||
}; | ||
)) | ||
} |
Oops, something went wrong.