Skip to content

Commit 26d672e

Browse files
authored
Restore prettyplease feature gate to bindgen library crate (#2537)
This is a partial revert of these two PRs: - #2491 - #2505 Allow formatting of generated bindings and the inclusion of the `prettyplease` dependency to be optional when depending on bindgen as a library. `prettyplease` remains required and enabled by bindgen-cli. In a project I maintain, `bindgen` is used in a build script to generate bindings for a native C library. Those bindings are written to `OUT_DIR` and are `include!`'d into the crate sources; these bindings don't need to be formatted. See for additional context: - #2491 (comment) - #2491 (comment) - #2491 (comment) I tested all of the following locally: ```shell cd bindgen cargo check cargo check --no-default-features cargo check --no-default-features --features prettyplease ``` The CI steps added in #2506 should be sufficient for maintaining the correctness of these conditional compilation features.
1 parent df984e2 commit 26d672e

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@
184184
them. To make the escaping clear and consistent, backslashes are also
185185
escaped.
186186
* Updated `bitflags` dependency to 2.2.1. This changes the API of `CodegenConfig`.
187-
187+
* Prettyplease formatting is gated by an optional, enabled by default Cargo
188+
feature when depending on `bindgen` as a library.
189+
188190
## Removed
189191

190192
## Fixed

bindgen/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ quote = { version = "1", default-features = false }
3535
syn = { version = "2.0", features = ["full", "extra-traits", "visit-mut"]}
3636
regex = { version = "1.5", default-features = false , features = ["std", "unicode"] }
3737
which = { version = "4.2.1", optional = true, default-features = false }
38-
prettyplease = { version = "0.2.0" }
38+
prettyplease = { version = "0.2.0", optional = true }
3939
annotate-snippets = { version = "0.9.1", features = ["color"], optional = true }
4040
shlex = "1"
4141
rustc-hash = "1.0.1"
4242
proc-macro2 = { version = "1", default-features = false }
4343
log = { version = "0.4", optional = true }
4444

4545
[features]
46-
default = ["logging", "runtime", "which-rustfmt"]
46+
default = ["logging", "prettyplease", "runtime", "which-rustfmt"]
4747
logging = ["log"]
4848
static = ["clang-sys/static"]
4949
runtime = ["clang-sys/runtime"]

bindgen/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ pub enum Formatter {
172172
None,
173173
/// Use `rustfmt` to format the bindings.
174174
Rustfmt,
175+
#[cfg(feature = "prettyplease")]
175176
/// Use `prettyplease` to format the bindings.
176177
Prettyplease,
177178
}
@@ -189,6 +190,7 @@ impl FromStr for Formatter {
189190
match s {
190191
"none" => Ok(Self::None),
191192
"rustfmt" => Ok(Self::Rustfmt),
193+
#[cfg(feature = "prettyplease")]
192194
"prettyplease" => Ok(Self::Prettyplease),
193195
_ => Err(format!("`{}` is not a valid formatter", s)),
194196
}
@@ -200,6 +202,7 @@ impl std::fmt::Display for Formatter {
200202
let s = match self {
201203
Self::None => "none",
202204
Self::Rustfmt => "rustfmt",
205+
#[cfg(feature = "prettyplease")]
203206
Self::Prettyplease => "prettyplease",
204207
};
205208

@@ -964,6 +967,7 @@ impl Bindings {
964967

965968
match self.options.formatter {
966969
Formatter::None => return Ok(tokens.to_string()),
970+
#[cfg(feature = "prettyplease")]
967971
Formatter::Prettyplease => {
968972
return Ok(prettyplease::unparse(&syn::parse_quote!(#tokens)));
969973
}

0 commit comments

Comments
 (0)