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 21, 2024
1 parent 3884fb5 commit bb983b6
Show file tree
Hide file tree
Showing 13 changed files with 351 additions and 271 deletions.
465 changes: 239 additions & 226 deletions serde_valid/tests/deprecated_custom_test.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn extract_field_validator_from_meta_list(
)
.map(|data| WithWarnings {
data,
warnings: vec![Warning::new_enumerate_path_deprecated(
warnings: vec![Warning::new_enumerate_meta_list_deprecated(
field.ident(),
validation.span(),
)],
Expand Down
6 changes: 2 additions & 4 deletions serde_valid_derive/src/attribute/struct_validate/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ pub fn extract_struct_validator(
) -> Result<WithWarnings<Validator>, crate::Errors> {
match &attribute.meta {
syn::Meta::Path(_) => Ok(WithWarnings::new(quote!())),
syn::Meta::List(list) => {
inner_extract_struct_validator(attribute, list).map(WithWarnings::new)
}
syn::Meta::List(list) => inner_extract_struct_validator(attribute, list),
syn::Meta::NameValue(name_value) => {
Err(vec![crate::Error::validate_meta_name_value_not_supported(
name_value,
Expand All @@ -39,7 +37,7 @@ pub fn extract_struct_validator(
fn inner_extract_struct_validator(
attribute: &syn::Attribute,
meta_list: &syn::MetaList,
) -> 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
27 changes: 21 additions & 6 deletions serde_valid_derive/src/attribute/struct_validate/meta/meta_list.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
use crate::attribute::{
common::message_format::MessageFormat,
struct_validate::generic::extract_generic_struct_custom_validator_from_meta_list,
MetaListStructValidation, Validator,
use syn::spanned::Spanned;

use crate::{
attribute::{
common::message_format::MessageFormat,
struct_validate::generic::extract_generic_struct_custom_validator_from_meta_list,
MetaListStructValidation, Validator,
},
warning::{Warning, WithWarnings},
};

pub fn extract_struct_validator_from_meta_list(
validation_type: MetaListStructValidation,
validation: &syn::MetaList,
message_format: MessageFormat,
) -> Result<Validator, crate::Errors> {
) -> Result<WithWarnings<Validator>, crate::Errors> {
match validation_type {
MetaListStructValidation::Custom => {
extract_generic_struct_custom_validator_from_meta_list(validation, message_format)
extract_generic_struct_custom_validator_from_meta_list(validation, message_format).map(
|validator| {
WithWarnings::new_with_warnings(
validator,
vec![Warning::new_custom_meta_list_deprecated(
validation.path.get_ident().unwrap(),
validation.span(),
)],
)
},
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
use crate::attribute::{
common::message_format::MessageFormat,
struct_validate::generic::extract_generic_struct_custom_validator_from_meta_name_value,
MetaNameValueStructValidation, Validator,
use crate::{
attribute::{
common::message_format::MessageFormat,
struct_validate::generic::extract_generic_struct_custom_validator_from_meta_name_value,
MetaNameValueStructValidation, Validator,
},
warning::WithWarnings,
};

#[inline]
pub fn extract_struct_validator_from_meta_name_value(
validation_type: MetaNameValueStructValidation,
validation: &syn::MetaNameValue,
message_format: MessageFormat,
) -> Result<Validator, crate::Errors> {
) -> Result<WithWarnings<Validator>, crate::Errors> {
match validation_type {
MetaNameValueStructValidation::Custom => {
extract_generic_struct_custom_validator_from_meta_name_value(validation, message_format)
}
}
.map(WithWarnings::new)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::attribute::{
common::message_format::MessageFormat, MetaPathStructValidation, Validator,
use crate::{
attribute::{common::message_format::MessageFormat, MetaPathStructValidation, Validator},
warning::WithWarnings,
};

#[inline]
pub fn extract_struct_validator_from_meta_path(
validation_type: MetaPathStructValidation,
_validation: &syn::Path,
_message_format: MessageFormat,
) -> Result<Validator, crate::Errors> {
) -> Result<WithWarnings<Validator>, crate::Errors> {
match validation_type {}
}
15 changes: 11 additions & 4 deletions serde_valid_derive/src/attribute/variant_validate.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
mod meta;

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

use self::meta::extract_variant_validator;

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

let validations = attributes
.iter()
.filter_map(|attribute| {
if attribute.path().is_ident("validate") {
match extract_variant_validator(attribute) {
Ok(validator) => Some(validator),
Ok(validator) => {
warnings.extend(validator.warnings);
Some(validator.data)
}
Err(validator_error) => {
errors.extend(validator_error);
None
Expand All @@ -27,7 +31,10 @@ pub fn collect_variant_custom_from_variant(
.collect::<Vec<_>>();

if errors.is_empty() {
Ok(Validator::from_iter(validations))
Ok(WithWarnings::new_with_warnings(
Validator::from_iter(validations),
warnings,
))
} else {
Err(errors)
}
Expand Down
9 changes: 6 additions & 3 deletions serde_valid_derive/src/attribute/variant_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,9 +20,11 @@ use self::{
meta_path::extract_variant_validator_from_meta_path,
};

pub fn extract_variant_validator(attribute: &syn::Attribute) -> Result<Validator, crate::Errors> {
pub fn extract_variant_validator(
attribute: &syn::Attribute,
) -> Result<WithWarnings<Validator>, crate::Errors> {
match &attribute.meta {
syn::Meta::Path(_) => Ok(quote!()),
syn::Meta::Path(_) => Ok(WithWarnings::new(quote!())),
syn::Meta::List(list) => inner_extract_variant_validator(attribute, list),
syn::Meta::NameValue(name_value) => {
Err(vec![crate::Error::validate_meta_name_value_not_supported(
Expand All @@ -34,7 +37,7 @@ pub fn extract_variant_validator(attribute: &syn::Attribute) -> Result<Validator
fn inner_extract_variant_validator(
attribute: &syn::Attribute,
meta_list: &syn::MetaList,
) -> 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
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
use crate::attribute::{
common::message_format::MessageFormat,
struct_validate::generic::extract_generic_struct_custom_validator_from_meta_list,
MetaListStructValidation, Validator,
use syn::spanned::Spanned;

use crate::{
attribute::{
common::message_format::MessageFormat,
struct_validate::generic::extract_generic_struct_custom_validator_from_meta_list,
MetaListStructValidation, Validator,
},
warning::{Warning, WithWarnings},
};

pub fn extract_variant_validator_from_meta_list(
validation_type: MetaListStructValidation,
validation: &syn::MetaList,
message_format: MessageFormat,
) -> Result<Validator, crate::Errors> {
) -> Result<WithWarnings<Validator>, crate::Errors> {
match validation_type {
MetaListStructValidation::Custom => {
extract_generic_struct_custom_validator_from_meta_list(validation, message_format)
extract_generic_struct_custom_validator_from_meta_list(validation, message_format).map(
|validator| {
WithWarnings::new_with_warnings(
validator,
vec![Warning::new_custom_meta_list_deprecated(
validation.path.get_ident().unwrap(),
validation.span(),
)],
)
},
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
use crate::attribute::{
common::message_format::MessageFormat,
struct_validate::generic::extract_generic_struct_custom_validator_from_meta_name_value,
MetaNameValueStructValidation, Validator,
use crate::{
attribute::{
common::message_format::MessageFormat,
struct_validate::generic::extract_generic_struct_custom_validator_from_meta_name_value,
MetaNameValueStructValidation, Validator,
},
warning::WithWarnings,
};

#[inline]
pub fn extract_variant_validator_from_meta_name_value(
validation_type: MetaNameValueStructValidation,
validation: &syn::MetaNameValue,
message_format: MessageFormat,
) -> Result<Validator, crate::Errors> {
) -> Result<WithWarnings<Validator>, crate::Errors> {
match validation_type {
MetaNameValueStructValidation::Custom => {
extract_generic_struct_custom_validator_from_meta_name_value(validation, message_format)
}
}
.map(WithWarnings::new)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::attribute::{
common::message_format::MessageFormat, MetaPathStructValidation, Validator,
use crate::{
attribute::{common::message_format::MessageFormat, MetaPathStructValidation, Validator},
warning::WithWarnings,
};

#[inline]
pub fn extract_variant_validator_from_meta_path(
validation_type: MetaPathStructValidation,
_validation: &syn::Path,
_message_format: MessageFormat,
) -> Result<Validator, crate::Errors> {
) -> Result<WithWarnings<Validator>, crate::Errors> {
match validation_type {}
}
14 changes: 10 additions & 4 deletions serde_valid_derive/src/derive/enum_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn expand_enum_variant_named_fields_validation(
rule_fields,
WithWarnings {
data: rules,
warnings,
mut warnings,
},
) = match collect_rules_from_named_struct(&variant.ident, &variant.attrs) {
Ok(field_rules) => field_rules,
Expand All @@ -113,7 +113,10 @@ fn expand_enum_variant_named_fields_validation(
};

let enum_validates = match collect_variant_custom_from_variant(&input.attrs) {
Ok(validations) => TokenStream::from_iter(validations),
Ok(validations) => {
warnings.extend(validations.warnings);
TokenStream::from_iter(validations.data)
}
Err(rule_errors) => {
errors.extend(rule_errors);
quote!()
Expand Down Expand Up @@ -184,7 +187,7 @@ fn expand_enum_variant_unnamed_fields_varidation(
rule_fields,
WithWarnings {
data: rules,
warnings,
mut warnings,
},
) = match collect_rules_from_unnamed_struct(&variant.ident, &variant.attrs) {
Ok(field_rules) => field_rules,
Expand All @@ -195,7 +198,10 @@ fn expand_enum_variant_unnamed_fields_varidation(
};

let enum_validates = match collect_variant_custom_from_variant(&input.attrs) {
Ok(validations) => TokenStream::from_iter(validations),
Ok(validations) => {
warnings.extend(validations.warnings);
TokenStream::from_iter(validations.data)
}
Err(rule_errors) => {
errors.extend(rule_errors);
quote!()
Expand Down
15 changes: 14 additions & 1 deletion serde_valid_derive/src/warning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ impl<T> WithWarnings<T> {
}
}

pub fn new_with_warnings(data: T, warnings: Vec<Warning>) -> Self {
Self { data, warnings }
}

pub fn from_iter(data: impl IntoIterator<Item = WithWarnings<T>>) -> WithWarnings<Vec<T>> {
let mut warnings = vec![];
let data = data
Expand Down Expand Up @@ -103,7 +107,7 @@ impl Warning {
}
}

pub fn new_enumerate_path_deprecated(ident: &syn::Ident, span: Span) -> Self {
pub fn new_enumerate_meta_list_deprecated(ident: &syn::Ident, span: Span) -> Self {
Self::Deprecated {
ident: ident.clone(),
note: "#[validate(enumerate(...))] is deprecated, please use #[validate(enumerate = [...])] instead."
Expand All @@ -112,6 +116,15 @@ impl Warning {
}
}

pub fn new_custom_meta_list_deprecated(ident: &syn::Ident, span: Span) -> Self {
Self::Deprecated {
ident: ident.clone(),
note: "#[validate(custom(...))] is deprecated, please use #[validate(custom = ...)] instead."
.to_string(),
span,
}
}

pub fn add_index(&self, index: usize) -> Self {
match self {
Self::Deprecated { ident, note, span } => Self::Deprecated {
Expand Down

0 comments on commit bb983b6

Please sign in to comment.