Skip to content

Commit

Permalink
fix: issue#54.
Browse files Browse the repository at this point in the history
  • Loading branch information
yassun7010 committed Mar 2, 2024
1 parent a2e8741 commit 7f2b109
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 deletions.
29 changes: 29 additions & 0 deletions serde_valid/tests/issues_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use serde_valid::Validate;

mod issue54 {
use super::*;

#[test]
fn test_enum_valians_works() {
#[derive(Validate)]
enum Works {
VariantB(),
VariantA,
}

assert!(Works::VariantA.validate().is_ok());
assert!(Works::VariantB().validate().is_ok());
}

#[test]
fn test_enum_valiant_fied_case() {
#[derive(Validate)]
enum Fails {
VariantA,
VariantB(),
}

assert!(Fails::VariantA.validate().is_ok());
assert!(Fails::VariantB().validate().is_ok());
}
}
54 changes: 18 additions & 36 deletions serde_valid_derive/src/derive/enum_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,27 @@ pub fn expand_enum_validate_derive(
let mut errors = vec![];

let validations_and_rules =
TokenStream::from_iter(variants.iter().enumerate().map(|(index, variant)| {
match &variant.fields {
syn::Fields::Named(named_fields) => {
match expand_enum_variant_named_fields(index, ident, variant, named_fields) {
Ok(variant_varidates_and_rules) => variant_varidates_and_rules,
Err(variant_errors) => {
errors.extend(variant_errors);
quote!()
}
TokenStream::from_iter(variants.iter().map(|variant| match &variant.fields {
syn::Fields::Named(named_fields) => {
match expand_enum_variant_named_fields(ident, variant, named_fields) {
Ok(variant_varidates_and_rules) => variant_varidates_and_rules,
Err(variant_errors) => {
errors.extend(variant_errors);
quote!()
}
}
syn::Fields::Unnamed(unnamed_fields) => {
match expand_enum_variant_unnamed_fields_varidation(
index,
ident,
variant,
unnamed_fields,
) {
Ok(variant_varidates_and_rules) => variant_varidates_and_rules,
Err(variant_errors) => {
errors.extend(variant_errors);
quote!()
}
}
syn::Fields::Unnamed(unnamed_fields) => {
match expand_enum_variant_unnamed_fields_varidation(ident, variant, unnamed_fields)
{
Ok(variant_varidates_and_rules) => variant_varidates_and_rules,
Err(variant_errors) => {
errors.extend(variant_errors);
quote!()
}
}
syn::Fields::Unit => quote!(),
}
syn::Fields::Unit => quote!(),
}));

if errors.is_empty() {
Expand All @@ -66,7 +60,6 @@ pub fn expand_enum_validate_derive(
}

fn expand_enum_variant_named_fields(
index: usize,
ident: &syn::Ident,
variant: &syn::Variant,
named_fields: &syn::FieldsNamed,
Expand All @@ -75,7 +68,6 @@ fn expand_enum_variant_named_fields(

let variant_ident = &variant.ident;
let mut fields_idents = CommaSeparatedTokenStreams::new();
let else_token = make_else_token(index);
let rename_map = collect_serde_rename_map(named_fields);

let (rule_fields, rules) = match collect_rules_from_named_struct(&variant.attrs) {
Expand Down Expand Up @@ -114,7 +106,7 @@ fn expand_enum_variant_named_fields(

if errors.is_empty() {
Ok(quote!(
#else_token if let #ident::#variant_ident{#fields_idents} = &self {
if let #ident::#variant_ident{#fields_idents} = &self {
let mut __rule_vec_errors = ::serde_valid::validation::VecErrors::new();
let mut __property_vec_errors_map = ::serde_valid::validation::PropertyVecErrorsMap::new();

Expand All @@ -132,7 +124,6 @@ fn expand_enum_variant_named_fields(
}

fn expand_enum_variant_unnamed_fields_varidation(
index: usize,
ident: &syn::Ident,
variant: &syn::Variant,
unnamed_fields: &syn::FieldsUnnamed,
Expand All @@ -141,7 +132,6 @@ fn expand_enum_variant_unnamed_fields_varidation(

let variant_ident = &variant.ident;
let mut fields_idents = CommaSeparatedTokenStreams::new();
let else_token = make_else_token(index);

let (rule_fields, rules) = match collect_rules_from_unnamed_struct(&variant.attrs) {
Ok(field_rules) => field_rules,
Expand Down Expand Up @@ -183,7 +173,7 @@ fn expand_enum_variant_unnamed_fields_varidation(

if errors.is_empty() {
Ok(quote!(
#else_token if let #ident::#variant_ident(#fields_idents) = &self {
if let #ident::#variant_ident(#fields_idents) = &self {
let mut __rule_vec_errors = ::serde_valid::validation::VecErrors::new();
let mut __item_vec_errors_map = ::serde_valid::validation::ItemVecErrorsMap::new();

Expand All @@ -199,11 +189,3 @@ fn expand_enum_variant_unnamed_fields_varidation(
Err(errors)
}
}

fn make_else_token(index: usize) -> TokenStream {
if index == 0 {
quote!()
} else {
quote!(else)
}
}

0 comments on commit 7f2b109

Please sign in to comment.