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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use super::Rule;

#[cfg(doc)]
use super::{All, Opt};

/// Rule to ensure that 2 other rules are [`Ok`].
///
/// The `rule` attribute accepts multiple rules and has the same behavior
/// but the [`And`] rule is useful for rules that accept a single rule such
/// as [`All`] and [`Opt`].
///
/// In case of error on both rules, the first one is returned.
///
/// # Example
/// ```
/// use type_rules::prelude::*;
///
/// #[derive(Validator)]
/// struct OptionalMail(
///     #[rule(Opt(And(MaxLength(1000), RegEx(r"^\S+@\S+\.\S+"))))]
///     Option<String>
/// );
/// ```
pub struct And<T, U>(pub T, pub U);

impl<T, U, F> Rule<F> for And<T, U>
where
    T: Rule<F>,
    U: Rule<F>,
{
    fn check(&self, value: &F) -> Result<(), String> {
        self.0.check(value)?;
        self.1.check(value)
    }
}

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

    const RULE: Opt<And<MaxLength, RegEx>> = Opt(And(MaxLength(20), RegEx(r"^\S+@\S+\.\S+")));

    #[test]
    fn and_ok() {
        assert_ok!(RULE.check(&Some("example@example.fr")));
    }

    #[test]
    fn and_0_err() {
        let val = Some("too.long.example@too.long.example.fr");

        let res_error_message = RULE.check(&val).expect_err("Should be an Err");

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

    #[test]
    fn and_1_err() {
        let val = Some("example.example.fr");

        let res_error_message = RULE.check(&val).expect_err("Should be an Err");

        assert_eq!(res_error_message, "The regex does not match")
    }

    #[test]
    fn and_0_1_err() {
        let val = Some("too.long.example.too.long.example.fr");

        let res_error_message = RULE.check(&val).expect_err("Should be an Err");

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