#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct AppEnvironment {
pub os: OsType,
pub family: OsFamily,
pub cpu_arch: CpuArchitecture,
pub dyn_lib_ext: DynLibExtension,
pub dyn_lib_prefix: DynLibPrefix,
}
impl AppEnvironment {
pub fn init() -> Self {
AppEnvironment {
os: OsType::which_os(),
family: OsFamily::which_os_family(),
cpu_arch: CpuArchitecture::which_cpu_arch(),
dyn_lib_ext: DynLibExtension::which_dyn_lib_ext(),
dyn_lib_prefix: DynLibPrefix::which_dyll_prefix(),
}
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub enum OsType {
Windows,
MacOs,
Ios,
Linux,
Android,
FreeBsd,
Dragonfly,
OpenBsd,
NetBsd,
Solaris,
UnrecognizedOs,
}
impl OsType {
pub fn which_os() -> Self {
std::env::consts::OS.into()
}
}
impl From<&str> for OsType {
fn from(value: &str) -> OsType {
match value {
"linux" => OsType::Linux,
"macos" => OsType::MacOs,
"ios" => OsType::Ios,
"freebsd" => OsType::FreeBsd,
"dragonfly" => OsType::Dragonfly,
"netbsd" => OsType::NetBsd,
"openbsd" => OsType::OpenBsd,
"solaris" => OsType::Solaris,
"android" => OsType::Android,
"windows" => OsType::Windows,
"unrecognized_os" => OsType::UnrecognizedOs,
_ => OsType::UnrecognizedOs,
}
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub enum OsFamily {
Unix,
Windows,
UnrecognizedFamily,
}
impl OsFamily {
pub fn which_os_family() -> Self {
std::env::consts::FAMILY.into()
}
}
impl From<&str> for OsFamily {
fn from(value: &str) -> Self {
match value {
"unix" => OsFamily::Unix,
"windows" => OsFamily::Windows,
"unrecognized_family" => OsFamily::UnrecognizedFamily,
_ => OsFamily::UnrecognizedFamily,
}
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub enum CpuArchitecture {
Intel32,
Intel64,
Arm32,
Arm64,
LoongArch64,
Mips32,
Mips64,
Powerpc32,
Powerpc64,
Riscv32,
Riscv64,
UnsupportedArch,
}
impl CpuArchitecture {
pub fn which_cpu_arch() -> Self {
std::env::consts::ARCH.into()
}
}
impl From<&str> for CpuArchitecture {
fn from(value: &str) -> CpuArchitecture {
match value {
"x86" => CpuArchitecture::Intel32,
"x86_64" => CpuArchitecture::Intel64,
"arm" => CpuArchitecture::Arm32,
"aarch64" => CpuArchitecture::Arm64,
"loongarch64" => CpuArchitecture::LoongArch64,
"mips" => CpuArchitecture::Mips32,
"mips64" => CpuArchitecture::Mips64,
"powerpc" => CpuArchitecture::Powerpc32,
"powerpc64" => CpuArchitecture::Powerpc64,
"riscv32" => CpuArchitecture::Riscv32,
"riscv64" => CpuArchitecture::Riscv64,
"unsupported_arch" => CpuArchitecture::UnsupportedArch,
_ => CpuArchitecture::UnsupportedArch,
}
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub enum DynLibExtension {
So,
Dylib,
Dll,
UnrecognizedDynLib,
}
impl DynLibExtension {
pub fn which_dyn_lib_ext() -> Self {
std::env::consts::DLL_EXTENSION.into()
}
}
impl From<&str> for DynLibExtension {
fn from(value: &str) -> DynLibExtension {
match value {
"so" => DynLibExtension::So,
"dylib" => DynLibExtension::Dylib,
"dll" => DynLibExtension::Dll,
"unrecognized_dyn_lib_extension" => DynLibExtension::UnrecognizedDynLib,
_ => DynLibExtension::UnrecognizedDynLib,
}
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub enum DynLibPrefix {
Lib,
None,
}
impl DynLibPrefix {
pub fn which_dyll_prefix() -> Self {
std::env::consts::DLL_PREFIX.into()
}
}
impl From<&str> for DynLibPrefix {
fn from(value: &str) -> DynLibPrefix {
match value {
"lib" => DynLibPrefix::Lib,
_ => DynLibPrefix::None,
}
}
}
#[cfg(test)]
mod sanity_test {
use crate::{AppEnvironment, OsFamily, OsType};
#[test]
fn detect_os() {
if cfg!(target_os = "linux") {
assert_eq!(OsType::Linux, OsType::which_os());
}
if cfg!(target_os = "windows") {
assert_eq!(OsType::Windows, OsType::which_os());
}
if cfg!(target_os = "macos") {
assert_eq!(OsType::MacOs, OsType::which_os());
}
if cfg!(target_os = "silly_os") {
assert_eq!(OsType::UnrecognizedOs, OsType::which_os());
}
}
#[test]
fn init_app_env() {
let init_app_env = AppEnvironment::init();
if cfg!(target_os = "linux") {
assert_eq!(init_app_env.os, OsType::which_os());
}
if cfg!(target_os = "windows") {
assert_eq!(init_app_env.os, OsType::which_os());
}
if cfg!(target_os = "macos") {
assert_eq!(init_app_env.os, OsType::which_os());
}
if cfg!(target_arch = "unix") {
assert_eq!(init_app_env.family, OsFamily::which_os_family());
}
if cfg!(target_family = "windows") {
assert_eq!(init_app_env.family, OsFamily::which_os_family());
}
}
}