Skip to content

Commit c24661e

Browse files
authored
Merge pull request #31 from EmbarkStudios/specify-edition
Specify edition in CLI
2 parents 0af2e60 + 3e6cd0a commit c24661e

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

proto-gen/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
<!-- next-header -->
1111
## [Unreleased] - ReleaseDate
12+
### Changed
13+
- [PR#31](https://github.com/EmbarkStudios/proto-gen/pull/31) changed the `--format` flag to take a rust edition as an argument, as previously the 2021 edition was hardcoded.
14+
1215
## [0.2.11] - 2025-01-23
1316
### Fixed
1417
- [PR#30](https://github.com/EmbarkStudios/proto-gen/pull/30) Improvements and fixes to the readme.
18+
1519
## [0.2.10] - 2025-01-16
1620
### Fixed
1721
- Update dependency `anstream` to `0.6.15` for advisory: <https://rustsec.org/advisories/RUSTSEC-2024-0404>
1822
- Build fixes that prevented v `0.2.9` from being fully released
23+
1924
## [0.2.9] - 2025-01-16
2025
### Added
2126
- [PR#27](https://github.com/EmbarkStudios/proto-gen/pull/27) Added `--prepend-header-file` option for specifying a file which will be prepended as a header in generated source files.

proto-gen/src/gen.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ pub fn run_generation(
2727
})?;
2828
let old = &proto_ws.output_dir;
2929
let new = &proto_ws.tmp_dir;
30-
if gen_opts.format {
31-
recurse_fmt(new)?;
32-
top_mod_content = fmt(&top_mod_content)?;
30+
if let Some(edition) = gen_opts.format.as_deref() {
31+
recurse_fmt(new, edition)?;
32+
top_mod_content = fmt(&top_mod_content, edition)?;
3333
}
3434
let diff = run_diff(old, new, &top_mod_content)?;
3535
if diff > 0 {
@@ -65,7 +65,7 @@ pub struct ProtoWorkspace {
6565
#[derive(Debug)]
6666
pub struct GenOptions {
6767
pub commit: bool,
68-
pub format: bool,
68+
pub format: Option<String>,
6969
pub prepend_header: Option<String>,
7070
pub toplevel_attribute: Option<String>,
7171
}
@@ -506,7 +506,7 @@ fn path_from_starts_with(root: &str, path: impl AsRef<Path> + Debug) -> Result<P
506506
Ok(pb)
507507
}
508508

509-
fn recurse_fmt(base: impl AsRef<Path>) -> Result<(), String> {
509+
fn recurse_fmt(base: impl AsRef<Path>, edition: &str) -> Result<(), String> {
510510
let path = base.as_ref();
511511
for file in
512512
fs::read_dir(path).map_err(|e| format!("failed to read_dir for path {path:?} \n{e}"))?
@@ -520,7 +520,7 @@ fn recurse_fmt(base: impl AsRef<Path>) -> Result<(), String> {
520520
let out = std::process::Command::new("rustfmt")
521521
.arg(&path)
522522
.arg("--edition")
523-
.arg("2021")
523+
.arg(edition)
524524
.output()
525525
.map_err(|e| format!("Failed to format generated code \n{e}"))?;
526526
if !out.status.success() {
@@ -531,19 +531,19 @@ fn recurse_fmt(base: impl AsRef<Path>) -> Result<(), String> {
531531
));
532532
}
533533
} else if metadata.is_dir() {
534-
recurse_fmt(path)?;
534+
recurse_fmt(path, edition)?;
535535
}
536536
}
537537
Ok(())
538538
}
539539

540-
fn fmt(code: &str) -> Result<String, String> {
540+
fn fmt(code: &str, edition: &str) -> Result<String, String> {
541541
use std::io::Write;
542542
use std::process::Stdio;
543543

544544
let mut child = std::process::Command::new("rustfmt")
545545
.arg("--edition")
546-
.arg("2021")
546+
.arg(edition)
547547
.stdin(Stdio::piped())
548548
.stdout(Stdio::piped())
549549
.spawn()

proto-gen/src/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ struct Opts {
2727
#[clap(flatten)]
2828
tonic: TonicOpts,
2929

30-
/// Use `rustfmt` on the code after generation, `rustfmt` needs to be on the path.
31-
#[clap(short, long)]
32-
format: bool,
30+
/// Run rustfmt on the generated code with the specified edition
31+
#[clap(short, long, value_name = "EDITION")]
32+
format: Option<String>,
3333

3434
/// Prepend header indicating tool version in generated source files.
3535
#[clap(short, long, default_value_t = false)]
@@ -301,7 +301,7 @@ message TestMessage {
301301
let test_cfg = create_simple_test_cfg(None);
302302
let opts = Opts {
303303
tonic: test_cfg.tonic.clone(),
304-
format: true,
304+
format: Some("2021".into()),
305305
routine: Routine::Generate {
306306
workspace: test_cfg.workspace.clone(),
307307
},
@@ -313,7 +313,7 @@ message TestMessage {
313313
run_with_opts(opts).unwrap();
314314
let opts = Opts {
315315
tonic: test_cfg.tonic.clone(),
316-
format: true,
316+
format: Some("2021".into()),
317317
routine: Routine::Validate {
318318
workspace: test_cfg.workspace.clone(),
319319
},
@@ -325,7 +325,7 @@ message TestMessage {
325325
run_with_opts(opts).unwrap();
326326
let opts = Opts {
327327
tonic: test_cfg.tonic.clone(),
328-
format: false,
328+
format: None,
329329
routine: Routine::Validate {
330330
workspace: test_cfg.workspace,
331331
},
@@ -348,7 +348,7 @@ message TestMessage {
348348
let test_cfg = create_simple_test_cfg(Some(my_output_tmp.path().to_path_buf()));
349349
let opts = Opts {
350350
tonic: test_cfg.tonic.clone(),
351-
format: false,
351+
format: None,
352352
routine: Routine::Generate {
353353
workspace: test_cfg.workspace,
354354
},
@@ -444,7 +444,7 @@ message NestedTransitiveMsg {
444444
};
445445
let opts = Opts {
446446
tonic,
447-
format: false,
447+
format: None,
448448
routine: Routine::Generate { workspace },
449449
prepend_header: true,
450450
prepend_header_file: None,

0 commit comments

Comments
 (0)