buffa-descriptor 0.7.0

Protobuf descriptor types (FileDescriptorProto, DescriptorProto, ...) for buffa
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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
//! Linked descriptor types for runtime reflection.
//!
//! These are the processed, feature-resolved form of the raw
//! [`FileDescriptorProto`](crate::generated::descriptor::FileDescriptorProto)
//! tree.  Where the raw protos use string `type_name` references and
//! unresolved `FeatureSet` options, these types use pool indices
//! ([`MessageIndex`], [`EnumIndex`]) and pre-resolved edition features
//! ([`FieldPresence`](buffa::editions::FieldPresence), `packed`, `delimited`).
//!
//! [`FieldKind`] flattens protobuf's orthogonal type × label × map-entry axes
//! into a single discriminant that maps 1:1 to runtime representation — the
//! same approach protobuf-es takes with its `fieldKind` union.
//!
//! These types are constructed only by [`DescriptorPool`](crate::DescriptorPool)
//! from a `FileDescriptorSet` and are immutable thereafter.  Fields are
//! private — read them through accessor methods (`name()`, `kind()`,
//! `full_name()`, etc.) so the pool's internal representation can evolve
//! without breaking consumers.  Mutation through `&mut` is unsupported —
//! the pool hands out shared references only.
//!
//! Downstream crates can't fabricate a `MessageDescriptor` directly. Test
//! fixtures should compile a `.proto` to a `FileDescriptorSet` and load it
//! through `DescriptorPool::decode` — anything subtler skips the
//! feature-resolution and validation passes and would diverge from
//! production behavior.
//!
//! # Limits
//!
//! Field indices within a message are stored as `u16`, capping the number of
//! fields per message at 65 535.  `DescriptorPool` enforces this at
//! construction time.  Field *numbers* remain `u32` per the protobuf spec.

use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;

use crate::generated::descriptor::field_descriptor_proto::Type as ProtoType;
use crate::generated::descriptor::{
    EnumOptions, EnumValueOptions, FieldOptions, MessageOptions, MethodOptions, OneofOptions,
    ServiceOptions,
};
use buffa::editions::{EnumType, FieldPresence};

/// Index of a [`MessageDescriptor`] within its owning pool.
///
/// The `Ord` impl is an arbitrary but stable total order over one pool's
/// indices (so they can key ordered collections); it is **not** a documented
/// relationship to declaration or registration order. Comparing indices from
/// different pools is meaningless (the same cross-pool hazard as
/// `PartialEq`).
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MessageIndex(pub(crate) u32);

/// Index of an [`EnumDescriptor`] within its owning pool.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EnumIndex(pub(crate) u32);

/// Protobuf scalar field types.
///
/// This is [`field_descriptor_proto::Type`](ProtoType) minus
/// `TYPE_MESSAGE`, `TYPE_GROUP`, and `TYPE_ENUM` — those get dedicated
/// [`SingularKind`] variants instead of being lumped in with scalars.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ScalarType {
    Double,
    Float,
    Int64,
    Uint64,
    Int32,
    Fixed64,
    Fixed32,
    Bool,
    String,
    Bytes,
    Uint32,
    Sfixed32,
    Sfixed64,
    Sint32,
    Sint64,
}

impl ScalarType {
    /// Convert a raw proto `Type` to a `ScalarType`.
    ///
    /// Returns `None` for `TYPE_MESSAGE`, `TYPE_GROUP`, and `TYPE_ENUM`,
    /// which are not scalar.
    pub fn from_proto(ty: ProtoType) -> Option<Self> {
        Some(match ty {
            ProtoType::TYPE_DOUBLE => Self::Double,
            ProtoType::TYPE_FLOAT => Self::Float,
            ProtoType::TYPE_INT64 => Self::Int64,
            ProtoType::TYPE_UINT64 => Self::Uint64,
            ProtoType::TYPE_INT32 => Self::Int32,
            ProtoType::TYPE_FIXED64 => Self::Fixed64,
            ProtoType::TYPE_FIXED32 => Self::Fixed32,
            ProtoType::TYPE_BOOL => Self::Bool,
            ProtoType::TYPE_STRING => Self::String,
            ProtoType::TYPE_BYTES => Self::Bytes,
            ProtoType::TYPE_UINT32 => Self::Uint32,
            ProtoType::TYPE_SFIXED32 => Self::Sfixed32,
            ProtoType::TYPE_SFIXED64 => Self::Sfixed64,
            ProtoType::TYPE_SINT32 => Self::Sint32,
            ProtoType::TYPE_SINT64 => Self::Sint64,
            ProtoType::TYPE_MESSAGE | ProtoType::TYPE_GROUP | ProtoType::TYPE_ENUM => return None,
        })
    }

    /// Whether this scalar is valid as a protobuf map key.
    ///
    /// Per the protobuf spec: integral types, bool, and string. Not floats,
    /// not bytes.
    pub fn is_valid_map_key(self) -> bool {
        !matches!(self, Self::Double | Self::Float | Self::Bytes)
    }
}

/// The element kind of a singular field, list element, or map value.
///
/// Separating this from [`FieldKind`] makes `List(List(...))` and
/// `Map { value: Map {..} }` unrepresentable — protobuf does not allow
/// nested repeated or map-of-map.  It also keeps [`FieldKind`] `Copy`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SingularKind {
    /// A scalar value.
    Scalar(ScalarType),
    /// An enum value, referencing an enum in the pool.
    Enum(EnumIndex),
    /// A message value, referencing a message in the pool.
    Message(MessageIndex),
}

/// The kind of a protobuf field, flattening type × cardinality × map-entry.
///
/// This discriminant maps 1:1 to the field's runtime representation.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum FieldKind {
    /// A singular (non-repeated, non-map) field.
    Singular(SingularKind),
    /// A `repeated` field.
    List(SingularKind),
    /// A `map<K, V>` field.
    Map {
        /// Key type. Always integral, bool, or string per the protobuf spec.
        key: ScalarType,
        /// Value kind.
        value: SingularKind,
    },
}

/// A linked, feature-resolved field descriptor.
///
/// Constructed only by [`DescriptorPool`](crate::DescriptorPool); not
/// constructible by downstream crates. The fields are accessed through
/// methods so the pool can change its internal representation without
/// breaking consumers.
#[derive(Clone, Debug)]
pub struct FieldDescriptor {
    pub(crate) name: String,
    pub(crate) json_name: String,
    pub(crate) number: u32,
    pub(crate) kind: FieldKind,
    pub(crate) presence: FieldPresence,
    pub(crate) packed: bool,
    pub(crate) delimited: bool,
    pub(crate) oneof_index: Option<u16>,
    /// Raw `FieldOptions`, boxed (`None` when the field declares none).
    pub(crate) options: Option<Box<FieldOptions>>,
}

impl FieldDescriptor {
    /// Proto field name (as written in the `.proto` file).
    #[inline]
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// JSON name — lowerCamelCase unless overridden by `[json_name = ...]`.
    #[inline]
    #[must_use]
    pub fn json_name(&self) -> &str {
        &self.json_name
    }

    /// Field number, in `[1, 2^29 - 1]`.
    #[inline]
    #[must_use]
    pub fn number(&self) -> u32 {
        self.number
    }

    /// Resolved kind (scalar/enum/message/list/map).
    #[inline]
    #[must_use]
    pub fn kind(&self) -> FieldKind {
        self.kind
    }

    /// Resolved presence discipline. For `List`/`Map` kinds this is always
    /// [`Implicit`](FieldPresence::Implicit) (repeated fields have no presence).
    #[inline]
    #[must_use]
    pub fn presence(&self) -> FieldPresence {
        self.presence
    }

    /// Whether a `List` of packable scalars uses packed wire encoding.
    /// Meaningless for non-list or non-packable kinds.
    #[inline]
    #[must_use]
    pub fn is_packed(&self) -> bool {
        self.packed
    }

    /// Whether a `Message` kind uses delimited (group-style) wire encoding.
    /// Meaningless for non-message kinds.
    #[inline]
    #[must_use]
    pub fn is_delimited(&self) -> bool {
        self.delimited
    }

    /// Index into the parent message's [`oneofs`](MessageDescriptor::oneofs),
    /// if this field belongs to a oneof (including proto3 synthetic oneofs
    /// for `optional`).
    #[inline]
    #[must_use]
    pub fn oneof_index(&self) -> Option<u16> {
        self.oneof_index
    }

    /// The raw `FieldOptions` for this field, if any were declared.
    ///
    /// Standard options (`deprecated`, etc.) read directly off the returned
    /// struct. **Custom options** — `[(my.pkg.opt) = ...]` — are extensions
    /// of `google.protobuf.FieldOptions`; they survive on the returned
    /// struct's unknown fields. To read one generically, register the
    /// option's defining proto (and `descriptor.proto`) in the same pool,
    /// then reflect over the options via
    /// [`DynamicMessage::from_options`](crate::reflect::DynamicMessage::from_options):
    ///
    /// ```no_run
    /// # #[cfg(feature = "reflect")] {
    /// # use std::sync::Arc;
    /// # use buffa_descriptor::{DescriptorPool, reflect::{DynamicMessage, ReflectMessage}};
    /// # fn demo(pool: Arc<DescriptorPool>, field: &buffa_descriptor::FieldDescriptor) -> Option<()> {
    /// let dyn_opts = DynamicMessage::from_options(Arc::clone(&pool), field.options()?)?;
    /// let ext = pool.extension_by_name("my.pkg.opt")?;
    /// let value = dyn_opts.get(ext.field());
    /// # let _ = value; Some(())
    /// # }
    /// # }
    /// ```
    #[inline]
    #[must_use]
    pub fn options(&self) -> Option<&FieldOptions> {
        self.options.as_deref()
    }
}

/// A linked message descriptor.
///
/// Constructed only by [`DescriptorPool`](crate::DescriptorPool); not
/// constructible by downstream crates.
#[derive(Clone, Debug)]
pub struct MessageDescriptor {
    pub(crate) full_name: String,
    pub(crate) fields: Vec<FieldDescriptor>,
    /// `(field_number, index_into_fields)`, sorted by field number for
    /// binary-search lookup. Internal index; not API.
    pub(crate) field_by_number: Vec<(u32, u16)>,
    /// `(name, index_into_fields)`, sorted by name. Holds both the proto
    /// name and (when distinct) the JSON name. Internal index; not API.
    pub(crate) field_by_name: Vec<(String, u16)>,
    pub(crate) oneofs: Vec<OneofDescriptor>,
    pub(crate) extension_ranges: Vec<(u32, u32)>,
    /// Raw `MessageOptions`, boxed (`None` when the message declares none).
    pub(crate) options: Option<Box<MessageOptions>>,
}

impl MessageDescriptor {
    /// Fully-qualified proto name without leading dot, e.g.
    /// `google.protobuf.Timestamp`.
    #[inline]
    #[must_use]
    pub fn full_name(&self) -> &str {
        &self.full_name
    }

    /// The raw `MessageOptions` for this message, if any were declared.
    ///
    /// See [`FieldDescriptor::options`] for how to read custom options.
    #[inline]
    #[must_use]
    pub fn options(&self) -> Option<&MessageOptions> {
        self.options.as_deref()
    }

    /// Fields in source (declaration) order.
    #[inline]
    #[must_use]
    pub fn fields(&self) -> &[FieldDescriptor] {
        &self.fields
    }

    /// Oneof declarations, including proto3 synthetic oneofs.
    #[inline]
    #[must_use]
    pub fn oneofs(&self) -> &[OneofDescriptor] {
        &self.oneofs
    }

    /// Extension ranges `[start, end)`.
    #[inline]
    #[must_use]
    pub fn extension_ranges(&self) -> &[(u32, u32)] {
        &self.extension_ranges
    }

    /// Look up a field by its proto field number. `O(log n)`.
    #[must_use]
    pub fn field(&self, number: u32) -> Option<&FieldDescriptor> {
        let i = self
            .field_by_number
            .binary_search_by_key(&number, |&(n, _)| n)
            .ok()?;
        let (_, idx) = self.field_by_number[i];
        debug_assert!(
            (idx as usize) < self.fields.len(),
            "field_by_number index {idx} out of bounds for {} fields",
            self.fields.len()
        );
        self.fields.get(idx as usize)
    }

    /// Look up a field by its proto field name or JSON name. `O(log n)`.
    ///
    /// CEL evaluators and JSON parsers both look fields up by name in a hot
    /// loop; this is the supported path. Both the proto field name and the
    /// camelCase JSON name resolve.
    #[must_use]
    pub fn field_by_name(&self, name: &str) -> Option<&FieldDescriptor> {
        let i = self
            .field_by_name
            .binary_search_by(|(n, _)| n.as_str().cmp(name))
            .ok()?;
        let (_, idx) = self.field_by_name[i];
        self.fields.get(idx as usize)
    }

    /// Whether `number` falls within any declared extension range.
    #[must_use]
    pub fn in_extension_range(&self, number: u32) -> bool {
        self.extension_ranges
            .iter()
            .any(|&(start, end)| start <= number && number < end)
    }
}

/// A oneof declaration within a message.
///
/// Constructed only by [`DescriptorPool`](crate::DescriptorPool); not
/// constructible by downstream crates.
#[derive(Clone, Debug)]
pub struct OneofDescriptor {
    pub(crate) name: String,
    pub(crate) field_indices: Vec<u16>,
    pub(crate) synthetic: bool,
    /// Raw `OneofOptions`, boxed (`None` when the oneof declares none).
    pub(crate) options: Option<Box<OneofOptions>>,
}

impl OneofDescriptor {
    /// Proto oneof name.
    #[inline]
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The raw `OneofOptions` for this oneof, if any were declared.
    ///
    /// See [`FieldDescriptor::options`] for how to read custom options.
    #[inline]
    #[must_use]
    pub fn options(&self) -> Option<&OneofOptions> {
        self.options.as_deref()
    }

    /// Indices into the parent message's [`fields`](MessageDescriptor::fields)
    /// for members of this oneof.
    #[inline]
    #[must_use]
    pub fn field_indices(&self) -> &[u16] {
        &self.field_indices
    }

    /// Whether this is a synthetic oneof generated for a proto3 `optional`
    /// field (exactly one member, not user-declared).
    #[inline]
    #[must_use]
    pub fn is_synthetic(&self) -> bool {
        self.synthetic
    }
}

/// A linked enum descriptor.
///
/// Constructed only by [`DescriptorPool`](crate::DescriptorPool); not
/// constructible by downstream crates.
#[derive(Clone, Debug)]
pub struct EnumDescriptor {
    pub(crate) full_name: String,
    pub(crate) values: Vec<EnumValueDescriptor>,
    pub(crate) enum_type: EnumType,
    /// Raw `EnumOptions`, boxed (`None` when the enum declares none).
    pub(crate) options: Option<Box<EnumOptions>>,
}

impl EnumDescriptor {
    /// Fully-qualified proto name without leading dot.
    #[inline]
    #[must_use]
    pub fn full_name(&self) -> &str {
        &self.full_name
    }

    /// The raw `EnumOptions` for this enum, if any were declared.
    ///
    /// See [`FieldDescriptor::options`] for how to read custom options.
    #[inline]
    #[must_use]
    pub fn options(&self) -> Option<&EnumOptions> {
        self.options.as_deref()
    }

    /// Declared values in source order.
    #[inline]
    #[must_use]
    pub fn values(&self) -> &[EnumValueDescriptor] {
        &self.values
    }

    /// Whether unknown numeric values are preserved
    /// ([`Open`](EnumType::Open)) or treated as unknown fields
    /// ([`Closed`](EnumType::Closed)). Resolved from edition features.
    #[inline]
    #[must_use]
    pub fn enum_type(&self) -> EnumType {
        self.enum_type
    }

    /// Look up a value by its numeric value.
    ///
    /// If the enum has aliases (`allow_alias = true`), returns the first
    /// declared value with that number.
    #[must_use]
    pub fn value(&self, number: i32) -> Option<&EnumValueDescriptor> {
        self.values.iter().find(|v| v.number == number)
    }

    /// Look up a value by its proto name.
    #[must_use]
    pub fn value_by_name(&self, name: &str) -> Option<&EnumValueDescriptor> {
        self.values.iter().find(|v| v.name == name)
    }
}

/// A single value within an enum.
///
/// Constructed only by [`DescriptorPool`](crate::DescriptorPool); not
/// constructible by downstream crates.
#[derive(Clone, Debug)]
pub struct EnumValueDescriptor {
    pub(crate) name: String,
    pub(crate) number: i32,
    /// Raw `EnumValueOptions`, boxed (`None` when the value declares none).
    pub(crate) options: Option<Box<EnumValueOptions>>,
}

impl EnumValueDescriptor {
    /// Proto value name, e.g. `FOO_BAR`.
    #[inline]
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Numeric value.
    #[inline]
    #[must_use]
    pub fn number(&self) -> i32 {
        self.number
    }

    /// The raw `EnumValueOptions` for this value, if any were declared.
    ///
    /// See [`FieldDescriptor::options`] for how to read custom options.
    #[inline]
    #[must_use]
    pub fn options(&self) -> Option<&EnumValueOptions> {
        self.options.as_deref()
    }
}

/// Pool-local index of a registered service.
///
/// Same contract as [`MessageIndex`] / [`EnumIndex`]: stable for the
/// lifetime of the pool, no cross-pool identity.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ServiceIndex(pub(crate) u32);

/// A linked service descriptor.
///
/// Carries the service's RPC methods. Used by gRPC server reflection,
/// transcoding gateways that route by method, and interceptors that need to
/// know an RPC's input/output types — the connect-rust use cases.
///
/// Constructed only by [`DescriptorPool`](crate::DescriptorPool); not
/// constructible by downstream crates.
#[derive(Clone, Debug)]
pub struct ServiceDescriptor {
    pub(crate) full_name: String,
    pub(crate) methods: Vec<MethodDescriptor>,
    /// Raw `ServiceOptions`, boxed (`None` when the service declares none).
    pub(crate) options: Option<Box<ServiceOptions>>,
}

impl ServiceDescriptor {
    /// Fully-qualified proto name without leading dot, e.g.
    /// `connectrpc.eliza.v1.ElizaService`.
    #[inline]
    #[must_use]
    pub fn full_name(&self) -> &str {
        &self.full_name
    }

    /// The raw `ServiceOptions` for this service, if any were declared.
    ///
    /// See [`FieldDescriptor::options`] for how to read custom options.
    #[inline]
    #[must_use]
    pub fn options(&self) -> Option<&ServiceOptions> {
        self.options.as_deref()
    }

    /// Methods in declaration order.
    #[inline]
    #[must_use]
    pub fn methods(&self) -> &[MethodDescriptor] {
        &self.methods
    }

    /// Look up a method by its proto name. `O(n)` over the methods slice —
    /// services rarely have more than a dozen methods.
    #[must_use]
    pub fn method(&self, name: &str) -> Option<&MethodDescriptor> {
        self.methods.iter().find(|m| m.name == name)
    }
}

/// A linked RPC method descriptor.
///
/// Constructed only by [`DescriptorPool`](crate::DescriptorPool); not
/// constructible by downstream crates.
#[derive(Clone, Debug)]
pub struct MethodDescriptor {
    pub(crate) name: String,
    pub(crate) input: MessageIndex,
    pub(crate) output: MessageIndex,
    pub(crate) client_streaming: bool,
    pub(crate) server_streaming: bool,
    /// Raw `MethodOptions`, boxed (`None` when the method declares none).
    pub(crate) options: Option<Box<MethodOptions>>,
}

impl MethodDescriptor {
    /// Proto method name, e.g. `Say`.
    #[inline]
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Pool index of the request message type.
    #[inline]
    #[must_use]
    pub fn input(&self) -> MessageIndex {
        self.input
    }

    /// Pool index of the response message type.
    #[inline]
    #[must_use]
    pub fn output(&self) -> MessageIndex {
        self.output
    }

    /// Whether the client streams multiple request messages.
    #[inline]
    #[must_use]
    pub fn is_client_streaming(&self) -> bool {
        self.client_streaming
    }

    /// Whether the server streams multiple response messages.
    #[inline]
    #[must_use]
    pub fn is_server_streaming(&self) -> bool {
        self.server_streaming
    }

    /// The raw `MethodOptions` for this method, if any were declared.
    ///
    /// `(google.api.http)` and other transcoding annotations live here as
    /// custom options. See [`FieldDescriptor::options`] for how to read them.
    #[inline]
    #[must_use]
    pub fn options(&self) -> Option<&MethodOptions> {
        self.options.as_deref()
    }
}

/// Pool-local index of a registered extension.
///
/// Same contract as [`MessageIndex`] / [`EnumIndex`]: stable for the
/// lifetime of the pool, no cross-pool identity.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ExtensionIndex(pub(crate) u32);

/// A linked extension descriptor.
///
/// An extension is a field declared *outside* the message it belongs to —
/// `extend Foo { optional int32 bar = 100; }` adds field 100 to `Foo` from
/// anywhere that can see `Foo`. Structurally it is a [`FieldDescriptor`]
/// plus the identity of the message it extends and the fully-qualified name
/// it is registered under.
///
/// The contained [`field()`](Self::field) descriptor is what the
/// [`ReflectMessage`](crate::reflect::ReflectMessage) accessors take —
/// `msg.get(ext.field())` reads an extension exactly like a declared field.
/// This mirrors protobuf-go, where `ExtensionDescriptor` *is* a
/// `FieldDescriptor` and the reflective accessors don't distinguish.
///
/// Constructed only by [`DescriptorPool`](crate::DescriptorPool); not
/// constructible by downstream crates.
#[derive(Clone, Debug)]
pub struct ExtensionDescriptor {
    /// The field this extension adds to the extendee. `name` is the
    /// extension's simple name; `json_name` is derived but unused (the JSON
    /// key for an extension is the bracketed [`full_name`](Self::full_name)).
    pub(crate) field: FieldDescriptor,
    /// Fully-qualified registration name, e.g. `pkg.ext_name` for a
    /// file-level extension or `pkg.Scope.ext_name` for one declared inside
    /// a message. This is what appears in JSON `"[...]"` keys.
    pub(crate) full_name: String,
    /// The bracketed JSON object key, `"[<full_name>]"`. Precomputed at link
    /// time so the JSON serializer doesn't allocate it per message.
    pub(crate) json_key: String,
    /// The message this extension extends.
    pub(crate) extendee: MessageIndex,
}

impl ExtensionDescriptor {
    /// The field this extension adds to the extendee.
    ///
    /// Pass this to [`ReflectMessage`](crate::reflect::ReflectMessage)
    /// accessors: `msg.get(ext.field())`, `msg.has(ext.field())`,
    /// `msg.set(ext.field(), value)`.
    ///
    /// The returned descriptor's
    /// [`json_name()`](FieldDescriptor::json_name) is **not** the JSON key
    /// for this extension — extensions serialize as the bracketed
    /// [`full_name()`](Self::full_name) (`"[pkg.ext_name]"`), not as a
    /// camelCase field name. A reflection-driven serializer must special-case
    /// extension fields.
    #[inline]
    #[must_use]
    pub fn field(&self) -> &FieldDescriptor {
        &self.field
    }

    /// Fully-qualified registration name (the JSON `"[...]"` key without
    /// the brackets).
    #[inline]
    #[must_use]
    pub fn full_name(&self) -> &str {
        &self.full_name
    }

    /// The JSON object key for this extension: the bracketed
    /// [`full_name()`](Self::full_name), e.g. `"[pkg.ext_name]"`.
    #[inline]
    #[must_use]
    pub fn json_key(&self) -> &str {
        &self.json_key
    }

    /// The message this extension extends.
    #[inline]
    #[must_use]
    pub fn extendee(&self) -> MessageIndex {
        self.extendee
    }
}

impl AsRef<FieldDescriptor> for ExtensionDescriptor {
    /// Equivalent to [`field()`](Self::field), for generic code that accepts
    /// "anything that is a field descriptor".
    fn as_ref(&self) -> &FieldDescriptor {
        &self.field
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn scalar_type_from_proto_scalars() {
        assert_eq!(
            ScalarType::from_proto(ProtoType::TYPE_INT32),
            Some(ScalarType::Int32)
        );
        assert_eq!(
            ScalarType::from_proto(ProtoType::TYPE_STRING),
            Some(ScalarType::String)
        );
        assert_eq!(
            ScalarType::from_proto(ProtoType::TYPE_SINT64),
            Some(ScalarType::Sint64)
        );
    }

    #[test]
    fn scalar_type_from_proto_rejects_composites() {
        assert_eq!(ScalarType::from_proto(ProtoType::TYPE_MESSAGE), None);
        assert_eq!(ScalarType::from_proto(ProtoType::TYPE_GROUP), None);
        assert_eq!(ScalarType::from_proto(ProtoType::TYPE_ENUM), None);
    }

    #[test]
    fn scalar_type_map_key_validity() {
        assert!(ScalarType::Int32.is_valid_map_key());
        assert!(ScalarType::String.is_valid_map_key());
        assert!(ScalarType::Bool.is_valid_map_key());
        assert!(ScalarType::Sfixed64.is_valid_map_key());
        assert!(!ScalarType::Double.is_valid_map_key());
        assert!(!ScalarType::Float.is_valid_map_key());
        assert!(!ScalarType::Bytes.is_valid_map_key());
    }

    fn scalar_field(name: &str, number: u32, ty: ScalarType) -> FieldDescriptor {
        FieldDescriptor {
            name: name.into(),
            json_name: name.into(),
            number,
            kind: FieldKind::Singular(SingularKind::Scalar(ty)),
            presence: FieldPresence::Implicit,
            packed: false,
            delimited: false,
            oneof_index: None,
            options: None,
        }
    }

    fn sample_message() -> MessageDescriptor {
        MessageDescriptor {
            full_name: "test.Foo".into(),
            fields: alloc::vec![
                scalar_field("a", 1, ScalarType::Int32),
                scalar_field("b", 5, ScalarType::String),
            ],
            field_by_number: alloc::vec![(1, 0), (5, 1)],
            field_by_name: alloc::vec![("a".into(), 0), ("b".into(), 1)],
            oneofs: Vec::new(),
            extension_ranges: alloc::vec![(100, 200), (1000, 2000)],
            options: None,
        }
    }

    #[test]
    fn message_field_lookup_by_number() {
        let m = sample_message();
        assert_eq!(m.field(1).unwrap().name, "a");
        assert_eq!(m.field(5).unwrap().name, "b");
        assert!(m.field(2).is_none());
        assert!(m.field(99).is_none());
    }

    #[test]
    fn message_field_lookup_by_name() {
        let m = sample_message();
        assert_eq!(m.field_by_name("a").unwrap().number, 1);
        assert_eq!(m.field_by_name("b").unwrap().number, 5);
        assert!(m.field_by_name("c").is_none());
        assert!(m.field_by_name("").is_none());
    }

    #[test]
    fn empty_message_field_lookup() {
        let m = MessageDescriptor {
            full_name: "test.Empty".into(),
            fields: Vec::new(),
            field_by_number: Vec::new(),
            field_by_name: Vec::new(),
            oneofs: Vec::new(),
            extension_ranges: Vec::new(),
            options: None,
        };
        assert!(m.field(1).is_none());
        assert!(m.field_by_name("anything").is_none());
        assert!(!m.in_extension_range(1));
    }

    #[test]
    fn message_extension_range_check() {
        let m = sample_message();
        assert!(m.in_extension_range(100));
        assert!(m.in_extension_range(150));
        assert!(m.in_extension_range(199));
        assert!(!m.in_extension_range(200)); // end is exclusive
        assert!(m.in_extension_range(1500));
        assert!(!m.in_extension_range(50));
        assert!(!m.in_extension_range(500));
    }

    #[test]
    fn enum_value_lookup() {
        let e = EnumDescriptor {
            full_name: "test.Color".into(),
            values: alloc::vec![
                EnumValueDescriptor {
                    name: "RED".into(),
                    number: 0,
                    options: None,
                },
                EnumValueDescriptor {
                    name: "GREEN".into(),
                    number: 1,
                    options: None,
                },
                EnumValueDescriptor {
                    name: "ALIAS_RED".into(),
                    number: 0,
                    options: None,
                },
            ],
            enum_type: EnumType::Open,
            options: None,
        };
        assert_eq!(e.value(1).unwrap().name, "GREEN");
        assert_eq!(e.value(0).unwrap().name, "RED"); // first wins on alias
        assert!(e.value(99).is_none());
        assert_eq!(e.value_by_name("GREEN").unwrap().number, 1);
        assert!(e.value_by_name("BLUE").is_none());
    }

    #[test]
    fn field_kind_is_copy() {
        let list = FieldKind::List(SingularKind::Message(MessageIndex(3)));
        let copied = list;
        assert_eq!(list, copied);

        let map = FieldKind::Map {
            key: ScalarType::String,
            value: SingularKind::Enum(EnumIndex(1)),
        };
        match map {
            FieldKind::Map { key, value } => {
                assert_eq!(key, ScalarType::String);
                assert_eq!(value, SingularKind::Enum(EnumIndex(1)));
            }
            _ => panic!(),
        }
    }

    #[test]
    fn scalar_type_from_proto_exhaustive() {
        use ProtoType::*;
        let all = [
            (TYPE_DOUBLE, ScalarType::Double),
            (TYPE_FLOAT, ScalarType::Float),
            (TYPE_INT64, ScalarType::Int64),
            (TYPE_UINT64, ScalarType::Uint64),
            (TYPE_INT32, ScalarType::Int32),
            (TYPE_FIXED64, ScalarType::Fixed64),
            (TYPE_FIXED32, ScalarType::Fixed32),
            (TYPE_BOOL, ScalarType::Bool),
            (TYPE_STRING, ScalarType::String),
            (TYPE_BYTES, ScalarType::Bytes),
            (TYPE_UINT32, ScalarType::Uint32),
            (TYPE_SFIXED32, ScalarType::Sfixed32),
            (TYPE_SFIXED64, ScalarType::Sfixed64),
            (TYPE_SINT32, ScalarType::Sint32),
            (TYPE_SINT64, ScalarType::Sint64),
        ];
        for (proto, scalar) in all {
            assert_eq!(ScalarType::from_proto(proto), Some(scalar));
        }
    }
}