1 pub(crate) mod generated;
2 
3 use crate::descriptor::OneofDescriptorProto;
4 use crate::reflect::file::index::OneofIndices;
5 use crate::reflect::file::FileDescriptorImpl;
6 use crate::reflect::oneof::generated::GeneratedOneofDescriptor;
7 use crate::reflect::FieldDescriptor;
8 use crate::reflect::FileDescriptor;
9 use crate::reflect::MessageDescriptor;
10 
11 /// Oneof descriptor.
12 #[derive(Eq, PartialEq, Clone, Debug)]
13 pub struct OneofDescriptor {
14     pub(crate) file_descriptor: FileDescriptor,
15     pub(crate) index: usize,
16 }
17 
18 pub(crate) enum OneofDescriptorImplRef {
19     Generated(&'static GeneratedOneofDescriptor),
20     Dynamic,
21 }
22 
23 impl OneofDescriptor {
index_entry(&self) -> &OneofIndices24     fn index_entry(&self) -> &OneofIndices {
25         &self.file_descriptor.common().oneofs[self.index]
26     }
27 
28     /// `.proto` part associated with this descriptor
proto(&self) -> &OneofDescriptorProto29     pub fn proto(&self) -> &OneofDescriptorProto {
30         let index_entry = self.index_entry();
31         let message_descriptor = self
32             .file_descriptor
33             .message_proto_by_index(index_entry.containing_message);
34         &message_descriptor.oneof_decl[index_entry.index_in_containing_message]
35     }
36 
37     /// Oneof name as specified in `.proto` file.
name(&self) -> &str38     pub fn name(&self) -> &str {
39         self.proto().name()
40     }
41 
42     #[allow(dead_code)]
_get_impl(&self) -> OneofDescriptorImplRef43     pub(crate) fn _get_impl(&self) -> OneofDescriptorImplRef {
44         match &self.file_descriptor.imp {
45             FileDescriptorImpl::Generated(g) => {
46                 OneofDescriptorImplRef::Generated(&g.oneofs[self.index])
47             }
48             FileDescriptorImpl::Dynamic(..) => OneofDescriptorImplRef::Dynamic,
49         }
50     }
51 
52     /// Message which contains this oneof.
containing_message(&self) -> MessageDescriptor53     pub fn containing_message(&self) -> MessageDescriptor {
54         MessageDescriptor {
55             file_descriptor: self.file_descriptor.clone(),
56             index: self.index_entry().containing_message,
57         }
58     }
59 
60     /// This oneof is not present in sources.
is_synthetic(&self) -> bool61     pub fn is_synthetic(&self) -> bool {
62         self.index_entry().synthetic
63     }
64 
65     /// Fully qualified name of oneof (fully qualified name of enclosing message
66     /// followed by oneof name).
full_name(&self) -> String67     pub fn full_name(&self) -> String {
68         format!("{}.{}", self.containing_message(), self.name())
69     }
70 
71     /// Fields in this oneof.
fields<'a>(&'a self) -> impl Iterator<Item = FieldDescriptor> + 'a72     pub fn fields<'a>(&'a self) -> impl Iterator<Item = FieldDescriptor> + 'a {
73         let message = self.containing_message();
74         self.index_entry()
75             .fields
76             .iter()
77             .map(move |&i| message.field_by_index(i))
78     }
79 }
80