Skip to content

Commit 56f03c1

Browse files
lopopoloemilio
authored andcommitted
Do not use which if which-rustfmt feature is disabled
This commit changes the API of rustfmt_path to return Result<Option<Cow<PathBuf>>>. Ok(None) is returned in the case where which is disabled and no rustfmt command is supplied explicitly either via configuration or env variable. Downstream code checks for the presence of None to directly return the source without emitting an error.
1 parent b8b1fe2 commit 56f03c1

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/lib.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern crate quote;
3232
extern crate proc_macro2;
3333
extern crate regex;
3434
extern crate shlex;
35+
#[cfg(feature = "which-rustfmt")]
3536
extern crate which;
3637

3738
#[cfg(feature = "logging")]
@@ -1900,18 +1901,21 @@ impl Bindings {
19001901
}
19011902

19021903
/// Gets the rustfmt path to rustfmt the generated bindings.
1903-
fn rustfmt_path<'a>(&'a self) -> io::Result<Cow<'a, PathBuf>> {
1904+
fn rustfmt_path<'a>(&'a self) -> io::Result<Option<Cow<'a, PathBuf>>> {
19041905
debug_assert!(self.options.rustfmt_bindings);
19051906
if let Some(ref p) = self.options.rustfmt_path {
1906-
return Ok(Cow::Borrowed(p));
1907+
return Ok(Some(Cow::Borrowed(p)));
19071908
}
19081909
if let Ok(rustfmt) = env::var("RUSTFMT") {
1909-
return Ok(Cow::Owned(rustfmt.into()));
1910+
return Ok(Some(Cow::Owned(rustfmt.into())));
19101911
}
1912+
#[cfg(feature = "which-rustfmt")]
19111913
match which::which("rustfmt") {
1912-
Ok(p) => Ok(Cow::Owned(p)),
1914+
Ok(p) => Ok(Some(Cow::Owned(p))),
19131915
Err(e) => Err(io::Error::new(io::ErrorKind::Other, format!("{}", e))),
19141916
}
1917+
#[cfg(not(feature = "which-rustfmt"))]
1918+
Ok(None)
19151919
}
19161920

19171921
/// Checks if rustfmt_bindings is set and runs rustfmt on the string
@@ -1926,7 +1930,11 @@ impl Bindings {
19261930
return Ok(Cow::Borrowed(source));
19271931
}
19281932

1929-
let rustfmt = self.rustfmt_path()?;
1933+
let rustfmt = if let Some(rustfmt) = self.rustfmt_path()? {
1934+
rustfmt
1935+
} else {
1936+
return Ok(Cow::Borrowed(source));
1937+
};
19301938
let mut cmd = Command::new(&*rustfmt);
19311939

19321940
cmd

0 commit comments

Comments
 (0)