Skip to main content

sal_os/
platform.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum PlatformError {
5    #[error("{0}: {1}")]
6    Generic(String, String),
7}
8
9impl PlatformError {
10    pub fn new(kind: &str, message: &str) -> Self {
11        PlatformError::Generic(kind.to_string(), message.to_string())
12    }
13}
14
15#[cfg(target_os = "macos")]
16pub fn is_osx() -> bool {
17    true
18}
19
20#[cfg(not(target_os = "macos"))]
21pub fn is_osx() -> bool {
22    false
23}
24
25#[cfg(target_os = "linux")]
26pub fn is_linux() -> bool {
27    true
28}
29
30#[cfg(not(target_os = "linux"))]
31pub fn is_linux() -> bool {
32    false
33}
34
35#[cfg(target_arch = "aarch64")]
36pub fn is_arm() -> bool {
37    true
38}
39
40#[cfg(not(target_arch = "aarch64"))]
41pub fn is_arm() -> bool {
42    false
43}
44
45#[cfg(target_arch = "x86_64")]
46pub fn is_x86() -> bool {
47    true
48}
49
50#[cfg(not(target_arch = "x86_64"))]
51pub fn is_x86() -> bool {
52    false
53}
54
55pub fn check_linux_x86() -> Result<(), PlatformError> {
56    if is_linux() && is_x86() {
57        Ok(())
58    } else {
59        Err(PlatformError::new(
60            "Platform Check Error",
61            "This operation is only supported on Linux x86_64.",
62        ))
63    }
64}
65
66pub fn check_macos_arm() -> Result<(), PlatformError> {
67    if is_osx() && is_arm() {
68        Ok(())
69    } else {
70        Err(PlatformError::new(
71            "Platform Check Error",
72            "This operation is only supported on macOS ARM.",
73        ))
74    }
75}