Skip to main content

lighty_core/
system.rs

1//! Compile-time OS / architecture detection and per-vendor name mapping.
2
3use crate::errors::{SystemError, SystemResult};
4use serde::Deserialize;
5use std::fmt::Display;
6
7/// Current operating system, resolved at compile time.
8pub const OS: OperatingSystem = if cfg!(target_os = "windows") {
9    OperatingSystem::WINDOWS
10} else if cfg!(target_os = "macos") {
11    OperatingSystem::OSX
12} else if cfg!(target_os = "linux") {
13    OperatingSystem::LINUX
14} else {
15    OperatingSystem::UNKNOWN
16};
17
18/// Current CPU architecture, resolved at compile time.
19pub const ARCHITECTURE: Architecture = if cfg!(target_arch = "x86") {
20    Architecture::X86
21} else if cfg!(target_arch = "x86_64") {
22    Architecture::X64
23} else if cfg!(target_arch = "arm") {
24    Architecture::ARM
25} else if cfg!(target_arch = "aarch64") {
26    Architecture::AARCH64
27} else {
28    Architecture::UNKNOWN
29};
30
31/// Supported operating systems.
32#[derive(Deserialize, PartialEq, Eq, Hash, Debug)]
33pub enum OperatingSystem {
34    #[serde(rename = "windows")]
35    WINDOWS,
36    #[serde(rename = "linux")]
37    LINUX,
38    #[serde(rename = "osx")]
39    OSX,
40    #[serde(rename = "unknown")]
41    UNKNOWN,
42}
43
44/// Supported CPU architectures.
45#[derive(Deserialize, Clone, PartialEq, Eq, Hash, Debug)]
46pub enum Architecture {
47    #[serde(rename = "x86")]
48    X86,
49    #[serde(rename = "x64")]
50    X64,
51    #[serde(rename = "arm")]
52    ARM,
53    #[serde(rename = "aarch64")]
54    AARCH64,
55    #[serde(rename = "unknown")]
56    UNKNOWN,
57}
58
59impl OperatingSystem {
60    /// Returns the OS name as used in Mojang's version manifest
61    /// (`"windows"`, `"linux"`, `"osx"`).
62    pub fn get_vanilla_os(&self) -> SystemResult<&'static str> {
63        match self {
64            OperatingSystem::WINDOWS => Ok("windows"),
65            OperatingSystem::LINUX => Ok("linux"),
66            OperatingSystem::OSX => Ok("osx"),
67            OperatingSystem::UNKNOWN => Err(SystemError::UnsupportedOS),
68        }
69    }
70
71    /// Returns the OS name as used by the Adoptium (Temurin) API
72    /// (`"windows"`, `"linux"`, `"mac"`).
73    pub fn get_adoptium_name(&self) -> SystemResult<&'static str> {
74        match self {
75            OperatingSystem::WINDOWS => Ok("windows"),
76            OperatingSystem::LINUX => Ok("linux"),
77            OperatingSystem::OSX => Ok("mac"),
78            OperatingSystem::UNKNOWN => Err(SystemError::UnsupportedOS),
79        }
80    }
81
82    /// Returns the OS name as used by the Oracle GraalVM download URLs
83    /// (`"windows"`, `"linux"`, `"macos"`).
84    pub fn get_graal_name(&self) -> SystemResult<&'static str> {
85        match self {
86            OperatingSystem::WINDOWS => Ok("windows"),
87            OperatingSystem::LINUX => Ok("linux"),
88            OperatingSystem::OSX => Ok("macos"),
89            OperatingSystem::UNKNOWN => Err(SystemError::UnsupportedOS),
90        }
91    }
92
93    /// Returns the OS name as used by the Azul / Foojay APIs (Zulu, Liberica)
94    /// (`"windows"`, `"linux"`, `"macos"`).
95    pub fn get_zulu_name(&self) -> SystemResult<&'static str> {
96        match self {
97            OperatingSystem::WINDOWS => Ok("windows"),
98            OperatingSystem::LINUX => Ok("linux"),
99            OperatingSystem::OSX => Ok("macos"),
100            OperatingSystem::UNKNOWN => Err(SystemError::UnsupportedOS),
101        }
102    }
103
104    /// Returns the archive extension Zulu publishes for this OS
105    /// (`"zip"` on Windows, `"tar.gz"` on Linux/macOS).
106    pub fn get_zulu_ext(&self) -> SystemResult<&'static str> {
107        match self {
108            OperatingSystem::WINDOWS => Ok("zip"),
109            OperatingSystem::LINUX | OperatingSystem::OSX => Ok("tar.gz"),
110            OperatingSystem::UNKNOWN => Err(SystemError::UnsupportedOS),
111        }
112    }
113
114    /// Returns the archive extension used by every supported JRE distribution
115    /// (`"zip"` on Windows, `"tar.gz"` on Linux/macOS).
116    pub fn get_archive_type(&self) -> SystemResult<&'static str> {
117        match self {
118            OperatingSystem::WINDOWS => Ok("zip"),
119            OperatingSystem::LINUX | OperatingSystem::OSX => Ok("tar.gz"),
120            OperatingSystem::UNKNOWN => Err(SystemError::UnsupportedOS),
121        }
122    }
123}
124
125impl Display for OperatingSystem {
126    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
127        match self {
128            OperatingSystem::WINDOWS => f.write_str("windows"),
129            OperatingSystem::LINUX => f.write_str("linux"),
130            OperatingSystem::OSX => f.write_str("osx"),
131            OperatingSystem::UNKNOWN => f.write_str("unknown"),
132        }
133    }
134}
135
136impl Architecture {
137    /// Returns the canonical architecture name (`"x86"`, `"x64"`, `"arm"`, `"aarch64"`).
138    pub fn get_simple_name(&self) -> SystemResult<&'static str> {
139        match self {
140            Architecture::X86 => Ok("x86"),
141            Architecture::X64 => Ok("x64"),
142            Architecture::ARM => Ok("arm"),
143            Architecture::AARCH64 => Ok("aarch64"),
144            Architecture::UNKNOWN => Err(SystemError::UnsupportedArchitecture),
145        }
146    }
147
148    /// Returns the architecture suffix Mojang appends to native classifier
149    /// names (`""` for x64, `"-x86"`, `"-arm"`, `"-arm64"`).
150    pub fn get_vanilla_arch(&self) -> SystemResult<&'static str> {
151        match self {
152            Architecture::X86 => Ok("-x86"),
153            Architecture::X64 => Ok(""),
154            Architecture::ARM => Ok("-arm"),
155            Architecture::AARCH64 => Ok("-arm64"),
156            Architecture::UNKNOWN => Err(SystemError::UnsupportedArchitecture),
157        }
158    }
159
160    /// Returns `"32"` or `"64"` — used to resolve the `${arch}` placeholder
161    /// in Mojang's library native classifier names.
162    pub fn get_arch_bits(&self) -> SystemResult<&'static str> {
163        match self {
164            Architecture::X86 => Ok("32"),
165            Architecture::X64 => Ok("64"),
166            Architecture::ARM => Ok("32"),
167            Architecture::AARCH64 => Ok("64"),
168            Architecture::UNKNOWN => Err(SystemError::UnsupportedArchitecture),
169        }
170    }
171
172    /// Returns the architecture name as used by the Azul Zulu API
173    /// (`"i686"`, `"x64"`, `"arm"`, `"aarch64"`).
174    pub fn get_zulu_arch(&self) -> SystemResult<&'static str> {
175        match self {
176            //TODO: rework this part for java 8 for macos
177            Architecture::X86 => Ok("i686"),
178            Architecture::X64 => Ok("x64"),
179            Architecture::ARM => Ok("arm"),
180            Architecture::AARCH64 => Ok("aarch64"),
181            Architecture::UNKNOWN => Err(SystemError::UnsupportedArchitecture),
182        }
183    }
184}
185
186impl Display for Architecture {
187    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
188        match self {
189            Architecture::X86 => f.write_str("x86"),
190            Architecture::X64 => f.write_str("x64"),
191            Architecture::ARM => f.write_str("arm"),
192            Architecture::AARCH64 => f.write_str("aarch64"),
193            Architecture::UNKNOWN => f.write_str("unknown"),
194        }
195    }
196}