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
use crate::;
/// A value that can be checked against its rules.
///
/// `#[derive(Validate)]` implements it from the rules on a struct's fields. It can also be implemented by hand:
///
/// - [`Validate::validate_into`] pushes every issue it finds into the report, each at the location of the value it
/// was found in.
/// - [`Validate::validate`] returns all of them, and [`Validate::validate_fail_fast`] only the first.
/// - When failing fast, the report is [finished](Report::is_finished) after its first issue and keeps
/// nothing more. So once a push leaves the report finished, `validate_into` should return.
///
/// ```
/// use annotated::{Report, Location, Problem, Validate};
///
/// struct NewPassword {
/// password: String,
/// repeated_password: String,
/// }
///
/// impl Validate for NewPassword {
/// fn validate_into(&self, at: &Location<'_>, found: &mut Report) {
/// if self.password.chars().count() < 8 {
/// found.push(&at.field("password"), Problem::custom("too_short", "must be 8 characters or more"));
/// if found.is_finished() {
/// return;
/// }
/// }
/// if self.repeated_password != self.password {
/// let passwords_differ = Problem::custom("passwords_differ", "must match the password");
/// found.push(&at.field("repeatedPassword"), passwords_differ);
/// }
/// }
/// }
///
/// let new_password = NewPassword { password: "horse".into(), repeated_password: "battery".into() };
///
/// assert_eq!(
/// new_password.validate().unwrap_err().to_string(),
/// "password: must be 8 characters or more\nrepeatedPassword: must match the password"
/// );
/// assert_eq!(new_password.validate_fail_fast().unwrap_err().to_string(), "password: must be 8 characters or more");
/// ```