lighty_core/
system.rs

1use crate::errors::{SystemError, SystemResult};
2use serde::Deserialize;
3use std::fmt::Display;
4
5pub const OS: OperatingSystem = if cfg!(target_os = "windows") {
6    OperatingSystem::WINDOWS
7} else if cfg!(target_os = "macos") {
8    OperatingSystem::OSX
9} else if cfg!(target_os = "linux") {
10    OperatingSystem::LINUX
11} else {
12    OperatingSystem::UNKNOWN
13};
14
15pub const ARCHITECTURE: Architecture = if cfg!(target_arch = "x86") {
16    Architecture::X86 // 32-bit
17} else if cfg!(target_arch = "x86_64") {
18    Architecture::X64 // 64-bit
19} else if cfg!(target_arch = "arm") {
20    Architecture::ARM // ARM
21} else if cfg!(target_arch = "aarch64") {
22    Architecture::AARCH64 // AARCH64
23} else {
24    Architecture::UNKNOWN // Unsupported architecture
25};
26
27#[derive(Deserialize, PartialEq, Eq, Hash, Debug)]
28pub enum OperatingSystem {
29    #[serde(rename = "windows")]
30    WINDOWS,
31    #[serde(rename = "linux")]
32    LINUX,
33    #[serde(rename = "osx")]
34    OSX,
35    #[serde(rename = "unknown")]
36    UNKNOWN,
37}
38
39#[derive(Deserialize, Clone, PartialEq, Eq, Hash, Debug)]
40pub enum Architecture {
41    #[serde(rename = "x86")]
42    X86,
43    #[serde(rename = "x64")]
44    X64,
45    #[serde(rename = "arm")]
46    ARM,
47    #[serde(rename = "aarch64")]
48    AARCH64,
49    #[serde(rename = "unknown")]
50    UNKNOWN,
51}
52
53impl OperatingSystem {
54    pub fn get_vanilla_os(&self) -> SystemResult<&'static str> {
55        match self {
56            OperatingSystem::WINDOWS => Ok("windows"),
57            OperatingSystem::LINUX => Ok("linux"),
58            OperatingSystem::OSX => Ok("osx"),
59            OperatingSystem::UNKNOWN => Err(SystemError::UnsupportedOS),
60        }
61    }
62
63    pub fn get_adoptium_name(&self) -> SystemResult<&'static str> {
64        match self {
65            OperatingSystem::WINDOWS => Ok("windows"),
66            OperatingSystem::LINUX => Ok("linux"),
67            OperatingSystem::OSX => Ok("mac"),
68            OperatingSystem::UNKNOWN => Err(SystemError::UnsupportedOS),
69        }
70    }
71
72    pub fn get_graal_name(&self) -> SystemResult<&'static str> {
73        match self {
74            OperatingSystem::WINDOWS => Ok("windows"),
75            OperatingSystem::LINUX => Ok("linux"),
76            OperatingSystem::OSX => Ok("macos"),
77            OperatingSystem::UNKNOWN => Err(SystemError::UnsupportedOS),
78        }
79    }
80
81    pub fn get_archive_type(&self) -> SystemResult<&'static str> {
82        match self {
83            OperatingSystem::WINDOWS => Ok("zip"),
84            OperatingSystem::LINUX | OperatingSystem::OSX => Ok("tar.gz"),
85            OperatingSystem::UNKNOWN => Err(SystemError::UnsupportedOS),
86        }
87    }
88}
89
90impl Display for OperatingSystem {
91    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92        match self {
93            OperatingSystem::WINDOWS => f.write_str("windows"),
94            OperatingSystem::LINUX => f.write_str("linux"),
95            OperatingSystem::OSX => f.write_str("osx"),
96            OperatingSystem::UNKNOWN => f.write_str("unknown"),
97        }
98    }
99}
100
101impl Architecture {
102    pub fn get_simple_name(&self) -> SystemResult<&'static str> {
103        match self {
104            Architecture::X86 => Ok("x86"),
105            Architecture::X64 => Ok("x64"),
106            Architecture::ARM => Ok("arm"),
107            Architecture::AARCH64 => Ok("aarch64"),
108            Architecture::UNKNOWN => Err(SystemError::UnsupportedArchitecture),
109        }
110    }
111
112    pub fn get_vanilla_arch(&self) -> SystemResult<&'static str> {
113        match self {
114            Architecture::X86 => Ok("-x86"),
115            Architecture::X64 => Ok(""),
116            Architecture::ARM => Ok("-arm"),
117            Architecture::AARCH64 => Ok("-arm64"),
118            Architecture::UNKNOWN => Err(SystemError::UnsupportedArchitecture),
119        }
120    }
121
122    /// Retourne "32" ou "64" pour résoudre ${arch}
123    pub fn get_arch_bits(&self) -> SystemResult<&'static str> {
124        match self {
125            Architecture::X86 => Ok("32"),
126            Architecture::X64 => Ok("64"),
127            Architecture::ARM => Ok("32"),
128            Architecture::AARCH64 => Ok("64"),
129            Architecture::UNKNOWN => Err(SystemError::UnsupportedArchitecture),
130        }
131    }
132}
133
134impl Display for Architecture {
135    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
136        match self {
137            Architecture::X86 => f.write_str("x86"),
138            Architecture::X64 => f.write_str("x64"),
139            Architecture::ARM => f.write_str("arm"),
140            Architecture::AARCH64 => f.write_str("aarch64"),
141            Architecture::UNKNOWN => f.write_str("unknown"),
142        }
143    }
144}