From e7deb3c5ba37b2e501f00856f9f1da05a097b1a9 Mon Sep 17 00:00:00 2001 From: Casper Meijn Date: Fri, 1 Nov 2024 14:39:02 +0100 Subject: [PATCH] feat: impl Name for Protobuf well-known wrapper types (#1174) Co-authored-by: Kelvin Hu --- prost/src/types.rs | 217 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 214 insertions(+), 3 deletions(-) diff --git a/prost/src/types.rs b/prost/src/types.rs index 5abb513bc..8300a7104 100644 --- a/prost/src/types.rs +++ b/prost/src/types.rs @@ -5,9 +5,8 @@ //! the `prost-types` crate in order to avoid a cyclic dependency between `prost` and //! `prost-build`. -#[cfg(not(feature = "std"))] +use alloc::format; use alloc::string::String; -#[cfg(not(feature = "std"))] use alloc::vec::Vec; use ::bytes::{Buf, BufMut, Bytes}; @@ -17,7 +16,7 @@ use crate::{ encoding::{ bool, bytes, double, float, int32, int64, skip_field, string, uint32, uint64, DecodeContext, }, - DecodeError, Message, + DecodeError, Message, Name, }; /// `google.protobuf.BoolValue` @@ -52,6 +51,16 @@ impl Message for bool { } } +/// `google.protobuf.BoolValue` +impl Name for bool { + const NAME: &'static str = "BoolValue"; + const PACKAGE: &'static str = "google.protobuf"; + + fn type_url() -> String { + googleapis_type_url_for::() + } +} + /// `google.protobuf.UInt32Value` impl Message for u32 { fn encode_raw(&self, buf: &mut impl BufMut) { @@ -84,6 +93,16 @@ impl Message for u32 { } } +/// `google.protobuf.UInt32Value` +impl Name for u32 { + const NAME: &'static str = "UInt32Value"; + const PACKAGE: &'static str = "google.protobuf"; + + fn type_url() -> String { + googleapis_type_url_for::() + } +} + /// `google.protobuf.UInt64Value` impl Message for u64 { fn encode_raw(&self, buf: &mut impl BufMut) { @@ -116,6 +135,16 @@ impl Message for u64 { } } +/// `google.protobuf.UInt64Value` +impl Name for u64 { + const NAME: &'static str = "UInt64Value"; + const PACKAGE: &'static str = "google.protobuf"; + + fn type_url() -> String { + googleapis_type_url_for::() + } +} + /// `google.protobuf.Int32Value` impl Message for i32 { fn encode_raw(&self, buf: &mut impl BufMut) { @@ -148,6 +177,16 @@ impl Message for i32 { } } +/// `google.protobuf.Int32Value` +impl Name for i32 { + const NAME: &'static str = "Int32Value"; + const PACKAGE: &'static str = "google.protobuf"; + + fn type_url() -> String { + googleapis_type_url_for::() + } +} + /// `google.protobuf.Int64Value` impl Message for i64 { fn encode_raw(&self, buf: &mut impl BufMut) { @@ -180,6 +219,16 @@ impl Message for i64 { } } +/// `google.protobuf.Int64Value` +impl Name for i64 { + const NAME: &'static str = "Int64Value"; + const PACKAGE: &'static str = "google.protobuf"; + + fn type_url() -> String { + googleapis_type_url_for::() + } +} + /// `google.protobuf.FloatValue` impl Message for f32 { fn encode_raw(&self, buf: &mut impl BufMut) { @@ -212,6 +261,16 @@ impl Message for f32 { } } +/// `google.protobuf.FloatValue` +impl Name for f32 { + const NAME: &'static str = "FloatValue"; + const PACKAGE: &'static str = "google.protobuf"; + + fn type_url() -> String { + googleapis_type_url_for::() + } +} + /// `google.protobuf.DoubleValue` impl Message for f64 { fn encode_raw(&self, buf: &mut impl BufMut) { @@ -244,6 +303,16 @@ impl Message for f64 { } } +/// `google.protobuf.DoubleValue` +impl Name for f64 { + const NAME: &'static str = "DoubleValue"; + const PACKAGE: &'static str = "google.protobuf"; + + fn type_url() -> String { + googleapis_type_url_for::() + } +} + /// `google.protobuf.StringValue` impl Message for String { fn encode_raw(&self, buf: &mut impl BufMut) { @@ -276,6 +345,16 @@ impl Message for String { } } +/// `google.protobuf.StringValue` +impl Name for String { + const NAME: &'static str = "StringValue"; + const PACKAGE: &'static str = "google.protobuf"; + + fn type_url() -> String { + googleapis_type_url_for::() + } +} + /// `google.protobuf.BytesValue` impl Message for Vec { fn encode_raw(&self, buf: &mut impl BufMut) { @@ -308,6 +387,16 @@ impl Message for Vec { } } +/// `google.protobuf.BytesValue` +impl Name for Vec { + const NAME: &'static str = "BytesValue"; + const PACKAGE: &'static str = "google.protobuf"; + + fn type_url() -> String { + googleapis_type_url_for::() + } +} + /// `google.protobuf.BytesValue` impl Message for Bytes { fn encode_raw(&self, buf: &mut impl BufMut) { @@ -340,6 +429,16 @@ impl Message for Bytes { } } +/// `google.protobuf.BytesValue` +impl Name for Bytes { + const NAME: &'static str = "BytesValue"; + const PACKAGE: &'static str = "google.protobuf"; + + fn type_url() -> String { + googleapis_type_url_for::() + } +} + /// `google.protobuf.Empty` impl Message for () { fn encode_raw(&self, _buf: &mut impl BufMut) {} @@ -357,3 +456,115 @@ impl Message for () { } fn clear(&mut self) {} } + +/// `google.protobuf.Empty` +impl Name for () { + const NAME: &'static str = "Empty"; + const PACKAGE: &'static str = "google.protobuf"; + + fn type_url() -> String { + googleapis_type_url_for::() + } +} + +/// Compute the type URL for the given `google.protobuf` type, using `type.googleapis.com` as the +/// authority for the URL. +fn googleapis_type_url_for() -> String { + format!("type.googleapis.com/{}.{}", T::PACKAGE, T::NAME) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_impl_name() { + assert_eq!("BoolValue", bool::NAME); + assert_eq!("google.protobuf", bool::PACKAGE); + assert_eq!("google.protobuf.BoolValue", bool::full_name()); + assert_eq!( + "type.googleapis.com/google.protobuf.BoolValue", + bool::type_url() + ); + + assert_eq!("UInt32Value", u32::NAME); + assert_eq!("google.protobuf", u32::PACKAGE); + assert_eq!("google.protobuf.UInt32Value", u32::full_name()); + assert_eq!( + "type.googleapis.com/google.protobuf.UInt32Value", + u32::type_url() + ); + + assert_eq!("UInt64Value", u64::NAME); + assert_eq!("google.protobuf", u64::PACKAGE); + assert_eq!("google.protobuf.UInt64Value", u64::full_name()); + assert_eq!( + "type.googleapis.com/google.protobuf.UInt64Value", + u64::type_url() + ); + + assert_eq!("Int32Value", i32::NAME); + assert_eq!("google.protobuf", i32::PACKAGE); + assert_eq!("google.protobuf.Int32Value", i32::full_name()); + assert_eq!( + "type.googleapis.com/google.protobuf.Int32Value", + i32::type_url() + ); + + assert_eq!("Int64Value", i64::NAME); + assert_eq!("google.protobuf", i64::PACKAGE); + assert_eq!("google.protobuf.Int64Value", i64::full_name()); + assert_eq!( + "type.googleapis.com/google.protobuf.Int64Value", + i64::type_url() + ); + + assert_eq!("FloatValue", f32::NAME); + assert_eq!("google.protobuf", f32::PACKAGE); + assert_eq!("google.protobuf.FloatValue", f32::full_name()); + assert_eq!( + "type.googleapis.com/google.protobuf.FloatValue", + f32::type_url() + ); + + assert_eq!("DoubleValue", f64::NAME); + assert_eq!("google.protobuf", f64::PACKAGE); + assert_eq!("google.protobuf.DoubleValue", f64::full_name()); + assert_eq!( + "type.googleapis.com/google.protobuf.DoubleValue", + f64::type_url() + ); + + assert_eq!("StringValue", String::NAME); + assert_eq!("google.protobuf", String::PACKAGE); + assert_eq!("google.protobuf.StringValue", String::full_name()); + assert_eq!( + "type.googleapis.com/google.protobuf.StringValue", + String::type_url() + ); + + assert_eq!("BytesValue", Vec::::NAME); + assert_eq!("google.protobuf", Vec::::PACKAGE); + assert_eq!("google.protobuf.BytesValue", Vec::::full_name()); + assert_eq!( + "type.googleapis.com/google.protobuf.BytesValue", + Vec::::type_url() + ); + + assert_eq!("BytesValue", Bytes::NAME); + assert_eq!("google.protobuf", Bytes::PACKAGE); + assert_eq!("google.protobuf.BytesValue", Bytes::full_name()); + assert_eq!( + "type.googleapis.com/google.protobuf.BytesValue", + Bytes::type_url() + ); + + assert_eq!("Empty", <()>::NAME); + assert_eq!("google.protobuf", <()>::PACKAGE); + assert_eq!("google.protobuf.Empty", <()>::full_name()); + assert_eq!( + "type.googleapis.com/google.protobuf.Empty", + <()>::type_url() + ); + } +}