-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathplatform.rs
More file actions
167 lines (150 loc) · 4.28 KB
/
platform.rs
File metadata and controls
167 lines (150 loc) · 4.28 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use std::fmt;
/// Represents a platform (OS + architecture) combination
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Platform {
pub os: Os,
pub arch: Arch,
}
/// Operating system
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Os {
Linux,
Darwin,
Windows,
}
/// CPU architecture
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Arch {
X64,
Arm64,
}
impl Platform {
/// Detect the current platform
#[must_use]
pub const fn current() -> Self {
Self { os: Os::current(), arch: Arch::current() }
}
}
impl fmt::Display for Platform {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}-{}", self.os, self.arch)
}
}
impl Os {
/// Detect the current operating system.
///
/// # Supported platforms
/// - Linux (`target_os = "linux"`)
/// - macOS (`target_os = "macos"`)
/// - Windows (`target_os = "windows"`)
///
/// Compilation will fail on unsupported operating systems.
#[must_use]
pub const fn current() -> Self {
#[cfg(any(target_os = "android", target_os = "linux"))]
{
return Self::Linux;
}
#[cfg(target_os = "macos")]
{
Self::Darwin
}
#[cfg(target_os = "windows")]
{
Self::Windows
}
#[cfg(not(any(
target_os = "android",
target_os = "linux",
target_os = "macos",
target_os = "windows"
)))]
{
compile_error!(
"Unsupported operating system. vite_js_runtime only supports Linux, macOS, and Windows."
)
}
}
}
impl fmt::Display for Os {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Linux => write!(f, "linux"),
Self::Darwin => write!(f, "darwin"),
Self::Windows => write!(f, "windows"),
}
}
}
impl Arch {
/// Detect the current CPU architecture.
///
/// # Supported architectures
/// - x86_64 (`target_arch = "x86_64"`)
/// - ARM64/AArch64 (`target_arch = "aarch64"`)
///
/// Compilation will fail on unsupported architectures.
#[must_use]
pub const fn current() -> Self {
#[cfg(target_arch = "x86_64")]
{
Self::X64
}
#[cfg(target_arch = "aarch64")]
{
Self::Arm64
}
#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
{
compile_error!(
"Unsupported CPU architecture. vite_js_runtime only supports x86_64 and aarch64."
)
}
}
}
impl fmt::Display for Arch {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::X64 => write!(f, "x64"),
Self::Arm64 => write!(f, "arm64"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_platform_detection() {
let platform = Platform::current();
// Just verify it doesn't panic and returns a valid platform
let platform_str = platform.to_string();
assert!(!platform_str.is_empty());
// Verify format is "os-arch"
let parts: Vec<&str> = platform_str.split('-').collect();
assert_eq!(parts.len(), 2);
}
#[test]
fn test_platform_display() {
let cases = [
(Platform { os: Os::Linux, arch: Arch::X64 }, "linux-x64"),
(Platform { os: Os::Linux, arch: Arch::Arm64 }, "linux-arm64"),
(Platform { os: Os::Darwin, arch: Arch::X64 }, "darwin-x64"),
(Platform { os: Os::Darwin, arch: Arch::Arm64 }, "darwin-arm64"),
(Platform { os: Os::Windows, arch: Arch::X64 }, "windows-x64"),
(Platform { os: Os::Windows, arch: Arch::Arm64 }, "windows-arm64"),
];
for (platform, expected) in cases {
assert_eq!(platform.to_string(), expected);
}
}
#[test]
fn test_os_display() {
assert_eq!(Os::Linux.to_string(), "linux");
assert_eq!(Os::Darwin.to_string(), "darwin");
assert_eq!(Os::Windows.to_string(), "windows");
}
#[test]
fn test_arch_display() {
assert_eq!(Arch::X64.to_string(), "x64");
assert_eq!(Arch::Arm64.to_string(), "arm64");
}
}