From de78c46f1ea36e07183d95e95b2261a8f95147c8 Mon Sep 17 00:00:00 2001 From: yassun7010 Date: Sun, 7 Jan 2024 00:56:49 +0900 Subject: [PATCH] refactor: custom message.x --- serde_valid_derive/src/error.rs | 24 ++++--- serde_valid_derive/src/validate.rs | 8 +-- .../src/validate/common/custom_message.rs | 65 ++++--------------- .../src/validate/meta/meta_list.rs | 4 -- 4 files changed, 31 insertions(+), 70 deletions(-) diff --git a/serde_valid_derive/src/error.rs b/serde_valid_derive/src/error.rs index 0a9c882..06905a5 100644 --- a/serde_valid_derive/src/error.rs +++ b/serde_valid_derive/src/error.rs @@ -1,10 +1,12 @@ -use crate::validate::{MetaListValidation, MetaNameValueValidation, MetaPathValidation}; +use crate::validate::{ + MetaListValidation, MetaNameValueCustomMessage, MetaNameValueValidation, MetaPathCustomMessage, + MetaPathValidation, +}; use proc_macro2::TokenStream; use quote::quote; use syn::spanned::Spanned; use crate::types::CommaSeparatedNestedMetas; -#[cfg(feature = "fluent")] use crate::validate::MetaListCustomMessage; pub fn object_errors_tokens() -> TokenStream { @@ -283,7 +285,12 @@ impl Error { ) } - pub fn unknown_validation_type(path: &syn::Path, unknown: &str, candidates: &[&str]) -> Self { + pub fn unknown_validation_type(path: &syn::Path, unknown: &str) -> Self { + let candidates = &(MetaPathValidation::iter().map(|x| x.name())) + .chain(MetaListValidation::iter().map(|x| x.name())) + .chain(MetaNameValueValidation::iter().map(|x| x.name())) + .collect::>(); + let filterd_candidates = did_you_mean(unknown, candidates).unwrap_or_else(|| candidates.to_vec()); @@ -293,11 +300,12 @@ impl Error { ) } - pub fn unknown_custom_message_type( - path: &syn::Path, - unknown: &str, - candidates: &[&str], - ) -> Self { + pub fn unknown_custom_message_type(path: &syn::Path, unknown: &str) -> Self { + let candidates = &(MetaPathCustomMessage::iter().map(|x| x.name())) + .chain(MetaListCustomMessage::iter().map(|x| x.name())) + .chain(MetaNameValueCustomMessage::iter().map(|x| x.name())) + .collect::>(); + let filterd_candidates = did_you_mean(unknown, candidates).unwrap_or_else(|| candidates.to_vec()); diff --git a/serde_valid_derive/src/validate.rs b/serde_valid_derive/src/validate.rs index 0cc7494..3612c88 100644 --- a/serde_valid_derive/src/validate.rs +++ b/serde_valid_derive/src/validate.rs @@ -7,10 +7,10 @@ mod numeric; mod object; mod string; -pub use common::{MetaListValidation, MetaNameValueValidation, MetaPathValidation}; +pub use common::{ + MetaListCustomMessage, MetaListValidation, MetaNameValueCustomMessage, MetaNameValueValidation, + MetaPathCustomMessage, MetaPathValidation, +}; pub use field::{FieldValidators, Validator}; pub use meta::extract_meta_validator; - -#[cfg(feature = "fluent")] -pub use common::MetaListCustomMessage; diff --git a/serde_valid_derive/src/validate/common/custom_message.rs b/serde_valid_derive/src/validate/common/custom_message.rs index 028cceb..8d15eda 100644 --- a/serde_valid_derive/src/validate/common/custom_message.rs +++ b/serde_valid_derive/src/validate/common/custom_message.rs @@ -69,11 +69,11 @@ pub fn extract_custom_message_tokens( (Ok(_), _, _, syn::Meta::Path(_)) => { unreachable!() } - (_, Ok(_), _, syn::Meta::List(custom_message)) => { - extract_custom_message_tokens_from_meta_list(custom_message) + (_, Ok(custom_message_type), _, syn::Meta::List(custom_message)) => { + extract_custom_message_tokens_from_meta_list(&custom_message_type, custom_message) } - (_, _, Ok(_), syn::Meta::NameValue(custom_message)) => { - extract_custom_message_tokens_from_name_value(custom_message) + (_, _, Ok(custom_message_type), syn::Meta::NameValue(custom_message)) => { + extract_custom_message_tokens_from_name_value(&custom_message_type, custom_message) } (Ok(_), _, _, _) => Err(vec![crate::Error::meta_path_custom_message_need_value( custom_message_path, @@ -92,84 +92,41 @@ pub fn extract_custom_message_tokens( _ => Err(vec![crate::Error::unknown_custom_message_type( custom_message_path, &custom_message_name, - &(MetaPathCustomMessage::iter().map(|x| x.name())) - .chain(MetaListCustomMessage::iter().map(|x| x.name())) - .chain(MetaNameValueCustomMessage::iter().map(|x| x.name())) - .collect::>(), )]), } } fn extract_custom_message_tokens_from_meta_list( + custom_message_type: &MetaListCustomMessage, meta_list: &syn::MetaList, ) -> Result { let path = &meta_list.path; let path_ident = SingleIdentPath::new(path).ident(); - let path_label = path_ident.to_string(); 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)])?; - match MetaListCustomMessage::from_str(&path_label) { - Ok(MetaListCustomMessage::MessageFn) => { + match custom_message_type { + MetaListCustomMessage::MessageFn => { get_message_fn_from_nested_meta(path, &message_fn_define) .map(CustomMessageToken::new_message_fn) } #[cfg(feature = "fluent")] - Ok(ref message_type @ (MetaListCustomMessage::I18n | MetaListCustomMessage::Fluent)) => { + message_type @ (MetaListCustomMessage::I18n | MetaListCustomMessage::Fluent) => { get_fluent_message_from_nested_meta(message_type, path, &message_fn_define) .map(CustomMessageToken::new_fluent_message) } - Err(unknown) => { - let error = if MetaNameValueCustomMessage::from_str(&path_label).is_ok() { - crate::Error::meta_list_validation_need_value(path, &path_label) - } else if MetaPathCustomMessage::from_str(&path_label).is_ok() { - crate::Error::meta_path_validation_need_value(path, &path_label) - } else { - crate::Error::unknown_validation_type( - path, - &unknown, - &MetaListCustomMessage::iter() - .map(|x| x.name()) - .collect::>(), - ) - }; - Err(vec![error]) - } } } fn extract_custom_message_tokens_from_name_value( + custom_message_type: &MetaNameValueCustomMessage, name_value: &syn::MetaNameValue, ) -> Result { - let path = &name_value.path; - let path_ident = SingleIdentPath::new(path).ident(); - let path_label = path_ident.to_string(); - - match MetaNameValueCustomMessage::from_str(&path_label) { - Ok(MetaNameValueCustomMessage::Message) => { + match custom_message_type { + MetaNameValueCustomMessage::Message => { get_message_from_expr(&name_value.value).map(CustomMessageToken::new_message_fn) } - Err(unknown) => if MetaListCustomMessage::from_str(&path_label).is_ok() { - Err(crate::Error::meta_list_validation_need_value( - path, - &path_label, - )) - } else if MetaPathCustomMessage::from_str(&path_label).is_ok() { - Err(crate::Error::meta_path_validation_need_value( - path, - &path_label, - )) - } else { - Err(crate::Error::unknown_validation_type( - path, - &unknown, - &MetaNameValueCustomMessage::iter() - .map(|x| x.name()) - .collect::>(), - )) - } - .map_err(|error| vec![error]), } } diff --git a/serde_valid_derive/src/validate/meta/meta_list.rs b/serde_valid_derive/src/validate/meta/meta_list.rs index 259154f..a23d716 100644 --- a/serde_valid_derive/src/validate/meta/meta_list.rs +++ b/serde_valid_derive/src/validate/meta/meta_list.rs @@ -104,10 +104,6 @@ pub fn extract_validator_from_meta_list( _ => Err(vec![crate::Error::unknown_validation_type( validation_path, &validation_name, - &(MetaPathValidation::iter().map(|x| x.name())) - .chain(MetaListValidation::iter().map(|x| x.name())) - .chain(MetaNameValueValidation::iter().map(|x| x.name())) - .collect::>(), )]), };