Skip to content

Commit 70734a9

Browse files
committed
Add root_module and wrap_in_module options
* When root_module=true do not emit code that fails to compile as a root module. * When wrap_in_module=name is given, add a wrapper around output: pub mod name { ... } This allows the output file to be "included" and compiled.
1 parent fd75c6d commit 70734a9

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

protobuf-codegen/src/code_writer.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// TODO: used by grpc-rust, should move it into separate crate.
22
#![doc(hidden)]
33

4+
use crate::customize::Customize;
45
use crate::rust_name::RustRelativePath;
56
use std::io::Write;
67

@@ -34,12 +35,18 @@ impl<'a> CodeWriter<'a> {
3435
.unwrap();
3536
}
3637

37-
pub fn write_generated(&mut self) {
38+
pub fn write_generated(&mut self, customize: &Customize) {
3839
self.write_line("// This file is generated. Do not edit");
39-
self.write_generated_common();
40+
self.write_generated_common(customize);
4041
}
4142

42-
pub fn write_generated_by(&mut self, pkg: &str, version: &str, parser: &str) {
43+
pub fn write_generated_by(
44+
&mut self,
45+
pkg: &str,
46+
version: &str,
47+
parser: &str,
48+
customize: &Customize,
49+
) {
4350
self.write_line(format!(
4451
"// This file is generated by {pkg} {version}. Do not edit",
4552
pkg = pkg,
@@ -49,10 +56,10 @@ impl<'a> CodeWriter<'a> {
4956
"// .proto file is parsed by {parser}",
5057
parser = parser
5158
));
52-
self.write_generated_common();
59+
self.write_generated_common(customize);
5360
}
5461

55-
fn write_generated_common(&mut self) {
62+
fn write_generated_common(&mut self, customize: &Customize) {
5663
// https://secure.phabricator.com/T784
5764
self.write_line("// @generated");
5865

@@ -62,7 +69,10 @@ impl<'a> CodeWriter<'a> {
6269
self.write_line("#![allow(clippy::all)]");
6370
self.write_line("");
6471
self.write_line("#![allow(unused_attributes)]");
65-
self.write_line("#![rustfmt::skip]");
72+
if customize.root_module != Some(true) {
73+
// does not compile as a root module
74+
self.write_line("#![rustfmt::skip]");
75+
}
6676
self.write_line("");
6777
self.write_line("#![allow(box_pointers)]");
6878
self.write_line("#![allow(dead_code)]");

protobuf-codegen/src/customize.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ pub struct Customize {
2929
/// Used internally to generate protos bundled in protobuf crate
3030
/// like `descriptor.proto`
3131
pub inside_protobuf: Option<bool>,
32+
/// Generate code suitable for compiling as a root module
33+
pub root_module: Option<bool>,
34+
/// Wrap output file in the named module
35+
pub wrap_in_module: Option<String>,
3236

3337
// When adding more options please keep in sync with `parse_from_parameter` below.
3438
/// Make sure `Customize` is always used with `..Default::default()`
@@ -78,6 +82,12 @@ impl Customize {
7882
if let Some(v) = that.inside_protobuf {
7983
self.inside_protobuf = Some(v);
8084
}
85+
if let Some(v) = that.root_module {
86+
self.root_module = Some(v);
87+
}
88+
if let Some(ref v) = that.wrap_in_module {
89+
self.wrap_in_module = Some(v.clone());
90+
}
8191
}
8292

8393
/// Update unset fields of self with fields from other customize
@@ -124,6 +134,10 @@ impl Customize {
124134
r.lite_runtime = Some(parse_bool(v)?);
125135
} else if n == "inside_protobuf" {
126136
r.inside_protobuf = Some(parse_bool(v)?);
137+
} else if n == "root_module" {
138+
r.root_module = Some(parse_bool(v)?);
139+
} else if n == "wrap_in_module" {
140+
r.wrap_in_module = Some(v.to_owned());
127141
} else {
128142
return Err(CustomizeParseParameterError::UnknownOptionName(
129143
n.to_owned(),
@@ -145,6 +159,8 @@ pub fn customize_from_rustproto_for_message(source: &MessageOptions) -> Customiz
145159
let serde_derive_cfg = rustproto::exts::serde_derive_cfg.get(source);
146160
let lite_runtime = None;
147161
let inside_protobuf = None;
162+
let root_module = None;
163+
let wrap_in_module = None;
148164
Customize {
149165
expose_oneof,
150166
expose_fields,
@@ -156,6 +172,8 @@ pub fn customize_from_rustproto_for_message(source: &MessageOptions) -> Customiz
156172
serde_derive_cfg,
157173
lite_runtime,
158174
inside_protobuf,
175+
root_module,
176+
wrap_in_module,
159177
_future_options: (),
160178
}
161179
}
@@ -172,6 +190,8 @@ pub fn customize_from_rustproto_for_field(source: &FieldOptions) -> Customize {
172190
let serde_derive_cfg = None;
173191
let lite_runtime = None;
174192
let inside_protobuf = None;
193+
let root_module = None;
194+
let wrap_in_module = None;
175195
Customize {
176196
expose_oneof,
177197
expose_fields,
@@ -183,6 +203,8 @@ pub fn customize_from_rustproto_for_field(source: &FieldOptions) -> Customize {
183203
serde_derive_cfg,
184204
lite_runtime,
185205
inside_protobuf,
206+
root_module,
207+
wrap_in_module,
186208
_future_options: (),
187209
}
188210
}
@@ -198,6 +220,8 @@ pub fn customize_from_rustproto_for_file(source: &FileOptions) -> Customize {
198220
let serde_derive_cfg = rustproto::exts::serde_derive_cfg_all.get(source);
199221
let lite_runtime = rustproto::exts::lite_runtime_all.get(source);
200222
let inside_protobuf = None;
223+
let root_module = None;
224+
let wrap_in_module = None;
201225
Customize {
202226
expose_oneof,
203227
expose_fields,
@@ -209,6 +233,8 @@ pub fn customize_from_rustproto_for_file(source: &FileOptions) -> Customize {
209233
serde_derive_cfg,
210234
lite_runtime,
211235
inside_protobuf,
236+
root_module,
237+
wrap_in_module,
212238
_future_options: (),
213239
}
214240
}

protobuf-codegen/src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,16 @@ fn gen_file(
274274

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

278-
w.write_generated_by("rust-protobuf", env!("CARGO_PKG_VERSION"), parser);
281+
w.write_generated_by(
282+
"rust-protobuf",
283+
env!("CARGO_PKG_VERSION"),
284+
parser,
285+
&customize,
286+
);
279287

280288
w.write_line("");
281289
w.write_line(&format!(
@@ -353,6 +361,10 @@ fn gen_file(
353361
w.write_line("");
354362
write_file_descriptor_data(file_descriptor, &customize, &mut w);
355363
}
364+
365+
if let Some(ref m) = customize.wrap_in_module {
366+
w.write_line(format!("}} // {}", m));
367+
}
356368
}
357369

358370
Some(compiler_plugin::GenResult {

0 commit comments

Comments
 (0)