Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: impl Name for some well-known types #933

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
121 changes: 120 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! the `prost-types` crate in order to avoid a cyclic dependency between `prost` and
//! `prost-build`.

use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;

Expand All @@ -15,9 +16,11 @@ use crate::{
bool, bytes, double, float, int32, int64, skip_field, string, uint32, uint64,
DecodeContext, WireType,
},
DecodeError, Message,
DecodeError, Message, Name,
};

const PACKAGE: &str = "google.protobuf";

/// `google.protobuf.BoolValue`
impl Message for bool {
fn encode_raw<B>(&self, buf: &mut B)
Expand Down Expand Up @@ -56,6 +59,16 @@ impl Message for bool {
}
}

/// `google.protobuf.BoolValue`
impl Name for bool {
const NAME: &'static str = "BoolValue";
const PACKAGE: &'static str = PACKAGE;

fn type_url() -> String {
type_url_for::<Self>()
}
}

/// `google.protobuf.UInt32Value`
impl Message for u32 {
fn encode_raw<B>(&self, buf: &mut B)
Expand Down Expand Up @@ -94,6 +107,16 @@ impl Message for u32 {
}
}

/// `google.protobuf.UInt32Value`
impl Name for u32 {
const NAME: &'static str = "UInt32Value";
const PACKAGE: &'static str = PACKAGE;

fn type_url() -> String {
type_url_for::<Self>()
}
}

/// `google.protobuf.UInt64Value`
impl Message for u64 {
fn encode_raw<B>(&self, buf: &mut B)
Expand Down Expand Up @@ -132,6 +155,16 @@ impl Message for u64 {
}
}

/// `google.protobuf.UInt64Value`
impl Name for u64 {
const NAME: &'static str = "UInt64Value";
const PACKAGE: &'static str = PACKAGE;

fn type_url() -> String {
type_url_for::<Self>()
}
}

/// `google.protobuf.Int32Value`
impl Message for i32 {
fn encode_raw<B>(&self, buf: &mut B)
Expand Down Expand Up @@ -170,6 +203,16 @@ impl Message for i32 {
}
}

/// `google.protobuf.Int32Value`
impl Name for i32 {
const NAME: &'static str = "Int32Value";
const PACKAGE: &'static str = PACKAGE;

fn type_url() -> String {
type_url_for::<Self>()
}
}

/// `google.protobuf.Int64Value`
impl Message for i64 {
fn encode_raw<B>(&self, buf: &mut B)
Expand Down Expand Up @@ -208,6 +251,16 @@ impl Message for i64 {
}
}

/// `google.protobuf.Int64Value`
impl Name for i64 {
const NAME: &'static str = "Int64Value";
const PACKAGE: &'static str = PACKAGE;

fn type_url() -> String {
type_url_for::<Self>()
}
}

/// `google.protobuf.FloatValue`
impl Message for f32 {
fn encode_raw<B>(&self, buf: &mut B)
Expand Down Expand Up @@ -246,6 +299,16 @@ impl Message for f32 {
}
}

/// `google.protobuf.FloatValue`
impl Name for f32 {
const NAME: &'static str = "FloatValue";
const PACKAGE: &'static str = PACKAGE;

fn type_url() -> String {
type_url_for::<Self>()
}
}

/// `google.protobuf.DoubleValue`
impl Message for f64 {
fn encode_raw<B>(&self, buf: &mut B)
Expand Down Expand Up @@ -284,6 +347,16 @@ impl Message for f64 {
}
}

/// `google.protobuf.DoubleValue`
impl Name for f64 {
const NAME: &'static str = "DoubleValue";
const PACKAGE: &'static str = PACKAGE;

fn type_url() -> String {
type_url_for::<Self>()
}
}

/// `google.protobuf.StringValue`
impl Message for String {
fn encode_raw<B>(&self, buf: &mut B)
Expand Down Expand Up @@ -322,6 +395,16 @@ impl Message for String {
}
}

/// `google.protobuf.StringValue`
impl Name for String {
const NAME: &'static str = "StringValue";
const PACKAGE: &'static str = PACKAGE;

fn type_url() -> String {
type_url_for::<Self>()
}
}

/// `google.protobuf.BytesValue`
impl Message for Vec<u8> {
fn encode_raw<B>(&self, buf: &mut B)
Expand Down Expand Up @@ -360,6 +443,16 @@ impl Message for Vec<u8> {
}
}

/// `google.protobuf.BytesValue`
impl Name for Vec<u8> {
const NAME: &'static str = "BytesValue";
const PACKAGE: &'static str = PACKAGE;

fn type_url() -> String {
type_url_for::<Self>()
}
}

/// `google.protobuf.BytesValue`
impl Message for Bytes {
fn encode_raw<B>(&self, buf: &mut B)
Expand Down Expand Up @@ -398,6 +491,16 @@ impl Message for Bytes {
}
}

/// `google.protobuf.BytesValue`
impl Name for Bytes {
const NAME: &'static str = "BytesValue";
const PACKAGE: &'static str = PACKAGE;

fn type_url() -> String {
type_url_for::<Self>()
}
}

/// `google.protobuf.Empty`
impl Message for () {
fn encode_raw<B>(&self, _buf: &mut B)
Expand All @@ -422,3 +525,19 @@ impl Message for () {
}
fn clear(&mut self) {}
}

/// `google.protobuf.Empty`
impl Name for () {
const NAME: &'static str = "Empty";
const PACKAGE: &'static str = PACKAGE;

fn type_url() -> String {
type_url_for::<Self>()
}
}

/// Compute the type URL for the given `google.protobuf` type, using `type.googleapis.com` as the
/// authority for the URL.
fn type_url_for<T: Name>() -> String {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is specific to googleapis url. I would reflect that in the function name.

Suggested change
fn type_url_for<T: Name>() -> String {
fn googleapis_type_url_for<T: Name>() -> String {

format!("type.googleapis.com/{}.{}", T::PACKAGE, T::NAME)
}