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
//! Validate variable internal state when the variable is accessed.
//!
//! - Implement trait `Validate` for a type `T` to define how to validate internal state of `T`.
//! - Wrapper struct `Valid<T: Validate>` implements `Deref` and `DerefMut` traits, and validates
//! the internal state when the variable is accessed.
//!
//! For example, If in your program you have a struct `Lt5 { v: u64 }` and you want to make sure
//! that `v` is always less than to `5`, you can implement `Validate` trait for `Foo` and use
//! `less!` macro to validate `a`.
//! ```
//! # use std::error::Error;
//! # use std::panic::catch_unwind;
//! # use validit::Validate;
//! # use validit::ValidateExt;
//! struct Lt5 { v: u64 }
//!
//! impl Validate for Lt5 {
//! fn validate(&self) -> Result<(), Box<dyn Error>> {
//! validit::less!(self.v, 5);
//! Ok(())
//! }
//! }
//!
//! # fn main() {
//! let v1 = Lt5 { v: 1 }.valid();
//! let _x = v1.v; // Good
//!
//! let v6 = Lt5 { v: 6 }.valid();
//! let res = catch_unwind(|| {
//! let _x = v6.v; // panic: panicked at 'invalid state: expect: self.v(6) < 5(5) ...
//! });
//! assert!(res.is_err());
//! # }
//! ```
pub use Valid;
pub use Validate;
pub use ValidateExt;