prost/name.rs
1//! Support for associating type name information with a [`Message`].
2
3use crate::Message;
4
5#[cfg(not(feature = "std"))]
6use alloc::{format, string::String};
7
8/// Associate a type name with a [`Message`] type.
9pub trait Name: Message {
10 /// Simple name for this [`Message`].
11 /// This name is the same as it appears in the source .proto file, e.g. `FooBar`.
12 const NAME: &'static str;
13
14 /// Package name this message type is contained in. They are domain-like
15 /// and delimited by `.`, e.g. `google.protobuf`.
16 const PACKAGE: &'static str;
17
18 /// Fully-qualified unique name for this [`Message`].
19 /// It's prefixed with the package name and names of any parent messages,
20 /// e.g. `google.rpc.BadRequest.FieldViolation`.
21 /// By default, this is the package name followed by the message name.
22 /// Fully-qualified names must be unique within a domain of Type URLs.
23 fn full_name() -> String {
24 if Self::PACKAGE.is_empty() {
25 Self::NAME.into()
26 } else {
27 format!("{}.{}", Self::PACKAGE, Self::NAME)
28 }
29 }
30
31 /// Type URL for this [`Message`], which by default is the full name with a
32 /// leading slash, but may also include a leading domain name, e.g.
33 /// `type.googleapis.com/google.profile.Person`.
34 /// This can be used when serializing into the `google.protobuf.Any` type.
35 fn type_url() -> String {
36 format!("/{}", Self::full_name())
37 }
38}