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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use Box;
use format;
use Error;
use crateConversionError;
/// Extracts an infallible result without introducing a potential panic.
///
/// ```
/// use core::convert::Infallible;
///
/// use miden_protobuf::unwrap_infallible;
///
/// assert_eq!(unwrap_infallible(Ok::<_, Infallible>(42)), 42);
/// ```
///
/// Fallible results are rejected at compile time:
///
/// ```compile_fail,E0308
/// use miden_protobuf::unwrap_infallible;
///
/// unwrap_infallible("42".parse::<u32>());
/// ```
/// Decodes a wire message or oneof without constructing its verified domain counterpart.
///
/// Derived implementations produce schema-shaped records. Atomic messages can select an existing
/// deserialized type instead, using its `TryFrom` implementation. Such adapters should check the
/// representation only, leaving application invariants to [`Verify`] or [`VerifyWith`].
/// Use [`DecodeMessageExt`] to combine field decoding with an explicit construction capability.
/// The decoded representation of a **wire message or oneof** `P`, not its verified domain
/// counterpart.
pub type Decoded<P> = Decoded;
/// Combines field decoding with an explicitly selected domain construction capability.
///
/// Implemented for every [`DecodeMessage`], including oneofs and handwritten adapters. Each
/// method requires only its corresponding capability on the decoded representation. These methods
/// consume an already parsed wire message; they do not decode Protobuf bytes.
///
/// All methods return [`ConversionError`] with a stage prefix: `failed to decode`,
/// `failed to verify`, or `failed to build unchecked`. The original error, including any field
/// path, is preserved in the source chain. Stage labels are separate from wire paths. Call
/// [`DecodeMessage::decode_fields`] and the construction method separately when typed domain
/// errors are needed directly.
/// Checks domain invariants and constructs the verified type using ordinary Rust.
///
/// Verification errors belong to the domain. Cross-field checks need not correspond to a
/// single wire field.
/// Types that require external context can implement [`VerifyWith`] instead.
/// Boxes delegate to the contained verifier, preserving its error type and boxing the output.
/// Generated decoded records retain collections in [`crate::OptionalField`],
/// [`crate::RepeatedField`], or [`crate::MapField`]. Calling `verify()` on these fields verifies
/// their elements with the generated field name and index or key context. These helpers return
/// [`ConversionError`], preserving the original source, and stop at the first error.
/// For element verifiers whose error is [`core::convert::Infallible`], the wrappers also provide
/// `verify_infallible()`, returning the verified collection directly.
/// Collection-wide invariants remain the responsibility of the containing verifier. Use the
/// wrappers' `map()` or `try_map()` methods for explicit element conversions; `try_map()` retains
/// field, index, and key context. Use `into_inner()` for custom collection-wide processing.
/// Checks domain invariants using caller-supplied context and constructs the verified type.
///
/// Context can be borrowed, such as a trusted parent header, or owned, such as a security level.
/// Use a named context struct when verification needs several inputs. Implementations must
/// document any trust requirements on the context.
///
/// This capability is independent of [`Verify`]: implementing it does not provide context-free
/// verification. Implementations are handwritten; decoding does not invoke verification.
/// Boxes delegate to the contained verifier without cloning the context or changing its error.
/// The collection field wrappers also implement this trait, retaining field, index, and key
/// context as with [`Verify`]. For vectors and maps, context is cloned once per element; pass
/// `&context` to share a context without cloning its contents.
/// Constructs a domain object from decoded fields while skipping selected verification checks.
///
/// This is an explicit, handwritten capability, independent of [`Verify`] and [`VerifyWith`].
/// Implement it only where the domain API supports unchecked construction. Construction can still
/// fail on remaining checks or conversions; use [`core::convert::Infallible`] when it cannot fail.
/// Collection field wrappers implement this capability when their elements do, preserving
/// presence, order, duplicates, and keys. They stop at the first construction error, retaining
/// its field, index, or key context. They do not invoke [`Verify`] or check collection invariants.
///
/// # Warning
///
/// Implementations must document precisely which checks are skipped, including nested checks,
/// and which invariants callers must ensure. The output is not guaranteed to be verified. This
/// operation must not bypass wire decoding checks or silently discard verification errors.
/// Skipping domain validation alone does not make this a Rust `unsafe` operation.
/// Preserves the box and delegates construction; the caller must ensure the invariants
/// documented by `S`'s `BuildUnchecked` implementation.