1use std::{
2 borrow::Cow,
3 fmt, iter,
4 ops::{Range, RangeInclusive},
5 sync::Arc,
6};
7
8use prost::{
9 bytes::{Buf, BufMut},
10 encoding::{self, WireType},
11 DecodeError, EncodeError, Message,
12};
13use prost_types::{
14 DescriptorProto, EnumDescriptorProto, EnumValueDescriptorProto, FieldDescriptorProto,
15 FileDescriptorProto, FileDescriptorSet, MethodDescriptorProto, OneofDescriptorProto,
16 ServiceDescriptorProto,
17};
18
19use crate::{
20 descriptor::{
21 error::DescriptorErrorKind,
22 find_enum_proto, find_message_proto, tag, to_index,
23 types::{self, Options},
24 Definition, DefinitionKind, DescriptorIndex, EnumDescriptorInner, EnumValueDescriptorInner,
25 ExtensionDescriptorInner, FieldDescriptorInner, FileDescriptorInner, KindIndex,
26 MessageDescriptorInner, MethodDescriptorInner, OneofDescriptorInner,
27 ServiceDescriptorInner, MAP_ENTRY_KEY_NUMBER, MAP_ENTRY_VALUE_NUMBER,
28 },
29 Cardinality, DescriptorError, DescriptorPool, DynamicMessage, EnumDescriptor,
30 EnumValueDescriptor, ExtensionDescriptor, FieldDescriptor, FileDescriptor, Kind,
31 MessageDescriptor, MethodDescriptor, OneofDescriptor, ServiceDescriptor, Syntax, Value,
32};
33
34impl fmt::Debug for Syntax {
35 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36 match *self {
37 Syntax::Proto2 => write!(f, "proto2"),
38 Syntax::Proto3 => write!(f, "proto3"),
39 }
40 }
41}
42
43impl Kind {
44 fn new(pool: &DescriptorPool, kind: KindIndex) -> Self {
45 match kind {
46 KindIndex::Double => Kind::Double,
47 KindIndex::Float => Kind::Float,
48 KindIndex::Int64 => Kind::Int64,
49 KindIndex::Uint64 => Kind::Uint64,
50 KindIndex::Int32 => Kind::Int32,
51 KindIndex::Fixed64 => Kind::Fixed64,
52 KindIndex::Fixed32 => Kind::Fixed32,
53 KindIndex::Bool => Kind::Bool,
54 KindIndex::String => Kind::String,
55 KindIndex::Bytes => Kind::Bytes,
56 KindIndex::Uint32 => Kind::Uint32,
57 KindIndex::Sfixed32 => Kind::Sfixed32,
58 KindIndex::Sfixed64 => Kind::Sfixed64,
59 KindIndex::Sint32 => Kind::Sint32,
60 KindIndex::Sint64 => Kind::Sint64,
61 KindIndex::Message(index) | KindIndex::Group(index) => {
62 Kind::Message(MessageDescriptor {
63 pool: pool.clone(),
64 index,
65 })
66 }
67 KindIndex::Enum(index) => Kind::Enum(EnumDescriptor {
68 pool: pool.clone(),
69 index,
70 }),
71 }
72 }
73
74 pub fn as_message(&self) -> Option<&MessageDescriptor> {
77 match self {
78 Kind::Message(desc) => Some(desc),
79 _ => None,
80 }
81 }
82
83 pub fn as_enum(&self) -> Option<&EnumDescriptor> {
86 match self {
87 Kind::Enum(desc) => Some(desc),
88 _ => None,
89 }
90 }
91
92 pub fn wire_type(&self) -> WireType {
97 match self {
98 Kind::Double | Kind::Fixed64 | Kind::Sfixed64 => WireType::SixtyFourBit,
99 Kind::Float | Kind::Fixed32 | Kind::Sfixed32 => WireType::ThirtyTwoBit,
100 Kind::Enum(_)
101 | Kind::Int32
102 | Kind::Int64
103 | Kind::Uint32
104 | Kind::Uint64
105 | Kind::Sint32
106 | Kind::Sint64
107 | Kind::Bool => WireType::Varint,
108 Kind::String | Kind::Bytes | Kind::Message(_) => WireType::LengthDelimited,
109 }
110 }
111}
112
113impl fmt::Debug for Kind {
114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115 match self {
116 Self::Double => write!(f, "double"),
117 Self::Float => write!(f, "float"),
118 Self::Int32 => write!(f, "int32"),
119 Self::Int64 => write!(f, "int64"),
120 Self::Uint32 => write!(f, "uint32"),
121 Self::Uint64 => write!(f, "uint64"),
122 Self::Sint32 => write!(f, "sint32"),
123 Self::Sint64 => write!(f, "sint64"),
124 Self::Fixed32 => write!(f, "fixed32"),
125 Self::Fixed64 => write!(f, "fixed64"),
126 Self::Sfixed32 => write!(f, "sfixed32"),
127 Self::Sfixed64 => write!(f, "sfixed64"),
128 Self::Bool => write!(f, "bool"),
129 Self::String => write!(f, "string"),
130 Self::Bytes => write!(f, "bytes"),
131 Self::Message(m) => write!(f, "{}", m.full_name()),
132 Self::Enum(e) => write!(f, "{}", e.full_name()),
133 }
134 }
135}
136
137impl DescriptorPool {
138 pub fn new() -> Self {
143 DescriptorPool::default()
144 }
145
146 pub fn from_file_descriptor_set(
153 file_descriptor_set: FileDescriptorSet,
154 ) -> Result<Self, DescriptorError> {
155 let mut pool = DescriptorPool::new();
156 pool.add_file_descriptor_set(file_descriptor_set)?;
157 Ok(pool)
158 }
159
160 pub fn decode<B>(bytes: B) -> Result<Self, DescriptorError>
175 where
176 B: Buf,
177 {
178 let file_descriptor_set = types::FileDescriptorSet::decode(bytes).map_err(|err| {
179 DescriptorError::new(vec![DescriptorErrorKind::DecodeFileDescriptorSet { err }])
180 })?;
181
182 let mut pool = DescriptorPool::new();
183 pool.build_files(file_descriptor_set.file.into_iter())?;
184 Ok(pool)
185 }
186
187 pub fn add_file_descriptor_set(
202 &mut self,
203 file_descriptor_set: FileDescriptorSet,
204 ) -> Result<(), DescriptorError> {
205 self.add_file_descriptor_protos(file_descriptor_set.file)
206 }
207
208 pub fn add_file_descriptor_protos<I>(&mut self, files: I) -> Result<(), DescriptorError>
220 where
221 I: IntoIterator<Item = FileDescriptorProto>,
222 {
223 self.build_files(
224 files
225 .into_iter()
226 .map(types::FileDescriptorProto::from_prost),
227 )
228 }
229
230 pub fn add_file_descriptor_proto(
242 &mut self,
243 file: FileDescriptorProto,
244 ) -> Result<(), DescriptorError> {
245 self.add_file_descriptor_protos(iter::once(file))
246 }
247
248 pub fn decode_file_descriptor_proto<B>(&mut self, bytes: B) -> Result<(), DescriptorError>
263 where
264 B: Buf,
265 {
266 let file = types::FileDescriptorProto::decode(bytes).map_err(|err| {
267 DescriptorError::new(vec![DescriptorErrorKind::DecodeFileDescriptorSet { err }])
268 })?;
269
270 self.build_files(iter::once(file))
271 }
272
273 pub fn decode_file_descriptor_set<B>(&mut self, bytes: B) -> Result<(), DescriptorError>
290 where
291 B: Buf,
292 {
293 let file = types::FileDescriptorSet::decode(bytes).map_err(|err| {
294 DescriptorError::new(vec![DescriptorErrorKind::DecodeFileDescriptorSet { err }])
295 })?;
296
297 self.build_files(file.file)
298 }
299
300 pub fn files(&self) -> impl ExactSizeIterator<Item = FileDescriptor> + '_ {
302 indices(&self.inner.files).map(|index| FileDescriptor {
303 pool: self.clone(),
304 index,
305 })
306 }
307
308 pub fn get_file_by_name(&self, name: &str) -> Option<FileDescriptor> {
310 if let Some(&index) = self.inner.file_names.get(name) {
311 Some(FileDescriptor {
312 pool: self.clone(),
313 index,
314 })
315 } else {
316 None
317 }
318 }
319
320 pub fn file_descriptor_protos(
322 &self,
323 ) -> impl ExactSizeIterator<Item = &FileDescriptorProto> + '_ {
324 indices(&self.inner.files).map(|index| &self.inner.files[index as usize].prost)
325 }
326
327 pub fn encode<B>(&self, buf: B) -> Result<(), EncodeError>
332 where
333 B: BufMut,
334 {
335 use prost::encoding::{encoded_len_varint, DecodeContext};
336
337 struct FileDescriptorSet<'a> {
338 files: &'a [FileDescriptorInner],
339 }
340
341 impl fmt::Debug for FileDescriptorSet<'_> {
342 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
343 f.debug_struct("FileDescriptorSet").finish_non_exhaustive()
344 }
345 }
346
347 impl Message for FileDescriptorSet<'_> {
348 fn encode_raw(&self, buf: &mut impl BufMut)
349 where
350 Self: Sized,
351 {
352 for file in self.files {
353 encoding::message::encode(
354 tag::file_descriptor_set::FILE as u32,
355 &file.raw,
356 buf,
357 );
358 }
359 }
360
361 fn encoded_len(&self) -> usize {
362 encoding::key_len(tag::file_descriptor_set::FILE as u32) * self.files.len()
363 + self
364 .files
365 .iter()
366 .map(|f| &f.raw)
367 .map(Message::encoded_len)
368 .map(|len| len + encoded_len_varint(len as u64))
369 .sum::<usize>()
370 }
371
372 fn merge_field(
373 &mut self,
374 _: u32,
375 _: WireType,
376 _: &mut impl Buf,
377 _: DecodeContext,
378 ) -> Result<(), DecodeError>
379 where
380 Self: Sized,
381 {
382 unimplemented!()
383 }
384
385 fn clear(&mut self) {
386 unimplemented!()
387 }
388 }
389
390 let mut buf = buf;
391 FileDescriptorSet {
392 files: &self.inner.files,
393 }
394 .encode(&mut buf)
395 }
396
397 pub fn encode_to_vec(&self) -> Vec<u8> {
402 let mut buf = Vec::new();
403 self.encode(&mut buf).expect("vec should have capacity");
404 buf
405 }
406
407 pub fn services(&self) -> impl ExactSizeIterator<Item = ServiceDescriptor> + '_ {
409 indices(&self.inner.services).map(|index| ServiceDescriptor {
410 pool: self.clone(),
411 index,
412 })
413 }
414
415 pub fn all_messages(&self) -> impl ExactSizeIterator<Item = MessageDescriptor> + '_ {
419 indices(&self.inner.messages).map(|index| MessageDescriptor {
420 pool: self.clone(),
421 index,
422 })
423 }
424
425 pub fn all_enums(&self) -> impl ExactSizeIterator<Item = EnumDescriptor> + '_ {
429 indices(&self.inner.enums).map(|index| EnumDescriptor {
430 pool: self.clone(),
431 index,
432 })
433 }
434
435 pub fn all_extensions(&self) -> impl ExactSizeIterator<Item = ExtensionDescriptor> + '_ {
439 indices(&self.inner.extensions).map(|index| ExtensionDescriptor {
440 pool: self.clone(),
441 index,
442 })
443 }
444
445 pub fn get_message_by_name(&self, name: &str) -> Option<MessageDescriptor> {
447 match self.inner.get_by_name(name) {
448 Some(&Definition {
449 kind: DefinitionKind::Message(index),
450 ..
451 }) => Some(MessageDescriptor {
452 pool: self.clone(),
453 index,
454 }),
455 _ => None,
456 }
457 }
458
459 pub fn get_enum_by_name(&self, name: &str) -> Option<EnumDescriptor> {
461 match self.inner.get_by_name(name) {
462 Some(&Definition {
463 kind: DefinitionKind::Enum(index),
464 ..
465 }) => Some(EnumDescriptor {
466 pool: self.clone(),
467 index,
468 }),
469 _ => None,
470 }
471 }
472
473 pub fn get_extension_by_name(&self, name: &str) -> Option<ExtensionDescriptor> {
475 match self.inner.get_by_name(name) {
476 Some(&Definition {
477 kind: DefinitionKind::Extension(index),
478 ..
479 }) => Some(ExtensionDescriptor {
480 pool: self.clone(),
481 index,
482 }),
483 _ => None,
484 }
485 }
486
487 pub fn get_service_by_name(&self, name: &str) -> Option<ServiceDescriptor> {
489 match self.inner.get_by_name(name) {
490 Some(&Definition {
491 kind: DefinitionKind::Service(index),
492 ..
493 }) => Some(ServiceDescriptor {
494 pool: self.clone(),
495 index,
496 }),
497 _ => None,
498 }
499 }
500}
501
502impl fmt::Debug for DescriptorPool {
503 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
504 f.debug_struct("DescriptorPool")
505 .field("files", &debug_fmt_iter(self.files()))
506 .field("services", &debug_fmt_iter(self.services()))
507 .field("all_messages", &debug_fmt_iter(self.all_messages()))
508 .field("all_enums", &debug_fmt_iter(self.all_enums()))
509 .field("all_extensions", &debug_fmt_iter(self.all_extensions()))
510 .finish()
511 }
512}
513
514impl PartialEq for DescriptorPool {
515 fn eq(&self, other: &Self) -> bool {
516 Arc::ptr_eq(&self.inner, &other.inner)
517 }
518}
519
520impl Eq for DescriptorPool {}
521
522impl FileDescriptor {
523 pub fn new(descriptor_pool: DescriptorPool, index: usize) -> Self {
529 debug_assert!(index < descriptor_pool.files().len());
530 FileDescriptor {
531 pool: descriptor_pool,
532 index: to_index(index),
533 }
534 }
535
536 pub fn parent_pool(&self) -> &DescriptorPool {
538 &self.pool
539 }
540
541 pub fn name(&self) -> &str {
544 self.inner().prost.name()
545 }
546
547 pub fn package_name(&self) -> &str {
551 self.inner().prost.package()
552 }
553
554 pub fn index(&self) -> usize {
556 self.index as usize
557 }
558
559 pub fn syntax(&self) -> Syntax {
561 self.inner().syntax
562 }
563
564 pub fn dependencies(&self) -> impl ExactSizeIterator<Item = FileDescriptor> + '_ {
568 let pool = self.parent_pool();
569 self.file_descriptor_proto()
570 .dependency
571 .iter()
572 .map(|name| pool.get_file_by_name(name).expect("file not found"))
573 }
574
575 pub fn public_dependencies(&self) -> impl ExactSizeIterator<Item = FileDescriptor> + '_ {
579 let pool = self.parent_pool();
580 let raw = self.file_descriptor_proto();
581 raw.public_dependency.iter().map(|&index| {
582 pool.get_file_by_name(&raw.dependency[index as usize])
583 .expect("file not found")
584 })
585 }
586
587 pub fn messages(&self) -> impl ExactSizeIterator<Item = MessageDescriptor> + '_ {
591 let pool = self.parent_pool();
592 let raw_file = self.file_descriptor_proto();
593 raw_file.message_type.iter().map(move |raw_message| {
594 pool.get_message_by_name(join_name(raw_file.package(), raw_message.name()).as_ref())
595 .expect("message not found")
596 })
597 }
598
599 pub fn enums(&self) -> impl ExactSizeIterator<Item = EnumDescriptor> + '_ {
603 let pool = self.parent_pool();
604 let raw_file = self.file_descriptor_proto();
605 raw_file.enum_type.iter().map(move |raw_enum| {
606 pool.get_enum_by_name(join_name(raw_file.package(), raw_enum.name()).as_ref())
607 .expect("enum not found")
608 })
609 }
610
611 pub fn extensions(&self) -> impl ExactSizeIterator<Item = ExtensionDescriptor> + '_ {
615 let pool = self.parent_pool();
616 let raw_file = self.file_descriptor_proto();
617 raw_file.extension.iter().map(move |raw_extension| {
618 pool.get_extension_by_name(join_name(raw_file.package(), raw_extension.name()).as_ref())
619 .expect("extension not found")
620 })
621 }
622
623 pub fn services(&self) -> impl ExactSizeIterator<Item = ServiceDescriptor> + '_ {
625 let pool = self.parent_pool();
626 let raw_file = self.file_descriptor_proto();
627 raw_file.service.iter().map(move |raw_service| {
628 pool.get_service_by_name(join_name(raw_file.package(), raw_service.name()).as_ref())
629 .expect("service not found")
630 })
631 }
632
633 pub fn file_descriptor_proto(&self) -> &FileDescriptorProto {
635 &self.inner().prost
636 }
637
638 pub fn encode<B>(&self, buf: B) -> Result<(), EncodeError>
643 where
644 B: BufMut,
645 {
646 let mut buf = buf;
647 self.inner().raw.encode(&mut buf)
648 }
649
650 pub fn encode_to_vec(&self) -> Vec<u8> {
655 let mut buf = Vec::new();
656 self.encode(&mut buf).expect("vec should have capacity");
657 buf
658 }
659
660 pub fn options(&self) -> DynamicMessage {
662 decode_options(
663 self.parent_pool(),
664 "google.protobuf.FileOptions",
665 &self.inner().raw.options,
666 )
667 }
668
669 fn inner(&self) -> &FileDescriptorInner {
670 &self.pool.inner.files[self.index as usize]
671 }
672}
673
674impl fmt::Debug for FileDescriptor {
675 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
676 f.debug_struct("FileDescriptor")
677 .field("name", &self.name())
678 .field("package_name", &self.package_name())
679 .finish()
680 }
681}
682
683impl MessageDescriptor {
684 pub fn parent_pool(&self) -> &DescriptorPool {
686 &self.pool
687 }
688
689 pub fn parent_file(&self) -> FileDescriptor {
691 FileDescriptor {
692 pool: self.pool.clone(),
693 index: self.inner().id.file,
694 }
695 }
696
697 pub fn parent_message(&self) -> Option<MessageDescriptor> {
699 self.inner().parent.map(|index| MessageDescriptor {
700 pool: self.pool.clone(),
701 index,
702 })
703 }
704
705 pub fn name(&self) -> &str {
707 self.inner().id.name()
708 }
709
710 pub fn full_name(&self) -> &str {
712 self.inner().id.full_name()
713 }
714
715 pub fn package_name(&self) -> &str {
719 self.raw_file().package()
720 }
721
722 pub fn path(&self) -> &[i32] {
726 &self.inner().id.path
727 }
728
729 pub fn parent_file_descriptor_proto(&self) -> &FileDescriptorProto {
731 &self.pool.inner.files[self.inner().id.file as usize].prost
732 }
733
734 pub fn descriptor_proto(&self) -> &DescriptorProto {
736 find_message_proto_prost(self.parent_file_descriptor_proto(), self.path())
737 }
738
739 pub fn options(&self) -> DynamicMessage {
741 decode_options(
742 self.parent_pool(),
743 "google.protobuf.MessageOptions",
744 &self.raw().options,
745 )
746 }
747
748 pub fn fields(&self) -> impl ExactSizeIterator<Item = FieldDescriptor> + '_ {
750 self.inner()
751 .field_numbers
752 .values()
753 .map(|&index| FieldDescriptor {
754 message: self.clone(),
755 index,
756 })
757 }
758
759 pub(crate) fn fields_in_index_order(
760 &self,
761 ) -> impl ExactSizeIterator<Item = FieldDescriptor> + '_ {
762 self.inner()
763 .fields
764 .iter()
765 .enumerate()
766 .map(|(index, _)| FieldDescriptor {
767 message: self.clone(),
768 index: index as u32,
769 })
770 }
771
772 pub fn oneofs(&self) -> impl ExactSizeIterator<Item = OneofDescriptor> + '_ {
774 indices(&self.inner().oneofs).map(|index| OneofDescriptor {
775 message: self.clone(),
776 index,
777 })
778 }
779
780 pub fn child_messages(&self) -> impl ExactSizeIterator<Item = MessageDescriptor> + '_ {
782 let pool = self.parent_pool();
783 let namespace = self.full_name();
784 let raw_message = self.descriptor_proto();
785 raw_message.nested_type.iter().map(move |raw_message| {
786 pool.get_message_by_name(join_name(namespace, raw_message.name()).as_ref())
787 .expect("message not found")
788 })
789 }
790
791 pub fn child_enums(&self) -> impl ExactSizeIterator<Item = EnumDescriptor> + '_ {
793 let pool = self.parent_pool();
794 let namespace = self.full_name();
795 let raw_message = self.descriptor_proto();
796 raw_message.enum_type.iter().map(move |raw_enum| {
797 pool.get_enum_by_name(join_name(namespace, raw_enum.name()).as_ref())
798 .expect("enum not found")
799 })
800 }
801
802 pub fn child_extensions(&self) -> impl ExactSizeIterator<Item = ExtensionDescriptor> + '_ {
807 let pool = self.parent_pool();
808 let namespace = self.full_name();
809 let raw_message = self.descriptor_proto();
810 raw_message.extension.iter().map(move |raw_extension| {
811 pool.get_extension_by_name(join_name(namespace, raw_extension.name()).as_ref())
812 .expect("extension not found")
813 })
814 }
815
816 pub fn extensions(&self) -> impl ExactSizeIterator<Item = ExtensionDescriptor> + '_ {
821 self.inner()
822 .extensions
823 .iter()
824 .map(|&index| ExtensionDescriptor {
825 pool: self.parent_pool().clone(),
826 index,
827 })
828 }
829
830 pub fn get_field(&self, number: u32) -> Option<FieldDescriptor> {
832 self.inner()
833 .field_numbers
834 .get(&number)
835 .map(|&index| FieldDescriptor {
836 message: self.clone(),
837 index,
838 })
839 }
840
841 pub fn get_field_by_name(&self, name: &str) -> Option<FieldDescriptor> {
843 self.inner()
844 .field_names
845 .get(name)
846 .map(|&index| FieldDescriptor {
847 message: self.clone(),
848 index,
849 })
850 }
851
852 pub fn get_field_by_json_name(&self, json_name: &str) -> Option<FieldDescriptor> {
854 self.inner()
855 .field_json_names
856 .get(json_name)
857 .map(|&index| FieldDescriptor {
858 message: self.clone(),
859 index,
860 })
861 }
862
863 pub fn is_map_entry(&self) -> bool {
876 self.raw()
877 .options
878 .as_ref()
879 .map(|o| o.value.map_entry())
880 .unwrap_or(false)
881 }
882
883 pub fn map_entry_key_field(&self) -> FieldDescriptor {
889 debug_assert!(self.is_map_entry());
890 self.get_field(MAP_ENTRY_KEY_NUMBER)
891 .expect("map entry should have key field")
892 }
893
894 pub fn map_entry_value_field(&self) -> FieldDescriptor {
900 debug_assert!(self.is_map_entry());
901 self.get_field(MAP_ENTRY_VALUE_NUMBER)
902 .expect("map entry should have value field")
903 }
904
905 pub fn reserved_ranges(&self) -> impl ExactSizeIterator<Item = Range<u32>> + '_ {
907 self.raw()
908 .reserved_range
909 .iter()
910 .map(|n| (n.start() as u32)..(n.end() as u32))
911 }
912
913 pub fn reserved_names(&self) -> impl ExactSizeIterator<Item = &str> + '_ {
915 self.raw().reserved_name.iter().map(|n| n.as_ref())
916 }
917
918 pub fn extension_ranges(&self) -> impl ExactSizeIterator<Item = Range<u32>> + '_ {
920 self.raw()
921 .extension_range
922 .iter()
923 .map(|n| (n.start() as u32)..(n.end() as u32))
924 }
925
926 pub fn get_extension(&self, number: u32) -> Option<ExtensionDescriptor> {
928 self.extensions().find(|ext| ext.number() == number)
929 }
930
931 pub fn get_extension_by_full_name(&self, name: &str) -> Option<ExtensionDescriptor> {
933 self.extensions().find(|ext| ext.full_name() == name)
934 }
935
936 pub fn get_extension_by_json_name(&self, name: &str) -> Option<ExtensionDescriptor> {
938 self.extensions().find(|ext| ext.json_name() == name)
939 }
940
941 fn inner(&self) -> &MessageDescriptorInner {
942 &self.pool.inner.messages[self.index as usize]
943 }
944
945 fn raw(&self) -> &types::DescriptorProto {
946 find_message_proto(self.raw_file(), self.path())
947 }
948
949 fn raw_file(&self) -> &types::FileDescriptorProto {
950 &self.pool.inner.files[self.inner().id.file as usize].raw
951 }
952}
953
954impl fmt::Debug for MessageDescriptor {
955 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
956 f.debug_struct("MessageDescriptor")
957 .field("name", &self.name())
958 .field("full_name", &self.full_name())
959 .field("is_map_entry", &self.is_map_entry())
960 .field("fields", &debug_fmt_iter(self.fields()))
961 .field("oneofs", &debug_fmt_iter(self.oneofs()))
962 .finish()
963 }
964}
965
966impl FieldDescriptor {
967 pub fn parent_pool(&self) -> &DescriptorPool {
969 self.message.parent_pool()
970 }
971
972 pub fn parent_file(&self) -> FileDescriptor {
974 self.message.parent_file()
975 }
976
977 pub fn parent_message(&self) -> &MessageDescriptor {
979 &self.message
980 }
981
982 pub fn name(&self) -> &str {
984 self.inner().id.name()
985 }
986
987 pub fn full_name(&self) -> &str {
989 self.inner().id.full_name()
990 }
991
992 pub fn path(&self) -> &[i32] {
996 &self.inner().id.path
997 }
998
999 pub fn field_descriptor_proto(&self) -> &FieldDescriptorProto {
1001 &self.parent_message().descriptor_proto().field[*self.path().last().unwrap() as usize]
1002 }
1003
1004 pub fn options(&self) -> DynamicMessage {
1006 decode_options(
1007 self.parent_pool(),
1008 "google.protobuf.FieldOptions",
1009 &self.raw().options,
1010 )
1011 }
1012
1013 pub fn number(&self) -> u32 {
1015 self.inner().number
1016 }
1017
1018 pub fn json_name(&self) -> &str {
1023 &self.inner().json_name
1024 }
1025
1026 pub fn is_group(&self) -> bool {
1028 matches!(self.inner().kind, KindIndex::Group(_))
1029 }
1030
1031 pub fn is_list(&self) -> bool {
1036 self.cardinality() == Cardinality::Repeated && !self.is_map()
1037 }
1038
1039 pub fn is_map(&self) -> bool {
1045 self.cardinality() == Cardinality::Repeated
1046 && match self.kind() {
1047 Kind::Message(message) => message.is_map_entry(),
1048 _ => false,
1049 }
1050 }
1051
1052 pub fn is_packed(&self) -> bool {
1054 self.inner().is_packed
1055 }
1056
1057 pub fn cardinality(&self) -> Cardinality {
1059 self.inner().cardinality
1060 }
1061
1062 pub fn supports_presence(&self) -> bool {
1069 self.inner().supports_presence
1070 }
1071
1072 pub fn kind(&self) -> Kind {
1074 Kind::new(self.parent_pool(), self.inner().kind)
1075 }
1076
1077 pub fn containing_oneof(&self) -> Option<OneofDescriptor> {
1080 self.inner().oneof.map(|index| OneofDescriptor {
1081 message: self.message.clone(),
1082 index,
1083 })
1084 }
1085
1086 pub(crate) fn default_value(&self) -> Option<&Value> {
1087 self.inner().default.as_ref()
1088 }
1089
1090 pub(crate) fn is_packable(&self) -> bool {
1091 self.inner().kind.is_packable()
1092 }
1093
1094 fn inner(&self) -> &FieldDescriptorInner {
1095 &self.message.inner().fields[self.index as usize]
1096 }
1097
1098 fn raw(&self) -> &types::FieldDescriptorProto {
1099 &self.message.raw().field[self.index as usize]
1100 }
1101}
1102
1103impl fmt::Debug for FieldDescriptor {
1104 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1105 f.debug_struct("FieldDescriptor")
1106 .field("name", &self.name())
1107 .field("full_name", &self.full_name())
1108 .field("json_name", &self.json_name())
1109 .field("number", &self.number())
1110 .field("kind", &self.kind())
1111 .field("cardinality", &self.cardinality())
1112 .field(
1113 "containing_oneof",
1114 &self.containing_oneof().map(|o| o.name().to_owned()),
1115 )
1116 .field("default_value", &self.default_value())
1117 .field("is_group", &self.is_group())
1118 .field("is_list", &self.is_list())
1119 .field("is_map", &self.is_map())
1120 .field("is_packed", &self.is_packed())
1121 .field("supports_presence", &self.supports_presence())
1122 .finish()
1123 }
1124}
1125
1126impl ExtensionDescriptor {
1127 pub fn parent_pool(&self) -> &DescriptorPool {
1129 &self.pool
1130 }
1131
1132 pub fn parent_file(&self) -> FileDescriptor {
1134 FileDescriptor {
1135 pool: self.pool.clone(),
1136 index: self.inner().id.file,
1137 }
1138 }
1139
1140 pub fn parent_message(&self) -> Option<MessageDescriptor> {
1145 self.inner().parent.map(|index| MessageDescriptor {
1146 pool: self.pool.clone(),
1147 index,
1148 })
1149 }
1150
1151 pub fn name(&self) -> &str {
1153 self.inner().id.name()
1154 }
1155
1156 pub fn full_name(&self) -> &str {
1160 self.inner().id.full_name()
1161 }
1162
1163 pub fn package_name(&self) -> &str {
1167 self.raw_file().package()
1168 }
1169
1170 pub fn path(&self) -> &[i32] {
1174 &self.inner().id.path
1175 }
1176
1177 pub fn parent_file_descriptor_proto(&self) -> &FileDescriptorProto {
1179 &self.pool.inner.files[self.inner().id.file as usize].prost
1180 }
1181
1182 pub fn field_descriptor_proto(&self) -> &FieldDescriptorProto {
1184 let file = self.parent_file_descriptor_proto();
1185 let path = self.path();
1186 debug_assert_ne!(path.len(), 0);
1187 debug_assert_eq!(path.len() % 2, 0);
1188 if path.len() == 2 {
1189 debug_assert_eq!(path[0], tag::file::EXTENSION);
1190 &file.extension[path[1] as usize]
1191 } else {
1192 let message = find_message_proto_prost(file, &path[..path.len() - 2]);
1193 debug_assert_eq!(path[path.len() - 2], tag::message::EXTENSION);
1194 &message.extension[path[path.len() - 1] as usize]
1195 }
1196 }
1197
1198 pub fn options(&self) -> DynamicMessage {
1200 decode_options(
1201 self.parent_pool(),
1202 "google.protobuf.FieldOptions",
1203 &self.raw().options,
1204 )
1205 }
1206
1207 pub fn number(&self) -> u32 {
1209 self.inner().number
1210 }
1211
1212 pub fn json_name(&self) -> &str {
1214 &self.inner().json_name
1215 }
1216
1217 pub fn is_group(&self) -> bool {
1219 matches!(self.inner().kind, KindIndex::Group(_))
1220 }
1221
1222 pub fn is_list(&self) -> bool {
1227 self.cardinality() == Cardinality::Repeated && !self.is_map()
1228 }
1229
1230 pub fn is_map(&self) -> bool {
1236 self.cardinality() == Cardinality::Repeated
1237 && match self.kind() {
1238 Kind::Message(message) => message.is_map_entry(),
1239 _ => false,
1240 }
1241 }
1242
1243 pub fn is_packed(&self) -> bool {
1245 self.inner().is_packed
1246 }
1247
1248 pub fn cardinality(&self) -> Cardinality {
1250 self.inner().cardinality
1251 }
1252
1253 pub fn supports_presence(&self) -> bool {
1258 self.cardinality() != Cardinality::Repeated
1259 }
1260
1261 pub fn kind(&self) -> Kind {
1263 Kind::new(&self.pool, self.inner().kind)
1264 }
1265
1266 pub fn containing_message(&self) -> MessageDescriptor {
1268 MessageDescriptor {
1269 pool: self.pool.clone(),
1270 index: self.inner().extendee,
1271 }
1272 }
1273
1274 pub(crate) fn default_value(&self) -> Option<&Value> {
1275 self.inner().default.as_ref()
1276 }
1277
1278 pub(crate) fn is_packable(&self) -> bool {
1279 self.inner().kind.is_packable()
1280 }
1281
1282 fn inner(&self) -> &ExtensionDescriptorInner {
1283 &self.pool.inner.extensions[self.index as usize]
1284 }
1285
1286 fn raw(&self) -> &types::FieldDescriptorProto {
1287 let file = self.raw_file();
1288 let path = self.path();
1289 debug_assert_ne!(path.len(), 0);
1290 debug_assert_eq!(path.len() % 2, 0);
1291 if path.len() == 2 {
1292 debug_assert_eq!(path[0], tag::file::EXTENSION);
1293 &file.extension[path[1] as usize]
1294 } else {
1295 let message = find_message_proto(file, &path[..path.len() - 2]);
1296 debug_assert_eq!(path[path.len() - 2], tag::message::EXTENSION);
1297 &message.extension[path[path.len() - 1] as usize]
1298 }
1299 }
1300
1301 fn raw_file(&self) -> &types::FileDescriptorProto {
1302 &self.pool.inner.files[self.inner().id.file as usize].raw
1303 }
1304}
1305
1306impl fmt::Debug for ExtensionDescriptor {
1307 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1308 f.debug_struct("ExtensionDescriptor")
1309 .field("name", &self.name())
1310 .field("full_name", &self.full_name())
1311 .field("json_name", &self.json_name())
1312 .field("number", &self.number())
1313 .field("kind", &self.kind())
1314 .field("cardinality", &self.cardinality())
1315 .field(
1316 "containing_message",
1317 &self.containing_message().name().to_owned(),
1318 )
1319 .field("default_value", &self.default_value())
1320 .field("is_group", &self.is_group())
1321 .field("is_list", &self.is_list())
1322 .field("is_map", &self.is_map())
1323 .field("is_packed", &self.is_packed())
1324 .field("supports_presence", &self.supports_presence())
1325 .finish()
1326 }
1327}
1328
1329impl EnumDescriptor {
1330 pub fn parent_pool(&self) -> &DescriptorPool {
1332 &self.pool
1333 }
1334
1335 pub fn parent_file(&self) -> FileDescriptor {
1337 FileDescriptor {
1338 pool: self.pool.clone(),
1339 index: self.inner().id.file,
1340 }
1341 }
1342
1343 pub fn parent_message(&self) -> Option<MessageDescriptor> {
1345 self.inner().parent.map(|index| MessageDescriptor {
1346 pool: self.pool.clone(),
1347 index,
1348 })
1349 }
1350
1351 pub fn name(&self) -> &str {
1353 self.inner().id.name()
1354 }
1355
1356 pub fn full_name(&self) -> &str {
1358 self.inner().id.full_name()
1359 }
1360
1361 pub fn package_name(&self) -> &str {
1365 self.raw_file().package()
1366 }
1367
1368 pub fn path(&self) -> &[i32] {
1372 &self.inner().id.path
1373 }
1374
1375 pub fn parent_file_descriptor_proto(&self) -> &FileDescriptorProto {
1377 &self.pool.inner.files[self.inner().id.file as usize].prost
1378 }
1379
1380 pub fn enum_descriptor_proto(&self) -> &EnumDescriptorProto {
1382 let file = self.parent_file_descriptor_proto();
1383 let path = self.path();
1384 debug_assert_ne!(path.len(), 0);
1385 debug_assert_eq!(path.len() % 2, 0);
1386 if path.len() == 2 {
1387 debug_assert_eq!(path[0], tag::file::ENUM_TYPE);
1388 &file.enum_type[path[1] as usize]
1389 } else {
1390 let message = find_message_proto_prost(file, &path[..path.len() - 2]);
1391 debug_assert_eq!(path[path.len() - 2], tag::message::ENUM_TYPE);
1392 &message.enum_type[path[path.len() - 1] as usize]
1393 }
1394 }
1395
1396 pub fn options(&self) -> DynamicMessage {
1398 decode_options(
1399 self.parent_pool(),
1400 "google.protobuf.EnumOptions",
1401 &self.raw().options,
1402 )
1403 }
1404
1405 pub fn default_value(&self) -> EnumValueDescriptor {
1407 EnumValueDescriptor {
1408 parent: self.clone(),
1409 index: 0,
1410 }
1411 }
1412
1413 pub fn get_value_by_name(&self, name: &str) -> Option<EnumValueDescriptor> {
1415 self.inner()
1416 .value_names
1417 .get(name)
1418 .map(|&index| EnumValueDescriptor {
1419 parent: self.clone(),
1420 index,
1421 })
1422 }
1423
1424 pub fn get_value(&self, number: i32) -> Option<EnumValueDescriptor> {
1429 self.inner()
1430 .value_numbers
1431 .binary_search_by(|(l, _)| l.cmp(&number))
1432 .ok()
1433 .map(|index| EnumValueDescriptor {
1434 parent: self.clone(),
1435 index: self.inner().value_numbers[index].1,
1436 })
1437 }
1438
1439 pub fn values(&self) -> impl ExactSizeIterator<Item = EnumValueDescriptor> + '_ {
1441 self.inner()
1442 .value_numbers
1443 .iter()
1444 .map(|&(_, index)| EnumValueDescriptor {
1445 parent: self.clone(),
1446 index,
1447 })
1448 }
1449
1450 pub fn reserved_ranges(&self) -> impl ExactSizeIterator<Item = RangeInclusive<i32>> + '_ {
1452 self.raw()
1453 .reserved_range
1454 .iter()
1455 .map(|n| n.start()..=n.end())
1456 }
1457
1458 pub fn reserved_names(&self) -> impl ExactSizeIterator<Item = &str> + '_ {
1460 self.raw().reserved_name.iter().map(|n| n.as_ref())
1461 }
1462
1463 fn inner(&self) -> &EnumDescriptorInner {
1464 &self.pool.inner.enums[self.index as usize]
1465 }
1466
1467 fn raw(&self) -> &types::EnumDescriptorProto {
1468 find_enum_proto(self.raw_file(), self.path())
1469 }
1470
1471 fn raw_file(&self) -> &types::FileDescriptorProto {
1472 &self.pool.inner.files[self.inner().id.file as usize].raw
1473 }
1474}
1475
1476impl fmt::Debug for EnumDescriptor {
1477 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1478 f.debug_struct("EnumDescriptor")
1479 .field("name", &self.name())
1480 .field("full_name", &self.full_name())
1481 .field("default_value", &self.default_value())
1482 .field("values", &debug_fmt_iter(self.values()))
1483 .finish()
1484 }
1485}
1486
1487impl EnumValueDescriptor {
1488 pub fn parent_pool(&self) -> &DescriptorPool {
1490 self.parent.parent_pool()
1491 }
1492
1493 pub fn parent_file(&self) -> FileDescriptor {
1495 self.parent.parent_file()
1496 }
1497
1498 pub fn parent_enum(&self) -> &EnumDescriptor {
1500 &self.parent
1501 }
1502
1503 pub fn name(&self) -> &str {
1505 self.inner().id.name()
1506 }
1507
1508 pub fn full_name(&self) -> &str {
1510 self.inner().id.full_name()
1511 }
1512
1513 pub fn path(&self) -> &[i32] {
1517 &self.inner().id.path
1518 }
1519
1520 pub fn enum_value_descriptor_proto(&self) -> &EnumValueDescriptorProto {
1522 &self.parent.enum_descriptor_proto().value[self.index as usize]
1523 }
1524
1525 pub fn options(&self) -> DynamicMessage {
1527 decode_options(
1528 self.parent_pool(),
1529 "google.protobuf.EnumValueOptions",
1530 &self.raw().options,
1531 )
1532 }
1533
1534 pub fn number(&self) -> i32 {
1536 self.inner().number
1537 }
1538
1539 fn inner(&self) -> &EnumValueDescriptorInner {
1540 &self.parent.inner().values[self.index as usize]
1541 }
1542
1543 fn raw(&self) -> &types::EnumValueDescriptorProto {
1544 &self.parent.raw().value[self.index as usize]
1545 }
1546}
1547
1548impl fmt::Debug for EnumValueDescriptor {
1549 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1550 f.debug_struct("EnumValueDescriptor")
1551 .field("name", &self.number())
1552 .field("full_name", &self.full_name())
1553 .field("number", &self.number())
1554 .finish()
1555 }
1556}
1557
1558impl OneofDescriptor {
1559 pub fn parent_pool(&self) -> &DescriptorPool {
1561 self.message.parent_pool()
1562 }
1563
1564 pub fn parent_file(&self) -> FileDescriptor {
1566 self.message.parent_file()
1567 }
1568
1569 pub fn parent_message(&self) -> &MessageDescriptor {
1571 &self.message
1572 }
1573
1574 pub fn name(&self) -> &str {
1576 self.inner().id.name()
1577 }
1578
1579 pub fn full_name(&self) -> &str {
1581 self.inner().id.full_name()
1582 }
1583
1584 pub fn path(&self) -> &[i32] {
1588 &self.inner().id.path
1589 }
1590
1591 pub fn oneof_descriptor_proto(&self) -> &OneofDescriptorProto {
1593 &self.message.descriptor_proto().oneof_decl[self.index as usize]
1594 }
1595
1596 pub fn options(&self) -> DynamicMessage {
1598 decode_options(
1599 self.parent_pool(),
1600 "google.protobuf.OneofOptions",
1601 &self.raw().options,
1602 )
1603 }
1604
1605 pub fn fields(&self) -> impl ExactSizeIterator<Item = FieldDescriptor> + '_ {
1607 self.inner().fields.iter().map(|&index| FieldDescriptor {
1608 message: self.parent_message().clone(),
1609 index,
1610 })
1611 }
1612
1613 fn inner(&self) -> &OneofDescriptorInner {
1614 &self.message.inner().oneofs[self.index as usize]
1615 }
1616
1617 fn raw(&self) -> &types::OneofDescriptorProto {
1618 &self.message.raw().oneof_decl[self.index as usize]
1619 }
1620}
1621
1622impl fmt::Debug for OneofDescriptor {
1623 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1624 f.debug_struct("OneofDescriptor")
1625 .field("name", &self.name())
1626 .field("full_name", &self.full_name())
1627 .field("fields", &debug_fmt_iter(self.fields()))
1628 .finish()
1629 }
1630}
1631
1632impl ServiceDescriptor {
1633 pub fn new(pool: DescriptorPool, index: usize) -> Self {
1639 debug_assert!(index < pool.services().len());
1640 ServiceDescriptor {
1641 pool,
1642 index: to_index(index),
1643 }
1644 }
1645
1646 pub fn index(&self) -> usize {
1648 self.index as usize
1649 }
1650
1651 pub fn parent_pool(&self) -> &DescriptorPool {
1653 &self.pool
1654 }
1655
1656 pub fn parent_file(&self) -> FileDescriptor {
1658 FileDescriptor {
1659 pool: self.pool.clone(),
1660 index: self.inner().id.file,
1661 }
1662 }
1663
1664 pub fn name(&self) -> &str {
1666 self.inner().id.name()
1667 }
1668
1669 pub fn full_name(&self) -> &str {
1671 self.inner().id.full_name()
1672 }
1673
1674 pub fn package_name(&self) -> &str {
1678 self.raw_file().package()
1679 }
1680
1681 pub fn path(&self) -> &[i32] {
1685 &self.inner().id.path
1686 }
1687
1688 pub fn parent_file_descriptor_proto(&self) -> &FileDescriptorProto {
1690 &self.pool.inner.files[self.inner().id.file as usize].prost
1691 }
1692
1693 pub fn service_descriptor_proto(&self) -> &ServiceDescriptorProto {
1695 let path = self.path();
1696 debug_assert!(!path.is_empty());
1697 &self.parent_file_descriptor_proto().service[*path.last().unwrap() as usize]
1698 }
1699
1700 pub fn options(&self) -> DynamicMessage {
1702 decode_options(
1703 self.parent_pool(),
1704 "google.protobuf.ServiceOptions",
1705 &self.raw().options,
1706 )
1707 }
1708
1709 pub fn methods(&self) -> impl ExactSizeIterator<Item = MethodDescriptor> + '_ {
1711 indices(&self.inner().methods).map(|index| MethodDescriptor {
1712 service: self.clone(),
1713 index,
1714 })
1715 }
1716
1717 fn inner(&self) -> &ServiceDescriptorInner {
1718 &self.pool.inner.services[self.index as usize]
1719 }
1720
1721 fn raw(&self) -> &types::ServiceDescriptorProto {
1722 let path = self.path();
1723 debug_assert!(!path.is_empty());
1724 &self.raw_file().service[*path.last().unwrap() as usize]
1725 }
1726
1727 fn raw_file(&self) -> &types::FileDescriptorProto {
1728 &self.pool.inner.files[self.inner().id.file as usize].raw
1729 }
1730}
1731
1732impl fmt::Debug for ServiceDescriptor {
1733 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1734 f.debug_struct("ServiceDescriptor")
1735 .field("name", &self.name())
1736 .field("full_name", &self.full_name())
1737 .field("index", &self.index())
1738 .field("methods", &debug_fmt_iter(self.methods()))
1739 .finish()
1740 }
1741}
1742
1743impl MethodDescriptor {
1744 pub fn new(service: ServiceDescriptor, index: usize) -> Self {
1750 debug_assert!(index < service.methods().len());
1751 MethodDescriptor {
1752 service,
1753 index: to_index(index),
1754 }
1755 }
1756
1757 pub fn index(&self) -> usize {
1759 self.index as usize
1760 }
1761
1762 pub fn parent_service(&self) -> &ServiceDescriptor {
1764 &self.service
1765 }
1766
1767 pub fn parent_pool(&self) -> &DescriptorPool {
1769 self.service.parent_pool()
1770 }
1771
1772 pub fn parent_file(&self) -> FileDescriptor {
1774 self.service.parent_file()
1775 }
1776
1777 pub fn name(&self) -> &str {
1779 self.inner().id.name()
1780 }
1781
1782 pub fn full_name(&self) -> &str {
1784 self.inner().id.full_name()
1785 }
1786
1787 pub fn path(&self) -> &[i32] {
1791 &self.inner().id.path
1792 }
1793
1794 pub fn method_descriptor_proto(&self) -> &MethodDescriptorProto {
1796 &self.service.service_descriptor_proto().method[self.index as usize]
1797 }
1798
1799 pub fn options(&self) -> DynamicMessage {
1801 decode_options(
1802 self.parent_pool(),
1803 "google.protobuf.MethodOptions",
1804 &self.raw().options,
1805 )
1806 }
1807
1808 pub fn input(&self) -> MessageDescriptor {
1810 MessageDescriptor {
1811 pool: self.parent_pool().clone(),
1812 index: self.inner().input,
1813 }
1814 }
1815
1816 pub fn output(&self) -> MessageDescriptor {
1818 MessageDescriptor {
1819 pool: self.parent_pool().clone(),
1820 index: self.inner().output,
1821 }
1822 }
1823
1824 pub fn is_client_streaming(&self) -> bool {
1826 self.raw().client_streaming()
1827 }
1828
1829 pub fn is_server_streaming(&self) -> bool {
1831 self.raw().server_streaming()
1832 }
1833
1834 fn inner(&self) -> &MethodDescriptorInner {
1835 &self.service.inner().methods[self.index as usize]
1836 }
1837
1838 fn raw(&self) -> &types::MethodDescriptorProto {
1839 &self.service.raw().method[self.index as usize]
1840 }
1841}
1842
1843impl fmt::Debug for MethodDescriptor {
1844 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1845 f.debug_struct("MethodDescriptor")
1846 .field("name", &self.name())
1847 .field("full_name", &self.full_name())
1848 .field("index", &self.index())
1849 .field("input", &self.input())
1850 .field("output", &self.output())
1851 .field("is_client_streaming", &self.is_client_streaming())
1852 .field("is_server_streaming", &self.is_server_streaming())
1853 .finish()
1854 }
1855}
1856
1857fn debug_fmt_iter<I>(i: I) -> impl fmt::Debug
1858where
1859 I: Iterator,
1860 I::Item: fmt::Debug,
1861{
1862 struct Wrapper<T>(Vec<T>);
1863
1864 impl<T> fmt::Debug for Wrapper<T>
1865 where
1866 T: fmt::Debug,
1867 {
1868 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1869 f.debug_list().entries(&self.0).finish()
1870 }
1871 }
1872
1873 Wrapper(i.collect())
1874}
1875
1876#[allow(clippy::ptr_arg)]
1877fn indices<T>(f: &Vec<T>) -> Range<DescriptorIndex> {
1878 0..to_index(f.len())
1879}
1880
1881fn join_name<'a>(namespace: &str, name: &'a str) -> Cow<'a, str> {
1882 if namespace.is_empty() {
1883 Cow::Borrowed(name)
1884 } else {
1885 Cow::Owned(format!("{}.{}", namespace, name))
1886 }
1887}
1888
1889fn decode_options<T>(
1890 pool: &DescriptorPool,
1891 name: &str,
1892 option: &Option<Options<T>>,
1893) -> DynamicMessage {
1894 let message_desc = pool
1895 .get_message_by_name(name)
1896 .unwrap_or_else(|| DescriptorPool::global().get_message_by_name(name).unwrap());
1897
1898 let bytes = option
1899 .as_ref()
1900 .map(|o| o.encoded.as_slice())
1901 .unwrap_or_default();
1902 DynamicMessage::decode(message_desc, bytes).unwrap()
1903}
1904
1905fn find_message_proto_prost<'a>(
1906 file: &'a FileDescriptorProto,
1907 path: &[i32],
1908) -> &'a DescriptorProto {
1909 debug_assert_ne!(path.len(), 0);
1910 debug_assert_eq!(path.len() % 2, 0);
1911
1912 let mut message: Option<&'a DescriptorProto> = None;
1913 for part in path.chunks(2) {
1914 match part[0] {
1915 tag::file::MESSAGE_TYPE => message = Some(&file.message_type[part[1] as usize]),
1916 tag::message::NESTED_TYPE => {
1917 message = Some(&message.unwrap().nested_type[part[1] as usize])
1918 }
1919 _ => panic!("invalid message path"),
1920 }
1921 }
1922
1923 message.unwrap()
1924}