Skip to content

Commit

Permalink
feat: add deprecated warning.
Browse files Browse the repository at this point in the history
  • Loading branch information
yassun7010 committed Jun 22, 2024
1 parent 815c6ce commit d5ff2cd
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 31 deletions.
41 changes: 28 additions & 13 deletions serde_valid_derive/src/attribute/common/message_format.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::attribute::{MetaListCustomMessage, MetaNameValueCustomMessage, MetaPathCustomMessage};
use crate::types::{CommaSeparatedNestedMetas, NestedMeta, SingleIdentPath};
use crate::warning::{Warning, WithWarnings};
use proc_macro2::TokenStream;
use quote::quote;
use std::str::FromStr;
use syn::spanned::Spanned;

use super::lit::get_str;
pub type MessageFormat = TokenStream;
Expand All @@ -11,7 +13,9 @@ pub fn default_message_format() -> MessageFormat {
quote!(::serde_valid::validation::error::Format::Default)
}

pub fn extract_custom_message_format(meta: &syn::Meta) -> Result<MessageFormat, crate::Errors> {
pub fn extract_custom_message_format(
meta: &syn::Meta,
) -> Result<WithWarnings<MessageFormat>, crate::Errors> {
let custom_message_path = match meta {
syn::Meta::Path(path) => path,
syn::Meta::List(list) => &list.path,
Expand Down Expand Up @@ -60,15 +64,24 @@ pub fn extract_custom_message_format(meta: &syn::Meta) -> Result<MessageFormat,
fn extract_custom_message_format_from_meta_list(
custom_message_type: &MetaListCustomMessage,
meta_list: &syn::MetaList,
) -> Result<MessageFormat, crate::Errors> {
) -> Result<WithWarnings<MessageFormat>, crate::Errors> {
let path = &meta_list.path;
let path_ident = SingleIdentPath::new(path).ident();
let message_fn_define = meta_list
.parse_args_with(CommaSeparatedNestedMetas::parse_terminated)
.map_err(|error| vec![crate::Error::custom_message_parse_error(path_ident, &error)])?;

match custom_message_type {
MetaListCustomMessage::MessageFn => get_message_fn_from_meta_list(path, &message_fn_define),
MetaListCustomMessage::MessageFn => get_message_fn_from_meta_list(path, &message_fn_define)
.map(|message_fn| {
WithWarnings::new_with_warnings(
message_fn,
vec![Warning::new_message_fn_list_deprecated(
path_ident,
path.span(),
)],
)
}),
#[cfg(feature = "fluent")]
message_type @ (MetaListCustomMessage::I18n | MetaListCustomMessage::Fluent) => {
get_fluent_message(message_type, path, &message_fn_define)
Expand All @@ -79,7 +92,7 @@ fn extract_custom_message_format_from_meta_list(
fn extract_custom_message_format_from_name_value(
custom_message_type: &MetaNameValueCustomMessage,
name_value: &syn::MetaNameValue,
) -> Result<MessageFormat, crate::Errors> {
) -> Result<WithWarnings<MessageFormat>, crate::Errors> {
match custom_message_type {
MetaNameValueCustomMessage::Message => get_message(&name_value.value),
MetaNameValueCustomMessage::MessageFn => get_message_fn_from_meta_name_value(name_value),
Expand Down Expand Up @@ -113,7 +126,7 @@ fn get_message_fn_from_meta_list(

fn get_message_fn_from_meta_name_value(
meta_name_value: &syn::MetaNameValue,
) -> Result<TokenStream, crate::Errors> {
) -> Result<WithWarnings<MessageFormat>, crate::Errors> {
let fn_define = match &meta_name_value.value {
syn::Expr::Path(syn::ExprPath { path, .. }) => quote!(#path),
syn::Expr::Call(call) => quote!(#call),
Expand All @@ -123,13 +136,15 @@ fn get_message_fn_from_meta_name_value(
])?,
};

Ok(quote!(::serde_valid::validation::error::Format::MessageFn(#fn_define)))
Ok(WithWarnings::new(
quote!(::serde_valid::validation::error::Format::MessageFn(#fn_define)),
))
}

fn get_message(expr: &syn::Expr) -> Result<TokenStream, crate::Errors> {
fn get_message(expr: &syn::Expr) -> Result<WithWarnings<MessageFormat>, crate::Errors> {
match expr {
syn::Expr::Lit(lit) => {
get_str(&lit.lit).map(|lit_str| quote!(::serde_valid::validation::error::Format::Message(#lit_str.to_string())))
get_str(&lit.lit).map(|lit_str| quote!(::serde_valid::validation::error::Format::Message(#lit_str.to_string()))).map(WithWarnings::new)
}
_ => Err(vec![crate::Error::literal_only(expr)]),
}
Expand All @@ -140,7 +155,7 @@ fn get_fluent_message(
message_type: &MetaListCustomMessage,
path: &syn::Path,
fn_define: &CommaSeparatedNestedMetas,
) -> Result<MessageFormat, crate::Errors> {
) -> Result<WithWarnings<MessageFormat>, crate::Errors> {
use quote::ToTokens;

use crate::types::CommaSeparatedTokenStreams;
Expand All @@ -151,14 +166,14 @@ fn get_fluent_message(
let id = get_fluent_id(&fn_define[0])
.ok_or_else(|| vec![crate::Error::fluent_allow_key(message_type, &fn_define[0])])?;

Ok(quote!(
Ok(WithWarnings::new(quote!(
::serde_valid::validation::error::Format::Fluent(
::serde_valid::fluent::Message{
id: #id,
args: vec![]
}
)
))
)))
}
_ => {
let mut errors = vec![];
Expand All @@ -180,14 +195,14 @@ fn get_fluent_message(
})
.collect::<CommaSeparatedTokenStreams>();
if errors.is_empty() {
Ok(quote!(
Ok(WithWarnings::new(quote!(
::serde_valid::validation::error::Format::Fluent(
::serde_valid::fluent::Message{
id: #id,
args: vec![#args]
}
)
))
)))
} else {
Err(errors)
}
Expand Down
18 changes: 14 additions & 4 deletions serde_valid_derive/src/attribute/field_validate/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ fn inner_extract_field_validator(
)]
})?;

let message_format = match nested.len() {
let WithWarnings {
data: message_format,
mut warnings,
} = match nested.len() {
0 => Err(vec![crate::Error::field_validation_type_required(
attribute,
)])?,
Expand Down Expand Up @@ -78,7 +81,7 @@ fn inner_extract_field_validator(
None
}
}
.unwrap_or_else(default_message_format);
.unwrap_or_else(|| WithWarnings::new(default_message_format()));

let meta = &nested[0];

Expand Down Expand Up @@ -146,12 +149,19 @@ fn inner_extract_field_validator(
validation_path,
&validation_name,
)]),
};
}
.map(|validator| {
warnings.extend(validator.warnings);
validator.data
});

match validator {
Ok(validator) => {
if errors.is_empty() {
Ok(validator)
Ok(WithWarnings {
data: validator,
warnings,
})
} else {
Err(errors)
}
Expand Down
18 changes: 14 additions & 4 deletions serde_valid_derive/src/attribute/struct_validate/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ fn inner_extract_struct_validator(
)]
})?;

let message_format = match nested.len() {
let WithWarnings {
data: message_format,
mut warnings,
} = match nested.len() {
0 => Err(vec![crate::Error::struct_validation_type_required(
attribute,
)])?,
Expand Down Expand Up @@ -75,7 +78,7 @@ fn inner_extract_struct_validator(
None
}
}
.unwrap_or_else(default_message_format);
.unwrap_or_else(|| WithWarnings::new(default_message_format()));

let meta = &nested[0];
let validation_path = match meta {
Expand Down Expand Up @@ -126,12 +129,19 @@ fn inner_extract_struct_validator(
validation_path,
&validation_name,
)]),
};
}
.map(|validator| {
warnings.extend(validator.warnings);
validator.data
});

match validator {
Ok(validator) => {
if errors.is_empty() {
Ok(validator)
Ok(WithWarnings {
data: validator,
warnings,
})
} else {
Err(errors)
}
Expand Down
18 changes: 14 additions & 4 deletions serde_valid_derive/src/attribute/variant_validate/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ fn inner_extract_variant_validator(
)]
})?;

let message_format = match nested.len() {
let WithWarnings {
data: message_format,
mut warnings,
} = match nested.len() {
0 => Err(vec![crate::Error::struct_validation_type_required(
attribute,
)])?,
Expand Down Expand Up @@ -75,7 +78,7 @@ fn inner_extract_variant_validator(
None
}
}
.unwrap_or_else(default_message_format);
.unwrap_or_else(|| WithWarnings::new(default_message_format()));

let meta = &nested[0];
let validation_path = match meta {
Expand Down Expand Up @@ -126,12 +129,19 @@ fn inner_extract_variant_validator(
validation_path,
&validation_name,
)]),
};
}
.map(|validator| {
warnings.extend(validator.warnings);
validator.data
});

match validator {
Ok(validator) => {
if errors.is_empty() {
Ok(validator)
Ok(WithWarnings {
data: validator,
warnings,
})
} else {
Err(errors)
}
Expand Down
15 changes: 9 additions & 6 deletions serde_valid_derive/src/warning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ impl<T> WithWarnings<T> {
.collect::<Vec<_>>();
WithWarnings { data, warnings }
}

#[allow(unused)]
pub fn extend_warnings(&mut self, warnings: Vec<Warning>) -> &mut Self {
self.warnings.extend(warnings);
self
}
}

impl<T> From<WithWarnings<T>> for WithWarnings<Vec<T>> {
Expand Down Expand Up @@ -125,6 +119,15 @@ impl Warning {
}
}

pub fn new_message_fn_list_deprecated(ident: &syn::Ident, span: Span) -> Self {
Self::Deprecated {
ident: ident.clone(),
note: "#[validate(message_fn(...))] is deprecated, please use #[validate(message_fn = ...)] 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 d5ff2cd

Please sign in to comment.