use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Platform {
X86_64Linux,
Aarch64Linux,
X86_64Darwin,
Aarch64Darwin,
X86_64Windows,
}
impl Platform {
pub fn current() -> Option<Self> {
let arch = std::env::consts::ARCH;
let os = std::env::consts::OS;
match (arch, os) {
("x86_64", "linux") => Some(Self::X86_64Linux),
("aarch64", "linux") => Some(Self::Aarch64Linux),
("x86_64", "macos") => Some(Self::X86_64Darwin),
("aarch64", "macos") => Some(Self::Aarch64Darwin),
("x86_64", "windows") => Some(Self::X86_64Windows),
_ => None,
}
}
pub fn url_suffix(&self) -> &'static str {
match self {
Self::X86_64Linux => "x86_64-unknown-linux",
Self::Aarch64Linux => "aarch64-unknown-linux",
Self::X86_64Darwin => "x86_64-apple-darwin",
Self::Aarch64Darwin => "aarch64-apple-darwin",
Self::X86_64Windows => "x86_64-unknown-mingw32",
}
}
pub fn bindist_suffix(&self) -> &'static str {
match self {
Self::X86_64Linux => "x86_64-deb10-linux",
Self::Aarch64Linux => "aarch64-deb10-linux",
_ => self.url_suffix(),
}
}
pub fn archive_extension(&self) -> &'static str {
"xz"
}
pub fn display_name(&self) -> &'static str {
match self {
Self::X86_64Linux => "x86_64-linux",
Self::Aarch64Linux => "aarch64-linux",
Self::X86_64Darwin => "x86_64-macos",
Self::Aarch64Darwin => "aarch64-macos (Apple Silicon)",
Self::X86_64Windows => "x86_64-windows",
}
}
pub fn is_unix(&self) -> bool {
!matches!(self, Self::X86_64Windows)
}
}
impl std::fmt::Display for Platform {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.display_name())
}
}
pub fn ghc_download_url(version: &str, platform: Platform) -> String {
format!(
"https://downloads.haskell.org/~ghc/{version}/ghc-{version}-{suffix}.tar.{ext}",
version = version,
suffix = platform.bindist_suffix(),
ext = platform.archive_extension()
)
}
pub fn ghc_archive_filename(version: &str, platform: Platform) -> String {
format!(
"ghc-{version}-{suffix}.tar.{ext}",
version = version,
suffix = platform.bindist_suffix(),
ext = platform.archive_extension()
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_platform_detection() {
let platform = Platform::current();
if cfg!(target_os = "linux") || cfg!(target_os = "macos") || cfg!(target_os = "windows") {
assert!(platform.is_some());
}
}
#[test]
fn test_url_suffix() {
assert_eq!(Platform::X86_64Linux.url_suffix(), "x86_64-unknown-linux");
assert_eq!(Platform::Aarch64Darwin.url_suffix(), "aarch64-apple-darwin");
assert_eq!(
Platform::X86_64Windows.url_suffix(),
"x86_64-unknown-mingw32"
);
}
#[test]
fn test_bindist_suffix() {
assert_eq!(Platform::X86_64Linux.bindist_suffix(), "x86_64-deb10-linux");
assert_eq!(
Platform::Aarch64Linux.bindist_suffix(),
"aarch64-deb10-linux"
);
assert_eq!(
Platform::Aarch64Darwin.bindist_suffix(),
Platform::Aarch64Darwin.url_suffix()
);
}
#[test]
fn test_ghc_download_url() {
let url = ghc_download_url("9.8.2", Platform::X86_64Darwin);
assert_eq!(
url,
"https://downloads.haskell.org/~ghc/9.8.2/ghc-9.8.2-x86_64-apple-darwin.tar.xz"
);
let url = ghc_download_url("9.10.3", Platform::Aarch64Linux);
assert_eq!(
url,
"https://downloads.haskell.org/~ghc/9.10.3/ghc-9.10.3-aarch64-deb10-linux.tar.xz"
);
let url = ghc_download_url("9.10.3", Platform::X86_64Linux);
assert_eq!(
url,
"https://downloads.haskell.org/~ghc/9.10.3/ghc-9.10.3-x86_64-deb10-linux.tar.xz"
);
}
#[test]
fn test_is_unix() {
assert!(Platform::X86_64Linux.is_unix());
assert!(Platform::X86_64Darwin.is_unix());
assert!(!Platform::X86_64Windows.is_unix());
}
}