use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Platform {
Linux64,
MacX64,
MacArm64,
Win32,
Win64,
}
impl Platform {
pub fn current() -> Option<Self> {
match (std::env::consts::OS, std::env::consts::ARCH) {
("linux", "x86_64") => Some(Self::Linux64),
("macos", "x86_64") => Some(Self::MacX64),
("macos", "aarch64") => Some(Self::MacArm64),
("windows", "x86_64") => Some(Self::Win64),
("windows", "x86") => Some(Self::Win32),
_ => None,
}
}
pub fn key(&self) -> &'static str {
match self {
Self::Linux64 => "linux64",
Self::MacX64 => "mac-x64",
Self::MacArm64 => "mac-arm64",
Self::Win32 => "win32",
Self::Win64 => "win64",
}
}
pub fn zip_root(&self) -> &'static str {
match self {
Self::Linux64 => "chrome-headless-shell-linux64",
Self::MacX64 => "chrome-headless-shell-mac-x64",
Self::MacArm64 => "chrome-headless-shell-mac-arm64",
Self::Win32 => "chrome-headless-shell-win32",
Self::Win64 => "chrome-headless-shell-win64",
}
}
pub fn binary_name(&self) -> &'static str {
if matches!(self, Self::Win32 | Self::Win64) {
"chrome-headless-shell.exe"
} else {
"chrome-headless-shell"
}
}
pub fn needs_chmod(&self) -> bool {
!matches!(self, Self::Win32 | Self::Win64)
}
}
impl fmt::Display for Platform {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.key())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn binary_name_includes_exe_on_windows() {
assert_eq!(Platform::Win64.binary_name(), "chrome-headless-shell.exe");
assert_eq!(Platform::Linux64.binary_name(), "chrome-headless-shell");
}
#[test]
fn current_resolves_on_supported_hosts() {
let p = Platform::current();
if cfg!(any(
all(target_os = "linux", target_arch = "x86_64"),
all(target_os = "macos", target_arch = "x86_64"),
all(target_os = "macos", target_arch = "aarch64"),
all(target_os = "windows", target_arch = "x86_64"),
all(target_os = "windows", target_arch = "x86"),
)) {
assert!(p.is_some(), "expected a known platform on this host");
}
}
}