Skip to content

Commit

Permalink
fix not error message
Browse files Browse the repository at this point in the history
  • Loading branch information
tomoikey committed Oct 30, 2024
1 parent 16979d0 commit b364f14
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 17 deletions.
31 changes: 25 additions & 6 deletions src/rule/composer/and.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -67,15 +67,33 @@ where
type Item = T;

fn validate(target: Self::Item) -> crate::Result<T> {
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::<RULE1>();
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::<RULE2>();
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<NonEmptyStringRule, AlphabetRule<String>>;

Expand All @@ -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]
Expand Down
20 changes: 17 additions & 3 deletions src/rule/composer/or.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -50,7 +50,21 @@ where
fn validate(target: Self::Item) -> crate::Result<T> {
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::<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})"),
))
}
}
}
};
bounded_rule(target)
}
Expand Down
2 changes: 1 addition & 1 deletion src/rule/number/equal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rule/number/even.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rule/number/greater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rule/number/less.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")))
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/rule/number/min_max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ macro_rules! define_min_max_rule {
pub type [<MinMax $t:camel>]<const MIN: $t, const MAX: $t> = $crate::Refined<[<MinMaxRule $t:camel>]<MIN, MAX>>;

/// Rule where the target value must be greater than or equal to `MIN` and less than or equal to `MAX`
pub type [<MinMaxRule $t:camel>]<const MIN: $t, const MAX: $t> = $crate::Or![
$crate::rule::[<RangeRule $t:camel>]<MIN, MAX>,
$crate::rule::[<EqualRule $t:camel>]<MAX>
pub type [<MinMaxRule $t:camel>]<const MIN: $t, const MAX: $t> = $crate::And![
$crate::rule::[<GreaterEqualRule $t:camel>]<MIN>,
$crate::rule::[<LessEqualRule $t:camel>]<MAX>
];
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/rule/number/odd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")))
}
}
}
Expand Down

0 comments on commit b364f14

Please sign in to comment.