Skip to main content

AirLibrary/Updates/
PlatformDetect.rs

1#![allow(unused_variables, dead_code, unused_imports)]
2
3//! Compile-time platform/architecture detection for update packaging.
4//!
5//! `detect_platform()` returns a `PlatformInfo` describing the current OS,
6//! CPU architecture, and the appropriate package format for update binaries.
7//! All values are resolved with `cfg!` at compile time so there is no runtime
8//! overhead and the logic is testable per target triple.
9
10/// Resolved platform description used to choose an update package format.
11#[derive(Debug, Clone)]
12pub struct PlatformInfo {
13	pub platform:&'static str,
14	pub arch:&'static str,
15	pub package_format:PackageFormat,
16}
17
18/// OS-native package format for update delivery.
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum PackageFormat {
21	WindowsExe,
22	MacOsDmg,
23	LinuxAppImage,
24	LinuxDeb,
25	LinuxRpm,
26}
27
28impl PackageFormat {
29	pub fn extension(&self) -> &'static str {
30		match self {
31			PackageFormat::WindowsExe => "exe",
32			PackageFormat::MacOsDmg => "dmg",
33			PackageFormat::LinuxAppImage => "AppImage",
34			PackageFormat::LinuxDeb => "deb",
35			PackageFormat::LinuxRpm => "rpm",
36		}
37	}
38}
39
40/// Detect the current compile-target platform and preferred package format.
41pub fn detect_platform() -> PlatformInfo {
42	let platform = if cfg!(target_os = "windows") {
43		"windows"
44	} else if cfg!(target_os = "macos") {
45		"macos"
46	} else if cfg!(target_os = "linux") {
47		"linux"
48	} else {
49		"unknown"
50	};
51
52	let arch = if cfg!(target_arch = "x86_64") {
53		"x64"
54	} else if cfg!(target_arch = "aarch64") {
55		"arm64"
56	} else if cfg!(target_arch = "x86") {
57		"ia32"
58	} else {
59		"unknown"
60	};
61
62	let package_format = match (platform, arch) {
63		("windows", _) => PackageFormat::WindowsExe,
64		("macos", _) => PackageFormat::MacOsDmg,
65		("linux", _) => PackageFormat::LinuxAppImage,
66		_ => PackageFormat::LinuxAppImage,
67	};
68
69	PlatformInfo { platform, arch, package_format }
70}