Skip to content

Add root_module and wrap_in_module options #523

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

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 16 additions & 6 deletions protobuf-codegen/src/code_writer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// TODO: used by grpc-rust, should move it into separate crate.
#![doc(hidden)]

use crate::customize::Customize;
use crate::rust_name::RustRelativePath;
use std::io::Write;

Expand Down Expand Up @@ -34,12 +35,18 @@ impl<'a> CodeWriter<'a> {
.unwrap();
}

pub fn write_generated(&mut self) {
pub fn write_generated(&mut self, customize: &Customize) {
self.write_line("// This file is generated. Do not edit");
self.write_generated_common();
self.write_generated_common(customize);
}

pub fn write_generated_by(&mut self, pkg: &str, version: &str, parser: &str) {
pub fn write_generated_by(
&mut self,
pkg: &str,
version: &str,
parser: &str,
customize: &Customize,
) {
self.write_line(format!(
"// This file is generated by {pkg} {version}. Do not edit",
pkg = pkg,
Expand All @@ -49,10 +56,10 @@ impl<'a> CodeWriter<'a> {
"// .proto file is parsed by {parser}",
parser = parser
));
self.write_generated_common();
self.write_generated_common(customize);
}

fn write_generated_common(&mut self) {
fn write_generated_common(&mut self, customize: &Customize) {
// https://secure.phabricator.com/T784
self.write_line("// @generated");

Expand All @@ -62,7 +69,10 @@ impl<'a> CodeWriter<'a> {
self.write_line("#![allow(clippy::all)]");
self.write_line("");
self.write_line("#![allow(unused_attributes)]");
self.write_line("#![rustfmt::skip]");
if customize.root_module != Some(true) {
// does not compile as a root module
self.write_line("#![rustfmt::skip]");
}
self.write_line("");
self.write_line("#![allow(box_pointers)]");
self.write_line("#![allow(dead_code)]");
Expand Down
26 changes: 26 additions & 0 deletions protobuf-codegen/src/customize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub struct Customize {
/// Used internally to generate protos bundled in protobuf crate
/// like `descriptor.proto`
pub inside_protobuf: Option<bool>,
/// Generate code suitable for compiling as a root module
pub root_module: Option<bool>,
/// Wrap output file in the named module
pub wrap_in_module: Option<String>,

// When adding more options please keep in sync with `parse_from_parameter` below.
/// Make sure `Customize` is always used with `..Default::default()`
Expand Down Expand Up @@ -78,6 +82,12 @@ impl Customize {
if let Some(v) = that.inside_protobuf {
self.inside_protobuf = Some(v);
}
if let Some(v) = that.root_module {
self.root_module = Some(v);
}
if let Some(ref v) = that.wrap_in_module {
self.wrap_in_module = Some(v.clone());
}
}

/// Update unset fields of self with fields from other customize
Expand Down Expand Up @@ -124,6 +134,10 @@ impl Customize {
r.lite_runtime = Some(parse_bool(v)?);
} else if n == "inside_protobuf" {
r.inside_protobuf = Some(parse_bool(v)?);
} else if n == "root_module" {
r.root_module = Some(parse_bool(v)?);
} else if n == "wrap_in_module" {
r.wrap_in_module = Some(v.to_owned());
} else {
return Err(CustomizeParseParameterError::UnknownOptionName(
n.to_owned(),
Expand All @@ -145,6 +159,8 @@ pub fn customize_from_rustproto_for_message(source: &MessageOptions) -> Customiz
let serde_derive_cfg = rustproto::exts::serde_derive_cfg.get(source);
let lite_runtime = None;
let inside_protobuf = None;
let root_module = None;
let wrap_in_module = None;
Customize {
expose_oneof,
expose_fields,
Expand All @@ -156,6 +172,8 @@ pub fn customize_from_rustproto_for_message(source: &MessageOptions) -> Customiz
serde_derive_cfg,
lite_runtime,
inside_protobuf,
root_module,
wrap_in_module,
_future_options: (),
}
}
Expand All @@ -172,6 +190,8 @@ pub fn customize_from_rustproto_for_field(source: &FieldOptions) -> Customize {
let serde_derive_cfg = None;
let lite_runtime = None;
let inside_protobuf = None;
let root_module = None;
let wrap_in_module = None;
Customize {
expose_oneof,
expose_fields,
Expand All @@ -183,6 +203,8 @@ pub fn customize_from_rustproto_for_field(source: &FieldOptions) -> Customize {
serde_derive_cfg,
lite_runtime,
inside_protobuf,
root_module,
wrap_in_module,
_future_options: (),
}
}
Expand All @@ -198,6 +220,8 @@ pub fn customize_from_rustproto_for_file(source: &FileOptions) -> Customize {
let serde_derive_cfg = rustproto::exts::serde_derive_cfg_all.get(source);
let lite_runtime = rustproto::exts::lite_runtime_all.get(source);
let inside_protobuf = None;
let root_module = None;
let wrap_in_module = None;
Customize {
expose_oneof,
expose_fields,
Expand All @@ -209,6 +233,8 @@ pub fn customize_from_rustproto_for_file(source: &FileOptions) -> Customize {
serde_derive_cfg,
lite_runtime,
inside_protobuf,
root_module,
wrap_in_module,
_future_options: (),
}
}
14 changes: 13 additions & 1 deletion protobuf-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,16 @@ fn gen_file(

{
let mut w = CodeWriter::new(&mut v);
if let Some(ref m) = customize.wrap_in_module {
w.write_line(&format!("pub mod {} {{", m));
}

w.write_generated_by("rust-protobuf", env!("CARGO_PKG_VERSION"), parser);
w.write_generated_by(
"rust-protobuf",
env!("CARGO_PKG_VERSION"),
parser,
&customize,
);

w.write_line("");
w.write_line(&format!(
Expand Down Expand Up @@ -353,6 +361,10 @@ fn gen_file(
w.write_line("");
write_file_descriptor_data(file_descriptor, &customize, &mut w);
}

if let Some(ref m) = customize.wrap_in_module {
w.write_line(format!("}} // {}", m));
}
}

Some(compiler_plugin::GenResult {
Expand Down