Skip to content

Commit aa11ecb

Browse files
jen20danburkert
authored andcommitted
Add include_file_descriptor_set to prost-build
This commit adds support for writing an encoded version of the appropriate FileDescriptorSet to a file named `file_descriptor_set.bin` in the output directory. This can be used with the `include_bytes` macro by consuming applications or libraries. The rationale for this pull request is to support the gRPC Server Reflection Protocol, which operates in terms of FileDescriptorProto, in Tonic. FileDescriptorProto instances are contained inside a FileDescriptorSet.
1 parent a5bb7bf commit aa11ecb

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ Compared to other Protocol Buffers implementations, `prost`
2121
* Respects the Protobuf `package` specifier when organizing generated code
2222
into Rust modules.
2323
* Preserves unknown enum values during deserialization.
24-
* Does not include support for runtime reflection or message descriptors.
24+
* Does not include support for runtime reflection or message descriptors, but
25+
allows encoded `FileDescriptorSet` messages to be saved alongside generated
26+
code for use of consumers.
2527

2628
## Using `prost` in a Cargo Project
2729

@@ -58,6 +60,12 @@ package foo.bar;
5860

5961
All Rust types generated from the file will be in the `foo::bar` module.
6062

63+
If the `include_file_descriptor_set` option is set on the `prost_build::Config`
64+
for the invocation of `prost_build`, an additional file named `file_descriptor_set.bin`
65+
will be written to the output directory. This can be used in conjunction with the
66+
`include_bytes` macro, and decoded using the `FileDescriptorSet` type from the
67+
`prost-types` crate in applications or libraries using Prost.
68+
6169
### Messages
6270

6371
Given a simple message declaration:

prost-build/src/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ use std::env;
118118
use std::ffi::{OsStr, OsString};
119119
use std::fmt;
120120
use std::fs;
121-
use std::io::{Error, ErrorKind, Result};
121+
use std::io::{Error, ErrorKind, Result, Write};
122122
use std::path::{Path, PathBuf};
123123
use std::process::Command;
124124

@@ -183,6 +183,7 @@ pub trait ServiceGenerator {
183183
///
184184
/// This configuration builder can be used to set non-default code generation options.
185185
pub struct Config {
186+
include_file_descriptor_set: bool,
186187
service_generator: Option<Box<dyn ServiceGenerator>>,
187188
btree_map: Vec<String>,
188189
bytes: Vec<String>,
@@ -552,6 +553,15 @@ impl Config {
552553
self
553554
}
554555

556+
/// Configures the code generator to output the encoded bytes of the `FileDescriptorSet`
557+
/// for this `protoc` invocation to a file named `file_descriptor_set.bin` in the configured
558+
/// output directory. This can be used in conjunction with the `include_bytes!` macro and
559+
/// the types in the `prost-types` crate for implementing some reflection capabilities.
560+
pub fn include_file_descriptor_set(&mut self) -> &mut Self {
561+
self.include_file_descriptor_set = true;
562+
self
563+
}
564+
555565
/// Configures the code generator to not strip the enum name from variant names.
556566
///
557567
/// Protobuf enum definitions commonly include the enum name as a prefix of every variant name.
@@ -674,6 +684,12 @@ impl Config {
674684
)
675685
})?;
676686

687+
if self.include_file_descriptor_set {
688+
let filename = target.join("file_descriptor_set.bin");
689+
let mut file = std::fs::File::create(filename)?;
690+
file.write_all(&buf)?;
691+
}
692+
677693
let modules = self.generate(descriptor_set.file)?;
678694
for (module, content) in modules {
679695
let mut filename = module.join(".");
@@ -740,6 +756,7 @@ impl Config {
740756
impl default::Default for Config {
741757
fn default() -> Config {
742758
Config {
759+
include_file_descriptor_set: false,
743760
service_generator: None,
744761
btree_map: Vec::new(),
745762
bytes: Vec::new(),

0 commit comments

Comments
 (0)