oxiproto-build 0.1.2

Pure Rust .proto → Rust codegen (no protoc required)
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
#![forbid(unsafe_code)]

//! Native proto2/proto3 AST types.
//!
//! Every node that spans source code carries a [`Span`] field.  All types
//! derive `Debug`, `Clone`, and `PartialEq`.

use crate::parser::span::Span;

// ---------------------------------------------------------------------------
// File-level container
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// Protobuf Edition
// ---------------------------------------------------------------------------

/// A Protobuf Edition identifier.
///
/// Editions replace the `syntax` statement starting from Edition 2023.
/// The descriptor builder treats Edition 2023 as proto3-like semantics
/// (implicit field presence, no required fields) with additional opt-in
/// feature flags via field options.
#[derive(Debug, Clone, PartialEq)]
pub enum Edition {
    /// `edition = "2023"` — the first generally-available Protobuf Edition.
    Edition2023,
    /// An unrecognised edition string, stored verbatim for forward compatibility.
    Unknown(String),
}

impl Edition {
    /// Construct an [`Edition`] from the string literal that followed `=`.
    pub fn parse(s: &str) -> Self {
        match s {
            "2023" => Edition::Edition2023,
            other => Edition::Unknown(other.to_owned()),
        }
    }

    /// Return the canonical edition string as it appears in `FileDescriptorProto.syntax`
    /// ("editions" is the sentinel value used by protoc for edition-based files).
    pub fn syntax_sentinel() -> &'static str {
        "editions"
    }
}

/// The top-level container for a parsed `.proto` file.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ProtoFile {
    /// Value of the `syntax` statement, e.g. `"proto3"`.
    pub syntax: Option<String>,
    /// Parsed edition from `edition = "2023";` (mutually exclusive with `syntax`).
    pub edition: Option<Edition>,
    /// Value of the `package` statement, e.g. `"google.protobuf"`.
    pub package: Option<String>,
    /// All `import` statements, in declaration order.
    pub imports: Vec<Import>,
    /// Top-level `option` statements.
    pub options: Vec<ProtoOption>,
    /// Top-level `message` definitions.
    pub messages: Vec<Message>,
    /// Top-level `enum` definitions.
    pub enums: Vec<Enum>,
    /// Top-level `service` definitions.
    pub services: Vec<Service>,
    /// Top-level `extend` blocks (proto2 only).
    pub extends: Vec<ExtendBlock>,
}

// ---------------------------------------------------------------------------
// Import
// ---------------------------------------------------------------------------

/// A single `import` statement.
#[derive(Debug, Clone, PartialEq)]
pub struct Import {
    /// The import path string, e.g. `"google/protobuf/timestamp.proto"`.
    pub path: String,
    /// Optional modifier (`public`, `weak`, or none).
    pub modifier: ImportModifier,
    /// Source span of the entire import statement.
    pub span: Span,
}

/// The optional modifier on an `import` statement.
#[derive(Debug, Clone, PartialEq)]
pub enum ImportModifier {
    /// Plain `import "..."`.
    None,
    /// `import public "..."`.
    Public,
    /// `import weak "..."`.
    Weak,
}

// ---------------------------------------------------------------------------
// Message
// ---------------------------------------------------------------------------

/// A `message` definition.
#[derive(Debug, Clone, PartialEq)]
pub struct Message {
    /// The message name.
    pub name: String,
    /// Regular fields (including map fields).
    pub fields: Vec<Field>,
    /// `oneof` blocks.
    pub oneofs: Vec<Oneof>,
    /// Nested `message` definitions.
    pub nested_messages: Vec<Message>,
    /// Nested `enum` definitions.
    pub nested_enums: Vec<Enum>,
    /// `reserved` statements.
    pub reserved: Vec<Reserved>,
    /// `option` statements inside the message body.
    pub options: Vec<ProtoOption>,
    /// `extensions` statements (proto2 only).
    pub extensions: Vec<ExtensionRange>,
    /// Source span of the entire message definition.
    pub span: Span,
}

// ---------------------------------------------------------------------------
// Field
// ---------------------------------------------------------------------------

/// A message field or oneof member field.
#[derive(Debug, Clone, PartialEq)]
pub struct Field {
    /// Field label (singular, optional, or repeated).
    pub label: FieldLabel,
    /// Field type.
    pub ty: FieldType,
    /// Field name.
    pub name: String,
    /// Field number.
    pub number: i32,
    /// Inline field options (inside `[...]`).
    pub options: Vec<ProtoOption>,
    /// Source span of the entire field declaration.
    pub span: Span,
}

/// The label on a field.
#[derive(Debug, Clone, PartialEq)]
pub enum FieldLabel {
    /// No explicit label (proto3 default: singular).
    Singular,
    /// Explicit `optional` keyword.
    Optional,
    /// `repeated` keyword.
    Repeated,
    /// `required` keyword (proto2 only).
    Required,
}

// ---------------------------------------------------------------------------
// Extension range (proto2)
// ---------------------------------------------------------------------------

/// An `extensions` range inside a proto2 message.
///
/// `extensions 100 to 199;` → `ExtensionRange { start: 100, end: Some(199) }`
/// `extensions 200;` → `ExtensionRange { start: 200, end: None }`
/// `extensions 1000 to max;` → `ExtensionRange { start: 1000, end: None }` (open-ended)
#[derive(Debug, Clone, PartialEq)]
pub struct ExtensionRange {
    /// Inclusive start of the range.
    pub start: u32,
    /// Inclusive end of the range, or `None` for open-ended / bare number.
    pub end: Option<u32>,
}

// ---------------------------------------------------------------------------
// Extend block (proto2)
// ---------------------------------------------------------------------------

/// A top-level `extend` block (proto2 only).
///
/// ```proto2
/// extend Foo {
///   optional int32 bar = 100;
/// }
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct ExtendBlock {
    /// The name of the message being extended.  May be a package-qualified
    /// name such as `"Foo"` or `"foo.Bar"`.
    pub extendee: String,
    /// The extension fields defined in this block.
    pub fields: Vec<Field>,
}

/// The type of a field.
#[derive(Debug, Clone, PartialEq)]
pub enum FieldType {
    /// A scalar (primitive) type.
    Scalar(ScalarType),
    /// A `map<K, V>` type.
    Map {
        /// Map key type (must be a scalar, not `bytes` or `float`/`double`
        /// in valid proto3, but we store whatever was parsed).
        key: ScalarType,
        /// Map value type.
        value: Box<FieldType>,
    },
    /// A named message or enum reference, e.g. `"Foo"`, `"foo.Bar"`,
    /// `".google.protobuf.Timestamp"`.
    Named(String),
    /// A proto2 `group` field type.  The string is the group/message name
    /// (capitalized, e.g. `"Result"`).  The actual wire field name is the
    /// lowercased version (e.g. `"result"`).
    ///
    /// A group synthesises a nested message in the enclosing message's
    /// `nested_messages` and emits a field with `type = TYPE_GROUP (10)`.
    Group(String),
}

/// The 15 proto3 scalar (primitive) types.
#[derive(Debug, Clone, PartialEq, Copy)]
pub enum ScalarType {
    /// `double` — 64-bit IEEE 754 floating point.
    Double,
    /// `float` — 32-bit IEEE 754 floating point.
    Float,
    /// `int32` — variable-length 32-bit signed integer.
    Int32,
    /// `int64` — variable-length 64-bit signed integer.
    Int64,
    /// `uint32` — variable-length 32-bit unsigned integer.
    Uint32,
    /// `uint64` — variable-length 64-bit unsigned integer.
    Uint64,
    /// `sint32` — ZigZag-encoded 32-bit signed integer.
    Sint32,
    /// `sint64` — ZigZag-encoded 64-bit signed integer.
    Sint64,
    /// `fixed32` — fixed 4-byte 32-bit unsigned integer.
    Fixed32,
    /// `fixed64` — fixed 8-byte 64-bit unsigned integer.
    Fixed64,
    /// `sfixed32` — fixed 4-byte 32-bit signed integer.
    Sfixed32,
    /// `sfixed64` — fixed 8-byte 64-bit signed integer.
    Sfixed64,
    /// `bool` — boolean.
    Bool,
    /// `string` — UTF-8 string.
    String,
    /// `bytes` — arbitrary byte sequence.
    Bytes,
}

// ---------------------------------------------------------------------------
// Oneof
// ---------------------------------------------------------------------------

/// A `oneof` block inside a message.
#[derive(Debug, Clone, PartialEq)]
pub struct Oneof {
    /// The oneof name.
    pub name: String,
    /// Members of this oneof.  Member fields have no label
    /// (they are implicitly `Optional`).
    pub fields: Vec<Field>,
    /// Options declared inside the oneof block.
    pub options: Vec<ProtoOption>,
    /// Source span of the entire oneof block.
    pub span: Span,
}

// ---------------------------------------------------------------------------
// Enum
// ---------------------------------------------------------------------------

/// An `enum` definition.
#[derive(Debug, Clone, PartialEq)]
pub struct Enum {
    /// The enum name.
    pub name: String,
    /// Enum value declarations.
    pub values: Vec<EnumValue>,
    /// `reserved` statements inside the enum.
    pub reserved: Vec<Reserved>,
    /// `option` statements inside the enum.
    pub options: Vec<ProtoOption>,
    /// Source span of the entire enum definition.
    pub span: Span,
}

/// A single value inside an `enum` definition.
#[derive(Debug, Clone, PartialEq)]
pub struct EnumValue {
    /// The value name, e.g. `UNKNOWN`.
    pub name: String,
    /// The numeric value, e.g. `0`.
    pub number: i32,
    /// Inline options (inside `[...]`).
    pub options: Vec<ProtoOption>,
    /// Source span of the entire enum value declaration.
    pub span: Span,
}

// ---------------------------------------------------------------------------
// Service
// ---------------------------------------------------------------------------

/// A `service` definition.
#[derive(Debug, Clone, PartialEq)]
pub struct Service {
    /// The service name.
    pub name: String,
    /// RPC method declarations.
    pub methods: Vec<Method>,
    /// `option` statements inside the service.
    pub options: Vec<ProtoOption>,
    /// Source span of the entire service definition.
    pub span: Span,
}

/// A single `rpc` method inside a service.
#[derive(Debug, Clone, PartialEq)]
pub struct Method {
    /// The method name.
    pub name: String,
    /// Input type name (without the `stream` keyword; streaming is in
    /// `client_streaming`).
    pub input_type: String,
    /// Output type name.
    pub output_type: String,
    /// `true` if the request is a client-streaming RPC.
    pub client_streaming: bool,
    /// `true` if the response is a server-streaming RPC.
    pub server_streaming: bool,
    /// `option` statements inside the rpc body (when a `{...}` block is used).
    pub options: Vec<ProtoOption>,
    /// Source span of the entire RPC declaration.
    pub span: Span,
}

// ---------------------------------------------------------------------------
// Options
// ---------------------------------------------------------------------------

/// A parsed `option` statement or inline field option.
#[derive(Debug, Clone, PartialEq)]
pub struct ProtoOption {
    /// The option name, e.g. `"deprecated"`, `"(foo.bar).baz"`.
    pub name: String,
    /// The option value.
    pub value: OptionValue,
    /// Source span of the option.
    pub span: Span,
}

/// The value of an option.
#[derive(Debug, Clone, PartialEq)]
pub enum OptionValue {
    /// An identifier value (enum member names, etc.), e.g. `LABEL`.
    Ident(String),
    /// A string literal value.
    Str(String),
    /// An integer value.
    Int(i64),
    /// A floating-point value.
    Float(f64),
    /// A boolean value (`true` or `false`).
    Bool(bool),
    /// A structured proto message literal value, e.g. `{ id: 1, name: "foo" }`.
    /// Each entry is `(field_name, value)`.
    MessageLiteral(Vec<(String, OptionValue)>),
}

// ---------------------------------------------------------------------------
// Reserved
// ---------------------------------------------------------------------------

/// A `reserved` statement inside a message or enum.
#[derive(Debug, Clone, PartialEq)]
pub enum Reserved {
    /// Reserved field/enum-value number ranges.
    Ranges(Vec<ReservedRange>),
    /// Reserved field/enum-value names.
    Names(Vec<String>),
}

/// A single range inside a `reserved` statement.
#[derive(Debug, Clone, PartialEq)]
pub struct ReservedRange {
    /// The start of the range (inclusive).
    pub from: i32,
    /// The end of the range.
    pub to: ReservedRangeTo,
}

/// The upper bound of a reserved range.
#[derive(Debug, Clone, PartialEq)]
pub enum ReservedRangeTo {
    /// An explicit numeric upper bound (inclusive).
    Number(i32),
    /// The `max` keyword — the range extends to the maximum field number.
    Max,
}