use crate::error::Error;
use guess_host_triple::guess_host_triple;
use miette::Result;
use std::str::FromStr;
use strum::{Display, EnumString};
#[derive(Display, Debug, Clone, EnumString, Default)]
pub enum HostTriple {
#[strum(serialize = "x86_64-unknown-linux-gnu")]
#[default]
X86_64UnknownLinuxGnu,
#[strum(serialize = "aarch64-unknown-linux-gnu")]
Aarch64UnknownLinuxGnu,
#[strum(serialize = "x86_64-pc-windows-msvc")]
X86_64PcWindowsMsvc,
#[strum(serialize = "x86_64-pc-windows-gnu")]
X86_64PcWindowsGnu,
#[strum(serialize = "x86_64-apple-darwin")]
X86_64AppleDarwin,
#[strum(serialize = "aarch64-apple-darwin")]
Aarch64AppleDarwin,
}
pub fn get_host_triple(host_triple_arg: Option<String>) -> Result<HostTriple, Error> {
let host_triple = if let Some(host_triple) = &host_triple_arg {
host_triple
} else {
guess_host_triple().unwrap()
};
HostTriple::from_str(host_triple).map_err(|_| Error::UnsupportedHostTriple(host_triple.into()))
}
#[cfg(test)]
mod tests {
use crate::host_triple::{get_host_triple, HostTriple};
#[test]
fn test_get_host_triple() {
assert!(matches!(
get_host_triple(Some("x86_64-unknown-linux-gnu".to_string())),
Ok(HostTriple::X86_64UnknownLinuxGnu)
));
assert!(matches!(
get_host_triple(Some("aarch64-unknown-linux-gnu".to_string())),
Ok(HostTriple::Aarch64UnknownLinuxGnu)
));
assert!(matches!(
get_host_triple(Some("x86_64-pc-windows-msvc".to_string())),
Ok(HostTriple::X86_64PcWindowsMsvc)
));
assert!(matches!(
get_host_triple(Some("x86_64-pc-windows-gnu".to_string())),
Ok(HostTriple::X86_64PcWindowsGnu)
));
assert!(matches!(
get_host_triple(Some("x86_64-apple-darwin".to_string())),
Ok(HostTriple::X86_64AppleDarwin)
));
assert!(matches!(
get_host_triple(Some("aarch64-apple-darwin".to_string())),
Ok(HostTriple::Aarch64AppleDarwin)
));
assert!(get_host_triple(Some("some-fake-triple".to_string())).is_err());
#[cfg(all(target_os = "linux", target_arch = "aarch64"))]
assert!(matches!(
get_host_triple(None),
Ok(HostTriple::Aarch64UnknownLinuxGnu)
));
#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
assert!(matches!(
get_host_triple(None),
Ok(HostTriple::X86_64UnknownLinuxGnu)
));
#[cfg(all(target_os = "windows", target_arch = "x86_64", target_env = "msvc"))]
assert!(matches!(
get_host_triple(None),
Ok(HostTriple::X86_64PcWindowsMsvc)
));
#[cfg(all(target_os = "windows", target_arch = "x86_64", target_env = "gnu"))]
assert!(matches!(
get_host_triple(None),
Ok(HostTriple::X86_64PcWindowsGnu)
));
#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
assert!(matches!(
get_host_triple(None),
Ok(HostTriple::X86_64AppleDarwin)
));
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
assert!(matches!(
get_host_triple(None),
Ok(HostTriple::Aarch64AppleDarwin)
));
}
}