1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Platform detection and Minecraft metadata naming helpers.
/// Operating system family used by rule and native-library selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Os {
/// Microsoft Windows.
Windows,
/// macOS, named `osx` in Minecraft metadata.
MacOs,
/// Linux.
Linux,
/// Any unsupported or unknown operating system.
Other,
}
/// CPU architecture family used by rule and native-library selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Arch {
/// 32-bit x86.
X86,
/// 64-bit x86.
X86_64,
/// 64-bit ARM.
Aarch64,
/// Any unsupported or unknown architecture.
Other,
}
/// Operating-system and architecture pair.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Platform {
/// Operating system family.
pub os: Os,
/// CPU architecture family.
pub arch: Arch,
}
impl Platform {
/// Detects the platform of the current process.
pub fn current() -> Self {
Self {
os: match std::env::consts::OS {
"windows" => Os::Windows,
"macos" => Os::MacOs,
"linux" => Os::Linux,
_ => Os::Other,
},
arch: match std::env::consts::ARCH {
"x86" | "i386" | "i586" | "i686" => Arch::X86,
"x86_64" | "amd64" => Arch::X86_64,
"aarch64" => Arch::Aarch64,
_ => Arch::Other,
},
}
}
/// Returns the operating-system name used in Minecraft metadata rules.
pub fn minecraft_os_name(self) -> &'static str {
match self.os {
Os::Windows => "windows",
Os::MacOs => "osx",
Os::Linux => "linux",
Os::Other => "unknown",
}
}
/// Returns true when the platform is 32-bit x86.
pub fn is_32_bit(self) -> bool {
self.arch == Arch::X86
}
}