pb_jelly/descriptor.rs
1use crate::wire_format;
2
3/// Trait implemented by all the messages defined in proto files.
4/// Provides rudimentary support for message descriptor, mostly as a way to get
5/// the type name for a given message rather than trying to implement the full
6/// reflection API. For more info, see:
7/// <https://developers.google.com/protocol-buffers/docs/techniques>
8/// <https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/descriptor.proto>
9pub struct MessageDescriptor {
10 /// The name of the message type, not including its scope.
11 pub name: &'static str,
12 /// The fully-qualified name of the message type, scope delimited by periods.
13 /// For example, message type "Foo" which is declared in package "bar" has full name "bar.Foo".
14 /// If a type "Baz" is nested within Foo, Baz's full_name is "bar.Foo_Baz". To get only the
15 /// part that comes after the last '.', use `message_name`.
16 pub full_name: &'static str,
17 /// Information about the fields in this message.
18 pub fields: &'static [FieldDescriptor],
19 /// Information about the oneofs in this message.
20 pub oneofs: &'static [OneofDescriptor],
21}
22
23/// Describes a field within a message.
24/// Provides rudimentary support for the proto field reflection API, with the intention of being
25/// able to serialize or introspect fields individually without having knowledge of the structure
26/// of the specific message itself. For more info, see:
27/// <https://developers.google.com/protocol-buffers/docs/techniques>
28/// <https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/descriptor.proto>
29pub struct FieldDescriptor {
30 /// The name of this field, not including its scope.
31 pub name: &'static str,
32 /// The fully-qualified name of this field, scope delimited by periods.
33 pub full_name: &'static str,
34 /// The index of this field, which has values from 0 inclusive to n exclusive, where n is the
35 /// number of fields in this message.
36 pub index: u32,
37 /// The number assigned to the field in the proto declaration.
38 pub number: u32,
39 pub typ: wire_format::Type,
40 pub label: Label,
41 /// If this field is part of a oneof group, this is an index into the `oneofs` of the parent
42 /// message. Otherwise None.
43 pub oneof_index: Option<u32>,
44}
45
46#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
47pub enum Label {
48 Optional = 1,
49 Required = 2,
50 Repeated = 3,
51}
52
53/// Describes a oneof.
54pub struct OneofDescriptor {
55 pub name: &'static str,
56}
57
58impl MessageDescriptor {
59 /// Gets a field by name.
60 pub fn get_field(&self, name: &str) -> Option<&FieldDescriptor> {
61 self.fields.iter().find(|f| f.name == name)
62 }
63}