herolib_rust/packages/system/os/src/platform.rs
2025-08-05 15:33:03 +02:00

76 lines
1.4 KiB
Rust

use thiserror::Error;
#[derive(Debug, Error)]
pub enum PlatformError {
#[error("{0}: {1}")]
Generic(String, String),
}
impl PlatformError {
pub fn new(kind: &str, message: &str) -> Self {
PlatformError::Generic(kind.to_string(), message.to_string())
}
}
#[cfg(target_os = "macos")]
pub fn is_osx() -> bool {
true
}
#[cfg(not(target_os = "macos"))]
pub fn is_osx() -> bool {
false
}
#[cfg(target_os = "linux")]
pub fn is_linux() -> bool {
true
}
#[cfg(not(target_os = "linux"))]
pub fn is_linux() -> bool {
false
}
#[cfg(target_arch = "aarch64")]
pub fn is_arm() -> bool {
true
}
#[cfg(not(target_arch = "aarch64"))]
pub fn is_arm() -> bool {
false
}
#[cfg(target_arch = "x86_64")]
pub fn is_x86() -> bool {
true
}
#[cfg(not(target_arch = "x86_64"))]
pub fn is_x86() -> bool {
false
}
pub fn check_linux_x86() -> Result<(), PlatformError> {
if is_linux() && is_x86() {
Ok(())
} else {
Err(PlatformError::new(
"Platform Check Error",
"This operation is only supported on Linux x86_64.",
))
}
}
pub fn check_macos_arm() -> Result<(), PlatformError> {
if is_osx() && is_arm() {
Ok(())
} else {
Err(PlatformError::new(
"Platform Check Error",
"This operation is only supported on macOS ARM.",
))
}
}