Skip to content

Commit

Permalink
refactor: error message.
Browse files Browse the repository at this point in the history
  • Loading branch information
yassun7010 committed Jan 6, 2024
1 parent 92373a5 commit e8c423a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 52 deletions.
87 changes: 49 additions & 38 deletions serde_valid_derive/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::spanned::Spanned;

use crate::types::CommaSeparatedNestedMetas;
use crate::validate::MetaListCustomMessage;

pub fn object_errors_tokens() -> TokenStream {
Expand Down Expand Up @@ -174,16 +173,19 @@ impl Error {
Self::new(path.span(), "#[rule(???)] needs rule_fn.")
}

pub fn rule_allow_single_function(nested_meta: &syn::Meta) -> Self {
Self::new(nested_meta.span(), "#[rule] allow single function.")
pub fn rule_allow_single_function(meta: &syn::Meta) -> Self {
Self::new(meta.span(), "#[rule] allows single function.")
}

pub fn rule_need_arguments(path: &syn::Path) -> Self {
Self::new(path.span(), "`rule` function needs arguments.")
}

pub fn rule_args_parse_error(path: &syn::MetaList, error: &syn::Error) -> Self {
Self::new(path.span(), format!("#[rule(...)] parse error: {error}"))
pub fn rule_args_parse_error(metalist: &syn::MetaList, error: &syn::Error) -> Self {
Self::new(
metalist.span(),
format!("#[rule(???)] parse error: {error}"),
)
}

pub fn rule_allow_path_arguments(
Expand All @@ -193,7 +195,7 @@ impl Error {
let rule_fn_name = quote!(#rule_fn_name_path).to_string();
Self::new(
meta.span(),
format!("#[rule({rule_fn_name}(???, ...))] allow field path only."),
format!("#[rule({rule_fn_name}(???, ...))] allows field path only."),
)
}

Expand All @@ -204,7 +206,7 @@ impl Error {
let rule_fn_name = quote!(#rule_fn_name_path).to_string();
Self::new(
meta.span(),
format!("#[rule({rule_fn_name}(???, ...))] allow index integer only."),
format!("#[rule({rule_fn_name}(???, ...))] allows index integer only."),
)
}

Expand Down Expand Up @@ -296,7 +298,7 @@ impl Error {

Self::new(
path.span(),
format!("Unknown: `{unknown}` validation type. Is it one of the following?\n{filterd_candidates:#?}"),
format!("`{unknown}` is unknown validation type. Is it one of the following?\n{filterd_candidates:#?}"),
)
}

Expand All @@ -311,45 +313,65 @@ impl Error {

Self::new(
path.span(),
format!("Unknown: `{unknown}` error message type. Is it one of the following?\n{filterd_candidates:#?}"),
format!("`{unknown}` is unkown error message type. Is it one of the following?\n{filterd_candidates:#?}"),
)
}

pub fn validate_enumerate_parse_error(path: &syn::Path, error: &syn::Error) -> Self {
pub fn validate_enumerate_parse_error(metalist: &syn::MetaList, error: &syn::Error) -> Self {
Self::new(
path.span(),
format!("#[validate(enumerate(...)] parse error: {error}"),
metalist.span(),
format!("#[validate(enumerate(???))] parse error: {error}"),
)
}

pub fn validate_enumerate_need_item(path: &syn::Path) -> Self {
Self::new(path.span(), "`enumerate` need items.")
Self::new(path.span(), "#[validate(enumerate(???))] needs items.")
}

pub fn validate_custom_need_item(path: &syn::Path) -> Self {
Self::new(path.span(), "`custom` need items.")
pub fn validate_custom_need_function(path: &syn::Path) -> Self {
Self::new(path.span(), "#[validate(custom(???))] needs function.")
}

pub fn validate_custom_tail_error(nested: &CommaSeparatedNestedMetas) -> Self {
Self::new(nested.span(), "`custom` support only 1 item.")
pub fn validate_custom_tail_error(nested: &crate::types::NestedMeta) -> Self {
Self::new(
nested.span(),
"#[validate(custom(???)] supports only 1 item.",
)
}

pub fn custom_message_parse_error(ident: &syn::Ident, error: &syn::Error) -> Self {
Self::new(
ident.span(),
format!("#[validate(..., {ident})] parse error: {error}"),
)
}

pub fn message_fn_need_item(path: &syn::Path) -> Self {
Self::new(path.span(), "`message_fn` need items.")
Self::new(
path.span(),
"#[validate(..., message_fn(???))] needs function.",
)
}

pub fn message_fn_parse_error(ident: &syn::Ident, error: &syn::Error) -> Self {
pub fn message_fn_allow_name_path(nested_meta: &crate::types::NestedMeta) -> Self {
Self::new(
ident.span(),
format!("#[validate(..., {ident})] parse error: {error}"),
nested_meta.span(),
"#[validate(..., message_fn(???))] allows only function name path.",
)
}

pub fn message_fn_tail_error(nested_meta: &crate::types::NestedMeta) -> Self {
Self::new(
nested_meta.span(),
"#[validate(..., message_fn(???))] allows only 1 item.",
)
}

#[cfg(feature = "fluent")]
pub fn fluent_need_item(message_type: &MetaListCustomMessage, path: &syn::Path) -> Self {
Self::new(
path.span(),
format!("`{}` need items.", message_type.name()),
format!("`{}` needs items.", message_type.name()),
)
}

Expand All @@ -361,19 +383,12 @@ impl Error {
Self::new(
nested_meta.span(),
format!(
"#[validate(..., {}(???, ...))] allow only fluent key str",
"#[validate(..., {}(???, ...))] allows only fluent key str",
message_type.name()
),
)
}

pub fn message_fn_allow_name_path(nested_meta: &crate::types::NestedMeta) -> Self {
Self::new(
nested_meta.span(),
"#[validate(..., message_fn(???))] allow only function name path.",
)
}

#[cfg(feature = "fluent")]
pub fn fluent_allow_args(
message_type: &MetaListCustomMessage,
Expand All @@ -382,16 +397,12 @@ impl Error {
Self::new(
nested_meta.span(),
format!(
"#[validate(..., {}(..., ???))] allow only fluent args key value.",
"#[validate(..., {}(..., ???))] allows only fluent args key value.",
message_type.name()
),
)
}

pub fn message_fn_tail_error(nested_meta: &crate::types::NestedMeta) -> Self {
Self::new(nested_meta.span(), "`message_fn` support only 1 item.")
}

pub fn literal_only(expr: &syn::Expr) -> Self {
Self::new(expr.span(), "Allow literal only.")
}
Expand All @@ -409,15 +420,15 @@ impl Error {
}

pub fn literal_not_support(lit: &syn::Lit) -> Self {
Self::new(lit.span(), "Literal does not support.")
Self::new(lit.span(), "Literal not support.")
}

pub fn meta_name_value_not_support(name_value: &syn::MetaNameValue) -> Self {
Self::new(name_value.span(), "Name value does not support.")
Self::new(name_value.span(), "Name value not support.")
}

pub fn meta_path_not_support(path: &syn::Path) -> Self {
Self::new(path.span(), "Path does not support.")
Self::new(path.span(), "Path not support.")
}

pub fn too_many_list_items(nested_meta: &syn::Meta) -> Self {
Expand Down
7 changes: 4 additions & 3 deletions serde_valid_derive/src/rule/named_struct_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ fn collect_rule(

match nested.len() {
0 => Err(vec![crate::Error::rule_need_function(&metalist.path)])?,
2.. => nested.iter().skip(1).for_each(|nested_meta| {
errors.push(crate::Error::rule_allow_single_function(nested_meta))
}),
2.. => nested
.iter()
.skip(1)
.for_each(|error| errors.push(crate::Error::rule_allow_single_function(error))),
_ => {}
}

Expand Down
2 changes: 1 addition & 1 deletion serde_valid_derive/src/validate/common/custom_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn extract_custom_message_tokens_from_meta_list(
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::message_fn_parse_error(path_ident, &error)])?;
.map_err(|error| vec![crate::Error::custom_message_parse_error(path_ident, &error)])?;

match custom_message_type {
MetaListCustomMessage::MessageFn => {
Expand Down
19 changes: 11 additions & 8 deletions serde_valid_derive/src/validate/generic/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ pub fn extract_generic_custom_validator(
let errors = field.errors_variable();
let nested = meta_list
.parse_args_with(CommaSeparatedNestedMetas::parse_terminated)
.map_err(|error| vec![crate::Error::message_fn_parse_error(path_ident, &error)])?;
.map_err(|error| vec![crate::Error::custom_message_parse_error(path_ident, &error)])?;

let custom_fn_name = match nested.len() {
0 => Err(crate::Error::validate_custom_need_item(path)),
0 => Err(vec![crate::Error::validate_custom_need_function(path)]),
1 => extract_custom_fn_name(&nested[0]),
_ => Err(crate::Error::validate_custom_tail_error(&nested)),
}
.map_err(|error| vec![error])?;
_ => Err(nested
.iter()
.skip(1)
.map(crate::Error::validate_custom_tail_error)
.collect()),
}?;

Ok(quote!(
if let Err(__error) = #custom_fn_name(#field_ident) {
Expand All @@ -39,18 +42,18 @@ pub fn extract_generic_custom_validator(

fn extract_custom_fn_name(
nested_meta: &crate::types::NestedMeta,
) -> Result<TokenStream, crate::Error> {
) -> Result<TokenStream, crate::Errors> {
match nested_meta {
crate::types::NestedMeta::Meta(meta) => match meta {
syn::Meta::List(list) => {
let fn_name = &list.path;
Ok(quote!(#fn_name))
}
syn::Meta::NameValue(name_value) => {
Err(crate::Error::meta_name_value_not_support(name_value))
Err(vec![crate::Error::meta_name_value_not_support(name_value)])
}
syn::Meta::Path(fn_name) => Ok(quote!(#fn_name)),
},
crate::types::NestedMeta::Lit(lit) => Err(crate::Error::literal_not_support(lit)),
crate::types::NestedMeta::Lit(lit) => Err(vec![crate::Error::literal_not_support(lit)]),
}
}
3 changes: 1 addition & 2 deletions serde_valid_derive/src/validate/generic/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ fn get_enumerate(meta_list: &syn::MetaList) -> Result<Lits, crate::Errors> {
.parse_args_with(crate::types::CommaSeparatedNestedMetas::parse_terminated)
.map_err(|error| {
vec![crate::Error::validate_enumerate_parse_error(
&meta_list.path,
&error,
meta_list, &error,
)]
})?;

Expand Down

0 comments on commit e8c423a

Please sign in to comment.