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
//! Field family markers and type-level field selection.
//!
//! A *field family* selects which concrete representation a field takes in a
//! data type that is generic over [`FieldFamily`]:
//!
//! * [`Plain`] — the validated domain value (e.g. `u32`)
//! * [`Input`] — the raw, unvalidated input (e.g. `String`)
//! * [`Form`] — the editable form state with a validation result attached
//! (e.g. `Edit<String>`)
//!
//! The [`Field`] and [`FieldWith`] type aliases resolve a field type for a
//! given family, so a single struct definition expands to three related
//! types: `T<Plain>`, `T<Input>`, and `T<Form>`.
use Debug;
use DefaultInputType;
/// The bound every field type (`P`, `E`, `I`) must satisfy.
///
/// This is `PartialEq + Clone + Debug`, plus `Serialize + DeserializeOwned`
/// when the `serde` feature is enabled (off by default).
/// The bound every field type (`P`, `E`, `I`) must satisfy.
///
/// This is `PartialEq + Clone + Debug`, plus `Serialize + DeserializeOwned`
/// when the `serde` feature is enabled (off by default).
/// Field family of validated domain values.
///
/// `T<Plain>` is the plain, validated representation of `T`. A value of a
/// `Plain` type is only obtained by validating a `T<Form>` or `T<Input>`
/// (or by deserializing from trusted storage), so holding a `T<Plain>`
/// means the data has passed validation.
;
/// Field family of raw, unvalidated input values.
///
/// `T<Input>` is `T<Form>` without the per-field validation results. It is
/// intended for input without a UI, such as the payload of an API request
/// sent from the frontend to the backend.
;
/// Field family of editable form states.
///
/// `T<Form>` represents the state of an input form while the user is editing
/// a `T`. Each field holds its raw input (numbers are edited as strings, for
/// example) together with the latest validation result (see [`Edit`](crate::Edit)).
;
/// Maps a field family to the concrete type of a field.
///
/// This is implemented by the three marker types [`Plain`], [`Input`], and
/// [`Form`]. The generic associated type picks one of the three candidate
/// types `P` (plain), `E` (form) and `I` (input).
///
/// All candidate types must satisfy [`FieldBounds`]; this bound is currently
/// a prerequisite for every field type used with this crate.
/// A field whose plain, form, and input types are given explicitly.
///
/// `FieldWith<F, P, E, I>` resolves to `P` when `F` is [`Plain`], to `E` when
/// `F` is [`Form`], and to `I` when `F` is [`Input`]. Use this instead of
/// [`Field`] when a type needs form/input representations different from the
/// defaults declared by its [`DefaultInputType`] implementation.
pub type FieldWith<F, P, E, I> = Type;
/// A field of plain type `P`, using `P`'s default form and input types.
///
/// The form and input representations are taken from `P`'s
/// [`DefaultInputType`] implementation. For example `u32` declares
/// `InputType = String` and `FormType = Edit<String>`, so:
///
/// ```
/// use validatable::*;
///
/// let plain: Field<Plain, u32> = 42u32;
/// let input: Field<Input, u32> = "42".to_string();
/// let form: Field<Form, u32> = Edit::from_input("42".to_string());
/// # let _ = (plain, input, form);
/// ```
pub type Field<F, P> =
;