diff options
author | Ryan Lopopolo <rjl@hyperbo.la> | 2019-09-05 15:41:42 -0700 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2019-09-17 09:35:52 +0900 |
commit | 56f03c182ffe807f5c9a4402ac9b2de4a6091208 (patch) | |
tree | f32a697dbaa5998fe2082e31b625926cc07a5ac9 | |
parent | b8b1fe2e41ef54db2e7221587669fd8188ed394c (diff) |
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.
-rw-r--r-- | src/lib.rs | 18 |
1 files changed, 13 insertions, 5 deletions
@@ -32,6 +32,7 @@ extern crate quote; extern crate proc_macro2; extern crate regex; extern crate shlex; +#[cfg(feature = "which-rustfmt")] extern crate which; #[cfg(feature = "logging")] @@ -1900,18 +1901,21 @@ impl Bindings { } /// Gets the rustfmt path to rustfmt the generated bindings. - fn rustfmt_path<'a>(&'a self) -> io::Result<Cow<'a, PathBuf>> { + fn rustfmt_path<'a>(&'a self) -> io::Result<Option<Cow<'a, PathBuf>>> { debug_assert!(self.options.rustfmt_bindings); if let Some(ref p) = self.options.rustfmt_path { - return Ok(Cow::Borrowed(p)); + return Ok(Some(Cow::Borrowed(p))); } if let Ok(rustfmt) = env::var("RUSTFMT") { - return Ok(Cow::Owned(rustfmt.into())); + return Ok(Some(Cow::Owned(rustfmt.into()))); } + #[cfg(feature = "which-rustfmt")] match which::which("rustfmt") { - Ok(p) => Ok(Cow::Owned(p)), + Ok(p) => Ok(Some(Cow::Owned(p))), Err(e) => Err(io::Error::new(io::ErrorKind::Other, format!("{}", e))), } + #[cfg(not(feature = "which-rustfmt"))] + Ok(None) } /// Checks if rustfmt_bindings is set and runs rustfmt on the string @@ -1926,7 +1930,11 @@ impl Bindings { return Ok(Cow::Borrowed(source)); } - let rustfmt = self.rustfmt_path()?; + let rustfmt = if let Some(rustfmt) = self.rustfmt_path()? { + rustfmt + } else { + return Ok(Cow::Borrowed(source)); + }; let mut cmd = Command::new(&*rustfmt); cmd |