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