Skip to main content

euv_core/platform/backend/
fn.rs

1use super::*;
2
3/// Detects the compile-time target `PlatformKind` for the current binary.
4///
5/// # Returns
6///
7/// - `PlatformKind` - Active platform kind (`iPadOS` is refined dynamically on iOS via `UIDevice`).
8#[must_use]
9pub fn detect_current_platform() -> PlatformKind {
10    #[cfg(target_arch = "wasm32")]
11    {
12        PlatformKind::Web
13    }
14    #[cfg(target_env = "ohos")]
15    {
16        PlatformKind::HarmonyOs
17    }
18    #[cfg(target_os = "android")]
19    {
20        PlatformKind::Android
21    }
22    #[cfg(target_os = "ios")]
23    {
24        ios_detect_platform_kind()
25    }
26    #[cfg(target_os = "macos")]
27    {
28        PlatformKind::MacOs
29    }
30    #[cfg(target_os = "windows")]
31    {
32        PlatformKind::Windows
33    }
34    #[cfg(all(target_os = "linux", not(target_env = "ohos")))]
35    {
36        PlatformKind::Linux
37    }
38    #[cfg(not(any(target_arch = "wasm32", unix, windows)))]
39    {
40        PlatformKind::Headless
41    }
42}
43
44/// Instantiates the native `PlatformBackend` for the active compile target.
45///
46/// # Returns
47///
48/// - `Box<dyn PlatformBackend>` - Ready-to-use native OS or web platform backend.
49#[must_use]
50pub fn create_native_platform() -> Box<dyn PlatformBackend> {
51    #[cfg(target_arch = "wasm32")]
52    {
53        Box::new(WebPlatform::default())
54    }
55    #[cfg(target_env = "ohos")]
56    {
57        Box::new(HarmonyPlatform::default())
58    }
59    #[cfg(target_os = "android")]
60    {
61        Box::new(AndroidPlatform::default())
62    }
63    #[cfg(target_os = "ios")]
64    {
65        Box::new(IosPlatform::default())
66    }
67    #[cfg(target_os = "macos")]
68    {
69        Box::new(MacOsPlatform::default())
70    }
71    #[cfg(target_os = "windows")]
72    {
73        Box::new(WindowsPlatform::default())
74    }
75    #[cfg(all(target_os = "linux", not(target_env = "ohos")))]
76    {
77        Box::new(LinuxPlatform::default())
78    }
79    #[cfg(not(any(target_arch = "wasm32", unix, windows)))]
80    {
81        Box::new(HeadlessPlatform::default())
82    }
83}
84
85/// Instantiates a ready-to-use `PlatformBackend` for any explicit `PlatformKind` uniformly across all hosts.
86///
87/// # Arguments
88///
89/// - `PlatformKind` - Target execution platform kind.
90///
91/// # Returns
92///
93/// - `Box<dyn PlatformBackend>` - Concrete native, adaptive, or emulated platform backend.
94#[must_use]
95pub fn create_platform_for_kind(kind: PlatformKind) -> Box<dyn PlatformBackend> {
96    match kind {
97        PlatformKind::Terminal => Box::new(TerminalPlatform::with_mode(
98            TerminalHostMode::InteractiveTui,
99        )),
100        PlatformKind::TerminalCli => {
101            Box::new(TerminalPlatform::with_mode(TerminalHostMode::StaticCli))
102        }
103        PlatformKind::Wearable => Box::new(WearablePlatform::default()),
104        PlatformKind::HarmonyOs => Box::new(HarmonyPlatform::default()),
105        PlatformKind::MiniApp => Box::new(MiniAppPlatform::default()),
106        PlatformKind::QuickApp => Box::new(QuickAppPlatform::default()),
107        PlatformKind::Headless => Box::new(HeadlessPlatform::default()),
108        other => {
109            if other == detect_current_platform() {
110                create_native_platform()
111            } else {
112                Box::new(HeadlessPlatform::emulating(
113                    other,
114                    1.0,
115                    SafeAreaInsets::default(),
116                ))
117            }
118        }
119    }
120}