From ee96a0430787e208ba41bc5af9304e2618e585ed Mon Sep 17 00:00:00 2001 From: Gabriel Hansson Date: Sun, 28 Apr 2024 19:29:34 +0200 Subject: [PATCH] fix: #1021 (#1022) --- prost-build/Cargo.toml | 1 + prost-build/src/config.rs | 23 ++++++++++------------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/prost-build/Cargo.toml b/prost-build/Cargo.toml index b108bff6e..cee09c096 100644 --- a/prost-build/Cargo.toml +++ b/prost-build/Cargo.toml @@ -33,6 +33,7 @@ tempfile = "3" once_cell = "1.17.1" regex = { version = "1.8.1", default-features = false, features = ["std", "unicode-bool"] } +# feature: format prettyplease = { version = "0.2", optional = true } syn = { version = "2", features = ["full"], optional = true } diff --git a/prost-build/src/config.rs b/prost-build/src/config.rs index 670c0befe..af57497f1 100644 --- a/prost-build/src/config.rs +++ b/prost-build/src/config.rs @@ -49,6 +49,7 @@ pub struct Config { pub(crate) skip_protoc_run: bool, pub(crate) include_file: Option, pub(crate) prost_path: Option, + #[cfg(feature = "format")] pub(crate) fmt: bool, } @@ -735,10 +736,12 @@ impl Config { self } + // IMPROVEMENT: https://github.com/tokio-rs/prost/pull/1022/files#r1563818651 /// Configures the code generator to format the output code via `prettyplease`. /// /// By default, this is enabled but if the `format` feature is not enabled this does /// nothing. + #[cfg(feature = "format")] pub fn format(&mut self, enabled: bool) -> &mut Self { self.fmt = enabled; self @@ -1056,8 +1059,13 @@ impl Config { } } + #[cfg(feature = "format")] if self.fmt { - self.fmt_modules(&mut modules); + for buf in modules.values_mut() { + let file = syn::parse_file(buf).unwrap(); + let formatted = prettyplease::unparse(&file); + *buf = formatted; + } } self.add_generated_modules(&mut modules); @@ -1071,18 +1079,6 @@ impl Config { *buf = with_generated; } } - - #[cfg(feature = "format")] - fn fmt_modules(&mut self, modules: &mut HashMap) { - for buf in modules.values_mut() { - let file = syn::parse_file(buf).unwrap(); - let formatted = prettyplease::unparse(&file); - *buf = formatted; - } - } - - #[cfg(not(feature = "format"))] - fn fmt_modules(&mut self, _: &mut HashMap) {} } impl default::Default for Config { @@ -1110,6 +1106,7 @@ impl default::Default for Config { skip_protoc_run: false, include_file: None, prost_path: None, + #[cfg(feature = "format")] fmt: true, } }