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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//! The `Codec` port trait — the seam between the canonical IR and one revision.
//!
//! # Why this trait, and why here
//!
//! The canonical model is the point of the crate: application code speaks one
//! version-agnostic IR, and per-revision codecs lift wire messages into it and
//! lower them back out for whatever version was negotiated. This trait is that
//! seam, and it is deliberately narrow — one associated wire type, two
//! directions.
//!
//! It lives beside its vocabulary rather than in the adapter because the
//! *terms* belong to the translation path, not to the serialisation crate. An
//! adapter that owned the trait would make itself the owner of "what a codec
//! is", and the second consumer — a bridge, a prober, a conformance runner —
//! would have to import from the first to say what it wants.
//!
//! # The two directions are not symmetric, and the asymmetry is the design
//!
//! [`Codec::lift`] is **total**: every well-formed wire value produces an IR
//! value, because there is no such thing as a message the peer sent that the
//! bridge may refuse to represent. Fields this build does not name go to
//! [`Extensions`] and unrecognised content blocks go to
//! [`UnknownContent`](crate::domain::ir::UnknownContent), so a message survives
//! the crossing intact. A lift that could fail would mean some inbound message
//! was dropped silently, which is the one outcome a version-translating bridge
//! cannot have.
//!
//! [`Codec::lower`] is **partial**, and the failure is not a limitation to work
//! around but the whole point. A value may need a feature the target revision
//! cannot express. Then lowering either refuses with
//! [`LowerError::FeatureUnavailable`], or applies a rule the caller's
//! [`DegradePolicy`] named and records it in the [`TranslationReport`]. It never
//! emits a value it cannot account for — see [`lower::lower_tool_result`],
//! which is the decision half of this and is shared by every implementation.
//!
//! # What an implementor actually writes
//!
//! A codec is mostly mechanical translation between a generated wire struct
//! and the IR. Two things it must *not* decide:
//!
//! - Whether the value is expressible. That is the gate, and it is the same
//! answer for every revision — asking it per-codec is how two revisions start
//! disagreeing about what they support.
//! - How a value is degraded. [`DegradeRule`] owns that, so the report means
//! the same thing whoever ran it.
//!
//! What an implementor *does* own is the shape: which fields its revision has,
//! how its content union is ordered, and how its own extra-field bag maps onto
//! [`Extensions`]. The generated types are the source of truth for all three,
//! and the union ordering in particular is not a detail — an `untagged` enum
//! matches the first member that round-trips, so a codec that assumes the wrong
//! order will silently accept an audio block as an image block.
//!
//! # Implementors
//!
//! See `adapters/secondary/codec` for the per-revision implementations. The
//! example below is the shape in miniature: a revision with one content kind
//! and no extra fields.
//!
//! ```
//! use protocol_droid::domain::ir::Codec;
//! use protocol_droid::domain::ir::lower::{LowerError, TranslationReport};
//! use protocol_droid::domain::ir::{Content, TextContent, ToolResult};
//! use protocol_droid::domain::{DegradePolicy, ProtocolVersion};
//!
//! /// A wire type, as the generated code would produce it.
//! #[derive(Debug, Clone, PartialEq)]
//! struct WireResult {
//! content: Vec<WireText>,
//! is_error: bool,
//! }
//!
//! #[derive(Debug, Clone, PartialEq)]
//! struct WireText {
//! type_name: String,
//! text: String,
//! }
//!
//! /// The revision that can only say one thing.
//! #[derive(Debug, Clone, Copy)]
//! struct TextOnlyRevision;
//!
//! impl Codec for TextOnlyRevision {
//! type Wire = WireResult;
//!
//! fn version(&self) -> ProtocolVersion {
//! ProtocolVersion::V2024_11_05
//! }
//!
//! fn lift(&self, wire: &Self::Wire) -> ToolResult {
//! let mut result = ToolResult::new(
//! wire.content
//! .iter()
//! .map(|block| Content::Text(TextContent::new(block.text.clone())))
//! .collect(),
//! );
//! if wire.is_error {
//! result = result.error();
//! }
//! result
//! }
//!
//! fn lower(
//! &self,
//! value: ToolResult,
//! policy: DegradePolicy,
//! report: &mut TranslationReport,
//! ) -> Result<Self::Wire, LowerError> {
//! // Every revision gates the same way, so ask once and delegate the
//! // decisions to the shared path rather than re-deriving them here.
//! let (lowered, _own) = protocol_droid::domain::ir::lower::lower_tool_result(
//! value,
//! &self.capabilities(),
//! self.version(),
//! policy,
//! )?;
//! let _ = report;
//! Ok(WireResult {
//! content: lowered
//! .content
//! .iter()
//! .map(|block| WireText {
//! type_name: "text".to_owned(),
//! text: match block {
//! Content::Text(text) => text.text.clone(),
//! // Unreachable: the gate refused anything else.
//! other => format!("{other:?}"),
//! },
//! })
//! .collect(),
//! is_error: lowered.is_error,
//! })
//! }
//!
//! fn capabilities(&self) -> protocol_droid::domain::FeatureProfile {
//! let caps = protocol_droid::domain::CapabilitySet::all_for(self.version());
//! protocol_droid::domain::resolve(protocol_droid::domain::Negotiation {
//! version: self.version(),
//! client_caps: caps,
//! server_caps: caps,
//! transport: protocol_droid::domain::TransportKind::Stdio,
//! })
//! }
//! }
//!
//! // It lifts and lowers, and a value it can express round-trips unchanged.
//! let codec = TextOnlyRevision;
//! let wire = WireResult {
//! content: vec![WireText {
//! type_name: "text".to_owned(),
//! text: "hello".to_owned(),
//! }],
//! is_error: false,
//! };
//! let lifted = codec.lift(&wire);
//! assert_eq!(lifted.content.len(), 1);
//!
//! let mut report = TranslationReport::new(codec.version());
//! let lowered = codec
//! .lower(lifted.clone(), DegradePolicy::strict(), &mut report)
//! .expect("expressible");
//! assert_eq!(lowered, wire);
//! assert!(report.is_empty());
//! ```
use crateToolResult;
use crate;
use crate;
/// Translates between one revision's wire type and the canonical IR.
///
/// See the module docs at the top of this file for the contract. The short version: [`lift`](Self::lift)
/// is total, [`lower`](Self::lower) is gated.