Skip to content

Commit ff642f9

Browse files
authored
feat(build): Builder: add {enum,message}_attributes (#1234)
Signed-off-by: Richard Leitner <[email protected]>
1 parent 4c69157 commit ff642f9

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

tests/disable_comments/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ prost = "0.11"
1313
tonic = { path = "../../tonic" }
1414

1515
[build-dependencies]
16-
prost-build = "0.11.4"
16+
prost-build = "0.11.6"
1717
tonic-build = { path = "../../tonic-build" }

tests/extern_path/uuid/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ version = "0.1.0"
1111
[dependencies]
1212
prost = "0.11"
1313
[build-dependencies]
14-
prost-build = "0.11.4"
14+
prost-build = "0.11.6"

tests/wellknown-compiled/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ prost = "0.11"
1616
tonic = {path = "../../tonic"}
1717

1818
[build-dependencies]
19-
prost-build = "0.11.4"
19+
prost-build = "0.11.6"
2020
tonic-build = {path = "../../tonic-build"}

tonic-build/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ version = "0.8.4"
1717
[dependencies]
1818
prettyplease = { version = "0.1" }
1919
proc-macro2 = "1.0"
20-
prost-build = { version = "0.11.4", optional = true }
20+
prost-build = { version = "0.11.6", optional = true }
2121
quote = "1.0"
2222
syn = "1.0"
2323

tonic-build/src/prost.rs

+32
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub fn configure() -> Builder {
2323
out_dir: None,
2424
extern_path: Vec::new(),
2525
field_attributes: Vec::new(),
26+
message_attributes: Vec::new(),
27+
enum_attributes: Vec::new(),
2628
type_attributes: Vec::new(),
2729
server_attributes: Attributes::default(),
2830
client_attributes: Attributes::default(),
@@ -225,6 +227,8 @@ pub struct Builder {
225227
pub(crate) extern_path: Vec<(String, String)>,
226228
pub(crate) field_attributes: Vec<(String, String)>,
227229
pub(crate) type_attributes: Vec<(String, String)>,
230+
pub(crate) message_attributes: Vec<(String, String)>,
231+
pub(crate) enum_attributes: Vec<(String, String)>,
228232
pub(crate) server_attributes: Attributes,
229233
pub(crate) client_attributes: Attributes,
230234
pub(crate) proto_path: String,
@@ -306,6 +310,28 @@ impl Builder {
306310
self
307311
}
308312

313+
/// Add additional attribute to matched messages.
314+
///
315+
/// Passed directly to `prost_build::Config.message_attribute`.
316+
pub fn message_attribute<P: AsRef<str>, A: AsRef<str>>(
317+
mut self,
318+
path: P,
319+
attribute: A,
320+
) -> Self {
321+
self.message_attributes
322+
.push((path.as_ref().to_string(), attribute.as_ref().to_string()));
323+
self
324+
}
325+
326+
/// Add additional attribute to matched enums.
327+
///
328+
/// Passed directly to `prost_build::Config.enum_attribute`.
329+
pub fn enum_attribute<P: AsRef<str>, A: AsRef<str>>(mut self, path: P, attribute: A) -> Self {
330+
self.enum_attributes
331+
.push((path.as_ref().to_string(), attribute.as_ref().to_string()));
332+
self
333+
}
334+
309335
/// Add additional attribute to matched server `mod`s. Matches on the package name.
310336
pub fn server_mod_attribute<P: AsRef<str>, A: AsRef<str>>(
311337
mut self,
@@ -447,6 +473,12 @@ impl Builder {
447473
for (prost_path, attr) in self.type_attributes.iter() {
448474
config.type_attribute(prost_path, attr);
449475
}
476+
for (prost_path, attr) in self.message_attributes.iter() {
477+
config.message_attribute(prost_path, attr);
478+
}
479+
for (prost_path, attr) in self.enum_attributes.iter() {
480+
config.enum_attribute(prost_path, attr);
481+
}
450482
if self.compile_well_known_types {
451483
config.compile_well_known_types();
452484
}

0 commit comments

Comments
 (0)