Skip to content

Commit a673ebd

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 a673ebd

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

protobuf-codegen/src/code_writer.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use crate::rust_name::RustRelativePath;
55
use std::io::Write;
6+
use crate::customize::Customize;
67

78
/// Field visibility.
89
pub(crate) enum Visibility {
@@ -34,12 +35,12 @@ 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(&mut self, pkg: &str, version: &str, parser: &str, customize: &Customize) {
4344
self.write_line(format!(
4445
"// This file is generated by {pkg} {version}. Do not edit",
4546
pkg = pkg,
@@ -49,10 +50,10 @@ impl<'a> CodeWriter<'a> {
4950
"// .proto file is parsed by {parser}",
5051
parser = parser
5152
));
52-
self.write_generated_common();
53+
self.write_generated_common(customize);
5354
}
5455

55-
fn write_generated_common(&mut self) {
56+
fn write_generated_common(&mut self, customize: &Customize) {
5657
// https://secure.phabricator.com/T784
5758
self.write_line("// @generated");
5859

@@ -62,7 +63,10 @@ impl<'a> CodeWriter<'a> {
6263
self.write_line("#![allow(clippy::all)]");
6364
self.write_line("");
6465
self.write_line("#![allow(unused_attributes)]");
65-
self.write_line("#![rustfmt::skip]");
66+
if customize.root_module != Some(true) {
67+
// does not compile as a root module
68+
self.write_line("#![rustfmt::skip]");
69+
}
6670
self.write_line("");
6771
self.write_line("#![allow(box_pointers)]");
6872
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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,11 @@ 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("rust-protobuf", env!("CARGO_PKG_VERSION"), parser, &customize);
279282

280283
w.write_line("");
281284
w.write_line(&format!(
@@ -353,6 +356,10 @@ fn gen_file(
353356
w.write_line("");
354357
write_file_descriptor_data(file_descriptor, &customize, &mut w);
355358
}
359+
360+
if let Some(ref m) = customize.wrap_in_module {
361+
w.write_line(format!("}} // {}", m));
362+
}
356363
}
357364

358365
Some(compiler_plugin::GenResult {

0 commit comments

Comments
 (0)