Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Place Config::format behind the format feature flag. #1022

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions prost-build/Cargo.toml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading