tabnas-proto 0.6.2

Parse Protocol Buffers .proto IDL (proto2, proto3, edition 2023/2024) into FileDescriptorProto-shaped values, using the tabnas engine and an ABNF grammar
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
/* Copyright (c) 2026 Richard Rodger and other contributors, MIT License */

//! FileDescriptorProto-shaped output types.
//!
//! This mirrors the canonical `descriptor.proto` message set (the shape
//! `protoc --descriptor_set_out` produces, in its JSON form): camelCase
//! fields, enum values as their string names (`TYPE_INT32`,
//! `LABEL_REPEATED`). Only fields the parser can populate from a single
//! `.proto` source are included; cross-file type resolution is left to a
//! later pass, so `type_name` is stored as written.
//!
//! Rust port of `ts/src/descriptor.ts`.

use indexmap::IndexMap;
use serde::{Serialize, Serializer};
use serde_json::value::RawValue;

use crate::jsnum::js_number_to_string;

/// A field's cardinality label.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum FieldLabel {
    #[serde(rename = "LABEL_OPTIONAL")]
    Optional,
    #[serde(rename = "LABEL_REQUIRED")]
    Required,
    #[serde(rename = "LABEL_REPEATED")]
    Repeated,
}

impl FieldLabel {
    /// The name descriptor JSON carries, such as `LABEL_REPEATED`.
    pub fn as_str(self) -> &'static str {
        match self {
            FieldLabel::Optional => "LABEL_OPTIONAL",
            FieldLabel::Required => "LABEL_REQUIRED",
            FieldLabel::Repeated => "LABEL_REPEATED",
        }
    }
}

/// A field's wire type, for the types a single source file settles.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum FieldType {
    #[serde(rename = "TYPE_DOUBLE")]
    Double,
    #[serde(rename = "TYPE_FLOAT")]
    Float,
    #[serde(rename = "TYPE_INT64")]
    Int64,
    #[serde(rename = "TYPE_UINT64")]
    Uint64,
    #[serde(rename = "TYPE_INT32")]
    Int32,
    #[serde(rename = "TYPE_FIXED64")]
    Fixed64,
    #[serde(rename = "TYPE_FIXED32")]
    Fixed32,
    #[serde(rename = "TYPE_BOOL")]
    Bool,
    #[serde(rename = "TYPE_STRING")]
    String,
    #[serde(rename = "TYPE_GROUP")]
    Group,
    #[serde(rename = "TYPE_MESSAGE")]
    Message,
    #[serde(rename = "TYPE_BYTES")]
    Bytes,
    #[serde(rename = "TYPE_UINT32")]
    Uint32,
    #[serde(rename = "TYPE_ENUM")]
    Enum,
    #[serde(rename = "TYPE_SFIXED32")]
    Sfixed32,
    #[serde(rename = "TYPE_SFIXED64")]
    Sfixed64,
    #[serde(rename = "TYPE_SINT32")]
    Sint32,
    #[serde(rename = "TYPE_SINT64")]
    Sint64,
}

impl FieldType {
    /// The name descriptor JSON carries, such as `TYPE_INT32`.
    pub fn as_str(self) -> &'static str {
        match self {
            FieldType::Double => "TYPE_DOUBLE",
            FieldType::Float => "TYPE_FLOAT",
            FieldType::Int64 => "TYPE_INT64",
            FieldType::Uint64 => "TYPE_UINT64",
            FieldType::Int32 => "TYPE_INT32",
            FieldType::Fixed64 => "TYPE_FIXED64",
            FieldType::Fixed32 => "TYPE_FIXED32",
            FieldType::Bool => "TYPE_BOOL",
            FieldType::String => "TYPE_STRING",
            FieldType::Group => "TYPE_GROUP",
            FieldType::Message => "TYPE_MESSAGE",
            FieldType::Bytes => "TYPE_BYTES",
            FieldType::Uint32 => "TYPE_UINT32",
            FieldType::Enum => "TYPE_ENUM",
            FieldType::Sfixed32 => "TYPE_SFIXED32",
            FieldType::Sfixed64 => "TYPE_SFIXED64",
            FieldType::Sint32 => "TYPE_SINT32",
            FieldType::Sint64 => "TYPE_SINT64",
        }
    }
}

/// Symbol visibility, an edition-2024 feature (`export` / `local`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum SymbolVisibility {
    #[serde(rename = "VISIBILITY_EXPORT")]
    Export,
    #[serde(rename = "VISIBILITY_LOCAL")]
    Local,
}

impl SymbolVisibility {
    /// The name descriptor JSON carries, such as `VISIBILITY_EXPORT`.
    pub fn as_str(self) -> &'static str {
        match self {
            SymbolVisibility::Export => "VISIBILITY_EXPORT",
            SymbolVisibility::Local => "VISIBILITY_LOCAL",
        }
    }
}

/// An option's value, as the source wrote it.
///
/// The canonical `OptionValue` union also admits a nested map, and no
/// input produces one: an aggregate value (`option (f) = { a: 1 };`) is
/// kept as the text between its braces, as protoc records it (every
/// comment turned to the spaces and newlines that hold the layout; see
/// `aggregate.rs`), so the three variants here are the whole range the
/// walk emits.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(untagged)]
pub enum OptionValue {
    Bool(bool),
    /// A numeric constant, read exactly as JavaScript's `Number()` reads
    /// it. Serializes as `null` when the source names a value no JSON
    /// number can hold, which is what `JSON.stringify` writes for `NaN`
    /// and the infinities.
    Number(#[serde(serialize_with = "json_number")] f64),
    Str(String),
}

/// An option set, keyed by the option name exactly as written
/// (`ctype`, `(foo)`, `foo.(.bar.baz).qux`), in source order.
pub type Options = IndexMap<String, OptionValue>;

/// A number spelt exactly as `JSON.stringify` spells it.
///
/// The descriptor's JSON is the package's output format, and matching the
/// canonical runtime means matching its TEXT, not only its value. Rust's
/// `f64` formatting is not ECMA-262 6.1.6.1.20: it renders every float in
/// float form (`1.0` for a field number), it switches to exponent form at
/// different magnitudes (`1e-6` where JavaScript writes `0.000001`, and
/// `1e+20` where JavaScript writes `100000000000000000000`), and it keeps
/// the sign of a negative zero. [`js_number_to_string`] is the algorithm;
/// this hands its text to `serde_json` verbatim through a `RawValue`.
///
/// Non-finite values have no JSON spelling at all: `JSON.stringify`
/// writes `null` for them and so does this.
pub(crate) fn json_number<S: Serializer>(value: &f64, serializer: S) -> Result<S::Ok, S::Error> {
    if !value.is_finite() {
        return serializer.serialize_none();
    }
    let text = js_number_to_string(*value);
    let raw = RawValue::from_string(text).map_err(serde::ser::Error::custom)?;
    raw.serialize(serializer)
}

/// A numeric range. `end` is EXCLUSIVE for message extension and reserved
/// ranges and INCLUSIVE for enum reserved ranges, the same asymmetry
/// protoc has.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct DescriptorRange {
    #[serde(serialize_with = "json_number")]
    pub start: f64,
    #[serde(serialize_with = "json_number")]
    pub end: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub options: Option<Options>,
}

impl DescriptorRange {
    /// A range with no options.
    pub fn new(start: f64, end: f64) -> Self {
        DescriptorRange {
            start,
            end,
            options: None,
        }
    }
}

/// One field, extension, or synthesised map-entry leaf.
#[derive(Debug, Clone, PartialEq, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct FieldDescriptorProto {
    pub name: String,
    #[serde(serialize_with = "json_number")]
    pub number: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<FieldLabel>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub r#type: Option<FieldType>,
    /// Set for message, enum and group field types (resolution deferred).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub type_name: Option<String>,
    /// For an `extend` member: the message being extended, as written.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extendee: Option<String>,
    /// The `json_name = "..."` pseudo-option, lifted out of `options`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub json_name: Option<String>,
    /// The `default = ...` pseudo-option, lifted out of `options`. Always
    /// a string, as in descriptor.proto; the literal is kept as written.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_value: Option<String>,
    /// proto3 explicit `optional`, which synthesises a single-field oneof.
    #[serde(skip_serializing_if = "is_false")]
    pub proto3_optional: bool,
    /// The oneof this field belongs to, as an index into the message's
    /// `oneof_decl`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub oneof_index: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub options: Option<Options>,
}

fn is_false(value: &bool) -> bool {
    !*value
}

/// One `NAME = number` entry in an enum.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct EnumValueDescriptorProto {
    pub name: String,
    #[serde(serialize_with = "json_number")]
    pub number: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub options: Option<Options>,
}

/// An enum definition.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EnumDescriptorProto {
    pub name: String,
    pub value: Vec<EnumValueDescriptorProto>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reserved_range: Option<Vec<DescriptorRange>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reserved_name: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub visibility: Option<SymbolVisibility>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub options: Option<Options>,
}

impl EnumDescriptorProto {
    /// An empty enum with the given name.
    pub fn new(name: impl Into<String>) -> Self {
        EnumDescriptorProto {
            name: name.into(),
            value: Vec::new(),
            reserved_range: None,
            reserved_name: None,
            visibility: None,
            options: None,
        }
    }
}

/// A oneof declaration.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct OneofDescriptorProto {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub options: Option<Options>,
}

/// A message type.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DescriptorProto {
    pub name: String,
    pub field: Vec<FieldDescriptorProto>,
    pub nested_type: Vec<DescriptorProto>,
    pub enum_type: Vec<EnumDescriptorProto>,
    pub oneof_decl: Vec<OneofDescriptorProto>,
    pub extension: Vec<FieldDescriptorProto>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extension_range: Option<Vec<DescriptorRange>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reserved_range: Option<Vec<DescriptorRange>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reserved_name: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub visibility: Option<SymbolVisibility>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub options: Option<Options>,
}

impl DescriptorProto {
    /// An empty message with the given name.
    pub fn new(name: impl Into<String>) -> Self {
        DescriptorProto {
            name: name.into(),
            field: Vec::new(),
            nested_type: Vec::new(),
            enum_type: Vec::new(),
            oneof_decl: Vec::new(),
            extension: Vec::new(),
            extension_range: None,
            reserved_range: None,
            reserved_name: None,
            visibility: None,
            options: None,
        }
    }
}

/// One rpc in a service.
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MethodDescriptorProto {
    pub name: String,
    pub input_type: String,
    pub output_type: String,
    #[serde(skip_serializing_if = "is_false")]
    pub client_streaming: bool,
    #[serde(skip_serializing_if = "is_false")]
    pub server_streaming: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub options: Option<Options>,
}

/// A service definition.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ServiceDescriptorProto {
    pub name: String,
    pub method: Vec<MethodDescriptorProto>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub options: Option<Options>,
}

/// The root descriptor for one parsed `.proto` file.
#[derive(Debug, Clone, PartialEq, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct FileDescriptorProto {
    /// Not present in source; callers may set it.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub package: Option<String>,
    pub dependency: Vec<String>,
    pub public_dependency: Vec<usize>,
    pub weak_dependency: Vec<usize>,
    /// `import option "..."` targets (edition 2024), when used.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub option_dependency: Option<Vec<String>>,
    pub message_type: Vec<DescriptorProto>,
    pub enum_type: Vec<EnumDescriptorProto>,
    pub service: Vec<ServiceDescriptorProto>,
    pub extension: Vec<FieldDescriptorProto>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub options: Option<Options>,
    /// `proto2` or `proto3` for syntax files; `editions` for edition
    /// files.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub syntax: Option<String>,
    /// `EDITION_2023` or `EDITION_2024` for edition files, absent
    /// otherwise.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub edition: Option<String>,
}

/// protoc's sentinel for `to max` in a message range. Field numbers stop
/// at 2^29-1, so an exclusive `end` is 2^29.
pub const MAX_FIELD_NUMBER_END: f64 = 536_870_912.0;
/// protoc's sentinel for `to max` in a `message_set_wire_format` message,
/// which may use the full 32-bit space.
pub const MAX_MESSAGE_SET_END: f64 = 2_147_483_647.0;
/// protoc's sentinel for `to max` in an enum reserved range. Enum numbers
/// are plain int32 and enum reserved ranges are inclusive.
pub const MAX_ENUM_NUMBER: f64 = 2_147_483_647.0;

/// Scalar protobuf types and their `FieldDescriptorProto` type, in the
/// order `ts/src/descriptor.ts` declares them. A field whose type is not
/// here is a message, enum or group reference (resolution deferred) and
/// gets a `type_name` instead.
pub const SCALAR_TYPES: &[(&str, FieldType)] = &[
    ("double", FieldType::Double),
    ("float", FieldType::Float),
    ("int32", FieldType::Int32),
    ("int64", FieldType::Int64),
    ("uint32", FieldType::Uint32),
    ("uint64", FieldType::Uint64),
    ("sint32", FieldType::Sint32),
    ("sint64", FieldType::Sint64),
    ("fixed32", FieldType::Fixed32),
    ("fixed64", FieldType::Fixed64),
    ("sfixed32", FieldType::Sfixed32),
    ("sfixed64", FieldType::Sfixed64),
    ("bool", FieldType::Bool),
    ("string", FieldType::String),
    ("bytes", FieldType::Bytes),
];

/// The scalar type a bare type name names, or `None` for a reference the
/// parser leaves unresolved.
pub fn scalar_type(name: &str) -> Option<FieldType> {
    SCALAR_TYPES
        .iter()
        .find(|(scalar, _)| *scalar == name)
        .map(|(_, field_type)| *field_type)
}