diff --git a/Cargo.lock b/Cargo.lock index c5e4f74..38826cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "aho-corasick" @@ -29,6 +29,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "env_home" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f84e12ccf0a7ddc17a6c41c93326024c42920d7ee630d04950e6926645c0fe" + [[package]] name = "equivalent" version = "1.0.0" @@ -37,9 +43,9 @@ checksum = "88bffebc5d80432c9b140ee17875ff173a8ab62faad5b257da912bd2f6c1c0a1" [[package]] name = "errno" -version = "0.3.8" +version = "0.3.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", "windows-sys", @@ -78,9 +84,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.151" +version = "0.2.177" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" +checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976" [[package]] name = "linux-raw-sys" @@ -88,6 +94,12 @@ version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4cd1a83af159aa67994778be9070f0ae1bd732942279cabb14f86f986a21456" +[[package]] +name = "linux-raw-sys" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" + [[package]] name = "memchr" version = "2.5.0" @@ -164,6 +176,7 @@ dependencies = [ "thiserror", "toml", "toolchain_find", + "which", ] [[package]] @@ -175,7 +188,20 @@ dependencies = [ "bitflags 2.4.1", "errno", "libc", - "linux-raw-sys", + "linux-raw-sys 0.4.12", + "windows-sys", +] + +[[package]] +name = "rustix" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd15f8a2c5551a84d56efdc1cd049089e409ac19a3072d5037a17fd70719ff3e" +dependencies = [ + "bitflags 2.4.1", + "errno", + "libc", + "linux-raw-sys 0.11.0", "windows-sys", ] @@ -243,7 +269,7 @@ dependencies = [ "cfg-if", "fastrand", "redox_syscall", - "rustix", + "rustix 0.38.28", "windows-sys", ] @@ -337,6 +363,17 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "which" +version = "8.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fabb953106c3c8eea8306e4393700d7657561cb43122571b172bbfb7c7ba1d" +dependencies = [ + "env_home", + "rustix 1.1.2", + "winsafe", +] + [[package]] name = "winapi" version = "0.3.9" @@ -442,3 +479,9 @@ checksum = "7c2e3184b9c4e92ad5167ca73039d0c42476302ab603e2fec4487511f38ccefc" dependencies = [ "memchr", ] + +[[package]] +name = "winsafe" +version = "0.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" diff --git a/Cargo.toml b/Cargo.toml index de6daab..09d903e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ tempfile = "3.9.0" thiserror = "1.0.55" toml = "0.8.8" toolchain_find = "0.4.0" +which = "8.0.0" [dev-dependencies] quote = "1.0.35" diff --git a/src/lib.rs b/src/lib.rs index ac0cfca..18576e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -67,7 +67,7 @@ pub fn rustfmt_config(mut config: config::Config, input: T) -> Resu toml::to_string_pretty(&config).unwrap(), )?; - let rustfmt = which_rustfmt().ok_or(Error::NoRustfmt)?; + let rustfmt = locate_rustfmt().ok_or(Error::NoRustfmt)?; let mut args = vec![format!("--config-path={}", outdir.path().to_str().unwrap())]; if config.unstable() { @@ -102,17 +102,13 @@ pub fn rustfmt_config(mut config: config::Config, input: T) -> Resu } } -fn which_rustfmt() -> Option { - match env::var_os("RUSTFMT") { - Some(which) => { - if which.is_empty() { - None - } else { - Some(PathBuf::from(which)) - } - } - None => toolchain_find::find_installed_component("rustfmt"), - } +fn locate_rustfmt() -> Option { + env::var_os("RUSTFMT") + .filter(|s| !s.is_empty()) + .map(PathBuf::from) + .or_else(|| toolchain_find::find_installed_component("rustfmt")) + .or_else(|| which::which("rustfmt").ok()) + .and_then(|p| p.canonicalize().ok()) } #[cfg(test)]