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 = "watchos")]
27    {
28        PlatformKind::Wearable
29    }
30    #[cfg(target_os = "macos")]
31    {
32        PlatformKind::MacOs
33    }
34    #[cfg(target_os = "windows")]
35    {
36        PlatformKind::Windows
37    }
38    #[cfg(all(target_os = "linux", not(target_env = "ohos")))]
39    {
40        PlatformKind::Linux
41    }
42    #[cfg(not(any(target_arch = "wasm32", unix, windows)))]
43    {
44        PlatformKind::Headless
45    }
46}
47
48/// Instantiates the native `PlatformBackend` for the active compile target.
49///
50/// # Returns
51///
52/// - `Box<dyn PlatformBackend>` - Ready-to-use native OS or web platform backend.
53#[must_use]
54pub fn create_native_platform() -> Box<dyn PlatformBackend> {
55    #[cfg(target_arch = "wasm32")]
56    {
57        Box::new(WebPlatform::default())
58    }
59    #[cfg(target_env = "ohos")]
60    {
61        Box::new(HarmonyPlatform::default())
62    }
63    #[cfg(target_os = "android")]
64    {
65        Box::new(AndroidPlatform::default())
66    }
67    #[cfg(target_os = "ios")]
68    {
69        Box::new(IosPlatform::default())
70    }
71    #[cfg(target_os = "watchos")]
72    {
73        Box::new(WearablePlatform::for_vendor(WearableVendor::WatchOs))
74    }
75    #[cfg(target_os = "macos")]
76    {
77        Box::new(MacOsPlatform::default())
78    }
79    #[cfg(target_os = "windows")]
80    {
81        Box::new(WindowsPlatform::default())
82    }
83    #[cfg(all(target_os = "linux", not(target_env = "ohos")))]
84    {
85        Box::new(LinuxPlatform::default())
86    }
87    #[cfg(not(any(target_arch = "wasm32", unix, windows)))]
88    {
89        Box::new(HeadlessPlatform::default())
90    }
91}
92
93/// Instantiates a ready-to-use `PlatformBackend` for any explicit `PlatformKind` uniformly across all hosts.
94///
95/// # Arguments
96///
97/// - `PlatformKind` - Target execution platform kind.
98///
99/// # Returns
100///
101/// - `Box<dyn PlatformBackend>` - Concrete native, adaptive, or emulated platform backend.
102#[must_use]
103pub fn create_platform_for_kind(kind: PlatformKind) -> Box<dyn PlatformBackend> {
104    match kind {
105        PlatformKind::Terminal => Box::new(TerminalPlatform::with_mode(
106            TerminalHostMode::InteractiveTui,
107        )),
108        PlatformKind::TerminalCli => {
109            Box::new(TerminalPlatform::with_mode(TerminalHostMode::StaticCli))
110        }
111        PlatformKind::Wearable => Box::new(WearablePlatform::default()),
112        PlatformKind::HarmonyOs => Box::new(HarmonyPlatform::default()),
113        PlatformKind::MiniApp => Box::new(MiniAppPlatform::default()),
114        PlatformKind::QuickApp => Box::new(QuickAppPlatform::default()),
115        PlatformKind::Headless => Box::new(HeadlessPlatform::default()),
116        other => {
117            if other == detect_current_platform() {
118                create_native_platform()
119            } else {
120                Box::new(HeadlessPlatform::emulating(
121                    other,
122                    1.0,
123                    SafeAreaInsets::default(),
124                ))
125            }
126        }
127    }
128}