Skip to content

Commit

Permalink
refactor: extract logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
yassun7010 committed Jan 8, 2024
1 parent 484bb5a commit 21ed798
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 21 deletions.
21 changes: 9 additions & 12 deletions serde_valid_derive/src/field_validate/common/custom_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,11 @@ fn extract_custom_message_tokens_from_meta_list(

match custom_message_type {
MetaListCustomMessage::MessageFn => {
get_message_fn_from_nested_meta(path, &message_fn_define)
.map(CustomMessageToken::new_message_fn)
get_message_fn(path, &message_fn_define).map(CustomMessageToken::new_message_fn)
}
#[cfg(feature = "fluent")]
message_type @ (MetaListCustomMessage::I18n | MetaListCustomMessage::Fluent) => {
get_fluent_message_from_nested_meta(message_type, path, &message_fn_define)
get_fluent_message(message_type, path, &message_fn_define)
.map(CustomMessageToken::new_fluent_message)
}
}
Expand All @@ -125,12 +124,12 @@ fn extract_custom_message_tokens_from_name_value(
) -> Result<CustomMessageToken, crate::Errors> {
match custom_message_type {
MetaNameValueCustomMessage::Message => {
get_message_from_expr(&name_value.value).map(CustomMessageToken::new_message_fn)
get_message(&name_value.value).map(CustomMessageToken::new_message_fn)
}
}
}

fn get_message_fn_from_nested_meta(
fn get_message_fn(
path: &syn::Path,
fn_define: &CommaSeparatedNestedMetas,
) -> Result<TokenStream, crate::Errors> {
Expand All @@ -151,19 +150,17 @@ fn get_message_fn_from_nested_meta(
}
}

fn get_message_from_expr(expr: &syn::Expr) -> Result<TokenStream, crate::Errors> {
fn get_message(expr: &syn::Expr) -> Result<TokenStream, crate::Errors> {
match expr {
syn::Expr::Lit(lit) => get_message_from_lit(&lit.lit),
syn::Expr::Lit(lit) => {
get_str(&lit.lit).map(|lit_str| quote!(|_| { #lit_str.to_string() }))
}
_ => Err(vec![crate::Error::literal_only(expr)]),
}
}

fn get_message_from_lit(lit: &syn::Lit) -> Result<TokenStream, crate::Errors> {
get_str(lit).map(|lit_str| quote!(|_| { #lit_str.to_string() }))
}

#[cfg(feature = "fluent")]
fn get_fluent_message_from_nested_meta(
fn get_fluent_message(
message_type: &MetaListCustomMessage,
path: &syn::Path,
fn_define: &CommaSeparatedNestedMetas,
Expand Down
12 changes: 6 additions & 6 deletions serde_valid_derive/src/struct_validate/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use quote::quote;
use std::str::FromStr;

use self::{
meta_list::extract_struct_validator_from_nested_meta_list,
meta_name_value::extract_struct_validator_from_nested_meta_name_value,
meta_path::extract_struct_validator_from_nested_meta_path,
meta_list::extract_struct_validator_from_meta_list,
meta_name_value::extract_struct_validator_from_meta_name_value,
meta_path::extract_struct_validator_from_meta_path,
};

pub fn extract_struct_validator(attribute: &syn::Attribute) -> Result<Validator, crate::Errors> {
Expand Down Expand Up @@ -70,15 +70,15 @@ fn inner_extract_struct_validator(
meta,
) {
(Ok(validation_type), _, _, syn::Meta::Path(validation)) => {
extract_struct_validator_from_nested_meta_path(validation_type, validation)
extract_struct_validator_from_meta_path(validation_type, validation)
}

(_, Ok(validation_type), _, syn::Meta::List(validation)) => {
extract_struct_validator_from_nested_meta_list(validation_type, validation)
extract_struct_validator_from_meta_list(validation_type, validation)
}

(_, _, Ok(validation_type), syn::Meta::NameValue(validation)) => {
extract_struct_validator_from_nested_meta_name_value(validation_type, validation)
extract_struct_validator_from_meta_name_value(validation_type, validation)
}

(Ok(_), _, _, _) => Err(vec![crate::Error::meta_path_validation_need_value(
Expand Down
2 changes: 1 addition & 1 deletion serde_valid_derive/src/struct_validate/meta/meta_list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::field_validate::{MetaListStructValidation, Validator};
use crate::struct_validate::generic::extract_generic_struct_custom_validator;

pub fn extract_struct_validator_from_nested_meta_list(
pub fn extract_struct_validator_from_meta_list(
validation_type: MetaListStructValidation,
validation: &syn::MetaList,
) -> Result<Validator, crate::Errors> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::field_validate::{MetaNameValueStructValidation, Validator};

#[inline]
pub fn extract_struct_validator_from_nested_meta_name_value(
pub fn extract_struct_validator_from_meta_name_value(
validation_type: MetaNameValueStructValidation,
_validation: &syn::MetaNameValue,
) -> Result<Validator, crate::Errors> {
Expand Down
2 changes: 1 addition & 1 deletion serde_valid_derive/src/struct_validate/meta/meta_path.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::field_validate::{MetaPathStructValidation, Validator};

#[inline]
pub fn extract_struct_validator_from_nested_meta_path(
pub fn extract_struct_validator_from_meta_path(
validation_type: MetaPathStructValidation,
_validation: &syn::Path,
) -> Result<Validator, crate::Errors> {
Expand Down

0 comments on commit 21ed798

Please sign in to comment.