Skip to content

Commit

Permalink
feat: support struct custsom.
Browse files Browse the repository at this point in the history
  • Loading branch information
yassun7010 committed Jun 21, 2024
1 parent fd3e0b2 commit bd49d9b
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 112 deletions.
18 changes: 9 additions & 9 deletions serde_valid/tests/custom_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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,
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -200,7 +200,7 @@ fn named_struct_custom_vec_errors_is_ok() {
}

#[derive(Validate)]
#[validate(custom(validation))]
#[validate(custom = validation)]
struct TestStruct {
val: i32,
}
Expand All @@ -220,7 +220,7 @@ fn named_struct_custom_vec_errors_is_err() {
}

#[derive(Validate)]
#[validate(custom(validation))]
#[validate(custom = validation)]
struct TestStruct {
val: i32,
}
Expand All @@ -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,
}
Expand All @@ -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,
}
Expand Down
1 change: 1 addition & 0 deletions serde_valid_derive/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ enum_str! {

enum_str! {
pub enum MetaNameValueStructValidation {
Custom = "custom",
}
}

Expand Down
2 changes: 1 addition & 1 deletion serde_valid_derive/src/attribute/struct_validate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod generic;
pub mod generic;
mod meta;

use crate::{attribute::Validator, warning::WithWarnings};
Expand Down
5 changes: 4 additions & 1 deletion serde_valid_derive/src/attribute/struct_validate/generic.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Original file line number Diff line number Diff line change
Expand Up @@ -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<Validator, crate::Errors> {
Expand Down Expand Up @@ -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<Validator, crate::Errors> {
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<Validator, crate::Errors> {
let rule_fn_name = &meta_path;

Expand All @@ -69,6 +83,14 @@ fn extract_struct_custom_from_meta_list(
))
}

fn extract_struct_custom_from_call(call: &syn::ExprCall) -> Result<Validator, crate::Errors> {
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<Validator, crate::Errors> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -11,7 +11,7 @@ pub fn extract_struct_validator_from_meta_list(
) -> Result<Validator, crate::Errors> {
match validation_type {
MetaListStructValidation::Custom => {
extract_generic_struct_custom_validator(validation, message_format)
extract_generic_struct_custom_validator_from_meta_list(validation, message_format)
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Validator, crate::Errors> {
match validation_type {}
match validation_type {
MetaNameValueStructValidation::Custom => {
extract_generic_struct_custom_validator_from_meta_name_value(validation, message_format)
}
}
}
1 change: 0 additions & 1 deletion serde_valid_derive/src/attribute/variant_validate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mod generic;
mod meta;

use crate::attribute::Validator;
Expand Down
3 changes: 0 additions & 3 deletions serde_valid_derive/src/attribute/variant_validate/generic.rs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -11,7 +11,7 @@ pub fn extract_variant_validator_from_meta_list(
) -> Result<Validator, crate::Errors> {
match validation_type {
MetaListStructValidation::Custom => {
extract_generic_variant_custom_validator(validation, message_format)
extract_generic_struct_custom_validator_from_meta_list(validation, message_format)
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Validator, crate::Errors> {
match validation_type {}
match validation_type {
MetaNameValueStructValidation::Custom => {
extract_generic_struct_custom_validator_from_meta_name_value(validation, message_format)
}
}
}

0 comments on commit bd49d9b

Please sign in to comment.