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
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]

/// A module that contains all the rules
pub mod rules;

mod valid;

/// A convenience module appropriate for glob imports `use type_rules::prelude::*;`
pub mod prelude;

#[cfg(feature = "derive")]
#[doc(hidden)]
pub use type_rules_derive::*;

#[doc(inline)]
pub use rules::Rule;

#[doc(inline)]
pub use valid::Valid;

/// Check the validity of a type
///
/// By implementing `Validator` for a type, you define the
/// rules to check is validity
///
/// Can be derived with the `derive` feature
///
/// # Example
///
/// Basic usage:
///
/// ```should_panic
/// use type_rules::prelude::*;
///
/// #[derive(Validator)]
/// struct NotEmptyString(#[rule(MinLength(1))] String);
///
/// let valid = NotEmptyString(String::from("Not empty"));
/// let not_valid = NotEmptyString(String::from(""));
///
/// valid.check_validity().unwrap(); // OK
/// not_valid.check_validity().unwrap(); // Value is too short
/// ```
pub trait Validator {
    fn check_validity(&self) -> Result<(), String>;
}