Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 50 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 8 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn rustfmt_config<T: ToString>(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() {
Expand Down Expand Up @@ -102,17 +102,13 @@ pub fn rustfmt_config<T: ToString>(mut config: config::Config, input: T) -> Resu
}
}

fn which_rustfmt() -> Option<PathBuf> {
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<PathBuf> {
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)]
Expand Down