svm/
platform.rs

1use std::fmt::Formatter;
2use std::str::FromStr;
3use std::{env, fmt};
4
5/// Types of supported platforms.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum Platform {
9    LinuxAmd64,
10    LinuxAarch64,
11    MacOsAmd64,
12    MacOsAarch64,
13    WindowsAmd64,
14    WindowsAarch64,
15    AndroidAarch64,
16    Unsupported,
17}
18
19impl fmt::Display for Platform {
20    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
21        let s = match self {
22            Self::LinuxAmd64 => "linux-amd64",
23            Self::LinuxAarch64 => "linux-aarch64",
24            Self::MacOsAmd64 => "macosx-amd64",
25            Self::MacOsAarch64 => "macosx-aarch64",
26            Self::WindowsAmd64 => "windows-amd64",
27            Self::WindowsAarch64 => "windows-aarch64",
28            Self::AndroidAarch64 => "android-aarch64",
29            Self::Unsupported => "Unsupported-platform",
30        };
31        f.write_str(s)
32    }
33}
34
35impl FromStr for Platform {
36    type Err = String;
37
38    fn from_str(s: &str) -> Result<Self, Self::Err> {
39        match s {
40            "linux-amd64" => Ok(Self::LinuxAmd64),
41            "linux-aarch64" => Ok(Self::LinuxAarch64),
42            "macosx-amd64" => Ok(Self::MacOsAmd64),
43            "macosx-aarch64" => Ok(Self::MacOsAarch64),
44            "windows-amd64" => Ok(Self::WindowsAmd64),
45            "windows-aarch64" => Ok(Self::WindowsAarch64),
46            "android-aarch64" => Ok(Self::AndroidAarch64),
47            s => Err(format!("unsupported platform {s}")),
48        }
49    }
50}
51
52pub fn is_nixos() -> bool {
53    cfg!(target_os = "linux")
54        && (std::path::Path::new("/etc/nixos").exists()
55            || std::path::Path::new("/etc/NIXOS").exists())
56        && std::fs::read_to_string("/etc/os-release").is_ok_and(|s| s.contains("NixOS"))
57}
58
59/// Read the current machine's platform.
60pub fn platform() -> Platform {
61    match (env::consts::OS, env::consts::ARCH) {
62        ("linux", "x86_64") => Platform::LinuxAmd64,
63        ("linux", "aarch64") => Platform::LinuxAarch64,
64        ("macos", "x86_64") => Platform::MacOsAmd64,
65        ("macos", "aarch64") => Platform::MacOsAarch64,
66        ("windows", "x86_64") => Platform::WindowsAmd64,
67        ("windows", "aarch64") => Platform::WindowsAarch64,
68        ("android", "aarch64") => Platform::AndroidAarch64,
69        _ => Platform::Unsupported,
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    #[cfg(all(target_os = "linux", target_arch = "x86_64"))]
79    fn get_platform() {
80        assert_eq!(platform(), Platform::LinuxAmd64);
81    }
82
83    #[test]
84    #[cfg(all(target_os = "linux", target_arch = "aarch64"))]
85    fn get_platform() {
86        assert_eq!(platform(), Platform::LinuxAarch64);
87    }
88
89    #[test]
90    #[cfg(all(target_os = "macos", target_arch = "x86_64"))]
91    fn get_platform() {
92        assert_eq!(platform(), Platform::MacOsAmd64);
93    }
94
95    #[test]
96    #[cfg(all(target_os = "macos", target_arch = "aarch64"))]
97    fn get_platform() {
98        assert_eq!(platform(), Platform::MacOsAarch64);
99    }
100
101    #[test]
102    #[cfg(all(target_os = "windows", target_arch = "x86_64"))]
103    fn get_platform() {
104        assert_eq!(platform(), Platform::WindowsAmd64);
105    }
106
107    #[test]
108    #[cfg(all(target_os = "windows", target_arch = "aarch64"))]
109    fn get_platform() {
110        assert_eq!(platform(), Platform::WindowsAarch64);
111    }
112
113    #[test]
114    #[cfg(all(target_os = "android", target_arch = "aarch64"))]
115    fn get_platform() {
116        assert_eq!(platform(), Platform::AndroidAarch64);
117    }
118}