prost_reflect/descriptor/
types.rs

1use std::fmt;
2
3use prost::{
4    bytes::{Buf, BufMut},
5    encoding::{encode_key, skip_field, DecodeContext, WireType},
6    DecodeError, Message,
7};
8
9pub(crate) use prost_types::{
10    enum_descriptor_proto, field_descriptor_proto, uninterpreted_option, EnumOptions,
11    EnumValueOptions, ExtensionRangeOptions, FieldOptions, FileOptions, MessageOptions,
12    MethodOptions, OneofOptions, ServiceOptions, SourceCodeInfo, UninterpretedOption,
13};
14
15#[derive(Clone, PartialEq, Message)]
16pub(crate) struct FileDescriptorSet {
17    #[prost(message, repeated, tag = "1")]
18    pub file: Vec<FileDescriptorProto>,
19}
20
21#[derive(Clone, PartialEq, Message)]
22pub(crate) struct FileDescriptorProto {
23    #[prost(string, optional, tag = "1")]
24    pub name: Option<String>,
25    #[prost(string, optional, tag = "2")]
26    pub package: Option<String>,
27    #[prost(string, repeated, tag = "3")]
28    pub dependency: Vec<String>,
29    #[prost(int32, repeated, packed = "false", tag = "10")]
30    pub public_dependency: Vec<i32>,
31    #[prost(int32, repeated, packed = "false", tag = "11")]
32    pub weak_dependency: Vec<i32>,
33    #[prost(message, repeated, tag = "4")]
34    pub message_type: Vec<DescriptorProto>,
35    #[prost(message, repeated, tag = "5")]
36    pub(crate) enum_type: Vec<EnumDescriptorProto>,
37    #[prost(message, repeated, tag = "6")]
38    pub service: Vec<ServiceDescriptorProto>,
39    #[prost(message, repeated, tag = "7")]
40    pub extension: Vec<FieldDescriptorProto>,
41    #[prost(message, optional, tag = "8")]
42    pub options: Option<Options<FileOptions>>,
43    #[prost(message, optional, tag = "9")]
44    pub source_code_info: Option<SourceCodeInfo>,
45    #[prost(string, optional, tag = "12")]
46    pub syntax: Option<String>,
47}
48
49#[derive(Clone, PartialEq, Message)]
50pub(crate) struct DescriptorProto {
51    #[prost(string, optional, tag = "1")]
52    pub name: Option<String>,
53    #[prost(message, repeated, tag = "2")]
54    pub field: Vec<FieldDescriptorProto>,
55    #[prost(message, repeated, tag = "6")]
56    pub extension: Vec<FieldDescriptorProto>,
57    #[prost(message, repeated, tag = "3")]
58    pub nested_type: Vec<DescriptorProto>,
59    #[prost(message, repeated, tag = "4")]
60    pub(crate) enum_type: Vec<EnumDescriptorProto>,
61    #[prost(message, repeated, tag = "5")]
62    pub extension_range: Vec<descriptor_proto::ExtensionRange>,
63    #[prost(message, repeated, tag = "8")]
64    pub oneof_decl: Vec<OneofDescriptorProto>,
65    #[prost(message, optional, tag = "7")]
66    pub options: Option<Options<MessageOptions>>,
67    #[prost(message, repeated, tag = "9")]
68    pub reserved_range: Vec<descriptor_proto::ReservedRange>,
69    #[prost(string, repeated, tag = "10")]
70    pub reserved_name: Vec<String>,
71}
72
73pub(crate) mod descriptor_proto {
74    pub(crate) use prost_types::descriptor_proto::ReservedRange;
75
76    use super::*;
77
78    #[derive(Clone, PartialEq, Message)]
79    pub(crate) struct ExtensionRange {
80        #[prost(int32, optional, tag = "1")]
81        pub start: Option<i32>,
82        #[prost(int32, optional, tag = "2")]
83        pub end: Option<i32>,
84        #[prost(message, optional, tag = "3")]
85        pub options: Option<Options<ExtensionRangeOptions>>,
86    }
87}
88
89#[derive(Clone, PartialEq, Message)]
90pub(crate) struct FieldDescriptorProto {
91    #[prost(string, optional, tag = "1")]
92    pub name: Option<String>,
93    #[prost(int32, optional, tag = "3")]
94    pub number: Option<i32>,
95    #[prost(enumeration = "field_descriptor_proto::Label", optional, tag = "4")]
96    pub label: Option<i32>,
97    #[prost(enumeration = "field_descriptor_proto::Type", optional, tag = "5")]
98    pub r#type: Option<i32>,
99    #[prost(string, optional, tag = "6")]
100    pub type_name: Option<String>,
101    #[prost(string, optional, tag = "2")]
102    pub extendee: Option<String>,
103    #[prost(string, optional, tag = "7")]
104    pub default_value: Option<String>,
105    #[prost(int32, optional, tag = "9")]
106    pub oneof_index: Option<i32>,
107    #[prost(string, optional, tag = "10")]
108    pub json_name: Option<String>,
109    #[prost(message, optional, tag = "8")]
110    pub options: Option<Options<FieldOptions>>,
111    #[prost(bool, optional, tag = "17")]
112    pub proto3_optional: Option<bool>,
113}
114
115#[derive(Clone, PartialEq, Message)]
116pub(crate) struct OneofDescriptorProto {
117    #[prost(string, optional, tag = "1")]
118    pub name: Option<String>,
119    #[prost(message, optional, tag = "2")]
120    pub options: Option<Options<OneofOptions>>,
121}
122
123#[derive(Clone, PartialEq, Message)]
124pub(crate) struct EnumDescriptorProto {
125    #[prost(string, optional, tag = "1")]
126    pub name: Option<String>,
127    #[prost(message, repeated, tag = "2")]
128    pub value: Vec<EnumValueDescriptorProto>,
129    #[prost(message, optional, tag = "3")]
130    pub options: Option<Options<EnumOptions>>,
131    #[prost(message, repeated, tag = "4")]
132    pub reserved_range: Vec<enum_descriptor_proto::EnumReservedRange>,
133    #[prost(string, repeated, tag = "5")]
134    pub reserved_name: Vec<String>,
135}
136
137#[derive(Clone, PartialEq, Message)]
138pub(crate) struct EnumValueDescriptorProto {
139    #[prost(string, optional, tag = "1")]
140    pub name: Option<String>,
141    #[prost(int32, optional, tag = "2")]
142    pub number: Option<i32>,
143    #[prost(message, optional, tag = "3")]
144    pub options: Option<Options<EnumValueOptions>>,
145}
146
147#[derive(Clone, PartialEq, Message)]
148pub(crate) struct ServiceDescriptorProto {
149    #[prost(string, optional, tag = "1")]
150    pub name: Option<String>,
151    #[prost(message, repeated, tag = "2")]
152    pub method: Vec<MethodDescriptorProto>,
153    #[prost(message, optional, tag = "3")]
154    pub options: Option<Options<ServiceOptions>>,
155}
156
157#[derive(Clone, PartialEq, Message)]
158pub(crate) struct MethodDescriptorProto {
159    #[prost(string, optional, tag = "1")]
160    pub name: Option<String>,
161    #[prost(string, optional, tag = "2")]
162    pub input_type: Option<String>,
163    #[prost(string, optional, tag = "3")]
164    pub output_type: Option<String>,
165    #[prost(message, optional, tag = "4")]
166    pub options: Option<Options<MethodOptions>>,
167    #[prost(bool, optional, tag = "5", default = "false")]
168    pub client_streaming: Option<bool>,
169    #[prost(bool, optional, tag = "6", default = "false")]
170    pub server_streaming: Option<bool>,
171}
172
173#[derive(Clone, Default, PartialEq)]
174pub(crate) struct Options<T> {
175    pub(crate) encoded: Vec<u8>,
176    pub(crate) value: T,
177}
178
179impl FileDescriptorProto {
180    pub(crate) fn from_prost(file: prost_types::FileDescriptorProto) -> FileDescriptorProto {
181        FileDescriptorProto {
182            name: file.name,
183            package: file.package,
184            dependency: file.dependency,
185            public_dependency: file.public_dependency,
186            weak_dependency: file.weak_dependency,
187            message_type: file
188                .message_type
189                .into_iter()
190                .map(DescriptorProto::from_prost)
191                .collect(),
192            enum_type: file
193                .enum_type
194                .into_iter()
195                .map(EnumDescriptorProto::from_prost)
196                .collect(),
197            service: file
198                .service
199                .into_iter()
200                .map(ServiceDescriptorProto::from_prost)
201                .collect(),
202            extension: file
203                .extension
204                .into_iter()
205                .map(FieldDescriptorProto::from_prost)
206                .collect(),
207            options: file.options.map(Options::from_prost),
208            source_code_info: file.source_code_info,
209            syntax: file.syntax,
210        }
211    }
212
213    pub(crate) fn to_prost(&self) -> prost_types::FileDescriptorProto {
214        prost_types::FileDescriptorProto {
215            name: self.name.clone(),
216            package: self.package.clone(),
217            dependency: self.dependency.clone(),
218            public_dependency: self.public_dependency.clone(),
219            weak_dependency: self.weak_dependency.clone(),
220            message_type: self
221                .message_type
222                .iter()
223                .map(DescriptorProto::to_prost)
224                .collect(),
225            enum_type: self
226                .enum_type
227                .iter()
228                .map(EnumDescriptorProto::to_prost)
229                .collect(),
230            service: self
231                .service
232                .iter()
233                .map(ServiceDescriptorProto::to_prost)
234                .collect(),
235            extension: self
236                .extension
237                .iter()
238                .map(FieldDescriptorProto::to_prost)
239                .collect(),
240            options: self.options.as_ref().map(Options::to_prost),
241            source_code_info: self.source_code_info.clone(),
242            syntax: self.syntax.clone(),
243        }
244    }
245}
246
247impl DescriptorProto {
248    pub(crate) fn from_prost(file: prost_types::DescriptorProto) -> DescriptorProto {
249        DescriptorProto {
250            name: file.name,
251            field: file
252                .field
253                .into_iter()
254                .map(FieldDescriptorProto::from_prost)
255                .collect(),
256            extension: file
257                .extension
258                .into_iter()
259                .map(FieldDescriptorProto::from_prost)
260                .collect(),
261            nested_type: file
262                .nested_type
263                .into_iter()
264                .map(DescriptorProto::from_prost)
265                .collect(),
266            enum_type: file
267                .enum_type
268                .into_iter()
269                .map(EnumDescriptorProto::from_prost)
270                .collect(),
271            extension_range: file
272                .extension_range
273                .into_iter()
274                .map(descriptor_proto::ExtensionRange::from_prost)
275                .collect(),
276            oneof_decl: file
277                .oneof_decl
278                .into_iter()
279                .map(OneofDescriptorProto::from_prost)
280                .collect(),
281            options: file.options.map(Options::from_prost),
282            reserved_range: file.reserved_range,
283            reserved_name: file.reserved_name,
284        }
285    }
286
287    pub(crate) fn to_prost(&self) -> prost_types::DescriptorProto {
288        prost_types::DescriptorProto {
289            name: self.name.clone(),
290            field: self
291                .field
292                .iter()
293                .map(FieldDescriptorProto::to_prost)
294                .collect(),
295            extension: self
296                .extension
297                .iter()
298                .map(FieldDescriptorProto::to_prost)
299                .collect(),
300            nested_type: self
301                .nested_type
302                .iter()
303                .map(DescriptorProto::to_prost)
304                .collect(),
305            enum_type: self
306                .enum_type
307                .iter()
308                .map(EnumDescriptorProto::to_prost)
309                .collect(),
310            extension_range: self
311                .extension_range
312                .iter()
313                .map(descriptor_proto::ExtensionRange::to_prost)
314                .collect(),
315            oneof_decl: self
316                .oneof_decl
317                .iter()
318                .map(OneofDescriptorProto::to_prost)
319                .collect(),
320            options: self.options.as_ref().map(Options::to_prost),
321            reserved_range: self.reserved_range.clone(),
322            reserved_name: self.reserved_name.clone(),
323        }
324    }
325}
326
327impl FieldDescriptorProto {
328    pub(crate) fn from_prost(file: prost_types::FieldDescriptorProto) -> FieldDescriptorProto {
329        FieldDescriptorProto {
330            name: file.name,
331            number: file.number,
332            label: file.label,
333            r#type: file.r#type,
334            type_name: file.type_name,
335            extendee: file.extendee,
336            default_value: file.default_value,
337            oneof_index: file.oneof_index,
338            json_name: file.json_name,
339            options: file.options.map(Options::from_prost),
340            proto3_optional: file.proto3_optional,
341        }
342    }
343
344    pub(crate) fn to_prost(&self) -> prost_types::FieldDescriptorProto {
345        prost_types::FieldDescriptorProto {
346            name: self.name.clone(),
347            number: self.number,
348            label: self.label,
349            r#type: self.r#type,
350            type_name: self.type_name.clone(),
351            extendee: self.extendee.clone(),
352            default_value: self.default_value.clone(),
353            oneof_index: self.oneof_index,
354            json_name: self.json_name.clone(),
355            options: self.options.as_ref().map(Options::to_prost),
356            proto3_optional: self.proto3_optional,
357        }
358    }
359}
360
361impl OneofDescriptorProto {
362    pub(crate) fn from_prost(file: prost_types::OneofDescriptorProto) -> OneofDescriptorProto {
363        OneofDescriptorProto {
364            name: file.name,
365            options: file.options.map(Options::from_prost),
366        }
367    }
368
369    pub(crate) fn to_prost(&self) -> prost_types::OneofDescriptorProto {
370        prost_types::OneofDescriptorProto {
371            name: self.name.clone(),
372            options: self.options.as_ref().map(Options::to_prost),
373        }
374    }
375}
376
377impl descriptor_proto::ExtensionRange {
378    pub(crate) fn from_prost(
379        file: prost_types::descriptor_proto::ExtensionRange,
380    ) -> descriptor_proto::ExtensionRange {
381        descriptor_proto::ExtensionRange {
382            start: file.start,
383            end: file.end,
384            options: file.options.map(Options::from_prost),
385        }
386    }
387
388    pub(crate) fn to_prost(&self) -> prost_types::descriptor_proto::ExtensionRange {
389        prost_types::descriptor_proto::ExtensionRange {
390            start: self.start,
391            end: self.end,
392            options: self.options.as_ref().map(Options::to_prost),
393        }
394    }
395}
396
397impl EnumDescriptorProto {
398    pub(crate) fn from_prost(file: prost_types::EnumDescriptorProto) -> EnumDescriptorProto {
399        EnumDescriptorProto {
400            name: file.name,
401            value: file
402                .value
403                .into_iter()
404                .map(EnumValueDescriptorProto::from_prost)
405                .collect(),
406            options: file.options.map(Options::from_prost),
407            reserved_range: file.reserved_range,
408            reserved_name: file.reserved_name,
409        }
410    }
411
412    pub(crate) fn to_prost(&self) -> prost_types::EnumDescriptorProto {
413        prost_types::EnumDescriptorProto {
414            name: self.name.clone(),
415            value: self
416                .value
417                .iter()
418                .map(EnumValueDescriptorProto::to_prost)
419                .collect(),
420            options: self.options.as_ref().map(Options::to_prost),
421            reserved_range: self.reserved_range.clone(),
422            reserved_name: self.reserved_name.clone(),
423        }
424    }
425}
426
427impl EnumValueDescriptorProto {
428    pub(crate) fn from_prost(
429        file: prost_types::EnumValueDescriptorProto,
430    ) -> EnumValueDescriptorProto {
431        EnumValueDescriptorProto {
432            name: file.name,
433            number: file.number,
434            options: file.options.map(Options::from_prost),
435        }
436    }
437
438    pub(crate) fn to_prost(&self) -> prost_types::EnumValueDescriptorProto {
439        prost_types::EnumValueDescriptorProto {
440            name: self.name.clone(),
441            number: self.number,
442            options: self.options.as_ref().map(Options::to_prost),
443        }
444    }
445}
446
447impl ServiceDescriptorProto {
448    pub(crate) fn from_prost(file: prost_types::ServiceDescriptorProto) -> ServiceDescriptorProto {
449        ServiceDescriptorProto {
450            name: file.name,
451            method: file
452                .method
453                .into_iter()
454                .map(MethodDescriptorProto::from_prost)
455                .collect(),
456            options: file.options.map(Options::from_prost),
457        }
458    }
459
460    pub(crate) fn to_prost(&self) -> prost_types::ServiceDescriptorProto {
461        prost_types::ServiceDescriptorProto {
462            name: self.name.clone(),
463            method: self
464                .method
465                .iter()
466                .map(MethodDescriptorProto::to_prost)
467                .collect(),
468            options: self.options.as_ref().map(Options::to_prost),
469        }
470    }
471}
472
473impl MethodDescriptorProto {
474    pub(crate) fn from_prost(file: prost_types::MethodDescriptorProto) -> MethodDescriptorProto {
475        MethodDescriptorProto {
476            name: file.name,
477            input_type: file.input_type,
478            output_type: file.output_type,
479            options: file.options.map(Options::from_prost),
480            client_streaming: file.client_streaming,
481            server_streaming: file.server_streaming,
482        }
483    }
484
485    pub(crate) fn to_prost(&self) -> prost_types::MethodDescriptorProto {
486        prost_types::MethodDescriptorProto {
487            name: self.name.clone(),
488            input_type: self.input_type.clone(),
489            output_type: self.output_type.clone(),
490            options: self.options.as_ref().map(Options::to_prost),
491            client_streaming: self.client_streaming,
492            server_streaming: self.server_streaming,
493        }
494    }
495}
496
497impl<T> Options<T>
498where
499    T: Message + Clone,
500{
501    pub(crate) fn from_prost(options: T) -> Self {
502        Options {
503            encoded: options.encode_to_vec(),
504            value: options,
505        }
506    }
507
508    fn to_prost(&self) -> T {
509        self.value.clone()
510    }
511}
512
513impl<T> fmt::Debug for Options<T>
514where
515    T: fmt::Debug,
516{
517    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
518        self.value.fmt(f)
519    }
520}
521
522impl<T> Message for Options<T>
523where
524    T: Message + Default,
525{
526    fn encode_raw(&self, buf: &mut impl BufMut)
527    where
528        Self: Sized,
529    {
530        buf.put(self.encoded.as_slice());
531    }
532
533    fn merge_field(
534        &mut self,
535        tag: u32,
536        wire_type: WireType,
537        buf: &mut impl Buf,
538        ctx: DecodeContext,
539    ) -> Result<(), DecodeError>
540    where
541        Self: Sized,
542    {
543        struct CopyBufAdapter<'a, B> {
544            dest: &'a mut Vec<u8>,
545            src: &'a mut B,
546        }
547
548        impl<B> Buf for CopyBufAdapter<'_, B>
549        where
550            B: Buf,
551        {
552            fn advance(&mut self, cnt: usize) {
553                self.dest.put((&mut self.src).take(cnt));
554            }
555
556            fn chunk(&self) -> &[u8] {
557                self.src.chunk()
558            }
559
560            fn remaining(&self) -> usize {
561                self.src.remaining()
562            }
563        }
564
565        encode_key(tag, wire_type, &mut self.encoded);
566        let start = self.encoded.len();
567        skip_field(
568            wire_type,
569            tag,
570            &mut CopyBufAdapter {
571                dest: &mut self.encoded,
572                src: buf,
573            },
574            ctx.clone(),
575        )?;
576        self.value
577            .merge_field(tag, wire_type, &mut &self.encoded[start..], ctx)?;
578
579        Ok(())
580    }
581
582    fn encoded_len(&self) -> usize {
583        self.encoded.len()
584    }
585
586    fn clear(&mut self) {
587        self.encoded.clear();
588        self.value.clear();
589    }
590}