Skip to content

Commit 2dc46ac

Browse files
committed
lib: Add a way to override rustfmt path.
I'll need it to format some stuff on mozilla-central.
1 parent 56bbbb0 commit 2dc46ac

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/lib.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,12 @@ impl Builder {
10791079
self
10801080
}
10811081

1082+
/// Sets an explicit path to rustfmt, to be used when rustfmt is enabled.
1083+
pub fn with_rustfmt<P: Into<PathBuf>>(mut self, path: P) -> Self {
1084+
self.options.rustfmt_path = Some(path.into());
1085+
self
1086+
}
1087+
10821088
/// Generate the Rust bindings using the options built up thus far.
10831089
pub fn generate(mut self) -> Result<Bindings, ()> {
10841090
self.options.input_header = self.input_headers.pop();
@@ -1216,6 +1222,9 @@ struct BindgenOptions {
12161222
/// generated code.
12171223
opaque_types: RegexSet,
12181224

1225+
/// The explicit rustfmt path.
1226+
rustfmt_path: Option<PathBuf>,
1227+
12191228
/// The set of types that we should have bindings for in the generated
12201229
/// code.
12211230
///
@@ -1441,6 +1450,7 @@ impl Default for BindgenOptions {
14411450
rust_features: rust_target.into(),
14421451
blacklisted_types: Default::default(),
14431452
opaque_types: Default::default(),
1453+
rustfmt_path: Default::default(),
14441454
whitelisted_types: Default::default(),
14451455
whitelisted_functions: Default::default(),
14461456
whitelisted_vars: Default::default(),
@@ -1709,10 +1719,19 @@ impl Bindings {
17091719
return Ok(Cow::Borrowed(source));
17101720
}
17111721

1712-
let rustfmt = which::which("rustfmt")
1713-
.map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_owned()))?;
1722+
let rustfmt = match self.options.rustfmt_path {
1723+
Some(ref p) => Cow::Borrowed(p),
1724+
None => {
1725+
let path = which::which("rustfmt")
1726+
.map_err(|e| {
1727+
io::Error::new(io::ErrorKind::Other, e.to_owned())
1728+
})?;
1729+
1730+
Cow::Owned(path)
1731+
}
1732+
};
17141733

1715-
let mut cmd = Command::new(rustfmt);
1734+
let mut cmd = Command::new(&*rustfmt);
17161735

17171736
cmd
17181737
.stdin(Stdio::piped())

0 commit comments

Comments
 (0)