-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: `Name` trait + `Any` encoding support As discussed in #299 and #858, adds a `Name` trait which associates a type name and package constants with a `Message` type. It also provides `full_name` and `type_url` methods. The `type_url` method is used by newly added methods on the `Any` type which can be used for decoding/encoding messages: - `Any::from_msg`: encodes a given `Message`, returning `Any`. - `Any::to_msg`: decodes `Any::value` as the given `Message`, first validating the message type has the expected type URL. * Add private `TypeUrl` type Implements the basic rules for parsing type URLs as documented in: https://github.com/protocolbuffers/protobuf/blob/a281c13/src/google/protobuf/any.proto#L129C2-L156C50 Notably this extracts the final path segment of the URL which contains the full name of the type, and uses that for type comparisons. * CI: bump test toolchain to 1.64 This is the MSRV of `petgraph` now: error: package `petgraph v0.6.4` cannot be built because it requires rustc 1.64 or newer, while the currently active rustc version is 1.63.0 * Add `Name` impls for well-known protobuf types Also adds tests for `Any::{from_msg, to_msg}`. * Fix no_std --------- Co-authored-by: Lucio Franco <luciofranco14@gmail.com>
- Loading branch information
1 parent
f9a3cff
commit 7ce9b97
Showing
3 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//! Support for associating type name information with a [`Message`]. | ||
|
||
use crate::Message; | ||
use alloc::{format, string::String}; | ||
|
||
/// Associate a type name with a [`Message`] type. | ||
pub trait Name: Message { | ||
/// Type name for this [`Message`]. This is the camel case name, | ||
/// e.g. `TypeName`. | ||
const NAME: &'static str; | ||
|
||
/// Package name this message type is contained in. They are domain-like | ||
/// and delimited by `.`, e.g. `google.protobuf`. | ||
const PACKAGE: &'static str; | ||
|
||
/// Full name of this message type containing both the package name and | ||
/// type name, e.g. `google.protobuf.TypeName`. | ||
fn full_name() -> String { | ||
format!("{}.{}", Self::NAME, Self::PACKAGE) | ||
} | ||
|
||
/// Type URL for this message, which by default is the full name with a | ||
/// leading slash, but may also include a leading domain name, e.g. | ||
/// `type.googleapis.com/google.profile.Person`. | ||
fn type_url() -> String { | ||
format!("/{}", Self::full_name()) | ||
} | ||
} |