type_rules/lib.rs
1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3
4/// A module that contains all the rules
5pub mod rules;
6
7mod valid;
8
9/// A convenience module appropriate for glob imports `use type_rules::prelude::*;`
10pub mod prelude;
11
12#[cfg(feature = "derive")]
13#[doc(hidden)]
14pub use type_rules_derive::*;
15
16#[doc(inline)]
17pub use rules::Rule;
18
19#[doc(inline)]
20pub use valid::Valid;
21
22/// Check the validity of a type
23///
24/// By implementing `Validator` for a type, you define the
25/// rules to check is validity
26///
27/// Can be derived with the `derive` feature
28///
29/// # Example
30///
31/// Basic usage:
32///
33/// ```should_panic
34/// use type_rules::prelude::*;
35///
36/// #[derive(Validator)]
37/// struct NotEmptyString(#[rule(MinLength(1))] String);
38///
39/// let valid = NotEmptyString(String::from("Not empty"));
40/// let not_valid = NotEmptyString(String::from(""));
41///
42/// valid.check_validity().unwrap(); // OK
43/// not_valid.check_validity().unwrap(); // Value is too short
44/// ```
45pub trait Validator {
46 fn check_validity(&self) -> Result<(), String>;
47}