From a06a8034c6b13ea82f1402076dbbdece777fdea3 Mon Sep 17 00:00:00 2001 From: tomoikey <55743826+tomoikey@users.noreply.github.com> Date: Thu, 31 Oct 2024 02:21:33 +0900 Subject: [PATCH] fix error message --- src/rule/composer/and.rs | 16 +++++----------- src/rule/composer/not.rs | 4 +++- src/rule/composer/or.rs | 7 +++---- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/rule/composer/and.rs b/src/rule/composer/and.rs index e50dbe1..33f5dbc 100644 --- a/src/rule/composer/and.rs +++ b/src/rule/composer/and.rs @@ -1,5 +1,6 @@ use crate::result::Error; use crate::rule::Rule; +use std::fmt::Debug; use std::marker::PhantomData; /// A macro to generate a `Rule` that combines multiple rules @@ -59,7 +60,7 @@ impl Default for And { } } -impl<'a, T, RULE1, RULE2> Rule for And +impl<'a, T: Debug, RULE1, RULE2> Rule for And where RULE1: Rule + 'a, RULE2: Rule + 'a, @@ -71,17 +72,10 @@ where 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)) - } + Ok(value) => Err(Error::new(value, rule1_error_message)), Err(err) => { - let rule2_type_name = std::any::type_name::(); - let message = format!( - "{rule1_error_message} ({rule1_type_name}) & {err} ({rule2_type_name})", - ); + let message = format!("[{rule1_error_message} && {err}]",); Err(Error::new(err.into_value(), message)) } } @@ -105,7 +99,7 @@ mod test { #[test] fn test_rule_binder_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>)"); + assert_eq!(Target::validate(11).unwrap_err().to_string(), "[the value must be even, but received 11 && the value must be less than 10, but received 11]"); } #[test] diff --git a/src/rule/composer/not.rs b/src/rule/composer/not.rs index 0fc9c9c..a37a4d4 100644 --- a/src/rule/composer/not.rs +++ b/src/rule/composer/not.rs @@ -28,7 +28,9 @@ where fn validate(target: Self::Item) -> crate::Result { let bounded_rule = |t: T| match RULE::validate(t) { Ok(value) => { - let type_name = std::any::type_name::(); + let type_name = std::any::type_name::() + .replace("refined_type::rule::composer::or::Or", "Or") + .replace("refined_type::rule::composer::and::And", "And"); let message = format!("{value:?} does not satisfy Not<{type_name}>"); Err(Error::new(value, message)) } diff --git a/src/rule/composer/or.rs b/src/rule/composer/or.rs index 7cbe65e..456350e 100644 --- a/src/rule/composer/or.rs +++ b/src/rule/composer/or.rs @@ -1,5 +1,6 @@ use crate::result::Error; use crate::rule::Rule; +use std::fmt::Debug; use std::marker::PhantomData; /// A macro to generate a `Rule` that combines multiple rules @@ -40,7 +41,7 @@ pub struct Or { _rule2: PhantomData, } -impl<'a, T, RULE1, RULE2> Rule for Or +impl<'a, T: Debug, RULE1, RULE2> Rule for Or where RULE1: Rule + 'a, RULE2: Rule + 'a, @@ -55,12 +56,10 @@ where 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})"), + format!("[{rule1_error_message} || {rule2_error_message}]"), )) } }