use crate::command::command_with_output;
use crate::errors::{CargoMSRVError, TResult};
pub type ToolchainSpecifier = String;
pub fn is_toolchain_installed<S: AsRef<str>>(name: S) -> TResult<()> {
let toolchain = name.as_ref();
command_with_output(&["toolchain", "list"]).and_then(|child| {
let output = child.wait_with_output()?;
String::from_utf8(output.stdout)
.map_err(From::from)
.and_then(|string| {
let mut lines = string.lines();
if let Some(first) = lines.next() {
if let Some(default) = first.split_ascii_whitespace().next() {
if default == toolchain {
return Ok(());
}
}
}
for line in lines {
if line == toolchain {
return Ok(());
}
}
Err(CargoMSRVError::ToolchainNotInstalled)
})
})
}
pub fn is_target_available<S: AsRef<str>>(name: S) -> TResult<()> {
let toolchain = name.as_ref();
command_with_output(&["target", "list"]).and_then(|child| {
let output = child.wait_with_output()?;
String::from_utf8(output.stdout)
.map_err(From::from)
.and_then(|string| {
for line in string.lines() {
if let Some(it) = line.split_ascii_whitespace().next() {
if it == toolchain {
return Ok(());
}
}
}
Err(CargoMSRVError::UnknownTarget)
})
})
}
pub fn default_target() -> TResult<String> {
command_with_output(&["show"]).and_then(|child| {
let output = child.wait_with_output()?;
String::from_utf8(output.stdout)
.map_err(From::from)
.and_then(|string| {
string
.lines()
.next()
.ok_or(CargoMSRVError::DefaultHostTripleNotFound)
.and_then(|line| {
line.split_ascii_whitespace()
.nth(2)
.ok_or(CargoMSRVError::DefaultHostTripleNotFound)
.map(String::from)
})
})
})
}