Skip to content

Commit 363435f

Browse files
committed
feat(pilota-build): add with_comments option for builder
1 parent 67eb3ec commit 363435f

File tree

23 files changed

+216
-58
lines changed

23 files changed

+216
-58
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pilota-build/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "pilota-build"
3-
version = "0.13.4"
3+
version = "0.13.5"
44
edition.workspace = true
55
homepage.workspace = true
66
repository.workspace = true

pilota-build/src/codegen/mod.rs

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,22 @@ where
122122
""
123123
};
124124

125-
let leading_comment = f.leading_comments.to_string();
126-
let trailing_comment = f.trailing_comments.to_string();
125+
if self.config.with_comments {
126+
let leading_comment = f.leading_comments.to_string();
127+
let trailing_comment = f.trailing_comments.to_string();
127128

128-
format! {
129-
r#"
129+
format! {
130+
r#"
130131
{leading_comment}
131132
{attrs}
132133
{deprecated_attr}pub {name}: {ty},{trailing_comment}"#
134+
}
135+
} else {
136+
format! {
137+
r#"
138+
{attrs}
139+
{deprecated_attr}pub {name}: {ty},"#
140+
}
133141
}
134142
})
135143
})
@@ -151,7 +159,11 @@ where
151159
""
152160
};
153161

154-
let trailing_comment = s.trailing_comments.to_string();
162+
let trailing_comment = if self.config.with_comments {
163+
s.trailing_comments.as_str()
164+
} else {
165+
""
166+
};
155167

156168
stream.push_str(&format! {
157169
r#"#[derive(Clone, PartialEq)]
@@ -177,14 +189,19 @@ where
177189
tracing::trace!("write item {}", item.symbol_name());
178190

179191
// write leading comments
180-
let comments = match &*item {
181-
middle::rir::Item::Message(s) => s.leading_comments.to_string(),
182-
middle::rir::Item::Enum(e) => e.leading_comments.to_string(),
183-
middle::rir::Item::Service(s) => s.leading_comments.to_string(),
184-
middle::rir::Item::NewType(t) => t.leading_comments.to_string(),
185-
middle::rir::Item::Const(c) => c.leading_comments.to_string(),
186-
_ => String::new(),
192+
let comments = if self.config.with_comments {
193+
match &*item {
194+
middle::rir::Item::Message(s) => s.leading_comments.as_str(),
195+
middle::rir::Item::Enum(e) => e.leading_comments.as_str(),
196+
middle::rir::Item::Service(s) => s.leading_comments.as_str(),
197+
middle::rir::Item::NewType(t) => t.leading_comments.as_str(),
198+
middle::rir::Item::Const(c) => c.leading_comments.as_str(),
199+
_ => "",
200+
}
201+
} else {
202+
""
187203
};
204+
188205
if !comments.is_empty() {
189206
stream.push_str(&format!("\n{comments}\n"));
190207
}
@@ -404,7 +421,11 @@ where
404421
format!("({fields})")
405422
};
406423

407-
let leading_comment = v.leading_comments.to_string();
424+
let leading_comment = if self.config.with_comments {
425+
v.leading_comments.as_str()
426+
} else {
427+
""
428+
};
408429

409430
format!(
410431
r#"{leading_comment}
@@ -418,7 +439,11 @@ where
418439
if self.cache.keep_unknown_fields.contains(&def_id) && keep {
419440
variants.push_str("_UnknownFields(::pilota::BytesVec),");
420441
}
421-
let trailing_comment = e.trailing_comments.to_string();
442+
let trailing_comment = if self.config.with_comments {
443+
e.trailing_comments.as_str()
444+
} else {
445+
""
446+
};
422447
stream.push_str(&format! {
423448
r#"
424449
#[derive(Clone, PartialEq)]
@@ -625,7 +650,7 @@ where
625650
for file_id in mod_files.get(mod_path).unwrap().iter() {
626651
let file = this.file(*file_id).unwrap();
627652
// 2.1.1 comments
628-
if !file.comments.is_empty() {
653+
if !file.comments.is_empty() && this.config.with_comments {
629654
stream.push_str(&format!("\n{}\n", file.comments));
630655
}
631656

pilota-build/src/codegen/workspace.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ where
110110
members = [
111111
{members}
112112
]
113+
edition = "2024"
114+
resolver = "3"
113115
114116
[workspace.dependencies]
115117
pilota = "*"

pilota-build/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ pub struct Builder<MkB, P> {
8888
with_descriptor: bool,
8989
with_field_mask: bool,
9090
temp_dir: Option<tempfile::TempDir>,
91+
with_comments: bool,
9192
}
9293

9394
impl Builder<MkThriftBackend, ThriftParser> {
@@ -111,6 +112,7 @@ impl Builder<MkThriftBackend, ThriftParser> {
111112
with_descriptor: false,
112113
with_field_mask: false,
113114
temp_dir: None,
115+
with_comments: false,
114116
}
115117
}
116118
}
@@ -155,6 +157,7 @@ impl Builder<MkPbBackend, ProtobufParser> {
155157
with_descriptor: false,
156158
with_field_mask: false,
157159
temp_dir,
160+
with_comments: false,
158161
}
159162
}
160163
}
@@ -187,6 +190,7 @@ impl<MkB, P> Builder<MkB, P> {
187190
with_descriptor: self.with_descriptor,
188191
with_field_mask: self.with_field_mask,
189192
temp_dir: self.temp_dir,
193+
with_comments: self.with_comments,
190194
}
191195
}
192196

@@ -259,6 +263,14 @@ impl<MkB, P> Builder<MkB, P> {
259263
self.with_field_mask = on;
260264
self
261265
}
266+
267+
/**
268+
* Generate comments for the generated code
269+
*/
270+
pub fn with_comments(mut self, on: bool) -> Self {
271+
self.with_comments = on;
272+
self
273+
}
262274
}
263275

264276
pub enum Output {
@@ -319,6 +331,7 @@ where
319331
split: bool,
320332
with_descriptor: bool,
321333
with_field_mask: bool,
334+
with_comments: bool,
322335
) -> Context {
323336
parser.inputs(services.iter().map(|s| &s.path));
324337
let ParseResult {
@@ -409,6 +422,7 @@ where
409422
with_descriptor,
410423
with_field_mask,
411424
!ignore_unused,
425+
with_comments,
412426
)
413427
}
414428

@@ -430,6 +444,7 @@ where
430444
self.split,
431445
self.with_descriptor,
432446
self.with_field_mask,
447+
self.with_comments,
433448
);
434449

435450
cx.exec_plugin(BoxedPlugin);
@@ -517,6 +532,7 @@ where
517532
self.split,
518533
self.with_descriptor,
519534
self.with_field_mask,
535+
self.with_comments,
520536
);
521537

522538
std::thread::scope(|_scope| {

pilota-build/src/middle/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub struct Config {
7676
pub with_field_mask: bool,
7777
pub touch_all: bool,
7878
pub common_crate_name: FastStr,
79+
pub with_comments: bool,
7980
}
8081

8182
#[derive(Clone)]
@@ -469,6 +470,7 @@ impl ContextBuilder {
469470
with_descriptor: bool,
470471
with_field_mask: bool,
471472
touch_all: bool,
473+
with_comments: bool,
472474
) -> Context {
473475
let mode = Arc::new(self.mode);
474476
SPECIAL_NAMINGS.get_or_init(|| special_namings);
@@ -490,6 +492,7 @@ impl ContextBuilder {
490492
with_field_mask,
491493
touch_all,
492494
common_crate_name,
495+
with_comments,
493496
},
494497
cache: Cache {
495498
adjusts: Default::default(),
@@ -1734,6 +1737,7 @@ mod tests {
17341737
with_field_mask: false,
17351738
touch_all: false,
17361739
common_crate_name: "common".into(),
1740+
with_comments: false,
17371741
},
17381742
cache: Cache {
17391743
adjusts: Arc::new(DashMap::default()),

pilota-build/src/test/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ fn test_with_split_builder<F: FnOnce(&Path, &Path)>(
246246
fn test_thrift(source: impl AsRef<Path>, target: impl AsRef<Path>) {
247247
test_with_builder(source, target, |source, target| {
248248
crate::Builder::thrift()
249+
.with_comments(true)
249250
.ignore_unused(false)
250251
.compile_with_config(
251252
vec![IdlService::from_path(source.to_owned())],
@@ -265,6 +266,7 @@ fn test_thrift_workspace(
265266
.collect();
266267
test_with_builder_workspace(input_dir, output_dir, |_, target| {
267268
crate::Builder::thrift()
269+
.with_comments(true)
268270
.ignore_unused(false)
269271
.compile_with_config(services, crate::Output::Workspace(target.into()));
270272
});
@@ -281,6 +283,7 @@ fn test_thrift_workspace_with_split(
281283
.collect();
282284
test_with_builder_workspace(input_dir, output_dir, |_, target| {
283285
crate::Builder::thrift()
286+
.with_comments(true)
284287
.ignore_unused(false)
285288
.split_generated_files(true)
286289
.compile_with_config(services, crate::Output::Workspace(target.into()))
@@ -294,6 +297,7 @@ fn test_thrift_with_split(
294297
) {
295298
test_with_split_builder(source, target, gen_dir, |source, target| {
296299
crate::Builder::thrift()
300+
.with_comments(true)
297301
.ignore_unused(false)
298302
.split_generated_files(true)
299303
.compile_with_config(

pilota-build/test_data/plugin/serde.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ pub mod serde {
196196
+ __protocol.struct_end_len()
197197
}
198198
}
199-
200199
#[derive(
201200
PartialOrd,
202201
Hash,

pilota-build/test_data/thrift_workspace/output/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
[workspace]
2+
edition = "2024"
23
members = [
34
"article",
45
"author",
56
"common",
67
"image",
78
]
9+
resolver = "3"
810

911
[workspace.dependencies]
1012
anyhow = "1"

pilota-build/test_data/thrift_workspace_with_split/output/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
[workspace]
2+
edition = "2024"
23
members = [
34
"article",
45
"author",
56
"common",
67
"image",
78
]
9+
resolver = "3"
810

911
[workspace.dependencies]
1012
anyhow = "1"

0 commit comments

Comments
 (0)