Skip to main content

AirLibrary/Updates/
PlatformDetect.rs

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