Skip to main content

chrome_for_testing/api/
platform.rs

1use crate::error::Error;
2use crate::error::Result;
3use serde::Deserialize;
4use std::borrow::Cow;
5use std::env::consts;
6use std::fmt::{Display, Formatter};
7
8/// Supported platforms for Chrome and `ChromeDriver` downloads.
9///
10/// This site <https://googlechromelabs.github.io/chrome-for-testing/> show the platform names
11/// defined here.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
13pub enum Platform {
14    /// Linux x64 platform.
15    #[serde(rename = "linux64")]
16    Linux64,
17
18    /// macOS ARM64 platform (Apple Silicon).
19    #[serde(rename = "mac-arm64")]
20    MacArm64,
21
22    /// macOS x64 platform (Intel).
23    #[serde(rename = "mac-x64")]
24    MacX64,
25
26    /// Windows 32-bit platform.
27    #[serde(rename = "win32")]
28    Win32,
29
30    /// Windows 64-bit platform.
31    #[serde(rename = "win64")]
32    Win64,
33}
34
35impl Display for Platform {
36    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
37        f.write_str(match self {
38            Platform::Linux64 => "linux64",
39            Platform::MacArm64 => "mac-arm64",
40            Platform::MacX64 => "mac-x64",
41            Platform::Win32 => "win32",
42            Platform::Win64 => "win64",
43        })
44    }
45}
46
47impl Platform {
48    /// Detect the platform identifier that should be used for the current system.
49    ///
50    /// # Errors
51    ///
52    /// Returns an error if the current OS/architecture combination is not supported.
53    pub fn detect() -> Result<Platform> {
54        match consts::OS {
55            os @ "windows" => match consts::ARCH {
56                "x86" => Ok(Platform::Win32),
57                "x86_64" => Ok(Platform::Win64),
58                arch => Err(Error::UnsupportedPlatform {
59                    os: Cow::Borrowed(os),
60                    arch: Cow::Borrowed(arch),
61                }),
62            },
63            os @ "linux" => match consts::ARCH {
64                "x86_64" => Ok(Platform::Linux64),
65                arch => Err(Error::UnsupportedPlatform {
66                    os: Cow::Borrowed(os),
67                    arch: Cow::Borrowed(arch),
68                }),
69            },
70            os @ "macos" => match consts::ARCH {
71                "x86_64" => Ok(Platform::MacX64),
72                "arm" | "aarch64" => Ok(Platform::MacArm64),
73                arch => Err(Error::UnsupportedPlatform {
74                    os: Cow::Borrowed(os),
75                    arch: Cow::Borrowed(arch),
76                }),
77            },
78            os => Err(Error::UnsupportedPlatform {
79                os: Cow::Borrowed(os),
80                arch: Cow::Borrowed(consts::ARCH),
81            }),
82        }
83    }
84
85    /// Filename of the chrome binary.
86    #[must_use]
87    pub fn chrome_binary_name(self) -> &'static str {
88        match self {
89            Platform::Linux64 | Platform::MacX64 => "chrome",
90            Platform::MacArm64 => "Google Chrome for Testing.app",
91            Platform::Win32 | Platform::Win64 => "chrome.exe",
92        }
93    }
94
95    /// Filename of the chromedriver binary.
96    #[must_use]
97    pub fn chromedriver_binary_name(self) -> &'static str {
98        match self {
99            Platform::Linux64 | Platform::MacX64 | Platform::MacArm64 => "chromedriver",
100            Platform::Win32 | Platform::Win64 => "chromedriver.exe",
101        }
102    }
103
104    /// Tells whether this platform identifier references the Linux OS.
105    #[must_use]
106    pub fn is_linux(&self) -> bool {
107        match self {
108            Platform::Linux64 => true,
109            Platform::MacArm64 | Platform::MacX64 | Platform::Win32 | Platform::Win64 => false,
110        }
111    }
112
113    /// Tells whether this platform identifier references macOS.
114    #[must_use]
115    pub fn is_macos(&self) -> bool {
116        match self {
117            Platform::MacArm64 | Platform::MacX64 => true,
118            Platform::Linux64 | Platform::Win32 | Platform::Win64 => false,
119        }
120    }
121
122    /// Tells whether this platform identifier references the Windows OS.
123    #[must_use]
124    pub fn is_windows(&self) -> bool {
125        match self {
126            Platform::Win32 | Platform::Win64 => true,
127            Platform::Linux64 | Platform::MacArm64 | Platform::MacX64 => false,
128        }
129    }
130}