Skip to content

Commit b35660c

Browse files
committed
add file header customize option
Signed-off-by: Matt Klein <[email protected]>
1 parent 8c26a4c commit b35660c

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

protobuf-codegen/src/customize/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ pub struct Customize {
113113
pub(crate) inside_protobuf: Option<bool>,
114114
/// When true, protobuf maps are represented with `std::collections::BTreeMap`
115115
pub(crate) btreemap: Option<bool>,
116+
/// Optional file header to insert at the top of every file.
117+
pub(crate) file_header: Option<String>,
116118
}
117119

118120
#[derive(Debug, thiserror::Error)]
@@ -157,6 +159,11 @@ impl Customize {
157159
self
158160
}
159161

162+
pub fn file_header(mut self, file_header: String) -> Self {
163+
self.file_header = Some(file_header);
164+
self
165+
}
166+
160167
/// Generate code for "lite runtime". Generated code contains no code for reflection.
161168
/// So the generated code (and more importantly, generated binary size) is smaller,
162169
/// but reflection, text format, JSON serialization won't work.
@@ -220,6 +227,9 @@ impl Customize {
220227
if let Some(v) = that.oneofs_non_exhaustive {
221228
self.oneofs_non_exhaustive = Some(v);
222229
}
230+
if let Some(v) = &that.file_header {
231+
self.file_header = Some(v.clone());
232+
}
223233
}
224234

225235
/// Update unset fields of self with fields from other customize

protobuf-codegen/src/customize/rustproto_proto.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub(crate) fn customize_from_rustproto_for_message(source: &MessageOptions) -> C
2828
gen_mod_rs,
2929
inside_protobuf,
3030
btreemap,
31+
file_header: None,
3132
}
3233
}
3334

@@ -57,6 +58,7 @@ pub(crate) fn customize_from_rustproto_for_field(source: &FieldOptions) -> Custo
5758
gen_mod_rs,
5859
inside_protobuf,
5960
btreemap,
61+
file_header: None,
6062
}
6163
}
6264

@@ -82,5 +84,6 @@ pub(crate) fn customize_from_rustproto_for_file(source: &FileOptions) -> Customi
8284
inside_protobuf,
8385
gen_mod_rs,
8486
btreemap,
87+
file_header: None,
8588
}
8689
}

protobuf-codegen/src/gen/file.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ pub(crate) fn gen_file(
5454
let lite_runtime = customize.for_elem.lite_runtime.unwrap_or(false);
5555

5656
let v = CodeWriter::with(|w| {
57+
if let Some(file_header) = customize.for_elem.file_header.as_ref() {
58+
w.write_line(file_header);
59+
}
60+
5761
w.write_generated_by("rust-protobuf", env!("CARGO_PKG_VERSION"), parser);
5862

5963
w.write_line("");

0 commit comments

Comments
 (0)