Skip to content

Commit

Permalink
refactor: custom message.x
Browse files Browse the repository at this point in the history
  • Loading branch information
yassun7010 committed Jan 6, 2024
1 parent 6bf3e30 commit de78c46
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 70 deletions.
24 changes: 16 additions & 8 deletions serde_valid_derive/src/error.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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::<Vec<_>>();

let filterd_candidates =
did_you_mean(unknown, candidates).unwrap_or_else(|| candidates.to_vec());

Expand All @@ -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::<Vec<_>>();

let filterd_candidates =
did_you_mean(unknown, candidates).unwrap_or_else(|| candidates.to_vec());

Expand Down
8 changes: 4 additions & 4 deletions serde_valid_derive/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
65 changes: 11 additions & 54 deletions serde_valid_derive/src/validate/common/custom_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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::<Vec<_>>(),
)]),
}
}

fn extract_custom_message_tokens_from_meta_list(
custom_message_type: &MetaListCustomMessage,
meta_list: &syn::MetaList,
) -> Result<CustomMessageToken, crate::Errors> {
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::<Vec<_>>(),
)
};
Err(vec![error])
}
}
}

fn extract_custom_message_tokens_from_name_value(
custom_message_type: &MetaNameValueCustomMessage,
name_value: &syn::MetaNameValue,
) -> Result<CustomMessageToken, crate::Errors> {
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::<Vec<_>>(),
))
}
.map_err(|error| vec![error]),
}
}

Expand Down
4 changes: 0 additions & 4 deletions serde_valid_derive/src/validate/meta/meta_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>(),
)]),
};

Expand Down

0 comments on commit de78c46

Please sign in to comment.