image_optimizer/updater/platform_detector.rs
1use anyhow::Result;
2
3/// Detects the current platform target for binary downloads.
4///
5/// This function determines the Rust target triple for the current platform,
6/// which is used to identify the correct binary asset to download from GitHub releases.
7/// The detection is based on the OS and architecture constants provided by Rust's
8/// standard library.
9///
10/// # Returns
11///
12/// Returns the Rust target triple as a `String` for the current platform.
13///
14/// # Errors
15///
16/// Returns an error if the current platform combination is not supported
17/// for automatic updates.
18///
19/// # Supported Platforms
20///
21/// - **Linux**: `x86_64`, aarch64
22/// - **macOS**: `x86_64` (Intel), aarch64 (Apple Silicon)
23/// - **Windows**: `x86_64`
24///
25/// # Examples
26///
27/// ```rust
28/// use image_optimizer::updater::platform_detector::get_platform_target;
29///
30/// # fn example() -> anyhow::Result<()> {
31/// let target = get_platform_target()?;
32/// // On Apple Silicon Mac: "aarch64-apple-darwin"
33/// // On Intel Mac: "x86_64-apple-darwin"
34/// // On Linux x64: "x86_64-unknown-linux-gnu"
35/// # Ok(())
36/// # }
37/// ```
38pub fn get_platform_target() -> Result<String> {
39 let os = std::env::consts::OS;
40 let arch = std::env::consts::ARCH;
41
42 let target = match (os, arch) {
43 ("linux", "x86_64") => "x86_64-unknown-linux-gnu",
44 ("linux", "aarch64") => "aarch64-unknown-linux-gnu",
45 ("macos", "x86_64") => "x86_64-apple-darwin",
46 ("macos", "aarch64") => "aarch64-apple-darwin",
47 ("windows", "x86_64") => "x86_64-pc-windows-msvc",
48 _ => return Err(anyhow::anyhow!("Unsupported platform: {}-{}", os, arch)),
49 };
50
51 Ok(target.to_string())
52}