Skip to content

Commit

Permalink
fix error message
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoikey committed Oct 30, 2024
1 parent b364f14 commit a06a803
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
16 changes: 5 additions & 11 deletions src/rule/composer/and.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -59,7 +60,7 @@ impl<RULE1, RULE2> Default for And<RULE1, RULE2> {
}
}

impl<'a, T, RULE1, RULE2> Rule for And<RULE1, RULE2>
impl<'a, T: Debug, RULE1, RULE2> Rule for And<RULE1, RULE2>
where
RULE1: Rule<Item = T> + 'a,
RULE2: Rule<Item = T> + 'a,
Expand All @@ -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::<RULE1>();
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::<RULE2>();
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))
}
}
Expand All @@ -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]
Expand Down
4 changes: 3 additions & 1 deletion src/rule/composer/not.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ where
fn validate(target: Self::Item) -> crate::Result<T> {
let bounded_rule = |t: T| match RULE::validate(t) {
Ok(value) => {
let type_name = std::any::type_name::<RULE>();
let type_name = std::any::type_name::<RULE>()
.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))
}
Expand Down
7 changes: 3 additions & 4 deletions src/rule/composer/or.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -40,7 +41,7 @@ pub struct Or<RULE1, RULE2> {
_rule2: PhantomData<RULE2>,
}

impl<'a, T, RULE1, RULE2> Rule for Or<RULE1, RULE2>
impl<'a, T: Debug, RULE1, RULE2> Rule for Or<RULE1, RULE2>
where
RULE1: Rule<Item = T> + 'a,
RULE2: Rule<Item = T> + 'a,
Expand All @@ -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::<RULE1>();
let rule2_type_name = std::any::type_name::<RULE2>();
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}]"),
))
}
}
Expand Down

0 comments on commit a06a803

Please sign in to comment.