Skip to content

Commit

Permalink
fix: #1021 (#1022)
Browse files Browse the repository at this point in the history
  • Loading branch information
gibbz00 committed Apr 28, 2024
1 parent b607e9b commit ee96a04
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
1 change: 1 addition & 0 deletions prost-build/Cargo.toml
Expand Up @@ -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 }

Expand Down
23 changes: 10 additions & 13 deletions prost-build/src/config.rs
Expand Up @@ -49,6 +49,7 @@ pub struct Config {
pub(crate) skip_protoc_run: bool,
pub(crate) include_file: Option<PathBuf>,
pub(crate) prost_path: Option<String>,
#[cfg(feature = "format")]
pub(crate) fmt: bool,
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -1071,18 +1079,6 @@ impl Config {
*buf = with_generated;
}
}

#[cfg(feature = "format")]
fn fmt_modules(&mut self, modules: &mut HashMap<Module, String>) {
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<Module, String>) {}
}

impl default::Default for Config {
Expand Down Expand Up @@ -1110,6 +1106,7 @@ impl default::Default for Config {
skip_protoc_run: false,
include_file: None,
prost_path: None,
#[cfg(feature = "format")]
fmt: true,
}
}
Expand Down

0 comments on commit ee96a04

Please sign in to comment.