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
194
195
196
197
198
199
// the doc is in another castle
extern crate self as peer_pressure;
use Infallible;
pub use *;
pub use peer_pressure_derive as derive;
pub use ;
/// Marker trait for types that are valid by construction.
///
/// Every [Valid] type trivially validates as itself via [Validate] and [ValidateFrom].
///
/// # Examples
///
/// ```
/// use peer_pressure::{Valid, Validate};
///
/// #[derive(Clone, Debug, PartialEq)]
/// pub struct NonEmptyVec<T>(Vec<T>);
///
/// impl<T> NonEmptyVec<T> {
/// pub fn new(inner: Vec<T>) -> Option<Self> {
/// (inner.len() > 0).then_some(Self(inner))
/// }
/// }
///
/// // `NonEmptyVec` can only be constructed via `NonEmptyVec::new` which ensures the non-emptiness
/// // invariant holds for all `NonEmptyVec` values.
/// impl<T> Valid for NonEmptyVec<T> {}
///
/// let p = NonEmptyVec::new(vec![1,2,3]).unwrap();
/// assert_eq!(p.clone().validate(), Ok(p));
/// ```
/// Deterministic validation.
///
/// Types implementing [Validate] commit to a single, canonical notion of validity, encoded
/// by the `Output` type. The implementation of [Validate] must ensure that all invariants
/// assumed of `Output` hold, and `Output` itself should not be constructible in a way that would
/// violate those invariants.
///
/// In other words, `Output` should correctly implement [Valid].
///
/// # Examples
///
/// ```rust
/// use peer_pressure::Validate;
///
/// struct RawAge(i32);
///
/// struct Age(u32);
///
/// impl Validate for RawAge {
/// // Age being non-negative is a context-free property
/// type Context = ();
/// type Output = Age;
/// type Error = &'static str;
///
/// fn validate_in_context(self, _: &()) -> Result<Age, &'static str> {
/// if self.0 >= 0 {
/// Ok(Age(self.0 as u32))
/// } else {
/// Err("negative age")
/// }
/// }
/// }
///
/// let age = RawAge(30).validate().unwrap();
/// assert_eq!(age.0, 30);
/// assert!(RawAge(-1).validate().is_err());
/// ```
///
/// ```rust
/// use peer_pressure::Validate;
///
/// type ScrabbleHand = Vec<char>;
/// pub struct Word(String);
///
/// pub struct LegalWord(String);
///
/// impl Validate for Word {
/// // A scrabble word is valid for a given set of letters on hand
/// type Context = ScrabbleHand;
/// type Output = LegalWord;
/// type Error = &'static str;
///
/// fn validate_in_context(self, hand: &ScrabbleHand) -> Result<LegalWord, Self::Error> {
/// todo!("cmon, you can implement that yourself")
/// }
/// }
/// ```
/// Target-side dual of [Validate].
///
/// Unlike [Validate], which forces a single validated output type, [ValidateFrom] allows
/// the same type to be validated from multiple source types, since many domain types may
/// arrive in various formats. This should not be abused to make a single raw value validate
/// into multiple types, but I can't force you.
///
/// # Examples
///
/// ```
/// use peer_pressure::ValidateFrom;
///
/// struct Email(String);
///
/// impl ValidateFrom<&str> for Email {
/// type Context = ();
/// type Error = &'static str;
///
/// fn validate_from_in_context(raw: &str, _: &()) -> Result<Self, Self::Error> {
/// if raw.contains('@') { Ok(Email(raw.to_string())) } else { Err("missing @") }
/// }
/// }
///
/// assert!(Email::validate_from("alice@example.com").is_ok());
/// assert!(Email::validate_from("not-an-email").is_err());
/// ```