From b364f14ef5a8cf9aaf13da54e66c897535d92694 Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Thu, 31 Oct 2024 01:47:35 +0900 Subject: [PATCH] fix not error message --- src/rule/composer/and.rs | 31 +++++++++++++++++++++++++------ src/rule/composer/or.rs | 20 +++++++++++++++++--- src/rule/number/equal.rs | 2 +- src/rule/number/even.rs | 2 +- src/rule/number/greater.rs | 2 +- src/rule/number/less.rs | 2 +- src/rule/number/min_max.rs | 6 +++--- src/rule/number/odd.rs | 2 +- 8 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/rule/composer/and.rs b/src/rule/composer/and.rs index 9a5ba7f..e50dbe1 100644 --- a/src/rule/composer/and.rs +++ b/src/rule/composer/and.rs @@ -1,6 +1,6 @@ -use std::marker::PhantomData; - +use crate::result::Error; use crate::rule::Rule; +use std::marker::PhantomData; /// A macro to generate a `Rule` that combines multiple rules /// # Example @@ -67,15 +67,33 @@ where type Item = T; fn validate(target: Self::Item) -> crate::Result { - let bounded_rule = |t: T| RULE1::validate(t).and_then(RULE2::validate); - bounded_rule(target) + match RULE1::validate(target) { + Ok(value) => RULE2::validate(value), + Err(err) => { + let rule1_error_message = err.to_string(); + let rule1_type_name = std::any::type_name::(); + match RULE2::validate(err.into_value()) { + Ok(value) => { + let message = format!("{rule1_error_message} ({rule1_type_name})"); + Err(Error::new(value, message)) + } + Err(err) => { + let rule2_type_name = std::any::type_name::(); + let message = format!( + "{rule1_error_message} ({rule1_type_name}) & {err} ({rule2_type_name})", + ); + Err(Error::new(err.into_value(), message)) + } + } + } + } } } #[cfg(test)] mod test { use crate::rule::composer::And; - use crate::rule::{AlphabetRule, EmailRule, NonEmptyStringRule, Rule}; + use crate::rule::{AlphabetRule, EmailRule, EvenRuleU8, LessRuleU8, NonEmptyStringRule, Rule}; type NonEmptyAlphabetString = And>; @@ -86,7 +104,8 @@ mod test { #[test] fn test_rule_binder_err() { - assert!(NonEmptyAlphabetString::validate("Hello1".to_string()).is_err()); + type Target = And![EvenRuleU8, LessRuleU8<10>]; + assert_eq!(Target::validate(11).unwrap_err().to_string(), "the value must be even, but received 11 (refined_type::rule::number::even::EvenRuleU8) & the value must be less than 10, but received 11 (refined_type::rule::number::less::LessRuleU8<10>)"); } #[test] diff --git a/src/rule/composer/or.rs b/src/rule/composer/or.rs index f1ce072..7cbe65e 100644 --- a/src/rule/composer/or.rs +++ b/src/rule/composer/or.rs @@ -1,6 +1,6 @@ -use std::marker::PhantomData; - +use crate::result::Error; use crate::rule::Rule; +use std::marker::PhantomData; /// A macro to generate a `Rule` that combines multiple rules /// # Example @@ -50,7 +50,21 @@ where fn validate(target: Self::Item) -> crate::Result { let bounded_rule = |t: T| match RULE1::validate(t) { Ok(value) => Ok(value), - Err(err) => RULE2::validate(err.into_value()), + Err(err) => { + let rule1_error_message = err.to_string(); + match RULE2::validate(err.into_value()) { + Ok(value) => Ok(value), + Err(err) => { + let rule1_type_name = std::any::type_name::(); + let rule2_type_name = std::any::type_name::(); + let rule2_error_message = err.to_string(); + Err(Error::new( + err.into_value(), + format!("{rule1_error_message} ({rule1_type_name}) | {rule2_error_message} ({rule2_type_name})"), + )) + } + } + } }; bounded_rule(target) } diff --git a/src/rule/number/equal.rs b/src/rule/number/equal.rs index 44e0056..bb01409 100644 --- a/src/rule/number/equal.rs +++ b/src/rule/number/equal.rs @@ -13,7 +13,7 @@ macro_rules! define_equal_rule { if target == EQUAL { Ok(target) } else { - Err($crate::result::Error::new(target, format!("{} does not equal {}", target, EQUAL))) + Err($crate::result::Error::new(target, format!("the value must be equal to {EQUAL}, but received {target}"))) } } } diff --git a/src/rule/number/even.rs b/src/rule/number/even.rs index cbaabab..a73077d 100644 --- a/src/rule/number/even.rs +++ b/src/rule/number/even.rs @@ -17,7 +17,7 @@ macro_rules! even_rule { if target % 2 == 0 { Ok(target) } else { - Err($crate::result::Error::new(target, format!("{} is not even number", target))) + Err($crate::result::Error::new(target, format!("the value must be even, but received {target}"))) } } } diff --git a/src/rule/number/greater.rs b/src/rule/number/greater.rs index 370b127..1c37cd4 100644 --- a/src/rule/number/greater.rs +++ b/src/rule/number/greater.rs @@ -15,7 +15,7 @@ macro_rules! define_greater_rule { if target > THAN { Ok(target) } else { - Err($crate::result::Error::new(target, format!("{} is not greater than {}", target, THAN))) + Err($crate::result::Error::new(target, format!("the value must be greater than {THAN}, but received {target}"))) } } } diff --git a/src/rule/number/less.rs b/src/rule/number/less.rs index de5a94c..c1dd8c2 100644 --- a/src/rule/number/less.rs +++ b/src/rule/number/less.rs @@ -15,7 +15,7 @@ macro_rules! define_less_rule { if target < THAN { Ok(target) } else { - Err($crate::result::Error::new(target, format!("{} is not less than {}", target, THAN))) + Err($crate::result::Error::new(target, format!("the value must be less than {THAN}, but received {target}"))) } } } diff --git a/src/rule/number/min_max.rs b/src/rule/number/min_max.rs index 50d9eb8..b2c00b6 100644 --- a/src/rule/number/min_max.rs +++ b/src/rule/number/min_max.rs @@ -5,9 +5,9 @@ macro_rules! define_min_max_rule { pub type [] = $crate::Refined<[]>; /// Rule where the target value must be greater than or equal to `MIN` and less than or equal to `MAX` - pub type [] = $crate::Or![ - $crate::rule::[], - $crate::rule::[] + pub type [] = $crate::And![ + $crate::rule::[], + $crate::rule::[] ]; } }; diff --git a/src/rule/number/odd.rs b/src/rule/number/odd.rs index 7f14bac..813f7a2 100644 --- a/src/rule/number/odd.rs +++ b/src/rule/number/odd.rs @@ -15,7 +15,7 @@ macro_rules! odd_rule { if target % 2 == 1 { Ok(target) } else { - Err($crate::result::Error::new(target, format!("{} is not odd number", target))) + Err($crate::result::Error::new(target, format!("the value must be odd, but received {target}"))) } } }