From e8c423a4aeecb298a3fbe7f330cf4bee31a50fb2 Mon Sep 17 00:00:00 2001 From: yassun7010 Date: Sun, 7 Jan 2024 08:32:40 +0900 Subject: [PATCH] refactor: error message. --- serde_valid_derive/src/error.rs | 87 +++++++++++-------- .../src/rule/named_struct_rule.rs | 7 +- .../src/validate/common/custom_message.rs | 2 +- .../src/validate/generic/custom.rs | 19 ++-- .../src/validate/generic/enumerate.rs | 3 +- 5 files changed, 66 insertions(+), 52 deletions(-) diff --git a/serde_valid_derive/src/error.rs b/serde_valid_derive/src/error.rs index 06905a5..b068056 100644 --- a/serde_valid_derive/src/error.rs +++ b/serde_valid_derive/src/error.rs @@ -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 { @@ -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( @@ -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."), ) } @@ -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."), ) } @@ -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:#?}"), ) } @@ -311,37 +313,57 @@ 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.", ) } @@ -349,7 +371,7 @@ impl Error { 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()), ) } @@ -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, @@ -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.") } @@ -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 { diff --git a/serde_valid_derive/src/rule/named_struct_rule.rs b/serde_valid_derive/src/rule/named_struct_rule.rs index a9f7329..3d8bbea 100644 --- a/serde_valid_derive/src/rule/named_struct_rule.rs +++ b/serde_valid_derive/src/rule/named_struct_rule.rs @@ -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))), _ => {} } diff --git a/serde_valid_derive/src/validate/common/custom_message.rs b/serde_valid_derive/src/validate/common/custom_message.rs index 8d15eda..66c4c92 100644 --- a/serde_valid_derive/src/validate/common/custom_message.rs +++ b/serde_valid_derive/src/validate/common/custom_message.rs @@ -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 => { diff --git a/serde_valid_derive/src/validate/generic/custom.rs b/serde_valid_derive/src/validate/generic/custom.rs index 6132cca..c235c8b 100644 --- a/serde_valid_derive/src/validate/generic/custom.rs +++ b/serde_valid_derive/src/validate/generic/custom.rs @@ -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) { @@ -39,7 +42,7 @@ pub fn extract_generic_custom_validator( fn extract_custom_fn_name( nested_meta: &crate::types::NestedMeta, -) -> Result { +) -> Result { match nested_meta { crate::types::NestedMeta::Meta(meta) => match meta { syn::Meta::List(list) => { @@ -47,10 +50,10 @@ fn extract_custom_fn_name( 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)]), } } diff --git a/serde_valid_derive/src/validate/generic/enumerate.rs b/serde_valid_derive/src/validate/generic/enumerate.rs index 9d1252d..4af44cf 100644 --- a/serde_valid_derive/src/validate/generic/enumerate.rs +++ b/serde_valid_derive/src/validate/generic/enumerate.rs @@ -51,8 +51,7 @@ fn get_enumerate(meta_list: &syn::MetaList) -> Result { .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, )] })?;