pub fn os() -> &'static str {
match std::env::consts::OS {
"macos" => "macos",
"linux" => "linux",
"windows" => "windows",
"freebsd" => "freebsd",
"openbsd" => "openbsd",
"netbsd" => "netbsd",
"dragonfly" => "dragonfly",
"ios" => "ios",
"android" => "android",
other => other,
}
}
pub fn arch() -> &'static str {
match std::env::consts::ARCH {
"x86_64" => "x64",
"aarch64" => "arm64",
"x86" | "i686" | "i386" => "x86",
"arm" => "arm",
"powerpc64" => "ppc64",
"powerpc" => "ppc",
"s390x" => "s390x",
"riscv64" => "riscv64",
"mips64" => "mips64",
"mips" => "mips",
other => other,
}
}
pub fn id() -> String {
format!("{}-{}", os(), arch())
}
pub fn is_macos() -> bool {
cfg!(target_os = "macos")
}
pub fn is_linux() -> bool {
cfg!(target_os = "linux")
}
pub fn is_windows() -> bool {
cfg!(target_os = "windows")
}
pub fn is_unix() -> bool {
cfg!(unix)
}
pub fn is_64bit() -> bool {
cfg!(target_pointer_width = "64")
}
pub fn is_arm() -> bool {
cfg!(any(target_arch = "aarch64", target_arch = "arm"))
}
pub fn is_x86() -> bool {
cfg!(any(target_arch = "x86_64", target_arch = "x86"))
}
pub fn os_family() -> &'static str {
std::env::consts::FAMILY
}
pub fn exe_suffix() -> &'static str {
std::env::consts::EXE_SUFFIX
}
pub fn dll_suffix() -> &'static str {
std::env::consts::DLL_SUFFIX
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_os() {
let os = os();
assert!(!os.is_empty());
assert!(
["macos", "linux", "windows", "freebsd", "openbsd"].contains(&os) || !os.is_empty()
);
}
#[test]
fn test_arch() {
let arch = arch();
assert!(!arch.is_empty());
assert!(
["x64", "arm64", "x86", "arm", "ppc64", "riscv64"].contains(&arch) || !arch.is_empty()
);
}
#[test]
fn test_id() {
let id = id();
assert!(id.contains('-'));
let parts: Vec<&str> = id.split('-').collect();
assert_eq!(parts.len(), 2);
}
#[test]
fn test_os_family() {
let family = os_family();
assert!(family == "unix" || family == "windows");
}
#[test]
fn test_is_64bit() {
#[cfg(target_pointer_width = "64")]
assert!(is_64bit());
#[cfg(target_pointer_width = "32")]
assert!(!is_64bit());
}
#[test]
fn test_is_unix() {
#[cfg(unix)]
assert!(is_unix());
#[cfg(not(unix))]
assert!(!is_unix());
}
#[test]
fn test_exe_suffix() {
let suffix = exe_suffix();
#[cfg(windows)]
assert_eq!(suffix, ".exe");
#[cfg(not(windows))]
assert_eq!(suffix, "");
}
}