1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use super::Rule;

/// Rule to apply an Or condition on two rules.
///
/// In case of error on both rules, the first one is returned.
///
/// # Example
/// ```
/// use type_rules::prelude::*;
///
/// #[derive(Validator)]
/// struct NotZeroInteger(
///     #[rule(Or(MaxRange(-1), MinRange(1)))]
///     i32
/// );
/// ```
pub struct Or<T, U>(pub T, pub U);

impl<T, U, F> Rule<F> for Or<T, U>
where
    T: Rule<F>,
    U: Rule<F>,
{
    fn check(&self, value: &F) -> Result<(), String> {
        let first_res = self.0.check(value);
        let second_res = self.1.check(value);

        if first_res.is_ok() || second_res.is_ok() {
            Ok(())
        } else {
            first_res
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::prelude::*;
    use claim::assert_ok;

    const RULE: Or<MaxRange<i32>, MinRange<i32>> = Or(MaxRange(-1), MinRange(1));

    #[test]
    fn or_0_ok() {
        assert_ok!(RULE.check(&-1));
    }

    #[test]
    fn or_1_ok() {
        assert_ok!(RULE.check(&1));
    }

    #[test]
    fn or_err() {
        let res_error_message = RULE.check(&0).expect_err("Should be an Err");

        assert_eq!(res_error_message, "Value is too high")
    }
}