Skip to main content

monaco_vscode_server/
platform.rs

1// platform.rs - Platform detection and support
2
3use std::fmt;
4use serde::{Deserialize, Serialize};
5
6/// Supported platforms for VSCode server
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
8pub enum Platform {
9    LinuxX64,
10    LinuxArm64,
11    LinuxArmhf,
12    DarwinX64,
13    DarwinArm64,
14    Win32X64,
15}
16
17impl Platform {
18    /// Detects the current platform
19    pub fn current() -> Result<Self, String> {
20        #[cfg(all(target_os = "linux", target_arch = "x86_64"))]
21        return Ok(Platform::LinuxX64);
22        
23        #[cfg(all(target_os = "linux", target_arch = "aarch64"))]
24        return Ok(Platform::LinuxArm64);
25        
26        #[cfg(all(target_os = "linux", target_arch = "arm"))]
27        return Ok(Platform::LinuxArmhf);
28        
29        #[cfg(all(target_os = "macos", target_arch = "x86_64"))]
30        return Ok(Platform::DarwinX64);
31        
32        #[cfg(all(target_os = "macos", target_arch = "aarch64"))]
33        return Ok(Platform::DarwinArm64);
34        
35        #[cfg(all(target_os = "windows", target_arch = "x86_64"))]
36        return Ok(Platform::Win32X64);
37        
38        #[cfg(not(any(
39            all(target_os = "linux", any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "arm")),
40            all(target_os = "macos", any(target_arch = "x86_64", target_arch = "aarch64")),
41            all(target_os = "windows", target_arch = "x86_64")
42        )))]
43        return Err(format!("Unsupported platform: {} {}", 
44            std::env::consts::OS, 
45            std::env::consts::ARCH
46        ));
47    }
48    
49    /// Gets the VSCode server flavor string for this platform
50    pub fn server_flavor(&self) -> &'static str {
51        match self {
52            Platform::LinuxX64 => "server-linux-x64",
53            Platform::LinuxArm64 => "server-linux-arm64",
54            Platform::LinuxArmhf => "server-linux-armhf",
55            Platform::DarwinX64 => "server-darwin-x64",
56            Platform::DarwinArm64 => "server-darwin-arm64",
57            Platform::Win32X64 => "server-win32-x64",
58        }
59    }
60    
61    /// Gets the URL suffix for downloading
62    pub fn url_suffix(&self) -> &'static str {
63        match self {
64            Platform::Win32X64 => "archive",
65            _ => "stable",
66        }
67    }
68    
69    /// Checks if this platform uses zip archives
70    pub fn uses_zip(&self) -> bool {
71        matches!(self, Platform::Win32X64)
72    }
73    
74    /// From target triple string
75    pub fn from_target_triple(target: &str) -> Result<Self, String> {
76        match target {
77            "x86_64-unknown-linux-gnu" | "x86_64-unknown-linux-musl" => Ok(Platform::LinuxX64),
78            "aarch64-unknown-linux-gnu" | "aarch64-unknown-linux-musl" => Ok(Platform::LinuxArm64),
79            "armv7-unknown-linux-gnueabihf" => Ok(Platform::LinuxArmhf),
80            "x86_64-apple-darwin" => Ok(Platform::DarwinX64),
81            "aarch64-apple-darwin" => Ok(Platform::DarwinArm64),
82            "x86_64-pc-windows-msvc" | "x86_64-pc-windows-gnu" => Ok(Platform::Win32X64),
83            _ => Err(format!("Unsupported target: {}", target)),
84        }
85    }
86}
87
88impl fmt::Display for Platform {
89    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90        write!(f, "{}", self.server_flavor())
91    }
92}