Skip to content

Commit

Permalink
feat: add warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
yassun7010 committed Jun 20, 2024
1 parent a16b444 commit 2c57a6c
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 85 deletions.
10 changes: 8 additions & 2 deletions serde_valid_derive/src/attribute/field_validate/field.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
use crate::attribute::Validator;
use crate::types::Field;
use crate::warning::WithWarnings;
use quote::quote;
use std::borrow::Cow;
use std::iter::FromIterator;

pub struct FieldValidators<'a, F: Field + Clone + 'a> {
field: Cow<'a, F>,
validators: Vec<Validator>,
pub warnings: Vec<crate::warning::Warning>,
}

impl<'a, F: Field + Clone> FieldValidators<'a, F> {
pub fn new(field: Cow<'a, F>, validators: Vec<Validator>) -> Self {
Self { field, validators }
pub fn new(field: Cow<'a, F>, validators: Vec<WithWarnings<Validator>>) -> Self {
Self {
field,
validators: validators.iter().map(|v| v.data.clone()).collect(),
warnings: validators.into_iter().flat_map(|v| v.warnings).collect(),
}
}

pub fn ident(&self) -> &syn::Ident {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use crate::attribute::Validator;
use crate::serde::rename::RenameMap;
use crate::types::Field;
use crate::warning::WithWarnings;
use quote::quote;

pub fn extract_generic_validate_validator(
field: &impl Field,
rename_map: &RenameMap,
) -> Result<Validator, crate::Errors> {
) -> Result<WithWarnings<Validator>, crate::Errors> {
let field_ident = field.ident();
let field_name = field.name();
let field_key = field.key();
let rename = rename_map.get(field_name).unwrap_or(&field_key);
let errors = field.errors_variable();

Ok(quote!(
Ok(WithWarnings::new(quote!(
if let Err(__inner_errors) = #field_ident.validate() {
match __inner_errors {
::serde_valid::validation::Errors::Object(__object_errors) => {
Expand All @@ -31,5 +32,5 @@ pub fn extract_generic_validate_validator(
}
}
}
))
)))
}
6 changes: 4 additions & 2 deletions serde_valid_derive/src/attribute/field_validate/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::attribute::{
use crate::serde::rename::RenameMap;
use crate::types::Field;
use crate::types::SingleIdentPath;
use crate::warning::WithWarnings;
use meta_list::extract_field_validator_from_meta_list;
use meta_name_value::extract_field_validator_from_meta_name_value;
use meta_path::extract_field_validator_from_meta_path;
Expand All @@ -22,7 +23,7 @@ pub fn extract_field_validator(
field: &impl Field,
attribute: &syn::Attribute,
rename_map: &RenameMap,
) -> Result<Validator, crate::Errors> {
) -> Result<WithWarnings<Validator>, crate::Errors> {
match &attribute.meta {
syn::Meta::List(list) => inner_extract_field_validator(field, attribute, list, rename_map),
syn::Meta::Path(_) => extract_generic_validate_validator(field, rename_map),
Expand All @@ -39,7 +40,7 @@ fn inner_extract_field_validator(
attribute: &syn::Attribute,
meta_list: &syn::MetaList,
rename_map: &RenameMap,
) -> Result<Validator, crate::Errors> {
) -> Result<WithWarnings<Validator>, crate::Errors> {
let mut errors = vec![];
let nested = meta_list
.parse_args_with(crate::types::CommaSeparatedMetas::parse_terminated)
Expand Down Expand Up @@ -123,6 +124,7 @@ fn inner_extract_field_validator(
message_format,
rename_map,
)
.map(WithWarnings::new)
}

(Ok(_), _, _, _) => Err(vec![crate::Error::meta_path_validation_need_value(
Expand Down
19 changes: 15 additions & 4 deletions serde_valid_derive/src/attribute/field_validate/meta/meta_list.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
use syn::spanned::Spanned;

use crate::attribute::common::message_format::MessageFormat;
use crate::attribute::field_validate::generic::{
extract_generic_custom_validator, extract_generic_enumerate_validator_from_list,
};
use crate::attribute::MetaListFieldValidation;
use crate::attribute::Validator;
use crate::attribute::{MetaListFieldValidation, Validator};
use crate::serde::rename::RenameMap;
use crate::types::Field;
use crate::warning::Warning;
use crate::warning::WithWarnings;

pub fn extract_field_validator_from_meta_list(
field: &impl Field,
validation_type: MetaListFieldValidation,
validation: &syn::MetaList,
message_format: MessageFormat,
rename_map: &RenameMap,
) -> Result<Validator, crate::Errors> {
) -> Result<WithWarnings<Validator>, crate::Errors> {
match validation_type {
MetaListFieldValidation::Enumerate => extract_generic_enumerate_validator_from_list(
field,
validation,
message_format,
rename_map,
),
)
.map(|data| WithWarnings {
data,
warnings: vec![Warning::new_enumerate_path_deprecated(
field.ident(),
validation.span(),
)],
}),
MetaListFieldValidation::Custom => {
extract_generic_custom_validator(field, validation, message_format, rename_map)
.map(WithWarnings::new)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ use crate::attribute::field_validate::array::extract_array_unique_items_validato
use crate::attribute::{MetaPathFieldValidation, Validator};
use crate::serde::rename::RenameMap;
use crate::types::Field;
use crate::warning::WithWarnings;

pub fn extract_field_validator_from_meta_path(
field: &impl Field,
validation_type: MetaPathFieldValidation,
_validation: &syn::Path,
message_format: MessageFormat,
rename_map: &RenameMap,
) -> Result<Validator, crate::Errors> {
) -> Result<WithWarnings<Validator>, crate::Errors> {
match validation_type {
MetaPathFieldValidation::UniqueItems => Ok(extract_array_unique_items_validator(
field,
message_format,
rename_map,
MetaPathFieldValidation::UniqueItems => Ok(WithWarnings::new(
extract_array_unique_items_validator(field, message_format, rename_map),
)),
}
}
8 changes: 4 additions & 4 deletions serde_valid_derive/src/attribute/rule/named_struct_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use quote::{quote, ToTokens};
use syn::spanned::Spanned;

use crate::{
output_stream::OutputStream,
types::{CommaSeparatedNestedMetas, CommaSeparatedTokenStreams},
warning::Warning,
warning::WithWarnings,
};

pub fn collect_rules_from_named_struct(
ident: &syn::Ident,
attributes: &[syn::Attribute],
) -> Result<(HashSet<syn::Ident>, OutputStream), crate::Errors> {
) -> Result<(HashSet<syn::Ident>, WithWarnings<TokenStream>), crate::Errors> {
let mut errors = vec![];

let mut rule_fields = HashSet::new();
Expand Down Expand Up @@ -50,8 +50,8 @@ pub fn collect_rules_from_named_struct(
if errors.is_empty() {
Ok((
rule_fields,
OutputStream {
output: TokenStream::from_iter(rules),
WithWarnings {
data: TokenStream::from_iter(rules),
warnings,
},
))
Expand Down
8 changes: 4 additions & 4 deletions serde_valid_derive/src/attribute/rule/unnamed_struct_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use quote::quote;
use syn::spanned::Spanned;

use crate::{
output_stream::OutputStream,
types::{CommaSeparatedNestedMetas, CommaSeparatedTokenStreams, NestedMeta},
warning::Warning,
warning::WithWarnings,
};

pub fn collect_rules_from_unnamed_struct(
ident: &syn::Ident,
attributes: &[syn::Attribute],
) -> Result<(HashSet<syn::Ident>, OutputStream), crate::Errors> {
) -> Result<(HashSet<syn::Ident>, WithWarnings<TokenStream>), crate::Errors> {
let mut errors = vec![];

let mut rule_fields = HashSet::new();
Expand Down Expand Up @@ -50,8 +50,8 @@ pub fn collect_rules_from_unnamed_struct(
if errors.is_empty() {
Ok((
rule_fields,
OutputStream {
output: TokenStream::from_iter(rules),
WithWarnings {
data: TokenStream::from_iter(rules),
warnings,
},
))
Expand Down
6 changes: 3 additions & 3 deletions serde_valid_derive/src/attribute/struct_validate.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
mod generic;
mod meta;

use crate::attribute::Validator;
use crate::{attribute::Validator, warning::WithWarnings};

use self::meta::extract_struct_validator;

pub fn collect_struct_custom_from_named_struct(
attributes: &[syn::Attribute],
) -> Result<Validator, crate::Errors> {
) -> Result<WithWarnings<Vec<Validator>>, crate::Errors> {
let mut errors = vec![];

let validations = attributes
Expand All @@ -28,7 +28,7 @@ pub fn collect_struct_custom_from_named_struct(
.collect::<Vec<_>>();

if errors.is_empty() {
Ok(Validator::from_iter(validations))
Ok(WithWarnings::from_iter(validations))
} else {
Err(errors)
}
Expand Down
11 changes: 8 additions & 3 deletions serde_valid_derive/src/attribute/struct_validate/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
Validator,
},
types::SingleIdentPath,
warning::WithWarnings,
};
use quote::quote;
use std::str::FromStr;
Expand All @@ -19,10 +20,14 @@ use self::{
meta_path::extract_struct_validator_from_meta_path,
};

pub fn extract_struct_validator(attribute: &syn::Attribute) -> Result<Validator, crate::Errors> {
pub fn extract_struct_validator(
attribute: &syn::Attribute,
) -> Result<WithWarnings<Validator>, crate::Errors> {
match &attribute.meta {
syn::Meta::Path(_) => Ok(quote!()),
syn::Meta::List(list) => inner_extract_struct_validator(attribute, list),
syn::Meta::Path(_) => Ok(WithWarnings::new(quote!())),
syn::Meta::List(list) => {
inner_extract_struct_validator(attribute, list).map(WithWarnings::new)
}
syn::Meta::NameValue(name_value) => {
Err(vec![crate::Error::validate_meta_name_value_not_supported(
name_value,
Expand Down
35 changes: 18 additions & 17 deletions serde_valid_derive/src/derive/enum_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use super::named_struct_derive::collect_named_fields_validators_list;
use super::unnamed_struct_derive::collect_unnamed_fields_validators_list;
use crate::attribute::rule::{collect_rules_from_named_struct, collect_rules_from_unnamed_struct};
use crate::attribute::variant_validate::collect_variant_custom_from_variant;
use crate::attribute::Validator;
use crate::error::{array_errors_tokens, new_type_errors_tokens, object_errors_tokens};
use crate::output_stream::OutputStream;
use crate::serde::rename::collect_serde_rename_map;
use crate::types::CommaSeparatedTokenStreams;
use crate::warning::WithWarnings;
use proc_macro2::TokenStream;
use quote::quote;
use std::collections::HashSet;
Expand Down Expand Up @@ -34,7 +35,7 @@ pub fn expand_enum_validate_derive(
Ok(variant_varidates_and_rules) => variant_varidates_and_rules,
Err(variant_errors) => {
errors.extend(variant_errors);
OutputStream::new()
WithWarnings::new(Validator::new())
}
}
}
Expand All @@ -48,18 +49,18 @@ pub fn expand_enum_validate_derive(
Ok(variant_varidates_and_rules) => variant_varidates_and_rules,
Err(variant_errors) => {
errors.extend(variant_errors);
OutputStream::new()
WithWarnings::new(Validator::new())
}
}
}
syn::Fields::Unit => OutputStream::new(),
syn::Fields::Unit => WithWarnings::new(Validator::new()),
})
.collect::<Vec<_>>();

let validations_and_rules = TokenStream::from_iter(
validations
.iter()
.map(|variant| variant.output.clone())
.map(|variant| variant.data.clone())
.collect::<Vec<_>>(),
);
let warnings = validations
Expand Down Expand Up @@ -90,7 +91,7 @@ fn expand_enum_variant_named_fields_validation(
input: &syn::DeriveInput,
variant: &syn::Variant,
named_fields: &syn::FieldsNamed,
) -> Result<OutputStream, crate::Errors> {
) -> Result<WithWarnings<Validator>, crate::Errors> {
let mut errors = vec![];

let variant_ident = &variant.ident;
Expand All @@ -99,15 +100,15 @@ fn expand_enum_variant_named_fields_validation(

let (
rule_fields,
OutputStream {
output: rules,
WithWarnings {
data: rules,
warnings,
},
) = match collect_rules_from_named_struct(&variant.ident, &variant.attrs) {
Ok(field_rules) => field_rules,
Err(variant_errors) => {
errors.extend(variant_errors);
(HashSet::new(), OutputStream::new())
(HashSet::new(), WithWarnings::new(Validator::new()))
}
};

Expand Down Expand Up @@ -146,8 +147,8 @@ fn expand_enum_variant_named_fields_validation(
let variant_errors = object_errors_tokens();

if errors.is_empty() {
Ok(OutputStream {
output: quote!(
Ok(WithWarnings {
data: quote!(
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 @@ -173,23 +174,23 @@ fn expand_enum_variant_unnamed_fields_varidation(
input: &syn::DeriveInput,
variant: &syn::Variant,
unnamed_fields: &syn::FieldsUnnamed,
) -> Result<OutputStream, crate::Errors> {
) -> Result<WithWarnings<Validator>, crate::Errors> {
let mut errors = vec![];

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

let (
rule_fields,
OutputStream {
output: rules,
WithWarnings {
data: rules,
warnings,
},
) = match collect_rules_from_unnamed_struct(&variant.ident, &variant.attrs) {
Ok(field_rules) => field_rules,
Err(variant_errors) => {
errors.extend(variant_errors);
(HashSet::new(), OutputStream::new())
(HashSet::new(), WithWarnings::new(Validator::new()))
}
};

Expand Down Expand Up @@ -232,8 +233,8 @@ fn expand_enum_variant_unnamed_fields_varidation(
};

if errors.is_empty() {
Ok(OutputStream {
output: quote!(
Ok(WithWarnings {
data: quote!(
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 Down
Loading

0 comments on commit 2c57a6c

Please sign in to comment.