type_rules/rules/
mod.rs

1mod all;
2mod and;
3mod any;
4mod eval;
5mod is_in;
6mod min_max_length;
7mod min_max_range;
8mod min_max_size;
9mod opt;
10mod or;
11#[cfg(feature = "regex")]
12#[cfg_attr(docsrs, doc(cfg(feature = "regex")))]
13mod regex;
14mod validate;
15
16pub use self::all::*;
17pub use self::and::*;
18pub use self::any::*;
19pub use self::eval::*;
20pub use self::is_in::*;
21pub use self::min_max_length::*;
22pub use self::min_max_range::*;
23pub use self::min_max_size::*;
24pub use self::opt::*;
25pub use self::or::*;
26pub use self::validate::*;
27
28#[cfg(feature = "regex")]
29#[cfg_attr(docsrs, doc(cfg(feature = "regex")))]
30pub use self::regex::*;
31
32/// Define a rule for a type
33///
34/// By implementing `Rule` for a type you define how
35/// it will be used to constraint a type `T`
36///
37/// # Example
38///
39/// ```
40/// use type_rules::prelude::*;
41///
42/// struct IsEven();
43///
44/// impl Rule<i32> for IsEven {
45///     fn check(&self, value: &i32) -> Result<(), String> {
46///         if value % 2 == 0 {
47///             Ok(())
48///         } else {
49///             Err("Value is not even".into())
50///         }
51///     }
52/// }
53///
54/// #[derive(Validator)]
55/// struct MyInteger(#[rule(IsEven())] i32);
56/// ```
57pub trait Rule<T: ?Sized> {
58    fn check(&self, value: &T) -> Result<(), String>;
59}