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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! A data structure validation library
//!
//! ```
//! use validatron::Validate;
//!
//! #[derive(Debug, Validate)]
//! struct MyStruct {
//!     #[validatron(min = 42)]
//!     a: i64,
//!     #[validatron(max_len = 5)]
//!     b: Vec<u32>,
//! }
//!
//! let x = MyStruct {
//!     a: 36,
//!     b: vec![]
//! };
//!
//! x.validate().is_err();
//! ```

/// An [`Error`](trait@std::error::Error) type for representing validation failures
pub mod error;

/// pre-rolled validators for data structures
pub mod validators;

// re-export derive macro
pub use error::{Error, Location};

/// A derive macro for validating data structures
pub use validatron_derive::Validate;

/// A convenience type for Results using the [`Error`] error type.
pub type Result<T> = std::result::Result<T, Error>;

/// The core Validatron trait, types that implement this trait can
/// be exhaustively validated.
///
/// Implementors should recursively validate internal structures.
pub trait Validate {
    /// Validate the implemented type exhaustively, returning all errors.
    fn validate(&self) -> Result<()>;
}

fn validate_seq<'a, I, T: 'a>(sequence: I) -> Result<()>
where
    I: IntoIterator<Item = &'a T>,
    T: Validate,
{
    let mut eb = Error::build();

    for (i, x) in sequence.into_iter().enumerate() {
        eb.try_at_index(i, x.validate());
    }

    eb.build()
}

impl<T> Validate for Vec<T>
where
    T: Validate,
{
    fn validate(&self) -> Result<()> {
        validate_seq(self)
    }
}

impl<T> Validate for std::collections::VecDeque<T>
where
    T: Validate,
{
    fn validate(&self) -> Result<()> {
        validate_seq(self)
    }
}

impl<T> Validate for std::collections::LinkedList<T>
where
    T: Validate,
{
    fn validate(&self) -> Result<()> {
        validate_seq(self)
    }
}

impl<K, V, S> Validate for std::collections::HashMap<K, V, S>
where
    K: std::fmt::Display,
    V: Validate,
{
    fn validate(&self) -> Result<()> {
        let mut eb = Error::build();

        for (k, v) in self {
            eb.try_at_named(k.to_string(), v.validate());
        }

        eb.build()
    }
}

impl<K, V> Validate for std::collections::BTreeMap<K, V>
where
    K: std::fmt::Display,
    V: Validate,
{
    fn validate(&self) -> Result<()> {
        let mut eb = Error::build();

        for (k, v) in self {
            eb.try_at_named(k.to_string(), v.validate());
        }

        eb.build()
    }
}

impl<T, S> Validate for std::collections::HashSet<T, S>
where
    T: Validate,
{
    fn validate(&self) -> Result<()> {
        validate_seq(self)
    }
}

impl<T> Validate for std::collections::BTreeSet<T>
where
    T: Validate,
{
    fn validate(&self) -> Result<()> {
        validate_seq(self)
    }
}

impl<T> Validate for std::collections::BinaryHeap<T>
where
    T: Validate,
{
    fn validate(&self) -> Result<()> {
        validate_seq(self)
    }
}

#[cfg(feature = "use-indexmap")]
impl<K, V> Validate for indexmap::IndexMap<K, V>
where
    K: std::fmt::Display,
    V: Validate,
{
    fn validate(&self) -> Result<()> {
        let mut eb = Error::build();

        for (k, v) in self {
            eb.try_at_named(k.to_string(), v.validate());
        }

        eb.build()
    }
}

#[cfg(feature = "use-indexmap")]
impl<T, S> Validate for indexmap::IndexSet<T, S>
where
    T: Validate,
{
    fn validate(&self) -> Result<()> {
        validate_seq(self)
    }
}

impl<T> Validate for Option<T>
where
    T: Validate,
{
    fn validate(&self) -> Result<()> {
        validate_seq(self)
    }
}

impl<T, E> Validate for std::result::Result<T, E>
where
    T: Validate,
{
    fn validate(&self) -> Result<()> {
        if let Ok(value) = self {
            value.validate()
        } else {
            Err(Error::new("value is an Error"))
        }
    }
}