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 } else if cfg!(target_arch = "x86_64") {
18 Architecture::X64 } else if cfg!(target_arch = "arm") {
20 Architecture::ARM } else if cfg!(target_arch = "aarch64") {
22 Architecture::AARCH64 } else {
24 Architecture::UNKNOWN };
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_zulu_name(&self) -> SystemResult<&'static str> {
82 match self {
83 OperatingSystem::WINDOWS => Ok("windows"),
84 OperatingSystem::LINUX => Ok("linux"),
85 OperatingSystem::OSX => Ok("macos"),
86 OperatingSystem::UNKNOWN => Err(SystemError::UnsupportedOS),
87 }
88 }
89
90 pub fn get_zulu_ext(&self) -> SystemResult<&'static str> {
91 match self {
92 OperatingSystem::WINDOWS => Ok("zip"),
93 OperatingSystem::LINUX | OperatingSystem::OSX => Ok("tar.gz"),
94 OperatingSystem::UNKNOWN => Err(SystemError::UnsupportedOS),
95 }
96 }
97
98 pub fn get_archive_type(&self) -> SystemResult<&'static str> {
99 match self {
100 OperatingSystem::WINDOWS => Ok("zip"),
101 OperatingSystem::LINUX | OperatingSystem::OSX => Ok("tar.gz"),
102 OperatingSystem::UNKNOWN => Err(SystemError::UnsupportedOS),
103 }
104 }
105}
106
107impl Display for OperatingSystem {
108 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
109 match self {
110 OperatingSystem::WINDOWS => f.write_str("windows"),
111 OperatingSystem::LINUX => f.write_str("linux"),
112 OperatingSystem::OSX => f.write_str("osx"),
113 OperatingSystem::UNKNOWN => f.write_str("unknown"),
114 }
115 }
116}
117
118impl Architecture {
119 pub fn get_simple_name(&self) -> SystemResult<&'static str> {
120 match self {
121 Architecture::X86 => Ok("x86"),
122 Architecture::X64 => Ok("x64"),
123 Architecture::ARM => Ok("arm"),
124 Architecture::AARCH64 => Ok("aarch64"),
125 Architecture::UNKNOWN => Err(SystemError::UnsupportedArchitecture),
126 }
127 }
128
129 pub fn get_vanilla_arch(&self) -> SystemResult<&'static str> {
130 match self {
131 Architecture::X86 => Ok("-x86"),
132 Architecture::X64 => Ok(""),
133 Architecture::ARM => Ok("-arm"),
134 Architecture::AARCH64 => Ok("-arm64"),
135 Architecture::UNKNOWN => Err(SystemError::UnsupportedArchitecture),
136 }
137 }
138
139 pub fn get_arch_bits(&self) -> SystemResult<&'static str> {
141 match self {
142 Architecture::X86 => Ok("32"),
143 Architecture::X64 => Ok("64"),
144 Architecture::ARM => Ok("32"),
145 Architecture::AARCH64 => Ok("64"),
146 Architecture::UNKNOWN => Err(SystemError::UnsupportedArchitecture),
147 }
148 }
149
150 pub fn get_zulu_arch(&self) -> SystemResult<&'static str> {
151 match self {
152 Architecture::X86 => Ok("i686"),
153 Architecture::X64 => Ok("x64"),
154 Architecture::ARM => Ok("arm"),
155 Architecture::AARCH64 => Ok("aarch64"),
156 Architecture::UNKNOWN => Err(SystemError::UnsupportedArchitecture),
157 }
158 }
159}
160
161impl Display for Architecture {
162 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
163 match self {
164 Architecture::X86 => f.write_str("x86"),
165 Architecture::X64 => f.write_str("x64"),
166 Architecture::ARM => f.write_str("arm"),
167 Architecture::AARCH64 => f.write_str("aarch64"),
168 Architecture::UNKNOWN => f.write_str("unknown"),
169 }
170 }
171}