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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
use std::ops::RangeInclusive;
use std::path::PathBuf;

use indexmap::IndexMap;
use proc_macro2::Span;
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::Ident;
use syn::LitBool;
use syn::LitFloat;
use syn::LitInt;
use syn::LitStr;
use syn::Token;
use syn_prelude::ToIdent;
use syn_prelude::ToLitStr;

use crate::dep::Deps;

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct ProtobufPath {
    pub segments: Punctuated<Ident, Token![.]>,
}

impl ProtobufPath {
    pub fn from_ident(name: Ident) -> Self {
        let mut slf = Self {
            segments: Punctuated::new(),
        };
        slf.segments.push(name);
        slf
    }

    pub fn new_empty(span: Span) -> Self {
        let mut segments = Punctuated::new();
        segments.push_value(("google", span).to_ident());
        segments.push_punct(Token![.](span));
        segments.push_value(("protobuf", span).to_ident());
        segments.push_punct(Token![.](span));
        segments.push(("Empty", span).to_ident());
        Self { segments }
    }
}

impl ProtobufPath {
    pub fn local_name(&self) -> &Ident {
        if let Some(last) = self.segments.last() {
            return last;
        } else {
            unreachable!()
        }
    }
}

/// Protobuf syntax.
#[derive(Debug, Clone, Copy)]
pub enum Syntax {
    /// Protobuf syntax [2](https://developers.google.com/protocol-buffers/docs/proto)
    Proto2(Span),
    /// Protobuf syntax [3](https://developers.google.com/protocol-buffers/docs/proto3) (default)
    Proto3(Option<Span>),
}

impl Syntax {
    pub fn version(&self) -> usize {
        match self {
            Self::Proto2(_) => 2,
            Self::Proto3(_) => 3,
        }
    }
}

/// A field rule
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum Modifier {
    /// A well-formed message can have zero or one of this field (but not more than one).
    Optional,
    /// This field can be repeated any number of times (including zero) in a well-formed message.
    /// The order of the repeated values will be preserved.
    Repeated,
    /// A well-formed message must have exactly one of this field.
    Required,
}

/// Protobuf group
#[derive(Debug, Clone)]
pub struct Group {
    /// Group name
    pub name: Ident,
    pub fields: Vec<Field>,
}

/// Protobuf supported field types
#[derive(Debug, Clone)]
pub enum FieldType {
    /// Protobuf int32
    ///
    /// # Remarks
    ///
    /// Uses variable-length encoding. Inefficient for encoding negative numbers – if
    /// your field is likely to have negative values, use sint32 instead.
    Int32(Span),
    /// Protobuf int64
    ///
    /// # Remarks
    ///
    /// Uses variable-length encoding. Inefficient for encoding negative numbers – if
    /// your field is likely to have negative values, use sint64 instead.
    Int64(Span),
    /// Protobuf uint32
    ///
    /// # Remarks
    ///
    /// Uses variable-length encoding.
    Uint32(Span),
    /// Protobuf uint64
    ///
    /// # Remarks
    ///
    /// Uses variable-length encoding.
    Uint64(Span),
    /// Protobuf sint32
    ///
    /// # Remarks
    ///
    /// Uses ZigZag variable-length encoding. Signed int value. These more efficiently
    /// encode negative numbers than regular int32s.
    Sint32(Span),
    /// Protobuf sint64
    ///
    /// # Remarks
    ///
    /// Uses ZigZag variable-length encoding. Signed int value. These more efficiently
    /// encode negative numbers than regular int32s.
    Sint64(Span),
    /// Protobuf bool
    Bool(Span),
    /// Protobuf fixed64
    ///
    /// # Remarks
    ///
    /// Always eight bytes. More efficient than uint64 if values are often greater than 2^56.
    Fixed64(Span),
    /// Protobuf sfixed64
    ///
    /// # Remarks
    ///
    /// Always eight bytes.
    Sfixed64(Span),
    /// Protobuf double
    Double(Span),
    /// Protobuf string
    ///
    /// # Remarks
    ///
    /// A string must always contain UTF-8 encoded or 7-bit ASCII text.
    String(Span),
    /// Protobuf bytes
    ///
    /// # Remarks
    ///
    /// May contain any arbitrary sequence of bytes.
    Bytes(Span),
    /// Protobut fixed32
    ///
    /// # Remarks
    ///
    /// Always four bytes. More efficient than uint32 if values are often greater than 2^28.
    Fixed32(Span),
    /// Protobut sfixed32
    ///
    /// # Remarks
    ///
    /// Always four bytes.
    Sfixed32(Span),
    /// Protobut float
    Float(Span),
    /// Protobuf message or enum (holds the name)
    MessageOrEnum(Type),
    /// Protobut map
    Map(MapType),
    /// Protobuf group (deprecated)
    Group(Group),
}

#[derive(Debug, Clone)]
pub struct MapType {
    pub span: Span,
    pub key: Box<FieldType>,
    pub value: Box<FieldType>,
}

impl FieldType {
    #[allow(unused)]
    pub fn span(&self) -> Span {
        match self {
            Self::Int32(span) => *span,
            Self::Int64(span) => *span,
            Self::Uint32(span) => *span,
            Self::Uint64(span) => *span,
            Self::Sint32(span) => *span,
            Self::Sint64(span) => *span,
            Self::Bool(span) => *span,
            Self::Fixed64(span) => *span,
            Self::Sfixed64(span) => *span,
            Self::Double(span) => *span,
            Self::String(span) => *span,
            Self::Bytes(span) => *span,
            Self::Fixed32(span) => *span,
            Self::Sfixed32(span) => *span,
            Self::Float(span) => *span,
            Self::MessageOrEnum(t) => t.type_path.span(),
            Self::Map(map) => map.span,
            Self::Group(group) => group.name.span(),
        }
    }
    pub fn is_message(&self) -> bool {
        match self {
            Self::MessageOrEnum(t) => t.target_is_message,
            _ => false,
        }
    }
}

#[derive(Debug, Clone)]
pub struct Type {
    pub type_path: ProtobufPath,
    pub target_is_message: bool,
    pub ty: syn::Type,
}

#[derive(Debug, Clone)]
pub enum TagValue {
    Value(Span, i32),
    AutoIncr,
}

impl ToLitStr for TagValue {
    fn to_lit_str(&self) -> LitStr {
        if let Self::Value(span, value) = self {
            LitStr::new(&format!("{}", value), *span)
        } else {
            unreachable!()
        }
    }
}

/// A Protobuf Field
#[derive(Debug, Clone)]
pub struct Field {
    /// Field name
    pub name: Ident,
    pub field_name: Ident,
    /// Field `Rule`
    pub modifier: Option<Modifier>,
    /// Field type
    pub typ: FieldType,
    /// Tag number
    pub tag: TagValue,
    /// Non-builtin options
    pub options: Vec<ProtobufOption>,
    pub enum_field: bool,
}

/// A Protobuf field of oneof group
#[derive(Debug, Clone)]
pub enum MessageElement {
    Field(Field),
    OneOf(OneOf),
}

#[derive(Debug, Clone, Copy)]
pub enum NestedTypeIndex {
    Message(usize),
    Enum(usize),
    Oneof(usize),
}

/// A protobuf message
#[derive(Debug, Clone)]
pub struct Message {
    /// Message name
    pub name: Ident,
    pub struct_name: Ident,
    pub nested_mod_name: Option<Ident>,
    /// Message fields and oneofs
    pub fields: Vec<MessageElement>,
    /// Message reserved numbers
    pub reserved_nums: Vec<RangeInclusive<i32>>,
    /// Message reserved names
    pub reserved_names: Vec<Ident>,
    /// Nested messages
    pub messages: Vec<Message>,
    /// Nested enums
    pub enums: Vec<Enumeration>,
    /// Non-builtin options
    pub options: Vec<ProtobufOption>,
    /// Extension field numbers
    pub extension_ranges: Vec<RangeInclusive<i32>>,
    /// Extensions
    pub extensions: Vec<Extension>,
    pub nested_types: Vec<NestedTypeIndex>,
}

/// A protobuf enumeration field
#[derive(Debug, Clone)]
pub struct EnumValue {
    /// enum value name
    pub name: Ident,
    /// variant name from Self::name
    pub variant_name: Ident,
    /// proto_name from Self::name
    pub proto_name: LitStr,
    /// enum value number
    pub tag: Option<LitInt>,
    pub tag_value: i32,
    /// enum value options
    pub options: Option<Vec<ProtobufOption>>,
}

/// A protobuf enumerator
#[derive(Debug, Clone)]
pub struct Enumeration {
    pub nested_mod_name: Option<Ident>,
    /// enum name
    pub name: Ident,
    /// enum values
    pub values: Vec<EnumValue>,
    /// enum options
    pub options: Vec<ProtobufOption>,
    /// enum reserved numbers
    pub reserved_nums: Vec<RangeInclusive<i32>>,
    /// enum reserved names
    pub reserved_names: Vec<Ident>,
}

/// A OneOf
#[derive(Debug, Clone)]
pub struct OneOf {
    /// OneOf name
    pub name: Ident,
    pub field_name: Ident,
    pub nested_mod_name: Ident,
    pub field_lit: LitStr,
    pub enum_name: Ident,
    pub tags: LitStr,
    /// OneOf fields
    pub fields: Vec<Field>,
    /// oneof options
    pub options: Vec<ProtobufOption>,
}

#[derive(Debug, Clone)]
pub struct Extension {
    /// Extend this type with field
    pub extendee: ProtobufPath,
    /// Extension field
    pub field: Field,
}

/// Service method
#[derive(Debug, Clone)]
pub struct Method {
    /// Method name
    pub name: Ident,
    // snake case name
    pub method_name: Ident,
    // generated input message
    pub input_message: Option<Message>,
    /// Input type
    pub input_type: Type,
    // generated output message
    pub output_message: Option<Message>,
    /// Output type
    pub output_type: Type,
    /// If this method is client streaming
    #[allow(dead_code)] // TODO
    pub client_streaming: Option<Span>,
    /// If this method is server streaming
    #[allow(dead_code)] // TODO
    pub server_streaming: Option<Span>,
    /// Method options
    pub options: Punctuated<ProtobufOption, Token![;]>,
}

/// Service definition
#[derive(Debug, Clone)]
pub struct Service {
    /// Service name
    pub name: Ident,
    pub code_name: Ident,
    pub methods: Vec<Method>,
    pub options: Vec<ProtobufOption>,
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct AnyTypeUrl {
    pub prefix: ProtobufPath,
    pub full_type_name: ProtobufPath,
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum ProtobufConstantMessageFieldName {
    Regular(Ident),
    Extension(ProtobufPath),
    AnyTypeUrl(AnyTypeUrl),
}

#[derive(Debug, Clone)]
pub struct ProtobufConstantMessage {
    pub fields: IndexMap<ProtobufConstantMessageFieldName, ProtobufConstant>,
}

#[derive(Debug, Clone)]
#[allow(unused)]
pub enum ProtobufConstant {
    U64(LitInt, bool),
    F64(LitFloat, bool),
    Bool(LitBool),
    Ident(ProtobufPath),
    String(LitStr),
    Message(ProtobufConstantMessage),
}

/// Equivalent of `UninterpretedOption.NamePart`.
#[derive(Debug, Clone)]
pub enum ProtobufOptionNamePart {
    Direct(Ident),
    Ext(ProtobufPath),
}

#[derive(Debug, Clone)]
pub struct ProtobufOptionNameExt(pub Vec<ProtobufOptionNamePart>);

#[derive(Debug, Clone)]
pub enum ProtobufOptionName {
    Builtin(Ident),
    Ext(ProtobufOptionNameExt),
}

impl ProtobufOptionName {
    pub fn is_option(&self, opt_name: &str) -> bool {
        match self {
            ProtobufOptionName::Builtin(name) => name.eq(opt_name),
            ProtobufOptionName::Ext(ext) => {
                let mut compared_index = 0;
                for (index, n) in opt_name.split('.').enumerate() {
                    if let Some(seg) = ext.0.get(index) {
                        match seg {
                            ProtobufOptionNamePart::Direct(d) => {
                                if !d.eq(n) {
                                    return false;
                                }
                            }
                            ProtobufOptionNamePart::Ext(_ext) => return false,
                        }
                    } else {
                        return false;
                    }
                    compared_index = index;
                }
                ext.0.len() == compared_index + 1
            }
        }
    }
}

#[derive(Debug, Clone)]
pub struct ProtobufOption {
    pub name: ProtobufOptionName,
    pub value: ProtobufConstant,
}

pub trait GetOption {
    fn get_option<'a>(&'a self, name: &str) -> Option<&'a ProtobufOption>;
}

impl GetOption for Vec<ProtobufOption> {
    fn get_option<'a>(&'a self, name: &str) -> Option<&'a ProtobufOption> {
        self.iter().find(|opt| opt.name.is_option(name))
    }
}

impl<P> GetOption for Punctuated<ProtobufOption, P> {
    fn get_option<'a>(&'a self, name: &str) -> Option<&'a ProtobufOption> {
        self.iter().find(|opt| opt.name.is_option(name))
    }
}

/// Visibility of import statement
#[derive(Debug, Clone)]
pub enum ImportVis {
    Default,
    Public(Span),
    Weak(Span),
}

impl Default for ImportVis {
    fn default() -> Self {
        ImportVis::Default
    }
}

/// Import statement
#[derive(Debug, Clone)]
pub struct Import {
    pub import_token: Span,
    pub path: LitStr,
    pub builtin: bool,
    pub vis: ImportVis,
    pub file_path: Option<FilePath>,
}

#[derive(Debug, Clone)]
pub struct FilePath {
    pub root: bool,
    pub bin: bool,
    pub example: bool,
    pub is_mod: bool,
    pub path: PathBuf,
    pub mod_path: syn::Path,
}

#[derive(Debug, Clone)]
pub struct Package {
    pub package: Ident,
}

#[derive(Debug, Clone)]
pub enum DeclIndex {
    Message(usize),
    Enum(usize),
    Service(usize),
}

/// A File descriptor representing a whole .proto file
#[derive(Debug, Clone)]
pub struct Protocol {
    /// Imports
    pub imports: Vec<Import>,
    /// Package
    pub package: Option<Package>,
    /// Protobuf Syntax
    pub syntax: Syntax,
    /// Top level messages
    pub messages: Vec<Message>,
    /// Enums
    pub enums: Vec<Enumeration>,
    /// Extensions
    pub extensions: Vec<Extension>,
    /// Services
    pub services: Vec<Service>,
    /// Non-builtin options
    pub options: Vec<ProtobufOption>,
    pub decls: Vec<DeclIndex>,
    pub deps: Deps,
}