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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*!
Native → reflection bridge.
This module provides the [`OxiReflect`] trait that connects types
implementing [`OxiMessage`] + [`OxiName`] to the native protobuf
reflection layer.
## Design
The bridge is deliberately a thin glue layer. A type that implements both
`OxiMessage` (native wire encode/decode) and `OxiName` (proto type metadata)
can be introspected reflectively by:
1. **Type-URL extraction** — `OxiName::type_url()` gives the fully-qualified
type URL used to locate the message in a descriptor pool.
2. **Wire round-trip** — the message is encoded to bytes via
`OxiMessage::encode_to_vec` and those bytes plus the full name are handed
to any reflection system that accepts `(full_name, &[u8])`.
3. **Opaque handle** — `OxiReflectHandle` packages a full name and encoded
bytes so the reflect layer can decode the message dynamically without
knowing the concrete type.
## Example
```rust
use oxiproto_core::reflect_bridge::{OxiReflect, OxiReflectHandle};
use oxiproto_core::{OxiMessage, OxiName};
use oxiproto_core::wire::{DecodeBuffer, EncodeBuffer};
use oxiproto_core::OxiProtoResult;
#[derive(Debug, Default, Clone)]
struct Point {
x: i32,
y: i32,
}
impl OxiName for Point {
const NAME: &'static str = "Point";
const PACKAGE: &'static str = "geometry";
}
impl OxiMessage for Point {
fn encoded_len(&self) -> usize {
let mut n = 0;
if self.x != 0 {
n += 1 + ::oxiproto_core::wire::varint::encoded_len_varint(self.x as i64 as u64);
}
if self.y != 0 {
n += 1 + ::oxiproto_core::wire::varint::encoded_len_varint(self.y as i64 as u64);
}
n
}
fn encode_raw(&self, buf: &mut EncodeBuffer) {
if self.x != 0 {
let _ = buf.write_tag(1, ::oxiproto_core::wire::WireType::Varint);
buf.write_varint_i32(self.x);
}
if self.y != 0 {
let _ = buf.write_tag(2, ::oxiproto_core::wire::WireType::Varint);
buf.write_varint_i32(self.y);
}
}
fn merge(&mut self, buf: &mut DecodeBuffer) -> OxiProtoResult<()> {
while !buf.is_empty() {
let tag = buf.read_tag().map_err(oxiproto_core::OxiProtoError::WireFormatError)?;
match tag.field_number {
1 => self.x = buf.read_varint_i32().map_err(oxiproto_core::OxiProtoError::WireFormatError)?,
2 => self.y = buf.read_varint_i32().map_err(oxiproto_core::OxiProtoError::WireFormatError)?,
_ => buf.skip_field(tag.wire_type).map_err(oxiproto_core::OxiProtoError::WireFormatError)?,
}
}
Ok(())
}
fn clear(&mut self) {
*self = Self::default();
}
}
let point = Point { x: 3, y: -7 };
let handle = point.reflect_handle();
assert_eq!(handle.full_name(), "geometry.Point");
assert_eq!(handle.type_url(), "type.googleapis.com/geometry.Point");
assert!(!handle.encoded_bytes().is_empty());
```
*/
use ;
use crate::;
// ---------------------------------------------------------------------------
// OxiReflectHandle — a type-erased snapshot of a reflected message
// ---------------------------------------------------------------------------
/// A type-erased snapshot of an [`OxiMessage`] suitable for passing to
/// reflection or introspection APIs.
///
/// The handle owns the message's fully-qualified proto name and a
/// wire-encoded byte snapshot. It is intentionally *not* tied to the
/// original concrete type — callers can pass it to any API that works
/// with `(full_name, &[u8])` without carrying generic type parameters.
///
/// Create one via [`OxiReflect::reflect_handle`].
// ---------------------------------------------------------------------------
// OxiReflect trait
// ---------------------------------------------------------------------------
/// Bridge trait that connects [`OxiMessage`] + [`OxiName`] types to the
/// native reflection layer.
///
/// Every type that implements both `OxiMessage` and `OxiName` automatically
/// gets a blanket `OxiReflect` implementation — there is nothing to implement
/// manually.
///
/// # Usage
///
/// ```rust
/// use oxiproto_core::reflect_bridge::OxiReflect;
/// use oxiproto_core::{OxiMessage, OxiName};
/// # use oxiproto_core::wire::{DecodeBuffer, EncodeBuffer};
/// # use oxiproto_core::OxiProtoResult;
/// #
/// # #[derive(Debug, Default, Clone)]
/// # struct Ping { seq: u32 }
/// # impl OxiName for Ping {
/// # const NAME: &'static str = "Ping";
/// # const PACKAGE: &'static str = "net";
/// # }
/// # impl OxiMessage for Ping {
/// # fn encoded_len(&self) -> usize { if self.seq != 0 { 2 } else { 0 } }
/// # fn encode_raw(&self, buf: &mut EncodeBuffer) {
/// # if self.seq != 0 {
/// # let _ = buf.write_tag(1, ::oxiproto_core::wire::WireType::Varint);
/// # buf.write_varint32(self.seq);
/// # }
/// # }
/// # fn merge(&mut self, buf: &mut DecodeBuffer) -> OxiProtoResult<()> {
/// # while !buf.is_empty() {
/// # let tag = buf.read_tag().map_err(::oxiproto_core::OxiProtoError::WireFormatError)?;
/// # match tag.field_number {
/// # 1 => self.seq = buf.read_varint32().map_err(::oxiproto_core::OxiProtoError::WireFormatError)?,
/// # _ => buf.skip_field(tag.wire_type).map_err(::oxiproto_core::OxiProtoError::WireFormatError)?,
/// # }
/// # }
/// # Ok(())
/// # }
/// # fn clear(&mut self) { *self = Self::default(); }
/// # }
///
/// let ping = Ping { seq: 7 };
/// // Via OxiReflect (auto-implemented):
/// let handle = ping.reflect_handle();
/// assert_eq!(handle.full_name(), "net.Ping");
/// let bytes = handle.encoded_bytes();
/// // Re-decode with the native codec:
/// let decoded = Ping::decode(bytes).unwrap();
/// assert_eq!(decoded.seq, 7);
/// ```
/// Blanket implementation: every type that is both `OxiMessage` and `OxiName`
/// automatically gains `OxiReflect`.
// ---------------------------------------------------------------------------
// BridgeDecoder — decode an OxiReflectHandle back to a concrete type
// ---------------------------------------------------------------------------
/// Decode an [`OxiReflectHandle`] back into a concrete `T: OxiMessage`.
///
/// This performs the inverse of [`OxiReflect::reflect_handle`]: it takes
/// the wire bytes stored in the handle and decodes them into `T`.
///
/// # Type Safety
///
/// The caller is responsible for ensuring `T` matches the message type that
/// was originally reflected. If the bytes were produced by a different
/// message type the decode will either succeed with garbage values, or fail
/// with a wire error. This mirrors standard protobuf dynamic-typing behaviour
/// (the wire format carries no type annotations).
///
/// # Errors
///
/// Returns an error if the wire bytes are malformed.
///
/// # Example
///
/// ```rust
/// use oxiproto_core::reflect_bridge::{decode_handle, OxiReflect};
/// use oxiproto_core::{OxiMessage, OxiName};
/// # use oxiproto_core::wire::{DecodeBuffer, EncodeBuffer};
/// # use oxiproto_core::OxiProtoResult;
/// #
/// # #[derive(Debug, Default, Clone, PartialEq)]
/// # struct Score { value: u64 }
/// # impl OxiName for Score {
/// # const NAME: &'static str = "Score";
/// # const PACKAGE: &'static str = "";
/// # }
/// # impl OxiMessage for Score {
/// # fn encoded_len(&self) -> usize {
/// # if self.value != 0 { 1 + ::oxiproto_core::wire::varint::encoded_len_varint(self.value) } else { 0 }
/// # }
/// # fn encode_raw(&self, buf: &mut EncodeBuffer) {
/// # if self.value != 0 {
/// # let _ = buf.write_tag(1, ::oxiproto_core::wire::WireType::Varint);
/// # buf.write_varint(self.value);
/// # }
/// # }
/// # fn merge(&mut self, buf: &mut DecodeBuffer) -> OxiProtoResult<()> {
/// # while !buf.is_empty() {
/// # let tag = buf.read_tag().map_err(::oxiproto_core::OxiProtoError::WireFormatError)?;
/// # match tag.field_number {
/// # 1 => self.value = buf.read_varint().map_err(::oxiproto_core::OxiProtoError::WireFormatError)?,
/// # _ => buf.skip_field(tag.wire_type).map_err(::oxiproto_core::OxiProtoError::WireFormatError)?,
/// # }
/// # }
/// # Ok(())
/// # }
/// # fn clear(&mut self) { *self = Self::default(); }
/// # }
///
/// let original = Score { value: 9999 };
/// let handle = original.reflect_handle();
/// let decoded: Score = decode_handle(&handle).unwrap();
/// assert_eq!(decoded, original);
/// ```
// ---------------------------------------------------------------------------
// ReflectMetadata — static type information without an instance
// ---------------------------------------------------------------------------
/// Static reflection metadata for a type that implements [`OxiName`].
///
/// Unlike [`OxiReflect`] (which requires a *value*), `ReflectMetadata` can
/// be constructed without an instance, making it useful for building
/// schema-level registries.
// ---------------------------------------------------------------------------
// MessageRegistry — a lightweight, type-erased registry of OxiMessage types
// ---------------------------------------------------------------------------
/// A lightweight registry mapping fully-qualified proto names to
/// type-erased encode/decode functions.
///
/// This enables runtime dispatch over a set of known `OxiMessage` types
/// without carrying generic parameters, bridging the gap between the statically-
/// typed native layer and a reflection consumer that wants to work by type-name.
///
/// ## Example
///
/// ```rust
/// use oxiproto_core::reflect_bridge::{MessageRegistry, OxiReflect};
/// use oxiproto_core::{OxiMessage, OxiName};
/// # use oxiproto_core::wire::{DecodeBuffer, EncodeBuffer};
/// # use oxiproto_core::OxiProtoResult;
/// #
/// # #[derive(Debug, Default, Clone, PartialEq)]
/// # struct Widget { id: u32 }
/// # impl OxiName for Widget {
/// # const NAME: &'static str = "Widget";
/// # const PACKAGE: &'static str = "ui";
/// # }
/// # impl OxiMessage for Widget {
/// # fn encoded_len(&self) -> usize { if self.id != 0 { 2 } else { 0 } }
/// # fn encode_raw(&self, buf: &mut EncodeBuffer) {
/// # if self.id != 0 {
/// # let _ = buf.write_tag(1, ::oxiproto_core::wire::WireType::Varint);
/// # buf.write_varint32(self.id);
/// # }
/// # }
/// # fn merge(&mut self, buf: &mut DecodeBuffer) -> OxiProtoResult<()> {
/// # while !buf.is_empty() {
/// # let tag = buf.read_tag().map_err(::oxiproto_core::OxiProtoError::WireFormatError)?;
/// # match tag.field_number {
/// # 1 => self.id = buf.read_varint32().map_err(::oxiproto_core::OxiProtoError::WireFormatError)?,
/// # _ => buf.skip_field(tag.wire_type).map_err(::oxiproto_core::OxiProtoError::WireFormatError)?,
/// # }
/// # }
/// # Ok(())
/// # }
/// # fn clear(&mut self) { *self = Self::default(); }
/// # }
///
/// let mut registry = MessageRegistry::new();
/// registry.register::<Widget>();
///
/// // Look up and re-encode a message from raw bytes
/// let widget = Widget { id: 123 };
/// let bytes = widget.encode_to_vec();
/// let re_encoded = registry.encode_by_name("ui.Widget", &bytes).unwrap();
/// assert_eq!(re_encoded, bytes);
/// ```
/// An entry in the registry stores the metadata and a pair of type-erased
/// functions that operate on raw wire bytes without knowing the concrete type.