Skip to main content

holodeck_simctl_core/models/
platform.rs

1use serde::{Deserialize, Serialize};
2
3use super::simctl_identifiers::RUNTIME_PREFIX;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
6pub enum Platform {
7    #[serde(rename = "iOS")]
8    IOS,
9    #[serde(rename = "watchOS")]
10    WatchOS,
11    #[serde(rename = "tvOS")]
12    TvOS,
13    #[serde(rename = "visionOS")]
14    VisionOS,
15}
16
17impl Platform {
18    pub fn raw_value(self) -> &'static str {
19        match self {
20            Platform::IOS => "iOS",
21            Platform::WatchOS => "watchOS",
22            Platform::TvOS => "tvOS",
23            Platform::VisionOS => "visionOS",
24        }
25    }
26
27    pub fn from_runtime_identifier(runtime_identifier: &str) -> Option<Self> {
28        let suffix = runtime_identifier.strip_prefix(RUNTIME_PREFIX)?;
29        let dash = suffix.find('-')?;
30        Self::from_simctl_name(&suffix[..dash])
31    }
32
33    pub(crate) fn from_simctl_name(simctl_name: &str) -> Option<Self> {
34        match simctl_name.to_lowercase().as_str() {
35            "ios" => Some(Platform::IOS),
36            "watchos" => Some(Platform::WatchOS),
37            "tvos" => Some(Platform::TvOS),
38            "xros" | "visionos" => Some(Platform::VisionOS),
39            _ => None,
40        }
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn parses_runtime_identifier() {
50        assert_eq!(Platform::from_runtime_identifier("com.apple.CoreSimulator.SimRuntime.iOS-18-0"), Some(Platform::IOS));
51    }
52
53    #[test]
54    fn xros_and_visionos_both_map_to_vision_os() {
55        assert_eq!(Platform::from_simctl_name("xros"), Some(Platform::VisionOS));
56        assert_eq!(Platform::from_simctl_name("visionos"), Some(Platform::VisionOS));
57    }
58
59    #[test]
60    fn rejects_unknown_prefix() {
61        assert_eq!(Platform::from_runtime_identifier("not-a-runtime"), None);
62    }
63}