sandbox_runtime/utils/
platform.rs

1//! Platform detection utilities.
2
3/// Supported platforms.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum Platform {
6    MacOS,
7    Linux,
8}
9
10impl Platform {
11    /// Detect the current platform.
12    pub fn current() -> Option<Self> {
13        #[cfg(target_os = "macos")]
14        {
15            Some(Platform::MacOS)
16        }
17        #[cfg(target_os = "linux")]
18        {
19            Some(Platform::Linux)
20        }
21        #[cfg(not(any(target_os = "macos", target_os = "linux")))]
22        {
23            None
24        }
25    }
26
27    /// Check if the current platform is supported.
28    pub fn is_supported() -> bool {
29        Self::current().is_some()
30    }
31
32    /// Get the platform name as a string.
33    pub fn name(&self) -> &'static str {
34        match self {
35            Platform::MacOS => "macOS",
36            Platform::Linux => "Linux",
37        }
38    }
39}
40
41/// Get the current platform, if supported.
42pub fn current_platform() -> Option<Platform> {
43    Platform::current()
44}
45
46/// Check if running on macOS.
47#[inline]
48pub fn is_macos() -> bool {
49    cfg!(target_os = "macos")
50}
51
52/// Check if running on Linux.
53#[inline]
54pub fn is_linux() -> bool {
55    cfg!(target_os = "linux")
56}
57
58/// Get the CPU architecture.
59pub fn get_arch() -> &'static str {
60    #[cfg(target_arch = "x86_64")]
61    {
62        "x64"
63    }
64    #[cfg(target_arch = "aarch64")]
65    {
66        "arm64"
67    }
68    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
69    {
70        "unknown"
71    }
72}