Skip to content

Commit

Permalink
build: Add file_descriptor_set option to compile_request
Browse files Browse the repository at this point in the history
This allows to have the contents of the `FileDescriptorSet` when using
the protoc plugin interface
  • Loading branch information
Tuetuopay committed Sep 22, 2021
1 parent 777d428 commit 6108221
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions prost-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ const DISABLE_COMMENTS: &str = "disable_comments";
const EXTERN_PATH: &str = "extern_path";
const RETAIN_ENUM_PREFIX: &str = "retain_enum_prefix";
const INCLUDE_FILE: &str = "include_file";
const FILE_DESCRIPTOR_SET: &str = "file_descriptor_set";
const GEN_CRATE: &str = "gen_crate";
pub const PROTOC_OPTS: [&str; 10] = [
pub const PROTOC_OPTS: [&str; 11] = [
BTREE_MAP,
BYTES,
FIELD_ATTR,
Expand All @@ -239,6 +240,7 @@ pub const PROTOC_OPTS: [&str; 10] = [
EXTERN_PATH,
RETAIN_ENUM_PREFIX,
INCLUDE_FILE,
FILE_DESCRIPTOR_SET,
GEN_CRATE,
];

Expand Down Expand Up @@ -302,6 +304,10 @@ impl Config {
[EXTERN_PATH, k, v] => self.extern_paths.push((k.to_string(), v.to_string())),
[RETAIN_ENUM_PREFIX] => self.strip_enum_prefix = false,
[INCLUDE_FILE, v] => self.include_file = Some(v.into()),
[FILE_DESCRIPTOR_SET] => {
self.file_descriptor_set_path = Some("file_descriptor_set".into())
}
[FILE_DESCRIPTOR_SET, v] => self.file_descriptor_set_path = Some(v.into()),
[GEN_CRATE, v] => self.manifest_tpl = Some(v.into()),
_ if log_unknown => eprintln!("prost: Unknown option `{}`", full_opt),
_ => (),
Expand Down Expand Up @@ -1014,6 +1020,31 @@ impl Config {
/// Compile `.proto` code into Rust code from a protoc build with additional code generator
/// configuration options.
pub fn compile_request(&mut self, req: CodeGeneratorRequest) -> CodeGeneratorResponse {
// Optionally output the file descriptor set
let set = self.file_descriptor_set_path.clone().map(|path| {
let name = self.filename(&vec![path.to_str().unwrap().to_owned()]);

let set = FileDescriptorSet {
file: req.proto_file.clone(),
};
let mut buf = Vec::new();
set.encode(&mut buf).unwrap();

let buf = buf.into_iter().chunks(16);
let lines = buf
.into_iter()
.map(|chunk| chunk.map(|byte| format!("0x{:02x}", byte)).join(", "))
.join(",\n ");
let content =
"pub const FILE_DESCRIPTOR_SET: &[u8] = &[\n ".to_owned() + &lines + ",\n];\n";

vec![File {
name: Some(name),
content: Some(content),
..Default::default()
}]
});

match self.generate(req.proto_file) {
Ok(modules) => {
let f = modules.into_iter().map(|(module, content)| File {
Expand All @@ -1025,7 +1056,7 @@ impl Config {
CodeGeneratorResponse {
error: None,
supported_features: None,
file: f.collect(),
file: f.chain(set.unwrap_or_default()).collect(),
}
}
Err(e) => CodeGeneratorResponse {
Expand Down Expand Up @@ -1088,6 +1119,19 @@ impl Config {
trace!("Generate include file: {:?}", include_file);

let mut include = String::new();
if let Some(mut set) = self.file_descriptor_set_path.clone() {
let path = if self.manifest_tpl.is_some() {
"../gen"
} else {
"."
};
set.set_extension("rs");
include.push_str(&format!(
"include!(\"{}/{}\");\n",
path,
set.to_str().unwrap()
));
}
self.write_includes(modules.keys().collect(), &mut include, 0);
modules.insert(vec![include_file], include);
}
Expand Down

0 comments on commit 6108221

Please sign in to comment.