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    pub fn detect() -> Result<Platform> {
50        match consts::OS {
51            os @ "windows" => match consts::ARCH {
52                "x86" => Ok(Platform::Win32),
53                "x86_64" => Ok(Platform::Win64),
54                arch => Err(Error::UnsupportedPlatform {
55                    os: Cow::Borrowed(os),
56                    arch: Cow::Borrowed(arch),
57                }),
58            },
59            os @ "linux" => match consts::ARCH {
60                "x86_64" => Ok(Platform::Linux64),
61                arch => Err(Error::UnsupportedPlatform {
62                    os: Cow::Borrowed(os),
63                    arch: Cow::Borrowed(arch),
64                }),
65            },
66            os @ "macos" => match consts::ARCH {
67                "x86_64" => Ok(Platform::MacX64),
68                "arm" | "aarch64" => Ok(Platform::MacArm64),
69                arch => Err(Error::UnsupportedPlatform {
70                    os: Cow::Borrowed(os),
71                    arch: Cow::Borrowed(arch),
72                }),
73            },
74            os => Err(Error::UnsupportedPlatform {
75                os: Cow::Borrowed(os),
76                arch: Cow::Borrowed(consts::ARCH),
77            }),
78        }
79    }
80
81    /// Filename of the chrome binary.
82    pub fn chrome_binary_name(self) -> &'static str {
83        match self {
84            Platform::Linux64 | Platform::MacX64 => "chrome",
85            Platform::MacArm64 => "Google Chrome for Testing.app",
86            Platform::Win32 | Platform::Win64 => "chrome.exe",
87        }
88    }
89
90    /// Filename of the chromedriver binary.
91    pub fn chromedriver_binary_name(self) -> &'static str {
92        match self {
93            Platform::Linux64 | Platform::MacX64 | Platform::MacArm64 => "chromedriver",
94            Platform::Win32 | Platform::Win64 => "chromedriver.exe",
95        }
96    }
97
98    /// Tells whether this platform identifier references the Linux OS.
99    pub fn is_linux(&self) -> bool {
100        match self {
101            Platform::Linux64 => true,
102            Platform::MacArm64 | Platform::MacX64 => false,
103            Platform::Win32 | Platform::Win64 => false,
104        }
105    }
106
107    /// Tells whether this platform identifier references macOS.
108    pub fn is_macos(&self) -> bool {
109        match self {
110            Platform::Linux64 => false,
111            Platform::MacArm64 | Platform::MacX64 => true,
112            Platform::Win32 | Platform::Win64 => false,
113        }
114    }
115
116    /// Tells whether this platform identifier references the Windows OS.
117    pub fn is_windows(&self) -> bool {
118        match self {
119            Platform::Linux64 => false,
120            Platform::MacArm64 | Platform::MacX64 => false,
121            Platform::Win32 | Platform::Win64 => true,
122        }
123    }
124}