Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add no_insert_include flag for tonic/serde #13

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions protoc-gen-prost-serde/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ from that crate:
* `extern_path=<proto_path>=<rust_path>`: [extern_path](https://docs.rs/pbjson-build/latest/pbjson_build/struct.Builder.html#method.extern_path)
* `retain_enum_prefix(=<boolean>)`: [retain_enum_prefix](https://docs.rs/pbjson-build/latest/pbjson_build/struct.Builder.html#method.retain_enum_prefix)

In addition, the following options can also be specified:

* `no_insert_include(=<boolean>)`: Disables the insertion of the include
macro into the file generated by `protoc-gen-prost`, required if run in
a separate `protoc` execution. Workaround for the error `Tried to insert
into file that doesn't exist`.

A note on parameter values:

* `<proto_path>`: Protobuf paths beginning with `.` will be matched from the
Expand Down
24 changes: 15 additions & 9 deletions protoc-gen-prost-serde/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const GENERATED_HEADER: &str = "// @generated\n";
pub struct PbJsonGenerator {
builder: pbjson_build::Builder,
prefixes: Vec<String>,
insert_include: bool,
}

impl Generator for PbJsonGenerator {
Expand All @@ -21,25 +22,29 @@ impl Generator for PbJsonGenerator {

let output_filename = format!("{}.serde.rs", request.proto_package_name());

let insertion = request.append_to_file(|buf| {
buf.push_str("include!(\"");
buf.push_str(&output_filename);
buf.push_str("\");\n");
})?;
let mut res = Vec::with_capacity(2);

if self.insert_include {
res.push(request.append_to_file(|buf| {
buf.push_str("include!(\"");
buf.push_str(&output_filename);
buf.push_str("\");\n");
})?);
}

let mut content = String::with_capacity(bytes.len() + GENERATED_HEADER.len());
content.push_str(GENERATED_HEADER);
content.push_str(
std::str::from_utf8(&bytes).expect("pbjson build produced non UTF-8 data"),
);

let data = File {
res.push(File {
name: Some(output_filename),
content: Some(content),
..File::default()
};
});

Some([data, insertion])
Some(res)
})
.flatten()
.map(Ok)
Expand All @@ -48,10 +53,11 @@ impl Generator for PbJsonGenerator {
}

impl PbJsonGenerator {
pub fn new(builder: pbjson_build::Builder) -> Self {
pub fn new(builder: pbjson_build::Builder, insert_include: bool) -> Self {
Self {
builder,
prefixes: vec![".".to_owned()],
insert_include,
}
}
}
5 changes: 4 additions & 1 deletion protoc-gen-prost-serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn execute(raw_request: &[u8]) -> protoc_gen_prost::Result {
params.default_package_filename.as_deref(),
)?;

let files = PbJsonGenerator::new(builder).generate(&module_request_set)?;
let files = PbJsonGenerator::new(builder, !params.no_insert_include).generate(&module_request_set)?;

Ok(files)
}
Expand All @@ -39,6 +39,7 @@ struct Parameters {
default_package_filename: Option<String>,
extern_path: Vec<(String, String)>,
retain_enum_prefix: bool,
no_insert_include: bool,
}

static PARAMETER: Lazy<regex::Regex> = Lazy::new(|| {
Expand Down Expand Up @@ -86,6 +87,8 @@ impl str::FromStr for Parameters {
ret_val.retain_enum_prefix = true
}
("retain_enum_prefix", Some("false"), None) => (),
("no_insert_include", Some("true") | None, None) => ret_val.no_insert_include = true,
("no_insert_include", Some("false"), None) => (),
("extern_path", Some(prefix), Some(module)) => ret_val
.extern_path
.push((prefix.to_string(), module.to_string())),
Expand Down
5 changes: 5 additions & 0 deletions protoc-gen-tonic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ see the related documentation from `tonic-build`:

no_server: bool,
no_client: bool,
no_insert_include: bool,


* `default_package_filename=<value>`: [default_package_filename](https://docs.rs/prost-build/latest/prost_build/struct.Config.html#method.default_package_filename)
Expand All @@ -50,6 +51,10 @@ In addition, the following options can also be specified:

* `no_server(=<boolean>)`: Disables generation of the server modules
* `no_client(=<boolean>)`: Disables generation of the client modules
* `no_insert_include(=<boolean>)`: Disables the insertion of the include
macro into the file generated by `protoc-gen-prost`, required if run in
a separate `protoc` execution. Workaround for the error `Tried to insert
into file that doesn't exist`.

A note on parameter values:

Expand Down
23 changes: 14 additions & 9 deletions protoc-gen-tonic/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub(crate) struct TonicGenerator {
pub(crate) server_attributes: Attributes,
pub(crate) client_attributes: Attributes,
pub(crate) emit_package: bool,
pub(crate) insert_include: bool,
}

impl Generator for TonicGenerator {
Expand All @@ -27,7 +28,7 @@ impl Generator for TonicGenerator {
}

impl TonicGenerator {
fn handle_module_request(&self, module: &Module, request: &ModuleRequest) -> Option<[File; 2]> {
fn handle_module_request(&self, module: &Module, request: &ModuleRequest) -> Option<Vec<File>> {
const PROTO_PATH: &str = "super";

let output_filename = format!("{}.tonic.rs", request.proto_package_name());
Expand Down Expand Up @@ -74,21 +75,25 @@ impl TonicGenerator {
if services.is_empty() {
None
} else {
let insertion = request.append_to_file(|buf| {
buf.push_str("include!(\"");
buf.push_str(&output_filename);
buf.push_str("\");\n");
})?;
let mut res = Vec::with_capacity(2);

let file = syn::parse2(services).expect("valid rust file");

let data = File {
if self.insert_include {
res.push(request.append_to_file(|buf| {
buf.push_str("include!(\"");
buf.push_str(&output_filename);
buf.push_str("\");\n");
})?);
}

res.push(File {
name: Some(output_filename),
content: Some(format!("// @generated\n{}", prettyplease::unparse(&file))),
..File::default()
};
});

Some([data, insertion])
Some(res)
}
}

Expand Down
4 changes: 4 additions & 0 deletions protoc-gen-tonic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub fn execute(raw_request: &[u8]) -> protoc_gen_prost::Result {
server_attributes: params.server_attributes,
client_attributes: params.client_attributes,
emit_package: !params.disable_package_emission,
insert_include: !params.no_insert_include,
};

let files = generator.generate(&module_request_set)?;
Expand All @@ -53,6 +54,7 @@ struct Parameters {
disable_package_emission: bool,
no_server: bool,
no_client: bool,
no_insert_include: bool,
}

static PARAMETER: Lazy<regex::Regex> = Lazy::new(|| {
Expand Down Expand Up @@ -95,6 +97,8 @@ impl str::FromStr for Parameters {
("no_server", Some("false"), None) => (),
("no_client", Some("true") | None, None) => ret_val.no_client = true,
("no_client", Some("false"), None) => (),
("no_insert_include", Some("true") | None, None) => ret_val.no_insert_include = true,
("no_insert_include", Some("false"), None) => (),
("client_mod_attribute", Some(prefix), Some(attribute)) => ret_val
.client_attributes
.push_mod(prefix, attribute.replace(r"\,", ",")),
Expand Down