prost/
types.rs

1//! Protocol Buffers well-known wrapper types.
2//!
3//! This module provides implementations of `Message` for Rust standard library types which
4//! correspond to a Protobuf well-known wrapper type. The remaining well-known types are defined in
5//! the `prost-types` crate in order to avoid a cyclic dependency between `prost` and
6//! `prost-build`.
7
8use alloc::format;
9use alloc::string::String;
10use alloc::vec::Vec;
11
12use ::bytes::{Buf, BufMut, Bytes};
13
14use crate::encoding::wire_type::WireType;
15use crate::{
16    encoding::{
17        bool, bytes, double, float, int32, int64, skip_field, string, uint32, uint64, DecodeContext,
18    },
19    DecodeError, Message, Name,
20};
21
22/// `google.protobuf.BoolValue`
23impl Message for bool {
24    fn encode_raw(&self, buf: &mut impl BufMut) {
25        if *self {
26            bool::encode(1, self, buf)
27        }
28    }
29    fn merge_field(
30        &mut self,
31        tag: u32,
32        wire_type: WireType,
33        buf: &mut impl Buf,
34        ctx: DecodeContext,
35    ) -> Result<(), DecodeError> {
36        if tag == 1 {
37            bool::merge(wire_type, self, buf, ctx)
38        } else {
39            skip_field(wire_type, tag, buf, ctx)
40        }
41    }
42    fn encoded_len(&self) -> usize {
43        if *self {
44            2
45        } else {
46            0
47        }
48    }
49    fn clear(&mut self) {
50        *self = false;
51    }
52}
53
54/// `google.protobuf.BoolValue`
55impl Name for bool {
56    const NAME: &'static str = "BoolValue";
57    const PACKAGE: &'static str = "google.protobuf";
58
59    fn type_url() -> String {
60        googleapis_type_url_for::<Self>()
61    }
62}
63
64/// `google.protobuf.UInt32Value`
65impl Message for u32 {
66    fn encode_raw(&self, buf: &mut impl BufMut) {
67        if *self != 0 {
68            uint32::encode(1, self, buf)
69        }
70    }
71    fn merge_field(
72        &mut self,
73        tag: u32,
74        wire_type: WireType,
75        buf: &mut impl Buf,
76        ctx: DecodeContext,
77    ) -> Result<(), DecodeError> {
78        if tag == 1 {
79            uint32::merge(wire_type, self, buf, ctx)
80        } else {
81            skip_field(wire_type, tag, buf, ctx)
82        }
83    }
84    fn encoded_len(&self) -> usize {
85        if *self != 0 {
86            uint32::encoded_len(1, self)
87        } else {
88            0
89        }
90    }
91    fn clear(&mut self) {
92        *self = 0;
93    }
94}
95
96/// `google.protobuf.UInt32Value`
97impl Name for u32 {
98    const NAME: &'static str = "UInt32Value";
99    const PACKAGE: &'static str = "google.protobuf";
100
101    fn type_url() -> String {
102        googleapis_type_url_for::<Self>()
103    }
104}
105
106/// `google.protobuf.UInt64Value`
107impl Message for u64 {
108    fn encode_raw(&self, buf: &mut impl BufMut) {
109        if *self != 0 {
110            uint64::encode(1, self, buf)
111        }
112    }
113    fn merge_field(
114        &mut self,
115        tag: u32,
116        wire_type: WireType,
117        buf: &mut impl Buf,
118        ctx: DecodeContext,
119    ) -> Result<(), DecodeError> {
120        if tag == 1 {
121            uint64::merge(wire_type, self, buf, ctx)
122        } else {
123            skip_field(wire_type, tag, buf, ctx)
124        }
125    }
126    fn encoded_len(&self) -> usize {
127        if *self != 0 {
128            uint64::encoded_len(1, self)
129        } else {
130            0
131        }
132    }
133    fn clear(&mut self) {
134        *self = 0;
135    }
136}
137
138/// `google.protobuf.UInt64Value`
139impl Name for u64 {
140    const NAME: &'static str = "UInt64Value";
141    const PACKAGE: &'static str = "google.protobuf";
142
143    fn type_url() -> String {
144        googleapis_type_url_for::<Self>()
145    }
146}
147
148/// `google.protobuf.Int32Value`
149impl Message for i32 {
150    fn encode_raw(&self, buf: &mut impl BufMut) {
151        if *self != 0 {
152            int32::encode(1, self, buf)
153        }
154    }
155    fn merge_field(
156        &mut self,
157        tag: u32,
158        wire_type: WireType,
159        buf: &mut impl Buf,
160        ctx: DecodeContext,
161    ) -> Result<(), DecodeError> {
162        if tag == 1 {
163            int32::merge(wire_type, self, buf, ctx)
164        } else {
165            skip_field(wire_type, tag, buf, ctx)
166        }
167    }
168    fn encoded_len(&self) -> usize {
169        if *self != 0 {
170            int32::encoded_len(1, self)
171        } else {
172            0
173        }
174    }
175    fn clear(&mut self) {
176        *self = 0;
177    }
178}
179
180/// `google.protobuf.Int32Value`
181impl Name for i32 {
182    const NAME: &'static str = "Int32Value";
183    const PACKAGE: &'static str = "google.protobuf";
184
185    fn type_url() -> String {
186        googleapis_type_url_for::<Self>()
187    }
188}
189
190/// `google.protobuf.Int64Value`
191impl Message for i64 {
192    fn encode_raw(&self, buf: &mut impl BufMut) {
193        if *self != 0 {
194            int64::encode(1, self, buf)
195        }
196    }
197    fn merge_field(
198        &mut self,
199        tag: u32,
200        wire_type: WireType,
201        buf: &mut impl Buf,
202        ctx: DecodeContext,
203    ) -> Result<(), DecodeError> {
204        if tag == 1 {
205            int64::merge(wire_type, self, buf, ctx)
206        } else {
207            skip_field(wire_type, tag, buf, ctx)
208        }
209    }
210    fn encoded_len(&self) -> usize {
211        if *self != 0 {
212            int64::encoded_len(1, self)
213        } else {
214            0
215        }
216    }
217    fn clear(&mut self) {
218        *self = 0;
219    }
220}
221
222/// `google.protobuf.Int64Value`
223impl Name for i64 {
224    const NAME: &'static str = "Int64Value";
225    const PACKAGE: &'static str = "google.protobuf";
226
227    fn type_url() -> String {
228        googleapis_type_url_for::<Self>()
229    }
230}
231
232/// `google.protobuf.FloatValue`
233impl Message for f32 {
234    fn encode_raw(&self, buf: &mut impl BufMut) {
235        if *self != 0.0 {
236            float::encode(1, self, buf)
237        }
238    }
239    fn merge_field(
240        &mut self,
241        tag: u32,
242        wire_type: WireType,
243        buf: &mut impl Buf,
244        ctx: DecodeContext,
245    ) -> Result<(), DecodeError> {
246        if tag == 1 {
247            float::merge(wire_type, self, buf, ctx)
248        } else {
249            skip_field(wire_type, tag, buf, ctx)
250        }
251    }
252    fn encoded_len(&self) -> usize {
253        if *self != 0.0 {
254            float::encoded_len(1, self)
255        } else {
256            0
257        }
258    }
259    fn clear(&mut self) {
260        *self = 0.0;
261    }
262}
263
264/// `google.protobuf.FloatValue`
265impl Name for f32 {
266    const NAME: &'static str = "FloatValue";
267    const PACKAGE: &'static str = "google.protobuf";
268
269    fn type_url() -> String {
270        googleapis_type_url_for::<Self>()
271    }
272}
273
274/// `google.protobuf.DoubleValue`
275impl Message for f64 {
276    fn encode_raw(&self, buf: &mut impl BufMut) {
277        if *self != 0.0 {
278            double::encode(1, self, buf)
279        }
280    }
281    fn merge_field(
282        &mut self,
283        tag: u32,
284        wire_type: WireType,
285        buf: &mut impl Buf,
286        ctx: DecodeContext,
287    ) -> Result<(), DecodeError> {
288        if tag == 1 {
289            double::merge(wire_type, self, buf, ctx)
290        } else {
291            skip_field(wire_type, tag, buf, ctx)
292        }
293    }
294    fn encoded_len(&self) -> usize {
295        if *self != 0.0 {
296            double::encoded_len(1, self)
297        } else {
298            0
299        }
300    }
301    fn clear(&mut self) {
302        *self = 0.0;
303    }
304}
305
306/// `google.protobuf.DoubleValue`
307impl Name for f64 {
308    const NAME: &'static str = "DoubleValue";
309    const PACKAGE: &'static str = "google.protobuf";
310
311    fn type_url() -> String {
312        googleapis_type_url_for::<Self>()
313    }
314}
315
316/// `google.protobuf.StringValue`
317impl Message for String {
318    fn encode_raw(&self, buf: &mut impl BufMut) {
319        if !self.is_empty() {
320            string::encode(1, self, buf)
321        }
322    }
323    fn merge_field(
324        &mut self,
325        tag: u32,
326        wire_type: WireType,
327        buf: &mut impl Buf,
328        ctx: DecodeContext,
329    ) -> Result<(), DecodeError> {
330        if tag == 1 {
331            string::merge(wire_type, self, buf, ctx)
332        } else {
333            skip_field(wire_type, tag, buf, ctx)
334        }
335    }
336    fn encoded_len(&self) -> usize {
337        if !self.is_empty() {
338            string::encoded_len(1, self)
339        } else {
340            0
341        }
342    }
343    fn clear(&mut self) {
344        self.clear();
345    }
346}
347
348/// `google.protobuf.StringValue`
349impl Name for String {
350    const NAME: &'static str = "StringValue";
351    const PACKAGE: &'static str = "google.protobuf";
352
353    fn type_url() -> String {
354        googleapis_type_url_for::<Self>()
355    }
356}
357
358/// `google.protobuf.BytesValue`
359impl Message for Vec<u8> {
360    fn encode_raw(&self, buf: &mut impl BufMut) {
361        if !self.is_empty() {
362            bytes::encode(1, self, buf)
363        }
364    }
365    fn merge_field(
366        &mut self,
367        tag: u32,
368        wire_type: WireType,
369        buf: &mut impl Buf,
370        ctx: DecodeContext,
371    ) -> Result<(), DecodeError> {
372        if tag == 1 {
373            bytes::merge(wire_type, self, buf, ctx)
374        } else {
375            skip_field(wire_type, tag, buf, ctx)
376        }
377    }
378    fn encoded_len(&self) -> usize {
379        if !self.is_empty() {
380            bytes::encoded_len(1, self)
381        } else {
382            0
383        }
384    }
385    fn clear(&mut self) {
386        self.clear();
387    }
388}
389
390/// `google.protobuf.BytesValue`
391impl Name for Vec<u8> {
392    const NAME: &'static str = "BytesValue";
393    const PACKAGE: &'static str = "google.protobuf";
394
395    fn type_url() -> String {
396        googleapis_type_url_for::<Self>()
397    }
398}
399
400/// `google.protobuf.BytesValue`
401impl Message for Bytes {
402    fn encode_raw(&self, buf: &mut impl BufMut) {
403        if !self.is_empty() {
404            bytes::encode(1, self, buf)
405        }
406    }
407    fn merge_field(
408        &mut self,
409        tag: u32,
410        wire_type: WireType,
411        buf: &mut impl Buf,
412        ctx: DecodeContext,
413    ) -> Result<(), DecodeError> {
414        if tag == 1 {
415            bytes::merge(wire_type, self, buf, ctx)
416        } else {
417            skip_field(wire_type, tag, buf, ctx)
418        }
419    }
420    fn encoded_len(&self) -> usize {
421        if !self.is_empty() {
422            bytes::encoded_len(1, self)
423        } else {
424            0
425        }
426    }
427    fn clear(&mut self) {
428        self.clear();
429    }
430}
431
432/// `google.protobuf.BytesValue`
433impl Name for Bytes {
434    const NAME: &'static str = "BytesValue";
435    const PACKAGE: &'static str = "google.protobuf";
436
437    fn type_url() -> String {
438        googleapis_type_url_for::<Self>()
439    }
440}
441
442/// `google.protobuf.Empty`
443impl Message for () {
444    fn encode_raw(&self, _buf: &mut impl BufMut) {}
445    fn merge_field(
446        &mut self,
447        tag: u32,
448        wire_type: WireType,
449        buf: &mut impl Buf,
450        ctx: DecodeContext,
451    ) -> Result<(), DecodeError> {
452        skip_field(wire_type, tag, buf, ctx)
453    }
454    fn encoded_len(&self) -> usize {
455        0
456    }
457    fn clear(&mut self) {}
458}
459
460/// `google.protobuf.Empty`
461impl Name for () {
462    const NAME: &'static str = "Empty";
463    const PACKAGE: &'static str = "google.protobuf";
464
465    fn type_url() -> String {
466        googleapis_type_url_for::<Self>()
467    }
468}
469
470/// Compute the type URL for the given `google.protobuf` type, using `type.googleapis.com` as the
471/// authority for the URL.
472fn googleapis_type_url_for<T: Name>() -> String {
473    format!("type.googleapis.com/{}.{}", T::PACKAGE, T::NAME)
474}
475
476#[cfg(test)]
477mod tests {
478    use super::*;
479
480    #[test]
481    fn test_impl_name() {
482        assert_eq!("BoolValue", bool::NAME);
483        assert_eq!("google.protobuf", bool::PACKAGE);
484        assert_eq!("google.protobuf.BoolValue", bool::full_name());
485        assert_eq!(
486            "type.googleapis.com/google.protobuf.BoolValue",
487            bool::type_url()
488        );
489
490        assert_eq!("UInt32Value", u32::NAME);
491        assert_eq!("google.protobuf", u32::PACKAGE);
492        assert_eq!("google.protobuf.UInt32Value", u32::full_name());
493        assert_eq!(
494            "type.googleapis.com/google.protobuf.UInt32Value",
495            u32::type_url()
496        );
497
498        assert_eq!("UInt64Value", u64::NAME);
499        assert_eq!("google.protobuf", u64::PACKAGE);
500        assert_eq!("google.protobuf.UInt64Value", u64::full_name());
501        assert_eq!(
502            "type.googleapis.com/google.protobuf.UInt64Value",
503            u64::type_url()
504        );
505
506        assert_eq!("Int32Value", i32::NAME);
507        assert_eq!("google.protobuf", i32::PACKAGE);
508        assert_eq!("google.protobuf.Int32Value", i32::full_name());
509        assert_eq!(
510            "type.googleapis.com/google.protobuf.Int32Value",
511            i32::type_url()
512        );
513
514        assert_eq!("Int64Value", i64::NAME);
515        assert_eq!("google.protobuf", i64::PACKAGE);
516        assert_eq!("google.protobuf.Int64Value", i64::full_name());
517        assert_eq!(
518            "type.googleapis.com/google.protobuf.Int64Value",
519            i64::type_url()
520        );
521
522        assert_eq!("FloatValue", f32::NAME);
523        assert_eq!("google.protobuf", f32::PACKAGE);
524        assert_eq!("google.protobuf.FloatValue", f32::full_name());
525        assert_eq!(
526            "type.googleapis.com/google.protobuf.FloatValue",
527            f32::type_url()
528        );
529
530        assert_eq!("DoubleValue", f64::NAME);
531        assert_eq!("google.protobuf", f64::PACKAGE);
532        assert_eq!("google.protobuf.DoubleValue", f64::full_name());
533        assert_eq!(
534            "type.googleapis.com/google.protobuf.DoubleValue",
535            f64::type_url()
536        );
537
538        assert_eq!("StringValue", String::NAME);
539        assert_eq!("google.protobuf", String::PACKAGE);
540        assert_eq!("google.protobuf.StringValue", String::full_name());
541        assert_eq!(
542            "type.googleapis.com/google.protobuf.StringValue",
543            String::type_url()
544        );
545
546        assert_eq!("BytesValue", Vec::<u8>::NAME);
547        assert_eq!("google.protobuf", Vec::<u8>::PACKAGE);
548        assert_eq!("google.protobuf.BytesValue", Vec::<u8>::full_name());
549        assert_eq!(
550            "type.googleapis.com/google.protobuf.BytesValue",
551            Vec::<u8>::type_url()
552        );
553
554        assert_eq!("BytesValue", Bytes::NAME);
555        assert_eq!("google.protobuf", Bytes::PACKAGE);
556        assert_eq!("google.protobuf.BytesValue", Bytes::full_name());
557        assert_eq!(
558            "type.googleapis.com/google.protobuf.BytesValue",
559            Bytes::type_url()
560        );
561
562        assert_eq!("Empty", <()>::NAME);
563        assert_eq!("google.protobuf", <()>::PACKAGE);
564        assert_eq!("google.protobuf.Empty", <()>::full_name());
565        assert_eq!(
566            "type.googleapis.com/google.protobuf.Empty",
567            <()>::type_url()
568        );
569    }
570}