buffa-reflect 0.2.0

Runtime reflection for the buffa protobuf implementation.
Documentation
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
//! [`Value`], [`MapKey`], and [`SetFieldError`] — the runtime value model
//! that backs [`super::DynamicMessage`].

use std::collections::HashMap;

use buffa::bytes::Bytes;

use crate::{
    dynamic::message::DynamicMessage,
    field::{FieldDescriptor, Kind},
};

/// A protobuf field value carried by [`super::DynamicMessage`].
///
/// One variant per scalar wire type, plus `EnumNumber` (open-enum
/// semantics — any `i32` is acceptable so unknown variants round-trip
/// losslessly), `Message`, `List`, and `Map`. The variant set mirrors
/// `prost-reflect`'s `Value` for consumer migration.
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
    /// `bool`.
    Bool(bool),
    /// `int32` / `sint32` / `sfixed32`.
    I32(i32),
    /// `int64` / `sint64` / `sfixed64`.
    I64(i64),
    /// `uint32` / `fixed32`.
    U32(u32),
    /// `uint64` / `fixed64`.
    U64(u64),
    /// `float`.
    F32(f32),
    /// `double`.
    F64(f64),
    /// `string` (UTF-8).
    String(String),
    /// `bytes`.
    Bytes(Bytes),
    /// Enum variant by number.
    ///
    /// `i32` rather than a typed variant so forward-compat decoding (an
    /// unknown enum number) round-trips byte-identically. Matches
    /// proto3's open-enum semantics.
    EnumNumber(i32),
    /// Sub-message.
    Message(DynamicMessage),
    /// Repeated value (every element validated against the list's
    /// declared element [`Kind`]).
    List(Vec<Value>),
    /// `map<K, V>` field.
    Map(HashMap<MapKey, Value>),
}

/// A protobuf map key.
///
/// Variants are limited to those allowed by the proto spec: floats,
/// bytes, and message/enum keys are forbidden.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MapKey {
    /// `bool`.
    Bool(bool),
    /// `int32` / `sint32` / `sfixed32`.
    I32(i32),
    /// `int64` / `sint64` / `sfixed64`.
    I64(i64),
    /// `uint32` / `fixed32`.
    U32(u32),
    /// `uint64` / `fixed64`.
    U64(u64),
    /// `string`.
    String(String),
}

/// Reasons [`super::DynamicMessage::try_set_field`] (and friends) can fail.
#[derive(Clone, Debug, PartialEq, thiserror::Error)]
pub enum SetFieldError {
    /// The named field / number does not exist on the descriptor.
    #[error("field not found")]
    NotFound,
    /// The supplied value's runtime shape did not match the field.
    #[error("invalid value for field `{}`", field.full_name())]
    InvalidType {
        /// The field that rejected the assignment.
        field: FieldDescriptor,
        /// The offending value (returned for diagnostic purposes).
        value: Box<Value>,
    },
}

impl Value {
    /// The proto default for the given [`Kind`] (singular form).
    ///
    /// For `Message` returns an empty [`super::DynamicMessage`] of the
    /// declared sub-message type. For `Enum`, the zero variant.
    #[must_use]
    pub fn default_value(kind: &Kind) -> Self {
        match kind {
            Kind::Bool => Value::Bool(false),
            Kind::Int32 | Kind::Sint32 | Kind::Sfixed32 => Value::I32(0),
            Kind::Int64 | Kind::Sint64 | Kind::Sfixed64 => Value::I64(0),
            Kind::Uint32 | Kind::Fixed32 => Value::U32(0),
            Kind::Uint64 | Kind::Fixed64 => Value::U64(0),
            Kind::Float => Value::F32(0.0),
            Kind::Double => Value::F64(0.0),
            Kind::String => Value::String(String::new()),
            Kind::Bytes => Value::Bytes(Bytes::new()),
            Kind::Enum(_) => Value::EnumNumber(0),
            Kind::Message(m) => Value::Message(DynamicMessage::new(m.clone())),
        }
    }

    /// The proto default for `field` accounting for cardinality (lists
    /// and maps default to the empty collection).
    ///
    /// Reads any pre-parsed `[default = …]` from the descriptor pool.
    /// When the descriptor declares no explicit default this returns
    /// the zero value for the field's [`Kind`].
    #[must_use]
    pub fn default_value_for_field(field: &FieldDescriptor) -> Self {
        if field.is_list() {
            return Value::List(Vec::new());
        }
        if field.is_map() {
            return Value::Map(HashMap::new());
        }
        if let Some(value) = field.parsed_default_value() {
            return value;
        }
        Value::default_value(&field.kind())
    }

    /// True iff `self` equals the proto default for `kind`.
    ///
    /// Lists and maps are default iff empty. `Message` is default iff
    /// it is a fresh, populated-fields-empty instance.
    #[must_use]
    pub fn is_default(&self, kind: &Kind) -> bool {
        match (self, kind) {
            (Value::Bool(b), Kind::Bool) => !*b,
            (Value::I32(v), Kind::Int32 | Kind::Sint32 | Kind::Sfixed32) => *v == 0,
            (Value::I64(v), Kind::Int64 | Kind::Sint64 | Kind::Sfixed64) => *v == 0,
            (Value::U32(v), Kind::Uint32 | Kind::Fixed32) => *v == 0,
            (Value::U64(v), Kind::Uint64 | Kind::Fixed64) => *v == 0,
            (Value::F32(v), Kind::Float) => *v == 0.0,
            (Value::F64(v), Kind::Double) => *v == 0.0,
            (Value::String(s), Kind::String) => s.is_empty(),
            (Value::Bytes(b), Kind::Bytes) => b.is_empty(),
            (Value::EnumNumber(n), Kind::Enum(_)) => *n == 0,
            (Value::Message(m), Kind::Message(_)) => m.is_empty(),
            (Value::List(l), _) => l.is_empty(),
            (Value::Map(m), _) => m.is_empty(),
            _ => false,
        }
    }

    /// Validate that `self` matches `field`'s declared shape.
    ///
    /// Recursive: list elements validate against the element kind, map
    /// keys/values against their declared kinds, and sub-message values
    /// must share the field's expected message descriptor (compared
    /// pool-pointer + index).
    #[must_use]
    pub fn is_valid_for_field(&self, field: &FieldDescriptor) -> bool {
        if field.is_list() {
            if let Value::List(items) = self {
                let kind = field.kind();
                return items.iter().all(|v| value_matches_kind(v, &kind));
            }
            return false;
        }
        if field.is_map() {
            if let Value::Map(entries) = self {
                let (key_kind, value_kind) = match map_entry_kinds(field) {
                    Some(kinds) => kinds,
                    None => return false,
                };
                return entries.iter().all(|(k, v)| {
                    map_key_matches_kind(k, &key_kind) && value_matches_kind(v, &value_kind)
                });
            }
            return false;
        }
        value_matches_kind(self, &field.kind())
    }

    // Typed accessors ------------------------------------------------------

    /// Returns the `bool` payload, if any.
    #[must_use]
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            Value::Bool(v) => Some(*v),
            _ => None,
        }
    }
    /// Returns the `i32` payload, if any.
    #[must_use]
    pub fn as_i32(&self) -> Option<i32> {
        match self {
            Value::I32(v) => Some(*v),
            _ => None,
        }
    }
    /// Returns the `i64` payload, if any.
    #[must_use]
    pub fn as_i64(&self) -> Option<i64> {
        match self {
            Value::I64(v) => Some(*v),
            _ => None,
        }
    }
    /// Returns the `u32` payload, if any.
    #[must_use]
    pub fn as_u32(&self) -> Option<u32> {
        match self {
            Value::U32(v) => Some(*v),
            _ => None,
        }
    }
    /// Returns the `u64` payload, if any.
    #[must_use]
    pub fn as_u64(&self) -> Option<u64> {
        match self {
            Value::U64(v) => Some(*v),
            _ => None,
        }
    }
    /// Returns the `f32` payload, if any.
    #[must_use]
    pub fn as_f32(&self) -> Option<f32> {
        match self {
            Value::F32(v) => Some(*v),
            _ => None,
        }
    }
    /// Returns the `f64` payload, if any.
    #[must_use]
    pub fn as_f64(&self) -> Option<f64> {
        match self {
            Value::F64(v) => Some(*v),
            _ => None,
        }
    }
    /// Returns the `&str` payload, if any.
    #[must_use]
    pub fn as_str(&self) -> Option<&str> {
        match self {
            Value::String(v) => Some(v.as_str()),
            _ => None,
        }
    }
    /// Returns the `&Bytes` payload, if any.
    #[must_use]
    pub fn as_bytes(&self) -> Option<&Bytes> {
        match self {
            Value::Bytes(v) => Some(v),
            _ => None,
        }
    }
    /// Returns the enum-number payload, if any.
    #[must_use]
    pub fn as_enum_number(&self) -> Option<i32> {
        match self {
            Value::EnumNumber(v) => Some(*v),
            _ => None,
        }
    }
    /// Returns the [`super::DynamicMessage`] payload, if any.
    #[must_use]
    pub fn as_message(&self) -> Option<&DynamicMessage> {
        match self {
            Value::Message(v) => Some(v),
            _ => None,
        }
    }
    /// Mutable form of [`Self::as_message`].
    pub fn as_message_mut(&mut self) -> Option<&mut DynamicMessage> {
        match self {
            Value::Message(v) => Some(v),
            _ => None,
        }
    }
    /// Returns the list payload, if any.
    #[must_use]
    pub fn as_list(&self) -> Option<&[Value]> {
        match self {
            Value::List(v) => Some(v.as_slice()),
            _ => None,
        }
    }
    /// Mutable form of [`Self::as_list`].
    pub fn as_list_mut(&mut self) -> Option<&mut Vec<Value>> {
        match self {
            Value::List(v) => Some(v),
            _ => None,
        }
    }
    /// Returns the map payload, if any.
    #[must_use]
    pub fn as_map(&self) -> Option<&HashMap<MapKey, Value>> {
        match self {
            Value::Map(v) => Some(v),
            _ => None,
        }
    }
    /// Mutable form of [`Self::as_map`].
    pub fn as_map_mut(&mut self) -> Option<&mut HashMap<MapKey, Value>> {
        match self {
            Value::Map(v) => Some(v),
            _ => None,
        }
    }
}

impl MapKey {
    /// The proto default for the given map-key kind.
    ///
    /// # Panics
    ///
    /// Panics if `kind` is not a legal map-key shape (floats, bytes,
    /// messages, and enums are forbidden by the proto spec). The pool
    /// builder rejects such map declarations, so this only fires for
    /// hand-constructed misuse.
    #[must_use]
    pub fn default_value(kind: &Kind) -> Self {
        match kind {
            Kind::Bool => MapKey::Bool(false),
            Kind::Int32 | Kind::Sint32 | Kind::Sfixed32 => MapKey::I32(0),
            Kind::Int64 | Kind::Sint64 | Kind::Sfixed64 => MapKey::I64(0),
            Kind::Uint32 | Kind::Fixed32 => MapKey::U32(0),
            Kind::Uint64 | Kind::Fixed64 => MapKey::U64(0),
            Kind::String => MapKey::String(String::new()),
            other => panic!("invalid map-key kind: {other:?}"),
        }
    }
}

/// Used by both singular validation and list-element / map-value validation.
pub(crate) fn value_matches_kind(value: &Value, kind: &Kind) -> bool {
    match (value, kind) {
        (Value::Bool(_), Kind::Bool) => true,
        (Value::I32(_), Kind::Int32 | Kind::Sint32 | Kind::Sfixed32) => true,
        (Value::I64(_), Kind::Int64 | Kind::Sint64 | Kind::Sfixed64) => true,
        (Value::U32(_), Kind::Uint32 | Kind::Fixed32) => true,
        (Value::U64(_), Kind::Uint64 | Kind::Fixed64) => true,
        (Value::F32(_), Kind::Float) => true,
        (Value::F64(_), Kind::Double) => true,
        (Value::String(_), Kind::String) => true,
        (Value::Bytes(_), Kind::Bytes) => true,
        (Value::EnumNumber(_), Kind::Enum(_)) => true,
        // Compare by FQN rather than `Arc::ptr_eq` so that pool
        // mutations (`add_file_descriptor_set` calls `Arc::make_mut`)
        // don't invalidate previously-constructed sub-messages.
        (Value::Message(m), Kind::Message(d)) => m.descriptor().full_name() == d.full_name(),
        _ => false,
    }
}

pub(crate) fn map_key_matches_kind(key: &MapKey, kind: &Kind) -> bool {
    matches!(
        (key, kind),
        (MapKey::Bool(_), Kind::Bool)
            | (MapKey::I32(_), Kind::Int32 | Kind::Sint32 | Kind::Sfixed32)
            | (MapKey::I64(_), Kind::Int64 | Kind::Sint64 | Kind::Sfixed64)
            | (MapKey::U32(_), Kind::Uint32 | Kind::Fixed32)
            | (MapKey::U64(_), Kind::Uint64 | Kind::Fixed64)
            | (MapKey::String(_), Kind::String)
    )
}

/// Resolve the `(key_kind, value_kind)` of a map field's synthetic
/// entry message.
pub(crate) fn map_entry_kinds(field: &FieldDescriptor) -> Option<(Kind, Kind)> {
    let Kind::Message(entry) = field.kind() else {
        return None;
    };
    if !entry.is_map_entry() {
        return None;
    }
    let key = entry.get_field_by_number(1)?.kind();
    let value = entry.get_field_by_number(2)?.kind();
    Some((key, value))
}

/// Convenience constructors for common literal types.
impl From<bool> for Value {
    fn from(v: bool) -> Self {
        Value::Bool(v)
    }
}
impl From<i32> for Value {
    fn from(v: i32) -> Self {
        Value::I32(v)
    }
}
impl From<i64> for Value {
    fn from(v: i64) -> Self {
        Value::I64(v)
    }
}
impl From<u32> for Value {
    fn from(v: u32) -> Self {
        Value::U32(v)
    }
}
impl From<u64> for Value {
    fn from(v: u64) -> Self {
        Value::U64(v)
    }
}
impl From<f32> for Value {
    fn from(v: f32) -> Self {
        Value::F32(v)
    }
}
impl From<f64> for Value {
    fn from(v: f64) -> Self {
        Value::F64(v)
    }
}
impl From<String> for Value {
    fn from(v: String) -> Self {
        Value::String(v)
    }
}
impl From<&str> for Value {
    fn from(v: &str) -> Self {
        Value::String(v.to_owned())
    }
}
impl From<Bytes> for Value {
    fn from(v: Bytes) -> Self {
        Value::Bytes(v)
    }
}
impl From<Vec<u8>> for Value {
    fn from(v: Vec<u8>) -> Self {
        Value::Bytes(Bytes::from(v))
    }
}
impl From<DynamicMessage> for Value {
    fn from(v: DynamicMessage) -> Self {
        Value::Message(v)
    }
}