chrome_for_testing/api/
platform.rs1use 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#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
13pub enum Platform {
14 #[serde(rename = "linux64")]
16 Linux64,
17
18 #[serde(rename = "mac-arm64")]
20 MacArm64,
21
22 #[serde(rename = "mac-x64")]
24 MacX64,
25
26 #[serde(rename = "win32")]
28 Win32,
29
30 #[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 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 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 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 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 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 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}