1use crate::error::{Error, Result};
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum Platform {
9 LinuxX64,
10 LinuxArm64,
11 WindowsX64,
12 DarwinUniversal,
13}
14
15impl Platform {
16 pub fn detect() -> Result<Self> {
18 Self::from_os_arch(std::env::consts::OS, std::env::consts::ARCH)
19 }
20
21 pub(crate) fn from_os_arch(os: &'static str, arch: &'static str) -> Result<Self> {
22 match (os, arch) {
23 ("linux", "x86_64") => Ok(Platform::LinuxX64),
24 ("linux", "aarch64") => Ok(Platform::LinuxArm64),
25 ("windows", "x86_64") => Ok(Platform::WindowsX64),
26 ("macos", _) => Ok(Platform::DarwinUniversal),
28 _ => Err(Error::UnsupportedPlatform { os, arch }),
29 }
30 }
31
32 pub fn key(self) -> &'static str {
34 match self {
35 Platform::LinuxX64 => "linux-x64",
36 Platform::LinuxArm64 => "linux-arm64",
37 Platform::WindowsX64 => "windows-x64",
38 Platform::DarwinUniversal => "darwin-universal",
39 }
40 }
41
42 pub fn binary_name(self) -> &'static str {
44 match self {
45 Platform::WindowsX64 => "stackql.exe",
46 _ => "stackql",
47 }
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn known_platforms_map_to_keys() {
57 assert_eq!(
58 Platform::from_os_arch("linux", "x86_64").unwrap().key(),
59 "linux-x64"
60 );
61 assert_eq!(
62 Platform::from_os_arch("linux", "aarch64").unwrap().key(),
63 "linux-arm64"
64 );
65 assert_eq!(
66 Platform::from_os_arch("windows", "x86_64").unwrap().key(),
67 "windows-x64"
68 );
69 assert_eq!(
70 Platform::from_os_arch("macos", "aarch64").unwrap().key(),
71 "darwin-universal"
72 );
73 assert_eq!(
74 Platform::from_os_arch("macos", "x86_64").unwrap().key(),
75 "darwin-universal"
76 );
77 }
78
79 #[test]
80 fn unsupported_platform_is_an_error() {
81 assert!(Platform::from_os_arch("freebsd", "x86_64").is_err());
82 assert!(Platform::from_os_arch("windows", "aarch64").is_err());
83 }
84
85 #[test]
86 fn windows_binary_has_exe_suffix() {
87 assert_eq!(Platform::WindowsX64.binary_name(), "stackql.exe");
88 assert_eq!(Platform::LinuxX64.binary_name(), "stackql");
89 }
90}