#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error("The lock used to work around https://github.com/rust-lang/rustup/issues/988 has been poisoned")]
StdSyncPoisonError,
#[error("`rustup toolchain install ...` failed for some reason")]
RustupToolchainInstallError,
}
pub type Result<T> = std::result::Result<T, Error>;
static RUSTUP_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());
pub fn install(toolchain: impl AsRef<str>) -> Result<()> {
if !is_installed(toolchain.as_ref())? {
run_rustup_install(toolchain)?;
}
Ok(())
}
#[deprecated(since = "0.1.4", note = "Renamed to `install()` for brevity.")]
pub fn ensure_installed(toolchain: &str) -> Result<()> {
install(toolchain)
}
pub fn is_installed(toolchain: &str) -> Result<bool> {
let _guard = RUSTUP_MUTEX.lock().map_err(|_| Error::StdSyncPoisonError)?;
Ok(std::process::Command::new("rustup")
.arg("run")
.arg(toolchain)
.arg("cargo")
.arg("--version")
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()?
.success())
}
fn run_rustup_install(toolchain: impl AsRef<str>) -> Result<()> {
let _guard = RUSTUP_MUTEX.lock().map_err(|_| Error::StdSyncPoisonError)?;
let status = std::process::Command::new("rustup")
.arg("toolchain")
.arg("install")
.arg("--no-self-update")
.arg("--profile")
.arg("minimal")
.arg(toolchain.as_ref())
.status()?;
if status.success() {
Ok(())
} else {
Err(Error::RustupToolchainInstallError)
}
}