yt_dlp/utils/platform.rs
1//! Platform and architecture detection.
2
3/// Represents the operating system where the program is running.
4#[derive(Clone, Debug, derive_more::Display)]
5pub enum Platform {
6 /// The Windows operating system.
7 #[display("Windows")]
8 Windows,
9 /// The Linux operating system.
10 #[display("Linux")]
11 Linux,
12 /// The macOS operating system.
13 #[display("Mac")]
14 Mac,
15
16 /// An unknown operating system.
17 #[display("Unknown(os={_0})")]
18 Unknown(String),
19}
20
21/// Represents the architecture of the CPU where the program is running.
22#[derive(Clone, Debug, derive_more::Display)]
23pub enum Architecture {
24 /// The x64 architecture.
25 #[display("x64")]
26 X64,
27 /// The x86_64 architecture.
28 #[display("x86")]
29 X86,
30 /// The ARMv7l architecture.
31 #[display("Armv7l")]
32 Armv7l,
33 /// The Aarch64 (Arm64) architecture.
34 #[display("Aarch64")]
35 Aarch64,
36
37 /// An unknown architecture.
38 #[display("Unknown(arch={_0})")]
39 Unknown(String),
40}
41
42impl Platform {
43 /// Returns the lowercase platform identifier used in binary names.
44 ///
45 /// # Returns
46 ///
47 /// A string slice with the platform name (e.g., "windows", "linux", "osx").
48 pub fn as_str(&self) -> &str {
49 match self {
50 Platform::Windows => "windows",
51 Platform::Linux => "linux",
52 Platform::Mac => "osx",
53 Platform::Unknown(s) => s,
54 }
55 }
56
57 /// Detects the current platform where the program is running.
58 ///
59 /// # Returns
60 ///
61 /// The detected `Platform` variant, or `Platform::Unknown` if the OS is not recognized.
62 pub fn detect() -> Self {
63 tracing::debug!("⚙️ Detecting current platform");
64
65 let os = std::env::consts::OS;
66
67 tracing::debug!(os = os, "✅ Detected platform");
68
69 match os {
70 "windows" => Platform::Windows,
71 "linux" => Platform::Linux,
72 "macos" => Platform::Mac,
73 _ => Platform::Unknown(os.to_string()),
74 }
75 }
76}
77
78impl Architecture {
79 /// Returns the lowercase architecture identifier used in binary names.
80 ///
81 /// # Returns
82 ///
83 /// A string slice with the architecture name (e.g., "x64", "x86", "arm64").
84 pub fn as_str(&self) -> &str {
85 match self {
86 Architecture::X64 => "x64",
87 Architecture::X86 => "x86",
88 Architecture::Armv7l => "armv7l",
89 Architecture::Aarch64 => "arm64",
90 Architecture::Unknown(s) => s,
91 }
92 }
93
94 /// Detects the current architecture of the CPU where the program is running.
95 ///
96 /// # Returns
97 ///
98 /// The detected `Architecture` variant, or `Architecture::Unknown` if the arch is not recognized.
99 pub fn detect() -> Self {
100 tracing::debug!("⚙️ Detecting current architecture");
101
102 let arch = std::env::consts::ARCH;
103
104 tracing::debug!(arch = arch, "✅ Detected architecture");
105
106 match arch {
107 "x86_64" => Architecture::X64,
108 "x86" => Architecture::X86,
109 "armv7l" => Architecture::Armv7l,
110 "aarch64" => Architecture::Aarch64,
111 _ => Architecture::Unknown(arch.to_string()),
112 }
113 }
114}